summaryrefslogtreecommitdiff
path: root/std/encoding
diff options
context:
space:
mode:
Diffstat (limited to 'std/encoding')
-rw-r--r--std/encoding/README.md99
-rw-r--r--std/encoding/csv.ts32
-rw-r--r--std/encoding/csv_test.ts6
-rw-r--r--std/encoding/mod.ts14
-rw-r--r--std/encoding/yaml.ts7
5 files changed, 48 insertions, 110 deletions
diff --git a/std/encoding/README.md b/std/encoding/README.md
index 767cbdcb4..ee06aa3ec 100644
--- a/std/encoding/README.md
+++ b/std/encoding/README.md
@@ -2,100 +2,21 @@
## 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)
+- **`parseCsv(input: string | BufReader, opt: ParseCsvOptions): Promise<unknown[]>`**:
+ Read the string/buffer into an
-### 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
+### Usage
```ts
-// input:
-// a,b,c
-// e,f,g
-
-const r = await parseFile(filepath, {
- header: false
-});
-// output:
-// [["a", "b", "c"], ["e", "f", "g"]]
+const string = "a,b,c\nd,e,f";
-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 };
- }
-});
+console.log(
+ await parseCsv(string, {
+ header: false
+ })
+);
// output:
-// [
-// { super: "a", street: "b", fighter: "c" },
-// { super: "e", street: "f", fighter: "g" }
-// ]
+// [["a", "b", "c"], ["d", "e", "f"]]
```
## TOML
diff --git a/std/encoding/csv.ts b/std/encoding/csv.ts
index 10d72a8a5..8cfa1cab9 100644
--- a/std/encoding/csv.ts
+++ b/std/encoding/csv.ts
@@ -28,7 +28,7 @@ export class ParseError extends Error {
* @property fieldsPerRecord - Enabling the check of fields for each row.
* If == 0, first row is used as referal for the number of fields.
*/
-export interface ParseOptions {
+export interface ReadOptions {
comma?: string;
comment?: string;
trimLeadingSpace?: boolean;
@@ -36,7 +36,7 @@ export interface ParseOptions {
fieldsPerRecord?: number;
}
-function chkOptions(opt: ParseOptions): void {
+function chkOptions(opt: ReadOptions): void {
if (!opt.comma) opt.comma = ",";
if (!opt.trimLeadingSpace) opt.trimLeadingSpace = false;
if (
@@ -51,7 +51,7 @@ function chkOptions(opt: ParseOptions): void {
async function read(
Startline: number,
reader: BufReader,
- opt: ParseOptions = { comma: ",", trimLeadingSpace: false }
+ opt: ReadOptions = { comma: ",", trimLeadingSpace: false }
): Promise<string[] | Deno.EOF> {
const tp = new TextProtoReader(reader);
let line: string;
@@ -107,9 +107,9 @@ async function read(
return result;
}
-export async function readAll(
+export async function readMatrix(
reader: BufReader,
- opt: ParseOptions = {
+ opt: ReadOptions = {
comma: ",",
trimLeadingSpace: false,
lazyQuotes: false
@@ -151,17 +151,17 @@ export async function readAll(
}
/**
- * HeaderOption provides the column definition
+ * HeaderOptions provides the column definition
* and the parse function for each entry of the
* column.
*/
-export interface HeaderOption {
+export interface HeaderOptions {
name: string;
parse?: (input: string) => unknown;
}
-export interface ExtendedParseOptions extends ParseOptions {
- header: boolean | string[] | HeaderOption[];
+export interface ParseOptions extends ReadOptions {
+ header: boolean | string[] | HeaderOptions[];
parse?: (input: unknown) => unknown;
}
@@ -188,26 +188,26 @@ export interface ExtendedParseOptions extends ParseOptions {
*/
export async function parse(
input: string | BufReader,
- opt: ExtendedParseOptions = {
+ opt: ParseOptions = {
header: false
}
): Promise<unknown[]> {
let r: string[][];
if (input instanceof BufReader) {
- r = await readAll(input, opt);
+ r = await readMatrix(input, opt);
} else {
- r = await readAll(new BufReader(new StringReader(input)), opt);
+ r = await readMatrix(new BufReader(new StringReader(input)), opt);
}
if (opt.header) {
- let headers: HeaderOption[] = [];
+ let headers: HeaderOptions[] = [];
let i = 0;
if (Array.isArray(opt.header)) {
if (typeof opt.header[0] !== "string") {
- headers = opt.header as HeaderOption[];
+ headers = opt.header as HeaderOptions[];
} else {
const h = opt.header as string[];
headers = h.map(
- (e): HeaderOption => {
+ (e): HeaderOptions => {
return {
name: e
};
@@ -216,7 +216,7 @@ export async function parse(
}
} else {
headers = r.shift()!.map(
- (e): HeaderOption => {
+ (e): HeaderOptions => {
return {
name: e
};
diff --git a/std/encoding/csv_test.ts b/std/encoding/csv_test.ts
index 65a86a353..daa30d0fb 100644
--- a/std/encoding/csv_test.ts
+++ b/std/encoding/csv_test.ts
@@ -2,7 +2,7 @@
// https://github.com/golang/go/blob/2cc15b1/src/encoding/csv/reader_test.go
import { test, runIfMain } from "../testing/mod.ts";
import { assertEquals, assert } from "../testing/asserts.ts";
-import { readAll, parse } from "./csv.ts";
+import { readMatrix, parse } from "./csv.ts";
import { StringReader } from "../io/readers.ts";
import { BufReader } from "../io/bufio.ts";
@@ -477,7 +477,7 @@ for (const t of testCases) {
if (t.Error) {
let err;
try {
- actual = await readAll(new BufReader(new StringReader(t.Input!)), {
+ actual = await readMatrix(new BufReader(new StringReader(t.Input!)), {
comma: comma,
comment: comment,
trimLeadingSpace: trim,
@@ -490,7 +490,7 @@ for (const t of testCases) {
assert(err);
assertEquals(err.message, t.Error);
} else {
- actual = await readAll(new BufReader(new StringReader(t.Input!)), {
+ actual = await readMatrix(new BufReader(new StringReader(t.Input!)), {
comma: comma,
comment: comment,
trimLeadingSpace: trim,
diff --git a/std/encoding/mod.ts b/std/encoding/mod.ts
new file mode 100644
index 000000000..03bde0294
--- /dev/null
+++ b/std/encoding/mod.ts
@@ -0,0 +1,14 @@
+export {
+ HeaderOptions as CsvHeaderOptions,
+ ParseError as CsvParseError,
+ ParseOptions as ParseCsvOptions,
+ parse as parseCsv
+} from "./csv.ts";
+export {
+ decode as decodeHex,
+ decodeString as decodeHexString,
+ encode as encodeToHex,
+ encodeToString as encodeToHexString
+} from "./hex.ts";
+export { parse as parseToml, stringify as tomlStringify } from "./toml.ts";
+export { parse as parseYaml, stringify as yamlStringify } from "./yaml.ts";
diff --git a/std/encoding/yaml.ts b/std/encoding/yaml.ts
index 15b12c04f..27c2874ec 100644
--- a/std/encoding/yaml.ts
+++ b/std/encoding/yaml.ts
@@ -3,6 +3,9 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-export * from "./yaml/parse.ts";
-export * from "./yaml/stringify.ts";
+export { ParseOptions, parse } from "./yaml/parse.ts";
+export {
+ DumpOptions as StringifyOptions,
+ stringify
+} from "./yaml/stringify.ts";
export * from "./yaml/schema/mod.ts";