1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/std/encoding
2019-11-18 09:39:32 -05:00
..
testdata Move everything into std subdir 2019-10-09 17:10:09 -04:00
yaml feat: Add std/encoding/yaml module (#3361) 2019-11-18 09:39:32 -05:00
csv.ts Update to TypeScript 3.7 (#3275) 2019-11-13 13:42:34 -05:00
csv_test.ts Update to TypeScript 3.7 (#3275) 2019-11-13 13:42:34 -05:00
hex.ts Move everything into std subdir 2019-10-09 17:10:09 -04:00
hex_test.ts Move everything into std subdir 2019-10-09 17:10:09 -04:00
README.md feat: Add std/encoding/yaml module (#3361) 2019-11-18 09:39:32 -05:00
toml.ts Update to TypeScript 3.7 (#3275) 2019-11-13 13:42:34 -05:00
toml_test.ts Update to TypeScript 3.7 (#3275) 2019-11-13 13:42:34 -05:00
yaml.ts feat: Add std/encoding/yaml module (#3361) 2019-11-18 09:39:32 -05:00
yaml_test.ts feat: Add std/encoding/yaml module (#3361) 2019-11-18 09:39:32 -05: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
  • parse(csvString: string, opt: ParseOption): Promise<unknown[]>: See parse

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 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[].
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.

Usage

// 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

This module parse TOML files. It follows as much as possible the TOML specs. 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.

Supported types and handling

Supported with warnings see Warning.

⚠️ Warning

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.

Local Time

Because local time does not exist in JavaScript, the local time is stored as a string.

Inline Table

Inline tables are supported. See below:

animal = { type = { name = "pug" } }
## Output
animal = { type.name = "pug" }
## Output { animal : { type : { name : "pug" } }
animal.as.leaders = "tosin"
## Output { animal: { as: { leaders: "tosin" } } }
"tosin.abasi" = "guitarist"
## Output
"tosin.abasi" : "guitarist"
Array of Tables

At the moment only simple declarations like below are supported:

[[bin]]
name = "deno"
path = "cli/main.rs"

[[bin]]
name = "deno_core"
path = "src/foo.rs"

[[nib]]
name = "node"
path = "not_found"

will output:

{
  "bin": [
    { "name": "deno", "path": "cli/main.rs" },
    { "name": "deno_core", "path": "src/foo.rs" }
  ],
  "nib": [{ "name": "node", "path": "not_found" }]
}

Usage

Parse

import { parse } from "./parser.ts";
import { readFileStrSync } from "../fs/read_file_str.ts";

const tomlObject = parse(readFileStrSync("file.toml"));

const tomlString = 'foo.bar = "Deno"';
const tomlObject22 = parse(tomlString);

Stringify

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);

YAML

YAML parser / dumper for Deno

Heavily inspired from js-yaml

Example

See ./yaml/example folder and js-yaml repository.

⚠️ Limitations

  • binary type is currently not stable
  • function, regexp, and undefined type are currently not supported

Basic usage

TBD