10 examples of 'javascript parse text file to array' in JavaScript

Every line of 'javascript parse text file to array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
40export function parseArray(text: string) {
41 return text.replace(/\r/g, '')
42 .split('\n')
43 .map((s) => s.trim())
44 .filter((s) => s);
45}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

515function ParseSourceFile(FileTextVar, array) {
516 var string = "",
517 onestring = "",
518 trimedstring = "",
519 noslashstring = "",
520 nofirstlaststring = "",
521 stringtolangpack = "",
522 clearstring = "";
523 //not store ?: functions LPGEN or LPGENT? or Translate(T or W) or _T, than any unnecessary space \s, than not stored ?: "(" followed by ' or " (stored and used as \1) than \S\s - magic with multiline capture, ending with not stored ?= \1 (we get " or ' after "("), than none or few spaces \x20 followed by )/m=multiline g=global
524 //var find= /(?:LPGEN[TW]?|Translate[TW]?||Translate[AW]_LP|_T)(?:\s*?\(\s*?L?\s*)(['"])([\S\s]*?)(?=\1,?\x20*?(?:tmp)?\))/mg;
525 //comment previous line and uncomment following line to output templates without _T() function in source files. Too many garbage from _T()..
526 var find = /(?:LPGEN[TW]?|Translate[TUW]?|Translate[AUW]_LP)(?:\s*?\(\s*?L?\s*)((?:(?:"[^"\\]*(?:\\[\S\s][^"\\]*)*")\s*)*)(?:\s*?,?\s*?(?:tmp)?\))/gm;
527 //now make a job, till end of matching regexp
528 while ((string = find.exec(FileTextVar)) !== null) {
529 //first, init empty var
530 //replace newlines and all spaces and tabs between two pairs of " or ' with the void string ("") in first [1] subregexp ([\S\s]*?), and Delphi newlines "'#13#10+" replace
531 onestring = string[1].replace(/["']?(?:\#13\#10)*?\\?\r*\n(?:(?:\x20|\t)*['"])?/g, "");
532 //trim single-line whitespaces - multi-line parsing catches whitespaces after last " in single-line case
533 trimedstring = onestring.replace(/[\s]*$/g, "");
534 //remove trailing slash from the string. This is a tree item, slash is a crap :)
535 noslashstring = trimedstring.replace(/\/(?=$)/g, "");
536 //remove first and last "
537 nofirstlaststring = noslashstring.slice(1, -1);
538 //remove escape slashes before ' and "
539 stringtolangpack = nofirstlaststring.replace(/\\(")/g, "$1");
540 ///if our string still exist, and length at least one symbol
541 if (stringtolangpack.length > 0) {
542 //brand new _T() crap filtering engine :)
543 clearstring = filter_T(stringtolangpack);
544 //finally put string into array including cover brackets []
545 if (clearstring) {
546 array.push("[" + clearstring + "]");
547 }
548 }
549 }
550}
47parse: function parseText(file) {
48 if (typeof file.data === 'string') {
49 return file.data;
50 }
51 else if (Buffer.isBuffer(file.data)) {
52 return file.data.toString(this.encoding);
53 }
54 else {
55 throw new Error('data is not text');
56 }
57}
75function parse(file) {
76 file = file || exports.spmrcfile;
77 if (!fs.existsSync(file)) {
78 return {};
79 }
80 var data;
81 if (_cache.hasOwnProperty(file)) {
82 data = _cache[file];
83 } else {
84 data = grunt.file.read(file);
85 _cache[file] = data;
86 }
87 var value = {};
88 var lines = data.split(/\r\n|\r|\n/);
89 var section = null;
90 var match;
91 lines.forEach(function(line) {
92 if (regex.comment.test(line)) {
93 return;
94 }
95 if (regex.param.test(line)) {
96 match = line.match(regex.param);
97 if (section) {
98 value[section][match[1]] = match[2];
99 }else {
100 value[match[1]] = match[2];
101 }
102 } else if (regex.section.test(line)) {
103 match = line.match(regex.section);
104 value[match[1]] = {};
105 section = match[1];
106 } else if (line.length === 0 && section) {
107 section = null;
108 }
109 });
110 return value;
111}
2function parseText(text, options) {
3 return JSON.parse(text);
4}
27function parse (text) {
28 var result = {
29 text: markdown.parse(text),
30 styles: [].concat(styles)
31 }
32
33 styles.length = 0
34
35 return result
36}
10function parse(text) {
11 var lines = text.trim().split('\n');
12 var rv = {};
13
14 lines.forEach(function(line) {
15 var bits = line.split('\t');
16 rv[bits[0]] = bits[1];
17 });
18
19 return rv;
20}
9function parseFile(file, successCallback, errorCallback) {
10 let r = new FileReader();
11
12 r.onload = e => {
13 // load the contents of the file into `contents`
14 let contents = e.target.result;
15 successCallback(contents);
16 };
17
18 r.onerror = errorCallback;
19
20 r.readAsText(file);
21}
7public parse(text: string, query: any = {}): object {
8 const {header, delimiter} = this.parseQuery(query);
9 const lineSeparator = /\r?\n/;
10 if (text.split) {
11 const lines = text.split(lineSeparator);
12 if (!header) {
13 return lines
14 .filter(line => line.length > 0)
15 .map((line: string) => line.split(delimiter));
16 } else if (lines[0]) {
17 return this.parseWithHeader(lines, delimiter);
18 }
19 }
20 return [];
21}
92function parse_csv(text) {
93 const lines = text.split('\n').filter(function (line) {
94 return line.length > 0 && line[0] !== '#';
95 });
96 let pos = new Float32Array(lines.length * 3);
97 let lattice_ids = [];
98 for (let i = 0; i < lines.length; i++) {
99 const nums = lines[i].split(',').map(Number);
100 for (let j = 0; j < 3; j++) {
101 pos[3*i+j] = nums[j];
102 }
103 lattice_ids.push(nums[3]);
104 }
105 return { pos, lattice_ids };
106}

Related snippets