mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
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.
This commit is contained in:
parent
c0485bf863
commit
90bd2ad881
1
archive/mod.ts
Normal file
1
archive/mod.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./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<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
|
||||
* @param buffer
|
||||
|
@ -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 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<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" }
|
||||
// ]
|
||||
// [["a", "b", "c"], ["d", "e", "f"]]
|
||||
```
|
||||
|
||||
## TOML
|
||||
|
@ -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
|
||||
};
|
||||
|
@ -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,
|
||||
|
14
encoding/mod.ts
Normal file
14
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 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";
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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"), "[31mfoo bar[39m");
|
||||
});
|
||||
|
||||
|
2
fmt/mod.ts
Normal file
2
fmt/mod.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./colors.ts";
|
||||
export * from "./sprintf.ts";
|
3
http/mod.ts
Normal file
3
http/mod.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from "./cookie.ts";
|
||||
export * from "./http_status.ts";
|
||||
export * from "./server.ts";
|
4
io/mod.ts
Normal file
4
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";
|
17
manual.md
17
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
|
||||
|
1
mime/mod.ts
Normal file
1
mime/mod.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./multipart.ts";
|
1
multipart/mod.ts
Normal file
1
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:
|
||||
|
||||
```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",
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user