2019-05-30 09:50:29 -04:00
|
|
|
# Encoding
|
|
|
|
|
|
|
|
## CSV
|
|
|
|
|
|
|
|
- **`readAll(reader: BufReader, opt: ParseOptions = { comma: ",", trimLeadingSpace: false, lazyQuotes: false } ): Promise<[string[][], BufState]>`**:
|
|
|
|
Read the whole buffer and output the structured CSV datas
|
2019-10-09 17:22:22 -04:00
|
|
|
- **`parse(csvString: string, opt: ParseOption): Promise<unknown[]>`**: See
|
|
|
|
[parse](###Parse)
|
2019-05-30 09:50:29 -04:00
|
|
|
|
|
|
|
### Parse
|
|
|
|
|
|
|
|
Parse the CSV string with the options provided.
|
|
|
|
|
|
|
|
#### Options
|
|
|
|
|
|
|
|
##### ParseOption
|
|
|
|
|
|
|
|
- **`header: boolean | string[] | HeaderOption[];`**: If a boolean is provided,
|
|
|
|
the first line will be used as Header definitions. If `string[]` or
|
2019-10-09 17:22:22 -04:00
|
|
|
`HeaderOption[]` those names will be used for header definition.
|
2019-05-30 09:50:29 -04:00
|
|
|
- **`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[]`.
|
|
|
|
|
|
|
|
##### HeaderOption
|
|
|
|
|
|
|
|
- **`name: string;`**: Name of the header to be used as property.
|
2019-10-09 17:22:22 -04:00
|
|
|
- **`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.
|
2019-05-30 09:50:29 -04:00
|
|
|
|
|
|
|
#### Usage
|
|
|
|
|
|
|
|
```ts
|
|
|
|
// input:
|
|
|
|
// a,b,c
|
|
|
|
// e,f,g
|
|
|
|
|
|
|
|
const r = await parseFile(filepath, {
|
|
|
|
header: false
|
|
|
|
});
|
|
|
|
// output:
|
|
|
|
// [["a", "b", "c"], ["e", "f", "g"]]
|
|
|
|
|
|
|
|
const r = await parseFile(filepath, {
|
|
|
|
header: true
|
|
|
|
});
|
|
|
|
// output:
|
|
|
|
// [{ a: "e", b: "f", c: "g" }]
|
|
|
|
|
|
|
|
const r = await parseFile(filepath, {
|
|
|
|
header: ["this", "is", "sparta"]
|
|
|
|
});
|
|
|
|
// output:
|
|
|
|
// [
|
|
|
|
// { this: "a", is: "b", sparta: "c" },
|
|
|
|
// { this: "e", is: "f", sparta: "g" }
|
|
|
|
// ]
|
|
|
|
|
|
|
|
const r = await parseFile(filepath, {
|
|
|
|
header: [
|
|
|
|
{
|
|
|
|
name: "this",
|
|
|
|
parse: (e: string): string => {
|
|
|
|
return `b${e}$$`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "is",
|
|
|
|
parse: (e: string): number => {
|
|
|
|
return e.length;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sparta",
|
|
|
|
parse: (e: string): unknown => {
|
|
|
|
return { bim: `boom-${e}` };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
// output:
|
|
|
|
// [
|
|
|
|
// { this: "ba$$", is: 1, sparta: { bim: `boom-c` } },
|
|
|
|
// { this: "be$$", is: 1, sparta: { bim: `boom-g` } }
|
|
|
|
// ]
|
|
|
|
|
|
|
|
const r = await parseFile(filepath, {
|
|
|
|
header: ["this", "is", "sparta"],
|
|
|
|
parse: (e: Record<string, unknown>) => {
|
|
|
|
return { super: e.this, street: e.is, fighter: e.sparta };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// output:
|
|
|
|
// [
|
|
|
|
// { super: "a", street: "b", fighter: "c" },
|
|
|
|
// { super: "e", street: "f", fighter: "g" }
|
|
|
|
// ]
|
|
|
|
```
|
|
|
|
|
|
|
|
## TOML
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
This module parse TOML files. It follows as much as possible the
|
|
|
|
[TOML specs](https://github.com/toml-lang/toml). Be sure to read the supported
|
|
|
|
types as not every specs is supported at the moment and the handling in
|
|
|
|
TypeScript side is a bit different.
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
### Supported types and handling
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
- :heavy_check_mark: [Keys](https://github.com/toml-lang/toml#string)
|
|
|
|
- :exclamation: [String](https://github.com/toml-lang/toml#string)
|
|
|
|
- :heavy_check_mark:
|
|
|
|
[Multiline String](https://github.com/toml-lang/toml#string)
|
|
|
|
- :heavy_check_mark: [Literal String](https://github.com/toml-lang/toml#string)
|
|
|
|
- :exclamation: [Integer](https://github.com/toml-lang/toml#integer)
|
|
|
|
- :heavy_check_mark: [Float](https://github.com/toml-lang/toml#float)
|
|
|
|
- :heavy_check_mark: [Boolean](https://github.com/toml-lang/toml#boolean)
|
|
|
|
- :heavy_check_mark:
|
|
|
|
[Offset Date-time](https://github.com/toml-lang/toml#offset-date-time)
|
|
|
|
- :heavy_check_mark:
|
|
|
|
[Local Date-time](https://github.com/toml-lang/toml#local-date-time)
|
|
|
|
- :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)
|
2019-10-09 17:22:22 -04:00
|
|
|
- :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)
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
:exclamation: _Supported with warnings see [Warning](#Warning)._
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
#### :warning: Warning
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### String
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
- Regex : Due to the spec, there is no flag to detect regex properly in a TOML
|
|
|
|
declaration. So the regex is stored as string.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Integer
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
For **Binary** / **Octal** / **Hexadecimal** numbers, they are stored as string
|
|
|
|
to be not interpreted as Decimal.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Local Time
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
Because local time does not exist in JavaScript, the local time is stored as a
|
|
|
|
string.
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Inline Table
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-04-04 06:00:24 -04:00
|
|
|
Inline tables are supported. See below:
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
```toml
|
2019-04-04 06:00:24 -04:00
|
|
|
animal = { type = { name = "pug" } }
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output
|
2019-03-28 12:31:15 -04:00
|
|
|
animal = { type.name = "pug" }
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output { animal : { type : { name : "pug" } }
|
2019-04-04 06:00:24 -04:00
|
|
|
animal.as.leaders = "tosin"
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output { animal: { as: { leaders: "tosin" } } }
|
2019-04-04 06:00:24 -04:00
|
|
|
"tosin.abasi" = "guitarist"
|
2019-05-30 09:50:29 -04:00
|
|
|
## Output
|
2019-04-04 06:00:24 -04:00
|
|
|
"tosin.abasi" : "guitarist"
|
2019-03-28 12:31:15 -04:00
|
|
|
```
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
##### Array of Tables
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
At the moment only simple declarations like below are supported:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[[bin]]
|
|
|
|
name = "deno"
|
|
|
|
path = "cli/main.rs"
|
|
|
|
|
|
|
|
[[bin]]
|
|
|
|
name = "deno_core"
|
|
|
|
path = "src/foo.rs"
|
|
|
|
|
|
|
|
[[nib]]
|
|
|
|
name = "node"
|
|
|
|
path = "not_found"
|
|
|
|
```
|
|
|
|
|
|
|
|
will output:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"bin": [
|
|
|
|
{ "name": "deno", "path": "cli/main.rs" },
|
|
|
|
{ "name": "deno_core", "path": "src/foo.rs" }
|
|
|
|
],
|
|
|
|
"nib": [{ "name": "node", "path": "not_found" }]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
### Usage
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
#### Parse
|
2019-04-05 00:23:05 -04:00
|
|
|
|
2019-03-28 12:31:15 -04:00
|
|
|
```ts
|
2019-04-25 18:47:18 -04:00
|
|
|
import { parse } from "./parser.ts";
|
|
|
|
import { readFileStrSync } from "../fs/read_file_str.ts";
|
2019-03-28 12:31:15 -04:00
|
|
|
|
2019-04-25 18:47:18 -04:00
|
|
|
const tomlObject = parse(readFileStrSync("file.toml"));
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
const tomlString = 'foo.bar = "Deno"';
|
|
|
|
const tomlObject22 = parse(tomlString);
|
|
|
|
```
|
2019-04-05 00:23:05 -04:00
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
#### Stringify
|
2019-04-05 00:23:05 -04:00
|
|
|
|
|
|
|
```ts
|
|
|
|
import { stringify } from "./parser.ts";
|
|
|
|
const obj = {
|
|
|
|
bin: [
|
|
|
|
{ name: "deno", path: "cli/main.rs" },
|
|
|
|
{ name: "deno_core", path: "src/foo.rs" }
|
|
|
|
],
|
|
|
|
nib: [{ name: "node", path: "not_found" }]
|
|
|
|
};
|
|
|
|
const tomlString = stringify(obj);
|
|
|
|
```
|