From 90bd2ad88164754fdecceaa7d1831d4e76bf1db1 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 20 Dec 2019 20:21:30 +0000 Subject: [PATCH] feat: Add missing mod.ts files in std (denoland/deno#3509) archive/tar.ts: - Remove FileReader. - Remove FileWriter. encoding/csv.ts: - ExtendedParseOptions -> ParseOptions - HeaderOption -> HeaderOptions - ParseOptions -> ReadOptions - readAll() -> readMatrix() encoding/yaml.ts: - DumpOptions -> StringifyOptions fmt/colors.ts: - getEnabled() -> getColorEnabled() - setEnabled() -> setColorEnabled() testing/mod.ts: - Re-export sibling modules. --- archive/mod.ts | 1 + archive/tar.ts | 24 +---------- encoding/README.md | 99 +++++--------------------------------------- encoding/csv.ts | 32 +++++++------- encoding/csv_test.ts | 6 +-- encoding/mod.ts | 14 +++++++ encoding/yaml.ts | 7 +++- fmt/colors.ts | 4 +- fmt/colors_test.ts | 6 +-- fmt/mod.ts | 2 + http/mod.ts | 3 ++ io/mod.ts | 4 ++ manual.md | 17 +++++--- mime/mod.ts | 1 + multipart/mod.ts | 1 + testing/README.md | 7 +++- testing/mod.ts | 7 ++++ 17 files changed, 89 insertions(+), 146 deletions(-) create mode 100644 archive/mod.ts create mode 100644 encoding/mod.ts create mode 100644 fmt/mod.ts create mode 100644 http/mod.ts create mode 100644 io/mod.ts create mode 100644 mime/mod.ts create mode 100644 multipart/mod.ts diff --git a/archive/mod.ts b/archive/mod.ts new file mode 100644 index 000000000..254b8266f --- /dev/null +++ b/archive/mod.ts @@ -0,0 +1 @@ +export * from "./tar.ts"; diff --git a/archive/tar.ts b/archive/tar.ts index 8ebfa7fb5..e53cd9111 100644 --- a/archive/tar.ts +++ b/archive/tar.ts @@ -35,7 +35,7 @@ const ustar = "ustar\u000000"; /** * Simple file reader */ -export class FileReader implements Deno.Reader { +class FileReader implements Deno.Reader { private file?: Deno.File; constructor(private filePath: string, private mode: Deno.OpenMode = "r") {} @@ -53,28 +53,6 @@ export class FileReader implements Deno.Reader { } } -/** - * Simple file writer (call FileWriter.dispose() after use) - */ -export class FileWriter implements Deno.Writer { - private file?: Deno.File; - - constructor(private filePath: string, private mode: Deno.OpenMode = "w") {} - - public async write(p: Uint8Array): Promise { - if (!this.file) { - this.file = await Deno.open(this.filePath, this.mode); - } - return Deno.write(this.file.rid, p); - } - - public dispose(): void { - if (!this.file) return; - Deno.close(this.file.rid); - this.file = undefined; - } -} - /** * Remove the trailing null codes * @param buffer diff --git a/encoding/README.md b/encoding/README.md index 767cbdcb4..ee06aa3ec 100644 --- a/encoding/README.md +++ b/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`**: See - [parse](###Parse) +- **`parseCsv(input: string | BufReader, opt: ParseCsvOptions): Promise`**: + 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 string = "a,b,c\nd,e,f"; -const r = await parseFile(filepath, { - header: false -}); +console.log( + await parseCsv(string, { + 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) => { - return { super: e.this, street: e.is, fighter: e.sparta }; - } -}); -// output: -// [ -// { super: "a", street: "b", fighter: "c" }, -// { super: "e", street: "f", fighter: "g" } -// ] +// [["a", "b", "c"], ["d", "e", "f"]] ``` ## TOML diff --git a/encoding/csv.ts b/encoding/csv.ts index 10d72a8a5..8cfa1cab9 100644 --- a/encoding/csv.ts +++ b/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 { 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 { 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/encoding/csv_test.ts b/encoding/csv_test.ts index 65a86a353..daa30d0fb 100644 --- a/encoding/csv_test.ts +++ b/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/encoding/mod.ts b/encoding/mod.ts new file mode 100644 index 000000000..03bde0294 --- /dev/null +++ b/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/encoding/yaml.ts b/encoding/yaml.ts index 15b12c04f..27c2874ec 100644 --- a/encoding/yaml.ts +++ b/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"; diff --git a/fmt/colors.ts b/fmt/colors.ts index 52ba22487..3f241d5b0 100644 --- a/fmt/colors.ts +++ b/fmt/colors.ts @@ -21,7 +21,7 @@ interface Code { let enabled = !noColor; -export function setEnabled(value: boolean): void { +export function setColorEnabled(value: boolean): void { if (noColor) { return; } @@ -29,7 +29,7 @@ export function setEnabled(value: boolean): void { enabled = value; } -export function getEnabled(): boolean { +export function getColorEnabled(): boolean { return enabled; } diff --git a/fmt/colors_test.ts b/fmt/colors_test.ts index 949bc34e7..467cda1bb 100644 --- a/fmt/colors_test.ts +++ b/fmt/colors_test.ts @@ -17,10 +17,10 @@ test(function replacesCloseCharacters(): void { }); test(function enablingColors(): void { - assertEquals(c.getEnabled(), true); - c.setEnabled(false); + assertEquals(c.getColorEnabled(), true); + c.setColorEnabled(false); assertEquals(c.bgBlue(c.red("foo bar")), "foo bar"); - c.setEnabled(true); + c.setColorEnabled(true); assertEquals(c.red("foo bar"), "foo bar"); }); diff --git a/fmt/mod.ts b/fmt/mod.ts new file mode 100644 index 000000000..a0f8feb8b --- /dev/null +++ b/fmt/mod.ts @@ -0,0 +1,2 @@ +export * from "./colors.ts"; +export * from "./sprintf.ts"; diff --git a/http/mod.ts b/http/mod.ts new file mode 100644 index 000000000..3746242a9 --- /dev/null +++ b/http/mod.ts @@ -0,0 +1,3 @@ +export * from "./cookie.ts"; +export * from "./http_status.ts"; +export * from "./server.ts"; diff --git a/io/mod.ts b/io/mod.ts new file mode 100644 index 000000000..ca07bac43 --- /dev/null +++ b/io/mod.ts @@ -0,0 +1,4 @@ +export * from "./bufio.ts"; +export * from "./ioutil.ts"; +export * from "./readers.ts"; +export * from "./writers.ts"; diff --git a/manual.md b/manual.md index ca4a269f6..b99c8f28f 100644 --- a/manual.md +++ b/manual.md @@ -533,8 +533,11 @@ browser JavaScript, Deno can import libraries directly from URLs. This example uses a URL to import a test runner library: ```ts -import { test, runIfMain } from "https://deno.land/std/testing/mod.ts"; -import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; +import { + assertEquals, + runIfMain, + test +} from "https://deno.land/std/testing/mod.ts"; test(function t1() { assertEquals("hello", "hello"); @@ -597,13 +600,15 @@ everywhere in a large project?** The solution is to import and re-export your external libraries in a central `deps.ts` file (which serves the same purpose as Node's `package.json` file). For example, let's say you were using the above testing library across a large project. Rather than importing -`"https://deno.land/std/testing/mod.ts"` and -`"https://deno.land/std/testing/asserts.ts"` everywhere, you could create a +`"https://deno.land/std/testing/mod.ts"` everywhere, you could create a `deps.ts` file that exports the third-party code: ```ts -export { runTests, test } from "https://deno.land/std/testing/mod.ts"; -export { assertEquals } from "https://deno.land/std/testing/asserts.ts"; +export { + assertEquals, + runTests, + test +} from "https://deno.land/std/testing/mod.ts"; ``` And throughout the same project, you can import from the `deps.ts` and avoid diff --git a/mime/mod.ts b/mime/mod.ts new file mode 100644 index 000000000..9b75002e0 --- /dev/null +++ b/mime/mod.ts @@ -0,0 +1 @@ +export * from "./multipart.ts"; diff --git a/multipart/mod.ts b/multipart/mod.ts new file mode 100644 index 000000000..8e8a665a9 --- /dev/null +++ b/multipart/mod.ts @@ -0,0 +1 @@ +export * from "./formfile.ts"; diff --git a/testing/README.md b/testing/README.md index 8d02f9d79..afaf76b86 100644 --- a/testing/README.md +++ b/testing/README.md @@ -48,8 +48,11 @@ Asserts are exposed in `testing/asserts.ts` module. Basic usage: ```ts -import { runTests, test } from "https://deno.land/std/testing/mod.ts"; -import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; +import { + assertEquals, + runTests, + test +} from "https://deno.land/std/testing/mod.ts"; test({ name: "testing example", diff --git a/testing/mod.ts b/testing/mod.ts index 0d5d2702a..06a1df958 100644 --- a/testing/mod.ts +++ b/testing/mod.ts @@ -1,5 +1,12 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +export * from "./asserts.ts"; +export * from "./bench.ts"; +import diff from "./diff.ts"; +export { diff }; +export * from "./format.ts"; +export * from "./runner.ts"; + import { bgRed, white,