diff options
Diffstat (limited to 'std/encoding')
-rw-r--r-- | std/encoding/README.md | 30 | ||||
-rw-r--r-- | std/encoding/csv.ts | 66 | ||||
-rw-r--r-- | std/encoding/toml.ts | 36 |
3 files changed, 72 insertions, 60 deletions
diff --git a/std/encoding/README.md b/std/encoding/README.md index f03e80ba2..2b209c6b6 100644 --- a/std/encoding/README.md +++ b/std/encoding/README.md @@ -4,8 +4,8 @@ - **`readAll(reader: BufReader, opt: ParseOptions = { comma: ",", trimLeadingSpace: false, lazyQuotes: false } ): Promise<[string[][], BufState]>`**: Read the whole buffer and output the structured CSV datas -- **`parse(csvString: string, opt: ParseOption): Promise<unknown[]>`**: - See [parse](###Parse) +- **`parse(csvString: string, opt: ParseOption): Promise<unknown[]>`**: See + [parse](###Parse) ### Parse @@ -17,8 +17,7 @@ Parse the CSV string with the options provided. - **`header: boolean | string[] | HeaderOption[];`**: If a boolean is provided, the first line will be used as Header definitions. If `string[]` or - `HeaderOption[]` - those names will be used for header definition. + `HeaderOption[]` those names will be used for header definition. - **`parse?: (input: unknown) => unknown;`**: Parse function for the row, which will be executed after parsing of all columns. Therefore if you don't provide header and parse function with headers, input will be `string[]`. @@ -26,9 +25,9 @@ Parse the CSV string with the options provided. ##### HeaderOption - **`name: string;`**: Name of the header to be used as property. -- **`parse?: (input: string) => unknown;`**: Parse function for the column. - This is executed on each entry of the header. This can be combined with the - Parse function of the rows. +- **`parse?: (input: string) => unknown;`**: Parse function for the column. This + is executed on each entry of the header. This can be combined with the Parse + function of the rows. #### Usage @@ -123,8 +122,10 @@ TypeScript side is a bit different. - :heavy_check_mark: [Local Date](https://github.com/toml-lang/toml#local-date) - :exclamation: [Local Time](https://github.com/toml-lang/toml#local-time) - :heavy_check_mark: [Table](https://github.com/toml-lang/toml#table) -- :heavy_check_mark: [Inline Table](https://github.com/toml-lang/toml#inline-table) -- :exclamation: [Array of Tables](https://github.com/toml-lang/toml#array-of-tables) +- :heavy_check_mark: + [Inline Table](https://github.com/toml-lang/toml#inline-table) +- :exclamation: + [Array of Tables](https://github.com/toml-lang/toml#array-of-tables) :exclamation: _Supported with warnings see [Warning](#Warning)._ @@ -132,17 +133,18 @@ TypeScript side is a bit different. ##### String -- Regex : Due to the spec, there is no flag to detect regex properly - in a TOML declaration. So the regex is stored as string. +- Regex : Due to the spec, there is no flag to detect regex properly in a TOML + declaration. So the regex is stored as string. ##### Integer -For **Binary** / **Octal** / **Hexadecimal** numbers, -they are stored as string to be not interpreted as Decimal. +For **Binary** / **Octal** / **Hexadecimal** numbers, they are stored as string +to be not interpreted as Decimal. ##### Local Time -Because local time does not exist in JavaScript, the local time is stored as a string. +Because local time does not exist in JavaScript, the local time is stored as a +string. ##### Inline Table diff --git a/std/encoding/csv.ts b/std/encoding/csv.ts index 10d72a8a5..ec2609f6c 100644 --- a/std/encoding/csv.ts +++ b/std/encoding/csv.ts @@ -84,23 +84,25 @@ 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'); } @@ -224,25 +226,27 @@ 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]; + 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 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/toml.ts b/std/encoding/toml.ts index 0cbd51ba0..3b4b03d20 100644 --- a/std/encoding/toml.ts +++ b/std/encoding/toml.ts @@ -393,9 +393,11 @@ 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("."); } @@ -415,20 +417,24 @@ 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]); + 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]); } - 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]); + ); + 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 k = propPrim.concat(propObj); for (let i = 0; i < k.length; i++) { const prop = k[i]; |