mirror of
https://github.com/denoland/deno.git
synced 2025-01-09 07:39:15 -05:00
feat: Add missing mod.ts files in std (#3509)
std/archive/tar.ts: - Remove FileReader. - Remove FileWriter. std/encoding/csv.ts: - ExtendedParseOptions -> ParseOptions - HeaderOption -> HeaderOptions - ParseOptions -> ReadOptions - readAll() -> readMatrix() std/encoding/yaml.ts: - DumpOptions -> StringifyOptions std/fmt/colors.ts: - getEnabled() -> getColorEnabled() - setEnabled() -> setColorEnabled() std/testing/mod.ts: - Re-export sibling modules.
This commit is contained in:
parent
29562ed61e
commit
e8d82a6348
17 changed files with 89 additions and 146 deletions
1
std/archive/mod.ts
Normal file
1
std/archive/mod.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export * from "./tar.ts";
|
|
@ -35,7 +35,7 @@ const ustar = "ustar\u000000";
|
||||||
/**
|
/**
|
||||||
* Simple file reader
|
* Simple file reader
|
||||||
*/
|
*/
|
||||||
export class FileReader implements Deno.Reader {
|
class FileReader implements Deno.Reader {
|
||||||
private file?: Deno.File;
|
private file?: Deno.File;
|
||||||
|
|
||||||
constructor(private filePath: string, private mode: Deno.OpenMode = "r") {}
|
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<number> {
|
|
||||||
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
|
* Remove the trailing null codes
|
||||||
* @param buffer
|
* @param buffer
|
||||||
|
|
|
@ -2,100 +2,21 @@
|
||||||
|
|
||||||
## CSV
|
## CSV
|
||||||
|
|
||||||
- **`readAll(reader: BufReader, opt: ParseOptions = { comma: ",", trimLeadingSpace: false, lazyQuotes: false } ): Promise<[string[][], BufState]>`**:
|
- **`parseCsv(input: string | BufReader, opt: ParseCsvOptions): Promise<unknown[]>`**:
|
||||||
Read the whole buffer and output the structured CSV datas
|
Read the string/buffer into an
|
||||||
- **`parse(csvString: string, opt: ParseOption): Promise<unknown[]>`**: See
|
|
||||||
[parse](###Parse)
|
|
||||||
|
|
||||||
### Parse
|
### Usage
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// input:
|
const string = "a,b,c\nd,e,f";
|
||||||
// a,b,c
|
|
||||||
// e,f,g
|
|
||||||
|
|
||||||
const r = await parseFile(filepath, {
|
console.log(
|
||||||
header: false
|
await parseCsv(string, {
|
||||||
});
|
header: false
|
||||||
|
})
|
||||||
|
);
|
||||||
// output:
|
// output:
|
||||||
// [["a", "b", "c"], ["e", "f", "g"]]
|
// [["a", "b", "c"], ["d", "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 };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// output:
|
|
||||||
// [
|
|
||||||
// { super: "a", street: "b", fighter: "c" },
|
|
||||||
// { super: "e", street: "f", fighter: "g" }
|
|
||||||
// ]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## TOML
|
## TOML
|
||||||
|
|
|
@ -28,7 +28,7 @@ export class ParseError extends Error {
|
||||||
* @property fieldsPerRecord - Enabling the check of fields for each row.
|
* @property fieldsPerRecord - Enabling the check of fields for each row.
|
||||||
* If == 0, first row is used as referal for the number of fields.
|
* If == 0, first row is used as referal for the number of fields.
|
||||||
*/
|
*/
|
||||||
export interface ParseOptions {
|
export interface ReadOptions {
|
||||||
comma?: string;
|
comma?: string;
|
||||||
comment?: string;
|
comment?: string;
|
||||||
trimLeadingSpace?: boolean;
|
trimLeadingSpace?: boolean;
|
||||||
|
@ -36,7 +36,7 @@ export interface ParseOptions {
|
||||||
fieldsPerRecord?: number;
|
fieldsPerRecord?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function chkOptions(opt: ParseOptions): void {
|
function chkOptions(opt: ReadOptions): void {
|
||||||
if (!opt.comma) opt.comma = ",";
|
if (!opt.comma) opt.comma = ",";
|
||||||
if (!opt.trimLeadingSpace) opt.trimLeadingSpace = false;
|
if (!opt.trimLeadingSpace) opt.trimLeadingSpace = false;
|
||||||
if (
|
if (
|
||||||
|
@ -51,7 +51,7 @@ function chkOptions(opt: ParseOptions): void {
|
||||||
async function read(
|
async function read(
|
||||||
Startline: number,
|
Startline: number,
|
||||||
reader: BufReader,
|
reader: BufReader,
|
||||||
opt: ParseOptions = { comma: ",", trimLeadingSpace: false }
|
opt: ReadOptions = { comma: ",", trimLeadingSpace: false }
|
||||||
): Promise<string[] | Deno.EOF> {
|
): Promise<string[] | Deno.EOF> {
|
||||||
const tp = new TextProtoReader(reader);
|
const tp = new TextProtoReader(reader);
|
||||||
let line: string;
|
let line: string;
|
||||||
|
@ -107,9 +107,9 @@ async function read(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readAll(
|
export async function readMatrix(
|
||||||
reader: BufReader,
|
reader: BufReader,
|
||||||
opt: ParseOptions = {
|
opt: ReadOptions = {
|
||||||
comma: ",",
|
comma: ",",
|
||||||
trimLeadingSpace: false,
|
trimLeadingSpace: false,
|
||||||
lazyQuotes: 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
|
* and the parse function for each entry of the
|
||||||
* column.
|
* column.
|
||||||
*/
|
*/
|
||||||
export interface HeaderOption {
|
export interface HeaderOptions {
|
||||||
name: string;
|
name: string;
|
||||||
parse?: (input: string) => unknown;
|
parse?: (input: string) => unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ExtendedParseOptions extends ParseOptions {
|
export interface ParseOptions extends ReadOptions {
|
||||||
header: boolean | string[] | HeaderOption[];
|
header: boolean | string[] | HeaderOptions[];
|
||||||
parse?: (input: unknown) => unknown;
|
parse?: (input: unknown) => unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,26 +188,26 @@ export interface ExtendedParseOptions extends ParseOptions {
|
||||||
*/
|
*/
|
||||||
export async function parse(
|
export async function parse(
|
||||||
input: string | BufReader,
|
input: string | BufReader,
|
||||||
opt: ExtendedParseOptions = {
|
opt: ParseOptions = {
|
||||||
header: false
|
header: false
|
||||||
}
|
}
|
||||||
): Promise<unknown[]> {
|
): Promise<unknown[]> {
|
||||||
let r: string[][];
|
let r: string[][];
|
||||||
if (input instanceof BufReader) {
|
if (input instanceof BufReader) {
|
||||||
r = await readAll(input, opt);
|
r = await readMatrix(input, opt);
|
||||||
} else {
|
} else {
|
||||||
r = await readAll(new BufReader(new StringReader(input)), opt);
|
r = await readMatrix(new BufReader(new StringReader(input)), opt);
|
||||||
}
|
}
|
||||||
if (opt.header) {
|
if (opt.header) {
|
||||||
let headers: HeaderOption[] = [];
|
let headers: HeaderOptions[] = [];
|
||||||
let i = 0;
|
let i = 0;
|
||||||
if (Array.isArray(opt.header)) {
|
if (Array.isArray(opt.header)) {
|
||||||
if (typeof opt.header[0] !== "string") {
|
if (typeof opt.header[0] !== "string") {
|
||||||
headers = opt.header as HeaderOption[];
|
headers = opt.header as HeaderOptions[];
|
||||||
} else {
|
} else {
|
||||||
const h = opt.header as string[];
|
const h = opt.header as string[];
|
||||||
headers = h.map(
|
headers = h.map(
|
||||||
(e): HeaderOption => {
|
(e): HeaderOptions => {
|
||||||
return {
|
return {
|
||||||
name: e
|
name: e
|
||||||
};
|
};
|
||||||
|
@ -216,7 +216,7 @@ export async function parse(
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
headers = r.shift()!.map(
|
headers = r.shift()!.map(
|
||||||
(e): HeaderOption => {
|
(e): HeaderOptions => {
|
||||||
return {
|
return {
|
||||||
name: e
|
name: e
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// https://github.com/golang/go/blob/2cc15b1/src/encoding/csv/reader_test.go
|
// https://github.com/golang/go/blob/2cc15b1/src/encoding/csv/reader_test.go
|
||||||
import { test, runIfMain } from "../testing/mod.ts";
|
import { test, runIfMain } from "../testing/mod.ts";
|
||||||
import { assertEquals, assert } from "../testing/asserts.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 { StringReader } from "../io/readers.ts";
|
||||||
import { BufReader } from "../io/bufio.ts";
|
import { BufReader } from "../io/bufio.ts";
|
||||||
|
|
||||||
|
@ -477,7 +477,7 @@ for (const t of testCases) {
|
||||||
if (t.Error) {
|
if (t.Error) {
|
||||||
let err;
|
let err;
|
||||||
try {
|
try {
|
||||||
actual = await readAll(new BufReader(new StringReader(t.Input!)), {
|
actual = await readMatrix(new BufReader(new StringReader(t.Input!)), {
|
||||||
comma: comma,
|
comma: comma,
|
||||||
comment: comment,
|
comment: comment,
|
||||||
trimLeadingSpace: trim,
|
trimLeadingSpace: trim,
|
||||||
|
@ -490,7 +490,7 @@ for (const t of testCases) {
|
||||||
assert(err);
|
assert(err);
|
||||||
assertEquals(err.message, t.Error);
|
assertEquals(err.message, t.Error);
|
||||||
} else {
|
} else {
|
||||||
actual = await readAll(new BufReader(new StringReader(t.Input!)), {
|
actual = await readMatrix(new BufReader(new StringReader(t.Input!)), {
|
||||||
comma: comma,
|
comma: comma,
|
||||||
comment: comment,
|
comment: comment,
|
||||||
trimLeadingSpace: trim,
|
trimLeadingSpace: trim,
|
||||||
|
|
14
std/encoding/mod.ts
Normal file
14
std/encoding/mod.ts
Normal file
|
@ -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";
|
|
@ -3,6 +3,9 @@
|
||||||
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
|
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
export * from "./yaml/parse.ts";
|
export { ParseOptions, parse } from "./yaml/parse.ts";
|
||||||
export * from "./yaml/stringify.ts";
|
export {
|
||||||
|
DumpOptions as StringifyOptions,
|
||||||
|
stringify
|
||||||
|
} from "./yaml/stringify.ts";
|
||||||
export * from "./yaml/schema/mod.ts";
|
export * from "./yaml/schema/mod.ts";
|
||||||
|
|
|
@ -21,7 +21,7 @@ interface Code {
|
||||||
|
|
||||||
let enabled = !noColor;
|
let enabled = !noColor;
|
||||||
|
|
||||||
export function setEnabled(value: boolean): void {
|
export function setColorEnabled(value: boolean): void {
|
||||||
if (noColor) {
|
if (noColor) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ export function setEnabled(value: boolean): void {
|
||||||
enabled = value;
|
enabled = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getEnabled(): boolean {
|
export function getColorEnabled(): boolean {
|
||||||
return enabled;
|
return enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,10 @@ test(function replacesCloseCharacters(): void {
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function enablingColors(): void {
|
test(function enablingColors(): void {
|
||||||
assertEquals(c.getEnabled(), true);
|
assertEquals(c.getColorEnabled(), true);
|
||||||
c.setEnabled(false);
|
c.setColorEnabled(false);
|
||||||
assertEquals(c.bgBlue(c.red("foo bar")), "foo bar");
|
assertEquals(c.bgBlue(c.red("foo bar")), "foo bar");
|
||||||
c.setEnabled(true);
|
c.setColorEnabled(true);
|
||||||
assertEquals(c.red("foo bar"), "[31mfoo bar[39m");
|
assertEquals(c.red("foo bar"), "[31mfoo bar[39m");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
2
std/fmt/mod.ts
Normal file
2
std/fmt/mod.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export * from "./colors.ts";
|
||||||
|
export * from "./sprintf.ts";
|
3
std/http/mod.ts
Normal file
3
std/http/mod.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export * from "./cookie.ts";
|
||||||
|
export * from "./http_status.ts";
|
||||||
|
export * from "./server.ts";
|
4
std/io/mod.ts
Normal file
4
std/io/mod.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export * from "./bufio.ts";
|
||||||
|
export * from "./ioutil.ts";
|
||||||
|
export * from "./readers.ts";
|
||||||
|
export * from "./writers.ts";
|
|
@ -533,8 +533,11 @@ browser JavaScript, Deno can import libraries directly from URLs. This example
|
||||||
uses a URL to import a test runner library:
|
uses a URL to import a test runner library:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { test, runIfMain } from "https://deno.land/std/testing/mod.ts";
|
import {
|
||||||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
assertEquals,
|
||||||
|
runIfMain,
|
||||||
|
test
|
||||||
|
} from "https://deno.land/std/testing/mod.ts";
|
||||||
|
|
||||||
test(function t1() {
|
test(function t1() {
|
||||||
assertEquals("hello", "hello");
|
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
|
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
|
Node's `package.json` file). For example, let's say you were using the above
|
||||||
testing library across a large project. Rather than importing
|
testing library across a large project. Rather than importing
|
||||||
`"https://deno.land/std/testing/mod.ts"` and
|
`"https://deno.land/std/testing/mod.ts"` everywhere, you could create a
|
||||||
`"https://deno.land/std/testing/asserts.ts"` everywhere, you could create a
|
|
||||||
`deps.ts` file that exports the third-party code:
|
`deps.ts` file that exports the third-party code:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
export { runTests, test } from "https://deno.land/std/testing/mod.ts";
|
export {
|
||||||
export { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
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
|
And throughout the same project, you can import from the `deps.ts` and avoid
|
||||||
|
|
1
std/mime/mod.ts
Normal file
1
std/mime/mod.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export * from "./multipart.ts";
|
1
std/multipart/mod.ts
Normal file
1
std/multipart/mod.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export * from "./formfile.ts";
|
|
@ -48,8 +48,11 @@ Asserts are exposed in `testing/asserts.ts` module.
|
||||||
Basic usage:
|
Basic usage:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { runTests, test } from "https://deno.land/std/testing/mod.ts";
|
import {
|
||||||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
|
assertEquals,
|
||||||
|
runTests,
|
||||||
|
test
|
||||||
|
} from "https://deno.land/std/testing/mod.ts";
|
||||||
|
|
||||||
test({
|
test({
|
||||||
name: "testing example",
|
name: "testing example",
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// 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 {
|
import {
|
||||||
bgRed,
|
bgRed,
|
||||||
white,
|
white,
|
||||||
|
|
Loading…
Reference in a new issue