diff options
Diffstat (limited to 'std/encoding')
-rw-r--r-- | std/encoding/csv.ts | 66 | ||||
-rw-r--r-- | std/encoding/csv_test.ts | 46 | ||||
-rw-r--r-- | std/encoding/toml.ts | 36 | ||||
-rw-r--r-- | std/encoding/toml_test.ts | 10 |
4 files changed, 91 insertions, 67 deletions
diff --git a/std/encoding/csv.ts b/std/encoding/csv.ts index ec2609f6c..10d72a8a5 100644 --- a/std/encoding/csv.ts +++ b/std/encoding/csv.ts @@ -84,25 +84,23 @@ async function read( result = line.split(opt.comma!); let quoteError = false; - result = result.map( - (r): string => { - if (opt.trimLeadingSpace) { - r = r.trimLeft(); - } - if (r[0] === '"' && r[r.length - 1] === '"') { - r = r.substring(1, r.length - 1); - } else if (r[0] === '"') { - r = r.substring(1, r.length); - } + result = result.map((r): string => { + if (opt.trimLeadingSpace) { + r = r.trimLeft(); + } + if (r[0] === '"' && r[r.length - 1] === '"') { + r = r.substring(1, r.length - 1); + } else if (r[0] === '"') { + r = r.substring(1, r.length); + } - if (!opt.lazyQuotes) { - if (r[0] !== '"' && r.indexOf('"') !== -1) { - quoteError = true; - } + if (!opt.lazyQuotes) { + if (r[0] !== '"' && r.indexOf('"') !== -1) { + quoteError = true; } - return r; } - ); + return r; + }); if (quoteError) { throw new ParseError(Startline, lineIndex, 'bare " in non-quoted-field'); } @@ -226,27 +224,25 @@ export async function parse( ); i++; } - return r.map( - (e): unknown => { - if (e.length !== headers.length) { - throw `Error number of fields line:${i}`; - } - i++; - const out: Record<string, unknown> = {}; - for (let j = 0; j < e.length; j++) { - const h = headers[j]; - if (h.parse) { - out[h.name] = h.parse(e[j]); - } else { - out[h.name] = e[j]; - } - } - if (opt.parse) { - return opt.parse(out); + return r.map((e): unknown => { + if (e.length !== headers.length) { + throw `Error number of fields line:${i}`; + } + i++; + const out: Record<string, unknown> = {}; + for (let j = 0; j < e.length; j++) { + const h = headers[j]; + if (h.parse) { + out[h.name] = h.parse(e[j]); + } else { + out[h.name] = e[j]; } - return out; } - ); + if (opt.parse) { + return opt.parse(out); + } + return out; + }); } if (opt.parse) { return r.map((e: string[]): unknown => opt.parse!(e)); diff --git a/std/encoding/csv_test.ts b/std/encoding/csv_test.ts index 88a3a24d7..65a86a353 100644 --- a/std/encoding/csv_test.ts +++ b/std/encoding/csv_test.ts @@ -20,7 +20,10 @@ const testCases = [ { Name: "CRLF", Input: "a,b\r\nc,d\r\n", - Output: [["a", "b"], ["c", "d"]] + Output: [ + ["a", "b"], + ["c", "d"] + ] }, { Name: "BareCR", @@ -64,12 +67,18 @@ const testCases = [ { Name: "BlankLine", Input: "a,b,c\n\nd,e,f\n\n", - Output: [["a", "b", "c"], ["d", "e", "f"]] + Output: [ + ["a", "b", "c"], + ["d", "e", "f"] + ] }, { Name: "BlankLineFieldCount", Input: "a,b,c\n\nd,e,f\n\n", - Output: [["a", "b", "c"], ["d", "e", "f"]], + Output: [ + ["a", "b", "c"], + ["d", "e", "f"] + ], UseFieldsPerRecord: true, FieldsPerRecord: 0 }, @@ -93,7 +102,10 @@ const testCases = [ { Name: "NoComment", Input: "#1,2,3\na,b,c", - Output: [["#1", "2", "3"], ["a", "b", "c"]] + Output: [ + ["#1", "2", "3"], + ["a", "b", "c"] + ] }, { Name: "LazyQuotes", @@ -159,7 +171,10 @@ const testCases = [ { Name: "FieldCount", Input: "a,b,c\nd,e", - Output: [["a", "b", "c"], ["d", "e"]] + Output: [ + ["a", "b", "c"], + ["d", "e"] + ] }, { Name: "TrailingCommaEOF", @@ -186,7 +201,11 @@ const testCases = [ { Name: "TrailingCommaLine3", Input: "a,b,c\nd,e,f\ng,hi,", - Output: [["a", "b", "c"], ["d", "e", "f"], ["g", "hi", ""]], + Output: [ + ["a", "b", "c"], + ["d", "e", "f"], + ["g", "hi", ""] + ], TrimLeadingSpace: true }, { @@ -223,13 +242,19 @@ x,,, { Name: "TrailingCommaIneffective1", Input: "a,b,\nc,d,e", - Output: [["a", "b", ""], ["c", "d", "e"]], + Output: [ + ["a", "b", ""], + ["c", "d", "e"] + ], TrimLeadingSpace: true }, { Name: "ReadAllReuseRecord", Input: "a,b\nc,d", - Output: [["a", "b"], ["c", "d"]], + Output: [ + ["a", "b"], + ["c", "d"] + ], ReuseRecord: true }, // { @@ -496,7 +521,10 @@ const parseTestCases = [ name: "multiline", in: "a,b,c\ne,f,g\n", header: false, - result: [["a", "b", "c"], ["e", "f", "g"]] + result: [ + ["a", "b", "c"], + ["e", "f", "g"] + ] }, { name: "header mapping boolean", diff --git a/std/encoding/toml.ts b/std/encoding/toml.ts index 3b4b03d20..0cbd51ba0 100644 --- a/std/encoding/toml.ts +++ b/std/encoding/toml.ts @@ -393,11 +393,9 @@ function joinKeys(keys: string[]): string { // Dotted keys are a sequence of bare or quoted keys joined with a dot. // This allows for grouping similar properties together: return keys - .map( - (str: string): string => { - return str.match(/[^A-Za-z0-9_-]/) ? `"${str}"` : str; - } - ) + .map((str: string): string => { + return str.match(/[^A-Za-z0-9_-]/) ? `"${str}"` : str; + }) .join("."); } @@ -417,24 +415,20 @@ class Dumper { _parse(obj: Record<string, unknown>, keys: string[] = []): string[] { const out = []; const props = Object.keys(obj); - const propObj = props.filter( - (e: string): boolean => { - if (obj[e] instanceof Array) { - const d: unknown[] = obj[e] as unknown[]; - return !this._isSimplySerializable(d[0]); - } - return !this._isSimplySerializable(obj[e]); + const propObj = props.filter((e: string): boolean => { + if (obj[e] instanceof Array) { + const d: unknown[] = obj[e] as unknown[]; + return !this._isSimplySerializable(d[0]); } - ); - const propPrim = props.filter( - (e: string): boolean => { - if (obj[e] instanceof Array) { - const d: unknown[] = obj[e] as unknown[]; - return this._isSimplySerializable(d[0]); - } - return this._isSimplySerializable(obj[e]); + return !this._isSimplySerializable(obj[e]); + }); + const propPrim = props.filter((e: string): boolean => { + if (obj[e] instanceof Array) { + const d: unknown[] = obj[e] as unknown[]; + return this._isSimplySerializable(d[0]); } - ); + return this._isSimplySerializable(obj[e]); + }); const k = propPrim.concat(propObj); for (let i = 0; i < k.length; i++) { const prop = k[i]; diff --git a/std/encoding/toml_test.ts b/std/encoding/toml_test.ts index 633ccd1db..065ab506c 100644 --- a/std/encoding/toml_test.ts +++ b/std/encoding/toml_test.ts @@ -112,7 +112,10 @@ test({ fn(): void { const expected = { arrays: { - data: [["gamma", "delta"], [1, 2]], + data: [ + ["gamma", "delta"], + [1, 2] + ], hosts: ["alpha", "omega"] } }; @@ -344,7 +347,10 @@ test({ sf4: NaN, sf5: NaN, sf6: NaN, - data: [["gamma", "delta"], [1, 2]], + data: [ + ["gamma", "delta"], + [1, 2] + ], hosts: ["alpha", "omega"] }; const expected = `deno = "is" |