chore: remove std directory (#9361)

This removes the std folder from the tree.

Various parts of the tests are pretty tightly dependent 
on std (47 direct imports and 75 indirect imports, not 
counting the cli tests that use them as fixtures) so I've 
added std as a submodule for now.
This commit is contained in:
Casper Beyer 2021-02-02 19:05:46 +08:00 committed by GitHub
parent a2b5d44f1a
commit 6abf126c2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
516 changed files with 82 additions and 72877 deletions

View File

@ -24,12 +24,9 @@
"cli/tests/inline_js_source_map*",
"cli/tests/badly_formatted.md",
"cli/tsc/*typescript.js",
"test_util/std",
"test_util/wpt",
"gh-pages",
"std/**/testdata",
"std/**/vendor",
"std/node_modules",
"std/hash/_wasm",
"target",
"third_party",
"tools/wpt/expectation.json",

View File

@ -57,8 +57,7 @@ jobs:
startsWith(matrix.os, 'ubuntu') &&
matrix.kind == 'test_release' &&
github.repository == 'denoland/deno' &&
startsWith(github.ref, 'refs/tags/') &&
!startsWith(github.ref, 'refs/tags/std/')
startsWith(github.ref, 'refs/tags/')
run: |
mkdir -p target/release
tar --exclude=.cargo_home --exclude=".git*" --exclude=target --exclude=third_party/prebuilt -czvf target/release/deno_src.tar.gz -C .. deno
@ -112,9 +111,8 @@ jobs:
runner.os != 'Windows' &&
matrix.kind == 'test_release' &&
github.repository == 'denoland/deno' &&
(github.ref == 'refs/heads/master' ||
startsWith(github.ref, 'refs/tags/') &&
!startsWith(github.ref, 'refs/tags/std/'))
github.ref == 'refs/heads/master' ||
startsWith(github.ref, 'refs/tags/')
uses: google-github-actions/setup-gcloud@master
with:
project_id: denoland
@ -126,9 +124,8 @@ jobs:
runner.os == 'Windows' &&
matrix.kind == 'test_release' &&
github.repository == 'denoland/deno' &&
(github.ref == 'refs/heads/master' ||
startsWith(github.ref, 'refs/tags/') &&
!startsWith(github.ref, 'refs/tags/std/'))
github.ref == 'refs/heads/master' ||
startsWith(github.ref, 'refs/tags/')
uses: google-github-actions/setup-gcloud@master
env:
CLOUDSDK_PYTHON: ${{env.pythonLocation}}\python.exe
@ -288,8 +285,7 @@ jobs:
runner.os != 'Windows' &&
matrix.kind == 'test_release' &&
github.repository == 'denoland/deno' &&
startsWith(github.ref, 'refs/tags/') &&
!startsWith(github.ref, 'refs/tags/std/')
startsWith(github.ref, 'refs/tags/')
run: |
gsutil cp ./target/release/*.zip gs://dl.deno.land/release/${GITHUB_REF#refs/*/}/
echo ${GITHUB_REF#refs/*/} > release-latest.txt
@ -300,8 +296,7 @@ jobs:
runner.os == 'Windows' &&
matrix.kind == 'test_release' &&
github.repository == 'denoland/deno' &&
startsWith(github.ref, 'refs/tags/') &&
!startsWith(github.ref, 'refs/tags/std/')
startsWith(github.ref, 'refs/tags/')
env:
CLOUDSDK_PYTHON: ${{env.pythonLocation}}\python.exe
shell: bash
@ -315,8 +310,7 @@ jobs:
if: |
matrix.kind == 'test_release' &&
github.repository == 'denoland/deno' &&
startsWith(github.ref, 'refs/tags/') &&
!startsWith(github.ref, 'refs/tags/std/')
startsWith(github.ref, 'refs/tags/')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:

8
.gitmodules vendored
View File

@ -2,11 +2,11 @@
path = third_party
url = https://github.com/denoland/deno_third_party.git
shallow = true
[submodule "std/wasi/testdata"]
path = std/wasi/testdata
url = https://github.com/khronosproject/wasi-test-suite.git
shallow = true
[submodule "test_util/wpt"]
path = test_util/wpt
url = https://github.com/denoland/wpt.git
shallow = true
[submodule "test_util/std"]
path = test_util/std
url = https://github.com/denoland/deno_std
shallow = true

View File

@ -1,5 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { serve, ServerRequest } from "../std/http/server.ts";
import { serve, ServerRequest } from "../test_util/std/http/server.ts";
const addr = Deno.args[0] || "127.0.0.1:4500";
const originAddr = Deno.args[1] || "127.0.0.1:4501";

View File

@ -191,7 +191,7 @@ fn deno_http(deno_exe: &str) -> Result<HttpBenchmarkResult> {
"--allow-net",
"--reload",
"--unstable",
"std/http/bench.ts",
"test_util/std/http/bench.ts",
&server_addr(port),
],
port,

View File

@ -79,7 +79,11 @@ const EXEC_TIME_BENCHMARKS: &[(&str, &[&str], Option<i32>)] = &[
),
(
"check",
&["cache", "--reload", "std/examples/chat/server_test.ts"],
&[
"cache",
"--reload",
"test_util/std/examples/chat/server_test.ts",
],
None,
),
(
@ -88,18 +92,22 @@ const EXEC_TIME_BENCHMARKS: &[(&str, &[&str], Option<i32>)] = &[
"cache",
"--reload",
"--no-check",
"std/examples/chat/server_test.ts",
"test_util/std/examples/chat/server_test.ts",
],
None,
),
(
"bundle",
&["bundle", "std/examples/chat/server_test.ts"],
&["bundle", "test_util/std/examples/chat/server_test.ts"],
None,
),
(
"bundle_no_check",
&["bundle", "--no-check", "std/examples/chat/server_test.ts"],
&[
"bundle",
"--no-check",
"test_util/std/examples/chat/server_test.ts",
],
None,
),
];
@ -254,8 +262,8 @@ fn get_binary_sizes(target_dir: &PathBuf) -> Result<HashMap<String, u64>> {
}
const BUNDLES: &[(&str, &str)] = &[
("file_server", "./std/http/file_server.ts"),
("gist", "./std/examples/gist.ts"),
("file_server", "./test_util/std/http/file_server.ts"),
("gist", "./test_util/std/examples/gist.ts"),
];
fn bundle_benchmark(deno_exe: &PathBuf) -> Result<HashMap<String, u64>> {
let mut sizes = HashMap::<String, u64>::new();

View File

@ -313,7 +313,7 @@ mod tests {
Some(
read(
test_util::root_path()
.join("std/http/testdata/tls/RootCA.pem")
.join("cli/tests/tls/RootCA.pem")
.to_str()
.unwrap(),
)
@ -345,7 +345,7 @@ mod tests {
Some(
read(
test_util::root_path()
.join("std/http/testdata/tls/RootCA.pem")
.join("cli/tests/tls/RootCA.pem")
.to_str()
.unwrap(),
)
@ -376,7 +376,7 @@ mod tests {
Some(
read(
test_util::root_path()
.join("std/http/testdata/tls/RootCA.pem")
.join("cli/tests/tls/RootCA.pem")
.to_str()
.unwrap(),
)
@ -416,7 +416,7 @@ mod tests {
Some(
read(
test_util::root_path()
.join("std/http/testdata/tls/RootCA.pem")
.join("cli/tests/tls/RootCA.pem")
.to_str()
.unwrap(),
)

View File

@ -1,4 +1,4 @@
import { assert } from "../../../std/testing/asserts.ts";
import { assert } from "../../../test_util/std/testing/asserts.ts";
import "./nest_imported.ts";
const handler = (e: Event): void => {

View File

@ -1,4 +1,4 @@
import { assert } from "../../../std/testing/asserts.ts";
import { assert } from "../../../test_util/std/testing/asserts.ts";
import "./imported.ts";
assert(window.hasOwnProperty("onload"));

View File

@ -1,4 +1,4 @@
import { assert } from "../../../std/testing/asserts.ts";
import { assert } from "../../../test_util/std/testing/asserts.ts";
const handler = (e: Event): void => {
assert(!e.cancelable);

View File

@ -1,3 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
const res = await fetch("http://localhost:4545/std/examples/colors.ts");
const res = await fetch(
"http://localhost:4545/test_util/std/examples/colors.ts",
);
console.log(`Response http: ${await res.text()}`);

View File

@ -1,6 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { serve, ServerRequest } from "../../std/http/server.ts";
import { assertEquals } from "../../std/testing/asserts.ts";
import { serve, ServerRequest } from "../../test_util/std/http/server.ts";
import { assertEquals } from "../../test_util/std/testing/asserts.ts";
const addr = Deno.args[1] || "127.0.0.1:4555";
@ -54,7 +54,7 @@ async function testModuleDownload(): Promise<void> {
"cache",
"--reload",
"--quiet",
"http://localhost:4545/std/examples/colors.ts",
"http://localhost:4545/test_util/std/examples/colors.ts",
],
stdout: "piped",
env: {
@ -96,7 +96,7 @@ async function testModuleDownloadNoProxy(): Promise<void> {
"cache",
"--reload",
"--quiet",
"http://localhost:4545/std/examples/colors.ts",
"http://localhost:4545/test_util/std/examples/colors.ts",
],
stdout: "piped",
env: {

View File

@ -1,4 +1,4 @@
Proxy server listening on [WILDCARD]
Proxy request to: http://localhost:4545/std/examples/colors.ts
Proxy request to: http://localhost:4545/std/examples/colors.ts
Proxy request to: http://localhost:4545/std/fmt/colors.ts
Proxy request to: http://localhost:4545/test_util/std/examples/colors.ts
Proxy request to: http://localhost:4545/test_util/std/examples/colors.ts
Proxy request to: http://localhost:4545/test_util/std/fmt/colors.ts

View File

@ -3,7 +3,7 @@ import {
assert,
assertEquals,
assertThrowsAsync,
} from "../../std/testing/asserts.ts";
} from "../../test_util/std/testing/asserts.ts";
Deno.test({
name: "Deno.emit() - sources provided",

View File

@ -1,64 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
const { args } = Deno;
import { createHash, SupportedAlgorithm } from "../../std/hash/mod.ts";
import { Md5 } from "../../std/hash/md5.ts";
import { Sha1 } from "../../std/hash/sha1.ts";
import { Sha256 } from "../../std/hash/sha256.ts";
import { Sha512 } from "../../std/hash/sha512.ts";
// deno-lint-ignore camelcase
import { Sha3_224, Sha3_256, Sha3_384, Sha3_512 } from "../../std/hash/sha3.ts";
if (args.length < 3) Deno.exit(0);
const method = args[0];
const alg = args[1];
const inputFile = args[2];
// deno-lint-ignore no-explicit-any
function getJsHash(alg: string): any {
switch (alg) {
case "md5":
return new Md5();
case "sha1":
return new Sha1();
case "sha224":
return new Sha256(true);
case "sha256":
return new Sha256();
case "sha3-224":
return new Sha3_224();
case "sha3-256":
return new Sha3_256();
case "sha3-384":
return new Sha3_384();
case "sha3-512":
return new Sha3_512();
case "sha512":
return new Sha512();
default:
return null;
}
}
const f = Deno.openSync(inputFile, { read: true });
const buffer = Deno.readAllSync(f);
f.close();
let hash = null;
console.time("hash");
if (method === "rust") {
hash = createHash(alg as SupportedAlgorithm);
} else if (method === "js") {
hash = getJsHash(alg);
}
if (hash === null) {
console.log(`unknown hash: ${alg}`);
Deno.exit(1);
}
hash.update(buffer);
hash.digest();
console.timeEnd("hash");

View File

@ -10,42 +10,6 @@ use std::process::Command;
use tempfile::TempDir;
use test_util as util;
#[test]
fn std_tests() {
let dir = TempDir::new().expect("tempdir fail");
let status = util::deno_cmd()
.env("DENO_DIR", dir.path())
.current_dir(util::root_path())
.arg("test")
.arg("--unstable")
.arg("--seed=86") // Some tests rely on specific random numbers.
.arg("-A")
// .arg("-Ldebug")
.arg("std/")
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(status.success());
}
#[test]
fn std_lint() {
let status = util::deno_cmd()
.arg("lint")
.arg("--unstable")
.arg(format!(
"--ignore={}",
util::root_path().join("std/node/tests").to_string_lossy()
))
.arg(util::root_path().join("std"))
.spawn()
.unwrap()
.wait()
.unwrap();
assert!(status.success());
}
#[test]
fn js_unit_tests_lint() {
let status = util::deno_cmd()
@ -4888,7 +4852,7 @@ console.log("finish");
.arg("--unstable")
.arg("--output")
.arg(&exe)
.arg("./std/examples/welcome.ts")
.arg("./test_util/std/examples/welcome.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()

View File

@ -1,6 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert } from "../../std/testing/asserts.ts";
import { assert } from "../../test_util/std/testing/asserts.ts";
Deno.test("fail1", function () {
assert(false, "fail1 assertion");

View File

@ -1,7 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, unitTest } from "./test_util.ts";
import { concat } from "../../../std/bytes/mod.ts";
import { decode } from "../../../std/encoding/utf8.ts";
import { concat } from "../../../test_util/std/bytes/mod.ts";
import { decode } from "../../../test_util/std/encoding/utf8.ts";
unitTest(function blobString(): void {
const b1 = new Blob(["Hello World"]);

View File

@ -15,7 +15,7 @@ import {
assertThrows,
unitTest,
} from "./test_util.ts";
import { stripColor } from "../../../std/fmt/colors.ts";
import { stripColor } from "../../../test_util/std/fmt/colors.ts";
const customInspect = Deno.customInspect;
const {

View File

@ -1,9 +1,12 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../../../std/testing/asserts.ts";
import * as colors from "../../../std/fmt/colors.ts";
import {
assert,
assertEquals,
} from "../../../test_util/std/testing/asserts.ts";
import * as colors from "../../../test_util/std/fmt/colors.ts";
export { colors };
import { resolve } from "../../../std/path/mod.ts";
import { resolve } from "../../../test_util/std/path/mod.ts";
export {
assert,
assertEquals,
@ -15,10 +18,10 @@ export {
assertThrowsAsync,
fail,
unreachable,
} from "../../../std/testing/asserts.ts";
export { deferred } from "../../../std/async/deferred.ts";
export { readLines } from "../../../std/io/bufio.ts";
export { parse as parseArgs } from "../../../std/flags/mod.ts";
} from "../../../test_util/std/testing/asserts.ts";
export { deferred } from "../../../test_util/std/async/deferred.ts";
export { readLines } from "../../../test_util/std/io/bufio.ts";
export { parse as parseArgs } from "../../../test_util/std/flags/mod.ts";
export interface Permissions {
read: boolean;

View File

@ -8,8 +8,8 @@ import {
deferred,
unitTest,
} from "./test_util.ts";
import { BufReader, BufWriter } from "../../../std/io/bufio.ts";
import { TextProtoReader } from "../../../std/textproto/mod.ts";
import { BufReader, BufWriter } from "../../../test_util/std/io/bufio.ts";
import { TextProtoReader } from "../../../test_util/std/textproto/mod.ts";
const encoder = new TextEncoder();
const decoder = new TextDecoder();

View File

@ -4,8 +4,8 @@ import {
assertEquals,
assertThrows,
fail,
} from "../../std/testing/asserts.ts";
import { deferred } from "../../std/async/deferred.ts";
} from "../../test_util/std/testing/asserts.ts";
import { deferred } from "../../test_util/std/async/deferred.ts";
Deno.test("invalid scheme", () => {
assertThrows(() => new WebSocket("foo://localhost:4242"));

View File

@ -1,4 +1,4 @@
import { fromFileUrl } from "../../../std/path/mod.ts";
import { fromFileUrl } from "../../../test_util/std/path/mod.ts";
const worker = new Worker(
new URL("./read_check_granular_worker.js", import.meta.url).href,

View File

@ -1,4 +1,4 @@
import { fromFileUrl } from "../../../std/path/mod.ts";
import { fromFileUrl } from "../../../test_util/std/path/mod.ts";
onmessage = async ({ data }) => {
const { state } = await Deno.permissions.query({

View File

@ -5,7 +5,7 @@ const data = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n";
const workerCount = 4;
const cmdsPerWorker = 400;
import { Deferred, deferred } from "../../std/async/deferred.ts";
import { Deferred, deferred } from "../../test_util/std/async/deferred.ts";
function handleAsyncMsgFromWorker(
promiseTable: Map<number, Deferred<string>>,

View File

@ -7,8 +7,8 @@ import {
assertEquals,
assertThrows,
fail,
} from "../../std/testing/asserts.ts";
import { deferred } from "../../std/async/deferred.ts";
} from "../../test_util/std/testing/asserts.ts";
import { deferred } from "../../test_util/std/async/deferred.ts";
Deno.test({
name: "worker terminate",

View File

@ -149,7 +149,12 @@ mod tests {
#[test]
fn supports_dirs() {
let root = test_util::root_path().join("std").join("http");
// TODO(caspervonb) generate some fixtures in a temporary directory instead, there's no need
// for this to rely on external fixtures.
let root = test_util::root_path()
.join("test_util")
.join("std")
.join("http");
println!("root {:?}", root);
let mut matched_urls =
prepare_test_modules_urls(vec![".".to_string()], &root).unwrap();

View File

@ -1,47 +0,0 @@
# Deno Standard Modules
These modules do not have external dependencies and they are reviewed by the
Deno core team. The intention is to have a standard set of high quality code
that all Deno projects can use fearlessly.
Contributions are welcome!
## How to use
These modules will eventually be tagged in accordance with Deno releases but as
of today we do not yet consider them stable and so we version the standard
modules differently from the Deno runtime to reflect this.
It is strongly recommended that you link to tagged releases to avoid unintended
updates and breaking changes.
Don't link to / import any module whose path:
- Has a name or parent with an underscore prefix: `_foo.ts`, `_util/bar.ts`.
- Is that of a test module or test data: `test.ts`, `foo_test.ts`,
`testdata/bar.txt`.
Don't import any symbol with an underscore prefix: `export function _baz() {}`.
These elements are not considered part of the public API, thus no stability is
guaranteed for them.
## Documentation
To browse documentation for modules:
- Go to https://deno.land/std/.
- Navigate to any module of interest.
- Click "View Documentation".
## Contributing
deno_std is a loose port of [Go's standard library](https://golang.org/pkg/).
When in doubt, simply port Go's source code, documentation, and tests. There are
many times when the nature of JavaScript, TypeScript, or Deno itself justifies
diverging from Go, but if possible we want to leverage the energy that went into
building Go. We generally welcome direct ports of Go's code.
Please ensure the copyright headers cite the code's origin.
Follow the [style guide](https://deno.land/manual/contributing/style_guide).

View File

@ -1,15 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export class DenoStdInternalError extends Error {
constructor(message: string) {
super(message);
this.name = "DenoStdInternalError";
}
}
/** Make an assertion, if not `true`, then throw. */
export function assert(expr: unknown, msg = ""): asserts expr {
if (!expr) {
throw new DenoStdInternalError(msg);
}
}

View File

@ -1,31 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, DenoStdInternalError } from "./assert.ts";
import { assertThrows } from "../testing/asserts.ts";
Deno.test({
name: "assert valid scenario",
fn(): void {
assert(true);
},
});
Deno.test({
name: "assert invalid scenario, no message",
fn(): void {
assertThrows(() => {
assert(false);
}, DenoStdInternalError);
},
});
Deno.test({
name: "assert invalid scenario, with message",
fn(): void {
assertThrows(
() => {
assert(false, "Oops! Should be true");
},
DenoStdInternalError,
"Oops! Should be true",
);
},
});

View File

@ -1,49 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert } from "./assert.ts";
export function deepAssign<T, U>(target: T, source: U): T & U;
export function deepAssign<T, U, V>(
target: T,
source1: U,
source2: V,
): T & U & V;
export function deepAssign<T, U, V, W>(
target: T,
source1: U,
source2: V,
source3: W,
): T & U & V & W;
export function deepAssign(
// deno-lint-ignore no-explicit-any
target: Record<string, any>,
// deno-lint-ignore no-explicit-any
...sources: any[]
): // deno-lint-ignore ban-types
object | undefined {
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
if (!source || typeof source !== `object`) {
return;
}
Object.entries(source).forEach(([key, value]): void => {
if (value instanceof Date) {
target[key] = new Date(value);
return;
}
if (!value || typeof value !== `object`) {
target[key] = value;
return;
}
if (Array.isArray(value)) {
target[key] = [];
}
// value is an Object
if (typeof target[key] !== `object` || !target[key]) {
target[key] = {};
}
assert(value);
deepAssign(target[key] as Record<string, unknown>, value);
});
}
return target;
}

View File

@ -1,23 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../testing/asserts.ts";
import { deepAssign } from "./deep_assign.ts";
Deno.test("deepAssignTest", function (): void {
const date = new Date("1979-05-27T07:32:00Z");
const reg = RegExp(/DENOWOWO/);
const obj1 = { deno: { bar: { deno: ["is", "not", "node"] } } };
const obj2 = { foo: { deno: date } };
const obj3 = { foo: { bar: "deno" }, reg: reg };
const actual = deepAssign(obj1, obj2, obj3);
const expected = {
foo: {
deno: new Date("1979-05-27T07:32:00Z"),
bar: "deno",
},
deno: { bar: { deno: ["is", "not", "node"] } },
reg: RegExp(/DENOWOWO/),
};
assert(date !== expected.foo.deno);
assert(reg !== expected.reg);
assertEquals(actual, expected);
});

View File

@ -1,30 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/**
* Determines whether an object has a property with the specified name.
* Avoid calling prototype builtin `hasOwnProperty` for two reasons:
*
* 1. `hasOwnProperty` is defined on the object as something else:
*
* const options = {
* ending: 'utf8',
* hasOwnProperty: 'foo'
* };
* options.hasOwnProperty('ending') // throws a TypeError
*
* 2. The object doesn't inherit from `Object.prototype`:
*
* const options = Object.create(null);
* options.ending = 'utf8';
* options.hasOwnProperty('ending'); // throws a TypeError
*
* @param obj A Object.
* @param v A property name.
* @see https://eslint.org/docs/rules/no-prototype-builtins
*/
export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
if (obj == null) {
return false;
}
return Object.prototype.hasOwnProperty.call(obj, v);
}

View File

@ -1,18 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
export const osType = (() => {
if (globalThis.Deno != null) {
return Deno.build.os;
}
// deno-lint-ignore no-explicit-any
const navigator = (globalThis as any).navigator;
if (navigator?.appVersion?.includes?.("Win") ?? false) {
return "windows";
}
return "linux";
})();
export const isWindows = osType === "windows";

View File

@ -1,60 +0,0 @@
# Usage
## Tar
```ts
import { Tar } from "https://deno.land/std@$STD_VERSION/archive/tar.ts";
const tar = new Tar();
const content = new TextEncoder().encode("Deno.land");
await tar.append("deno.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
// Or specifying a filePath.
await tar.append("land.txt", {
filePath: "./land.txt",
});
// use tar.getReader() to read the contents.
const writer = await Deno.open("./out.tar", { write: true, create: true });
await Deno.copy(tar.getReader(), writer);
writer.close();
```
## Untar
```ts
import { Untar } from "https://deno.land/std@$STD_VERSION/archive/tar.ts";
import { ensureFile } from "https://deno.land/std@$STD_VERSION/fs/ensure_file.ts";
import { ensureDir } from "https://deno.land/std@$STD_VERSION/fs/ensure_dir.ts";
const reader = await Deno.open("./out.tar", { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
console.log(entry); // metadata
/*
fileName: "archive/deno.txt",
fileMode: 33204,
mtime: 1591657305,
uid: 0,
gid: 0,
size: 24400,
type: 'file'
*/
if (entry.type === "directory") {
await ensureDir(entry.fileName);
continue;
}
await ensureFile(entry.fileName);
const file = await Deno.open(entry.fileName, { write: true });
// <entry> is a reader.
await Deno.copy(entry, file);
}
reader.close();
```

View File

@ -1,639 +0,0 @@
/**
* Ported and modified from: https://github.com/beatgammit/tar-js and
* licensed as:
*
* (The MIT License)
*
* Copyright (c) 2011 T. Jameson Little
* Copyright (c) 2019 Jun Kato
* Copyright (c) 2018-2021 the Deno authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import { MultiReader } from "../io/readers.ts";
import { PartialReadError } from "../io/bufio.ts";
import { assert } from "../_util/assert.ts";
type Reader = Deno.Reader;
type Seeker = Deno.Seeker;
const recordSize = 512;
const ustar = "ustar\u000000";
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06
// eight checksum bytes taken to be ascii spaces (decimal value 32)
const initialChecksum = 8 * 32;
async function readBlock(
reader: Deno.Reader,
p: Uint8Array,
): Promise<number | null> {
let bytesRead = 0;
while (bytesRead < p.length) {
const rr = await reader.read(p.subarray(bytesRead));
if (rr === null) {
if (bytesRead === 0) {
return null;
} else {
throw new PartialReadError();
}
}
bytesRead += rr;
}
return bytesRead;
}
/**
* Simple file reader
*/
class FileReader implements Reader {
private file?: Deno.File;
constructor(private filePath: string) {}
public async read(p: Uint8Array): Promise<number | null> {
if (!this.file) {
this.file = await Deno.open(this.filePath, { read: true });
}
const res = await Deno.read(this.file.rid, p);
if (res === null) {
Deno.close(this.file.rid);
this.file = undefined;
}
return res;
}
}
/**
* Remove the trailing null codes
* @param buffer
*/
function trim(buffer: Uint8Array): Uint8Array {
const index = buffer.findIndex((v): boolean => v === 0);
if (index < 0) return buffer;
return buffer.subarray(0, index);
}
/**
* Initialize Uint8Array of the specified length filled with 0
* @param length
*/
function clean(length: number): Uint8Array {
const buffer = new Uint8Array(length);
buffer.fill(0, 0, length - 1);
return buffer;
}
function pad(num: number, bytes: number, base = 8): string {
const numString = num.toString(base);
return "000000000000".substr(numString.length + 12 - bytes) + numString;
}
enum FileTypes {
"file" = 0,
"link" = 1,
"symlink" = 2,
"character-device" = 3,
"block-device" = 4,
"directory" = 5,
"fifo" = 6,
"contiguous-file" = 7,
}
/*
struct posix_header { // byte offset
char name[100]; // 0
char mode[8]; // 100
char uid[8]; // 108
char gid[8]; // 116
char size[12]; // 124
char mtime[12]; // 136
char chksum[8]; // 148
char typeflag; // 156
char linkname[100]; // 157
char magic[6]; // 257
char version[2]; // 263
char uname[32]; // 265
char gname[32]; // 297
char devmajor[8]; // 329
char devminor[8]; // 337
char prefix[155]; // 345
// 500
};
*/
const ustarStructure: Array<{ field: string; length: number }> = [
{
field: "fileName",
length: 100,
},
{
field: "fileMode",
length: 8,
},
{
field: "uid",
length: 8,
},
{
field: "gid",
length: 8,
},
{
field: "fileSize",
length: 12,
},
{
field: "mtime",
length: 12,
},
{
field: "checksum",
length: 8,
},
{
field: "type",
length: 1,
},
{
field: "linkName",
length: 100,
},
{
field: "ustar",
length: 8,
},
{
field: "owner",
length: 32,
},
{
field: "group",
length: 32,
},
{
field: "majorNumber",
length: 8,
},
{
field: "minorNumber",
length: 8,
},
{
field: "fileNamePrefix",
length: 155,
},
{
field: "padding",
length: 12,
},
];
/**
* Create header for a file in a tar archive
*/
function formatHeader(data: TarData): Uint8Array {
const encoder = new TextEncoder(),
buffer = clean(512);
let offset = 0;
ustarStructure.forEach(function (value): void {
const entry = encoder.encode(data[value.field as keyof TarData] || "");
buffer.set(entry, offset);
offset += value.length; // space it out with nulls
});
return buffer;
}
/**
* Parse file header in a tar archive
* @param length
*/
function parseHeader(buffer: Uint8Array): { [key: string]: Uint8Array } {
const data: { [key: string]: Uint8Array } = {};
let offset = 0;
ustarStructure.forEach(function (value): void {
const arr = buffer.subarray(offset, offset + value.length);
data[value.field] = arr;
offset += value.length;
});
return data;
}
interface TarHeader {
[key: string]: Uint8Array;
}
export interface TarData {
fileName?: string;
fileNamePrefix?: string;
fileMode?: string;
uid?: string;
gid?: string;
fileSize?: string;
mtime?: string;
checksum?: string;
type?: string;
ustar?: string;
owner?: string;
group?: string;
}
export interface TarDataWithSource extends TarData {
/**
* file to read
*/
filePath?: string;
/**
* buffer to read
*/
reader?: Reader;
}
export interface TarInfo {
fileMode?: number;
mtime?: number;
uid?: number;
gid?: number;
owner?: string;
group?: string;
type?: string;
}
export interface TarOptions extends TarInfo {
/**
* append file
*/
filePath?: string;
/**
* append any arbitrary content
*/
reader?: Reader;
/**
* size of the content to be appended
*/
contentSize?: number;
}
export interface TarMeta extends TarInfo {
fileName: string;
fileSize?: number;
}
// deno-lint-ignore no-empty-interface
interface TarEntry extends TarMeta {}
/**
* A class to create a tar archive
*/
export class Tar {
data: TarDataWithSource[];
constructor() {
this.data = [];
}
/**
* Append a file to this tar archive
* @param fn file name
* e.g., test.txt; use slash for directory separators
* @param opts options
*/
async append(fn: string, opts: TarOptions): Promise<void> {
if (typeof fn !== "string") {
throw new Error("file name not specified");
}
let fileName = fn;
// separate file name into two parts if needed
let fileNamePrefix: string | undefined;
if (fileName.length > 100) {
let i = fileName.length;
while (i >= 0) {
i = fileName.lastIndexOf("/", i);
if (i <= 155) {
fileNamePrefix = fileName.substr(0, i);
fileName = fileName.substr(i + 1);
break;
}
i--;
}
const errMsg =
"ustar format does not allow a long file name (length of [file name" +
"prefix] + / + [file name] must be shorter than 256 bytes)";
if (i < 0 || fileName.length > 100) {
throw new Error(errMsg);
} else {
assert(fileNamePrefix != null);
if (fileNamePrefix.length > 155) {
throw new Error(errMsg);
}
}
}
opts = opts || {};
// set meta data
let info: Deno.FileInfo | undefined;
if (opts.filePath) {
info = await Deno.stat(opts.filePath);
if (info.isDirectory) {
info.size = 0;
opts.reader = new Deno.Buffer();
}
}
const mode = opts.fileMode || (info && info.mode) ||
parseInt("777", 8) & 0xfff,
mtime = Math.floor(
opts.mtime ?? (info?.mtime ?? new Date()).valueOf() / 1000,
),
uid = opts.uid || 0,
gid = opts.gid || 0;
if (typeof opts.owner === "string" && opts.owner.length >= 32) {
throw new Error(
"ustar format does not allow owner name length >= 32 bytes",
);
}
if (typeof opts.group === "string" && opts.group.length >= 32) {
throw new Error(
"ustar format does not allow group name length >= 32 bytes",
);
}
const fileSize = info?.size ?? opts.contentSize;
assert(fileSize != null, "fileSize must be set");
const type = opts.type
? FileTypes[opts.type as keyof typeof FileTypes]
: (info?.isDirectory ? FileTypes.directory : FileTypes.file);
const tarData: TarDataWithSource = {
fileName,
fileNamePrefix,
fileMode: pad(mode, 7),
uid: pad(uid, 7),
gid: pad(gid, 7),
fileSize: pad(fileSize, 11),
mtime: pad(mtime, 11),
checksum: " ",
type: type.toString(),
ustar,
owner: opts.owner || "",
group: opts.group || "",
filePath: opts.filePath,
reader: opts.reader,
};
// calculate the checksum
let checksum = 0;
const encoder = new TextEncoder();
Object.keys(tarData)
.filter((key): boolean => ["filePath", "reader"].indexOf(key) < 0)
.forEach(function (key): void {
checksum += encoder
.encode(tarData[key as keyof TarData])
.reduce((p, c): number => p + c, 0);
});
tarData.checksum = pad(checksum, 6) + "\u0000 ";
this.data.push(tarData);
}
/**
* Get a Reader instance for this tar data
*/
getReader(): Reader {
const readers: Reader[] = [];
this.data.forEach((tarData): void => {
let { reader } = tarData;
const { filePath } = tarData;
const headerArr = formatHeader(tarData);
readers.push(new Deno.Buffer(headerArr));
if (!reader) {
assert(filePath != null);
reader = new FileReader(filePath);
}
readers.push(reader);
// to the nearest multiple of recordSize
assert(tarData.fileSize != null, "fileSize must be set");
readers.push(
new Deno.Buffer(
clean(
recordSize -
(parseInt(tarData.fileSize, 8) % recordSize || recordSize),
),
),
);
});
// append 2 empty records
readers.push(new Deno.Buffer(clean(recordSize * 2)));
return new MultiReader(...readers);
}
}
class TarEntry implements Reader {
#header: TarHeader;
#reader: Reader | (Reader & Deno.Seeker);
#size: number;
#read = 0;
#consumed = false;
#entrySize: number;
constructor(
meta: TarMeta,
header: TarHeader,
reader: Reader | (Reader & Deno.Seeker),
) {
Object.assign(this, meta);
this.#header = header;
this.#reader = reader;
// File Size
this.#size = this.fileSize || 0;
// Entry Size
const blocks = Math.ceil(this.#size / recordSize);
this.#entrySize = blocks * recordSize;
}
get consumed(): boolean {
return this.#consumed;
}
async read(p: Uint8Array): Promise<number | null> {
// Bytes left for entry
const entryBytesLeft = this.#entrySize - this.#read;
const bufSize = Math.min(
// bufSize can't be greater than p.length nor bytes left in the entry
p.length,
entryBytesLeft,
);
if (entryBytesLeft <= 0) {
this.#consumed = true;
return null;
}
const block = new Uint8Array(bufSize);
const n = await readBlock(this.#reader, block);
const bytesLeft = this.#size - this.#read;
this.#read += n || 0;
if (n === null || bytesLeft <= 0) {
if (n === null) this.#consumed = true;
return null;
}
// Remove zero filled
const offset = bytesLeft < n ? bytesLeft : n;
p.set(block.subarray(0, offset), 0);
return offset < 0 ? n - Math.abs(offset) : offset;
}
async discard(): Promise<void> {
// Discard current entry
if (this.#consumed) return;
this.#consumed = true;
if (typeof (this.#reader as Seeker).seek === "function") {
await (this.#reader as Seeker).seek(
this.#entrySize - this.#read,
Deno.SeekMode.Current,
);
this.#read = this.#entrySize;
} else {
await Deno.readAll(this);
}
}
}
/**
* A class to extract a tar archive
*/
export class Untar {
reader: Reader;
block: Uint8Array;
#entry: TarEntry | undefined;
constructor(reader: Reader) {
this.reader = reader;
this.block = new Uint8Array(recordSize);
}
#checksum = (header: Uint8Array): number => {
let sum = initialChecksum;
for (let i = 0; i < 512; i++) {
if (i >= 148 && i < 156) {
// Ignore checksum header
continue;
}
sum += header[i];
}
return sum;
};
#getHeader = async (): Promise<TarHeader | null> => {
await readBlock(this.reader, this.block);
const header = parseHeader(this.block);
// calculate the checksum
const decoder = new TextDecoder();
const checksum = this.#checksum(this.block);
if (parseInt(decoder.decode(header.checksum), 8) !== checksum) {
if (checksum === initialChecksum) {
// EOF
return null;
}
throw new Error("checksum error");
}
const magic = decoder.decode(header.ustar);
if (magic.indexOf("ustar")) {
throw new Error(`unsupported archive format: ${magic}`);
}
return header;
};
#getMetadata = (header: TarHeader): TarMeta => {
const decoder = new TextDecoder();
// get meta data
const meta: TarMeta = {
fileName: decoder.decode(trim(header.fileName)),
};
const fileNamePrefix = trim(header.fileNamePrefix);
if (fileNamePrefix.byteLength > 0) {
meta.fileName = decoder.decode(fileNamePrefix) + "/" + meta.fileName;
}
(["fileMode", "mtime", "uid", "gid"] as [
"fileMode",
"mtime",
"uid",
"gid",
]).forEach((key): void => {
const arr = trim(header[key]);
if (arr.byteLength > 0) {
meta[key] = parseInt(decoder.decode(arr), 8);
}
});
(["owner", "group", "type"] as ["owner", "group", "type"]).forEach(
(key): void => {
const arr = trim(header[key]);
if (arr.byteLength > 0) {
meta[key] = decoder.decode(arr);
}
},
);
meta.fileSize = parseInt(decoder.decode(header.fileSize), 8);
meta.type = FileTypes[parseInt(meta.type!)] ?? meta.type;
return meta;
};
async extract(): Promise<TarEntry | null> {
if (this.#entry && !this.#entry.consumed) {
// If entry body was not read, discard the body
// so we can read the next entry.
await this.#entry.discard();
}
const header = await this.#getHeader();
if (header === null) return null;
const meta = this.#getMetadata(header);
this.#entry = new TarEntry(meta, header, this.reader);
return this.#entry;
}
async *[Symbol.asyncIterator](): AsyncIterableIterator<TarEntry> {
while (true) {
const entry = await this.extract();
if (entry === null) return;
yield entry;
}
}
}

View File

@ -1,435 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/**
* Tar test
*
* **test summary**
* - create a tar archive in memory containing output.txt and dir/tar.ts.
* - read and deflate a tar archive containing output.txt
*
* **to run this test**
* deno run --allow-read archive/tar_test.ts
*/
import { assert, assertEquals } from "../testing/asserts.ts";
import { dirname, fromFileUrl, resolve } from "../path/mod.ts";
import { Tar, Untar } from "./tar.ts";
const moduleDir = dirname(fromFileUrl(import.meta.url));
const testdataDir = resolve(moduleDir, "testdata");
const filePath = resolve(testdataDir, "example.txt");
interface TestEntry {
name: string;
content?: Uint8Array;
filePath?: string;
}
async function createTar(entries: TestEntry[]): Promise<Tar> {
const tar = new Tar();
// put data on memory
for (const file of entries) {
let options;
if (file.content) {
options = {
reader: new Deno.Buffer(file.content),
contentSize: file.content.byteLength,
};
} else {
options = { filePath: file.filePath };
}
await tar.append(file.name, options);
}
return tar;
}
Deno.test("createTarArchive", async function (): Promise<void> {
// initialize
const tar = new Tar();
// put data on memory
const content = new TextEncoder().encode("hello tar world!");
await tar.append("output.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
// put a file
await tar.append("dir/tar.ts", { filePath });
// write tar data to a buffer
const writer = new Deno.Buffer();
const wrote = await Deno.copy(tar.getReader(), writer);
/**
* 3072 = 512 (header) + 512 (content) + 512 (header) + 512 (content)
* + 1024 (footer)
*/
assertEquals(wrote, 3072);
});
Deno.test("deflateTarArchive", async function (): Promise<void> {
const fileName = "output.txt";
const text = "hello tar world!";
// create a tar archive
const tar = new Tar();
const content = new TextEncoder().encode(text);
await tar.append(fileName, {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
// read data from a tar archive
const untar = new Untar(tar.getReader());
const result = await untar.extract();
assert(result !== null);
const untarText = new TextDecoder("utf-8").decode(await Deno.readAll(result));
assertEquals(await untar.extract(), null); // EOF
// tests
assertEquals(result.fileName, fileName);
assertEquals(untarText, text);
});
Deno.test("appendFileWithLongNameToTarArchive", async function (): Promise<
void
> {
// 9 * 15 + 13 = 148 bytes
const fileName = new Array(10).join("long-file-name/") + "file-name.txt";
const text = "hello tar world!";
// create a tar archive
const tar = new Tar();
const content = new TextEncoder().encode(text);
await tar.append(fileName, {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
// read data from a tar archive
const untar = new Untar(tar.getReader());
const result = await untar.extract();
assert(result !== null);
assert(!result.consumed);
const untarText = new TextDecoder("utf-8").decode(await Deno.readAll(result));
assert(result.consumed);
// tests
assertEquals(result.fileName, fileName);
assertEquals(untarText, text);
});
Deno.test("untarAsyncIterator", async function (): Promise<void> {
const entries: TestEntry[] = [
{
name: "output.txt",
content: new TextEncoder().encode("hello tar world!"),
},
{
name: "dir/tar.ts",
filePath,
},
];
const tar = await createTar(entries);
// read data from a tar archive
const untar = new Untar(tar.getReader());
let lastEntry;
for await (const entry of untar) {
const expected = entries.shift();
assert(expected);
let content = expected.content;
if (expected.filePath) {
content = await Deno.readFile(expected.filePath);
}
assertEquals(content, await Deno.readAll(entry));
assertEquals(expected.name, entry.fileName);
if (lastEntry) assert(lastEntry.consumed);
lastEntry = entry;
}
assert(lastEntry);
assert(lastEntry.consumed);
assertEquals(entries.length, 0);
});
Deno.test("untarAsyncIteratorWithoutReadingBody", async function (): Promise<
void
> {
const entries: TestEntry[] = [
{
name: "output.txt",
content: new TextEncoder().encode("hello tar world!"),
},
{
name: "dir/tar.ts",
filePath,
},
];
const tar = await createTar(entries);
// read data from a tar archive
const untar = new Untar(tar.getReader());
for await (const entry of untar) {
const expected = entries.shift();
assert(expected);
assertEquals(expected.name, entry.fileName);
}
assertEquals(entries.length, 0);
});
Deno.test(
"untarAsyncIteratorWithoutReadingBodyFromFileReader",
async function (): Promise<void> {
const entries: TestEntry[] = [
{
name: "output.txt",
content: new TextEncoder().encode("hello tar world!"),
},
{
name: "dir/tar.ts",
filePath,
},
];
const outputFile = resolve(testdataDir, "test.tar");
const tar = await createTar(entries);
const file = await Deno.open(outputFile, { create: true, write: true });
await Deno.copy(tar.getReader(), file);
file.close();
const reader = await Deno.open(outputFile, { read: true });
// read data from a tar archive
const untar = new Untar(reader);
for await (const entry of untar) {
const expected = entries.shift();
assert(expected);
assertEquals(expected.name, entry.fileName);
}
reader.close();
await Deno.remove(outputFile);
assertEquals(entries.length, 0);
},
);
Deno.test("untarAsyncIteratorFromFileReader", async function (): Promise<void> {
const entries: TestEntry[] = [
{
name: "output.txt",
content: new TextEncoder().encode("hello tar world!"),
},
{
name: "dir/tar.ts",
filePath,
},
];
const outputFile = resolve(testdataDir, "test.tar");
const tar = await createTar(entries);
const file = await Deno.open(outputFile, { create: true, write: true });
await Deno.copy(tar.getReader(), file);
file.close();
const reader = await Deno.open(outputFile, { read: true });
// read data from a tar archive
const untar = new Untar(reader);
for await (const entry of untar) {
const expected = entries.shift();
assert(expected);
let content = expected.content;
if (expected.filePath) {
content = await Deno.readFile(expected.filePath);
}
assertEquals(content, await Deno.readAll(entry));
assertEquals(expected.name, entry.fileName);
}
reader.close();
await Deno.remove(outputFile);
assertEquals(entries.length, 0);
});
Deno.test(
"untarAsyncIteratorReadingLessThanRecordSize",
async function (): Promise<void> {
// record size is 512
const bufSizes = [1, 53, 256, 511];
for (const bufSize of bufSizes) {
const entries: TestEntry[] = [
{
name: "output.txt",
content: new TextEncoder().encode("hello tar world!".repeat(100)),
},
// Need to test at least two files, to make sure the first entry doesn't over-read
// Causing the next to fail with: chesum error
{
name: "deni.txt",
content: new TextEncoder().encode("deno!".repeat(250)),
},
];
const tar = await createTar(entries);
// read data from a tar archive
const untar = new Untar(tar.getReader());
for await (const entry of untar) {
const expected = entries.shift();
assert(expected);
assertEquals(expected.name, entry.fileName);
const writer = new Deno.Buffer();
while (true) {
const buf = new Uint8Array(bufSize);
const n = await entry.read(buf);
if (n === null) break;
await writer.write(buf.subarray(0, n));
}
assertEquals(writer.bytes(), expected!.content);
}
assertEquals(entries.length, 0);
}
},
);
Deno.test("untarLinuxGeneratedTar", async function (): Promise<void> {
const filePath = resolve(testdataDir, "deno.tar");
const file = await Deno.open(filePath, { read: true });
const expectedEntries = [
{
fileName: "archive/",
fileSize: 0,
fileMode: 509,
mtime: 1591800767,
uid: 1001,
gid: 1001,
owner: "deno",
group: "deno",
type: "directory",
},
{
fileName: "archive/deno/",
fileSize: 0,
fileMode: 509,
mtime: 1591799635,
uid: 1001,
gid: 1001,
owner: "deno",
group: "deno",
type: "directory",
},
{
fileName: "archive/deno/land/",
fileSize: 0,
fileMode: 509,
mtime: 1591799660,
uid: 1001,
gid: 1001,
owner: "deno",
group: "deno",
type: "directory",
},
{
fileName: "archive/deno/land/land.txt",
fileMode: 436,
fileSize: 5,
mtime: 1591799660,
uid: 1001,
gid: 1001,
owner: "deno",
group: "deno",
type: "file",
content: new TextEncoder().encode("land\n"),
},
{
fileName: "archive/file.txt",
fileMode: 436,
fileSize: 5,
mtime: 1591799626,
uid: 1001,
gid: 1001,
owner: "deno",
group: "deno",
type: "file",
content: new TextEncoder().encode("file\n"),
},
{
fileName: "archive/deno.txt",
fileMode: 436,
fileSize: 5,
mtime: 1591799642,
uid: 1001,
gid: 1001,
owner: "deno",
group: "deno",
type: "file",
content: new TextEncoder().encode("deno\n"),
},
];
const untar = new Untar(file);
for await (const entry of untar) {
const expected = expectedEntries.shift();
assert(expected);
const content = expected.content;
delete expected.content;
assertEquals(entry, expected);
if (content) {
assertEquals(content, await Deno.readAll(entry));
}
}
file.close();
});
Deno.test("directoryEntryType", async function (): Promise<void> {
const tar = new Tar();
tar.append("directory/", {
reader: new Deno.Buffer(),
contentSize: 0,
type: "directory",
});
const filePath = resolve(testdataDir);
tar.append("archive/testdata/", {
filePath,
});
const outputFile = resolve(testdataDir, "directory_type_test.tar");
const file = await Deno.open(outputFile, { create: true, write: true });
await Deno.copy(tar.getReader(), file);
await file.close();
const reader = await Deno.open(outputFile, { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
assertEquals(entry.type, "directory");
}
await reader.close();
await Deno.remove(outputFile);
});

Binary file not shown.

View File

@ -1 +0,0 @@
hello world!

View File

@ -1,85 +0,0 @@
# async
async is a module to provide help with asynchronous tasks.
# Usage
The following functions and class are exposed in `mod.ts`:
## deferred
Create a Promise with the `reject` and `resolve` functions.
```typescript
import { deferred } from "https://deno.land/std/async/mod.ts";
const p = deferred<number>();
// ...
p.resolve(42);
```
## delay
Resolve a Promise after a given amount of milliseconds.
```typescript
import { delay } from "https://deno.land/std/async/mod.ts";
// ...
const delayedPromise = delay(100);
const result = await delayedPromise;
// ...
```
## MuxAsyncIterator
The MuxAsyncIterator class multiplexes multiple async iterators into a single
stream.
The class makes an assumption that the final result (the value returned and not
yielded from the iterator) does not matter. If there is any result, it is
discarded.
```typescript
import { MuxAsyncIterator } from "https://deno.land/std/async/mod.ts";
async function* gen123(): AsyncIterableIterator<number> {
yield 1;
yield 2;
yield 3;
}
async function* gen456(): AsyncIterableIterator<number> {
yield 4;
yield 5;
yield 6;
}
const mux = new MuxAsyncIterator<number>();
mux.add(gen123());
mux.add(gen456());
for await (const value of mux) {
// ...
}
// ..
```
## pooledMap
Transform values from an (async) iterable into another async iterable. The
transforms are done concurrently, with a max concurrency defined by the
poolLimit.
```typescript
import { pooledMap } from "https://deno.land/std/async/mod.ts";
const results = pooledMap(
2,
[1, 2, 3],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
);
for await (const value of results) {
// ...
}
```

View File

@ -1,26 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// TODO(ry) It'd be better to make Deferred a class that inherits from
// Promise, rather than an interface. This is possible in ES2016, however
// typescript produces broken code when targeting ES5 code.
// See https://github.com/Microsoft/TypeScript/issues/15202
// At the time of writing, the github issue is closed but the problem remains.
export interface Deferred<T> extends Promise<T> {
resolve: (value?: T | PromiseLike<T>) => void;
// deno-lint-ignore no-explicit-any
reject: (reason?: any) => void;
}
/** Creates a Promise with the `reject` and `resolve` functions
* placed as methods on the promise object itself. It allows you to do:
*
* const p = deferred<number>();
* // ...
* p.resolve(42);
*/
export function deferred<T>(): Deferred<T> {
let methods;
const promise = new Promise<T>((resolve, reject): void => {
methods = { resolve, reject };
});
return Object.assign(promise, methods) as Deferred<T>;
}

View File

@ -1,17 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import { deferred } from "./deferred.ts";
Deno.test("[async] deferred: resolve", async function (): Promise<void> {
const d = deferred<string>();
d.resolve("🦕");
assertEquals(await d, "🦕");
});
Deno.test("[async] deferred: reject", async function (): Promise<void> {
const d = deferred<number>();
d.reject(new Error("A deno error 🦕"));
await assertThrowsAsync(async () => {
await d;
});
});

View File

@ -1,9 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/* Resolves after the given number of milliseconds. */
export function delay(ms: number): Promise<void> {
return new Promise((res): number =>
setTimeout((): void => {
res();
}, ms)
);
}

View File

@ -1,12 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { delay } from "./delay.ts";
import { assert } from "../testing/asserts.ts";
Deno.test("[async] delay", async function (): Promise<void> {
const start = new Date();
const delayedPromise = delay(100);
const result = await delayedPromise;
const diff = new Date().getTime() - start.getTime();
assert(result === undefined);
assert(diff >= 100);
});

View File

@ -1,5 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export * from "./deferred.ts";
export * from "./delay.ts";
export * from "./mux_async_iterator.ts";
export * from "./pool.ts";

View File

@ -1,69 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Deferred, deferred } from "./deferred.ts";
interface TaggedYieldedValue<T> {
iterator: AsyncIterableIterator<T>;
value: T;
}
/** The MuxAsyncIterator class multiplexes multiple async iterators into a
* single stream. It currently makes an assumption:
* - The final result (the value returned and not yielded from the iterator)
* does not matter; if there is any, it is discarded.
*/
export class MuxAsyncIterator<T> implements AsyncIterable<T> {
private iteratorCount = 0;
private yields: Array<TaggedYieldedValue<T>> = [];
// deno-lint-ignore no-explicit-any
private throws: any[] = [];
private signal: Deferred<void> = deferred();
add(iterator: AsyncIterableIterator<T>): void {
++this.iteratorCount;
this.callIteratorNext(iterator);
}
private async callIteratorNext(
iterator: AsyncIterableIterator<T>,
): Promise<void> {
try {
const { value, done } = await iterator.next();
if (done) {
--this.iteratorCount;
} else {
this.yields.push({ iterator, value });
}
} catch (e) {
this.throws.push(e);
}
this.signal.resolve();
}
async *iterate(): AsyncIterableIterator<T> {
while (this.iteratorCount > 0) {
// Sleep until any of the wrapped iterators yields.
await this.signal;
// Note that while we're looping over `yields`, new items may be added.
for (let i = 0; i < this.yields.length; i++) {
const { iterator, value } = this.yields[i];
yield value;
this.callIteratorNext(iterator);
}
if (this.throws.length) {
for (const e of this.throws) {
throw e;
}
this.throws.length = 0;
}
// Clear the `yields` list and reset the `signal` promise.
this.yields.length = 0;
this.signal = deferred();
}
}
[Symbol.asyncIterator](): AsyncIterableIterator<T> {
return this.iterate();
}
}

View File

@ -1,50 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import { MuxAsyncIterator } from "./mux_async_iterator.ts";
async function* gen123(): AsyncIterableIterator<number> {
yield 1;
yield 2;
yield 3;
}
async function* gen456(): AsyncIterableIterator<number> {
yield 4;
yield 5;
yield 6;
}
async function* genThrows(): AsyncIterableIterator<number> {
yield 7;
throw new Error("something went wrong");
}
Deno.test("[async] MuxAsyncIterator", async function (): Promise<void> {
const mux = new MuxAsyncIterator<number>();
mux.add(gen123());
mux.add(gen456());
const results = new Set();
for await (const value of mux) {
results.add(value);
}
assertEquals(results.size, 6);
});
Deno.test({
name: "[async] MuxAsyncIterator throws",
async fn() {
const mux = new MuxAsyncIterator<number>();
mux.add(gen123());
mux.add(genThrows());
const results = new Set();
await assertThrowsAsync(
async () => {
for await (const value of mux) {
results.add(value);
}
},
Error,
"something went wrong",
);
},
});

View File

@ -1,68 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/**
* pooledMap transforms values from an (async) iterable into another async
* iterable. The transforms are done concurrently, with a max concurrency
* defined by the poolLimit.
*
* If an error is thrown from `iterableFn`, no new transformations will begin.
* All currently executing transformations are allowed to finish and still
* yielded on success. After that, the rejections among them are gathered and
* thrown by the iterator in an `AggregateError`.
*
* @param poolLimit The maximum count of items being processed concurrently.
* @param array The input array for mapping.
* @param iteratorFn The function to call for every item of the array.
*/
export function pooledMap<T, R>(
poolLimit: number,
array: Iterable<T> | AsyncIterable<T>,
iteratorFn: (data: T) => Promise<R>,
): AsyncIterableIterator<R> {
// Create the async iterable that is returned from this function.
const res = new TransformStream<Promise<R>, R>({
async transform(
p: Promise<R>,
controller: TransformStreamDefaultController<R>,
): Promise<void> {
controller.enqueue(await p);
},
});
// Start processing items from the iterator
(async (): Promise<void> => {
const writer = res.writable.getWriter();
const executing: Array<Promise<unknown>> = [];
try {
for await (const item of array) {
const p = Promise.resolve().then(() => iteratorFn(item));
// Only write on success. If we `writer.write()` a rejected promise,
// that will end the iteration. We don't want that yet. Instead let it
// fail the race, taking us to the catch block where all currently
// executing jobs are allowed to finish and all rejections among them
// can be reported together.
p.then((v) => writer.write(Promise.resolve(v))).catch(() => {});
const e: Promise<unknown> = p.then(() =>
executing.splice(executing.indexOf(e), 1)
);
executing.push(e);
if (executing.length >= poolLimit) {
await Promise.race(executing);
}
}
// Wait until all ongoing events have processed, then close the writer.
await Promise.all(executing);
writer.close();
} catch {
const errors = [];
for (const result of await Promise.allSettled(executing)) {
if (result.status == "rejected") {
errors.push(result.reason);
}
}
writer.write(Promise.reject(
new AggregateError(errors, "Threw while mapping."),
)).catch(() => {});
}
})();
return res.readable[Symbol.asyncIterator]();
}

View File

@ -1,44 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { delay } from "./delay.ts";
import { pooledMap } from "./pool.ts";
import {
assert,
assertEquals,
assertStringIncludes,
assertThrowsAsync,
} from "../testing/asserts.ts";
Deno.test("[async] pooledMap", async function (): Promise<void> {
const start = new Date();
const results = pooledMap(
2,
[1, 2, 3],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
);
for await (const value of results) {
console.log(value);
}
const diff = new Date().getTime() - start.getTime();
assert(diff >= 2000);
assert(diff < 3000);
});
Deno.test("[async] pooledMap errors", async function (): Promise<void> {
async function mapNumber(n: number): Promise<number> {
if (n <= 2) {
throw new Error(`Bad number: ${n}`);
}
await delay(100);
return n;
}
const mappedNumbers: number[] = [];
const error = await assertThrowsAsync(async () => {
for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) {
mappedNumbers.push(m);
}
}, AggregateError) as AggregateError;
assertEquals(mappedNumbers, [3]);
assertEquals(error.errors.length, 2);
assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1");
assertStringIncludes(error.errors[1].stack, "Error: Bad number: 2");
});

View File

@ -1,2 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import "./mod.ts";

View File

@ -1,137 +0,0 @@
# bytes
bytes module is made to provide helpers to manipulation of bytes slice.
# usage
All the following functions are exposed in `mod.ts`.
## indexOf
Find first index of binary pattern from given binary array, or -1 if it is not
present.
```typescript
import { indexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
indexOf(
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]),
); // => returns 2
indexOf(
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]),
3,
); // => returns 5
```
## lastIndexOf
Find last index of binary pattern from given binary array, or -1 if it is not
present.
```typescript
import { lastIndexOf } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
lastIndexOf(
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
new Uint8Array([0, 1, 2]),
); // => returns 5
lastIndexOf(
new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
new Uint8Array([0, 1, 2]),
3,
); // => returns 0
```
## equals
Check whether given binary arrays are equal to each other.
```typescript
import { equals } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false
```
## startsWith
Check whether binary array starts with prefix.
```typescript
import { startsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false
```
## endsWith
Check whether binary array ends with suffix.
```typescript
import { endsWith } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true
```
## repeat
Repeat bytes of given binary array and return new one.
```typescript
import { repeat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]
```
## concat
Concatenate multiple binary arrays and return new one.
```typescript
import { concat } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ]
concat(
new Uint8Array([1, 2]),
new Uint8Array([3, 4]),
new Uint8Array([5, 6]),
new Uint8Array([7, 8]),
); // => returns Uint8Array(8) [ 1, 2, 3, 4, 5, 6, 7, 8 ]
```
## contains
Check source array contains pattern array.
```typescript
import { contains } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
contains(
new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
new Uint8Array([0, 1, 2]),
); // => returns true
contains(
new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
new Uint8Array([2, 2]),
); // => returns false
```
## copy
Copy bytes from one binary array to another.
```typescript
import { copy } from "https://deno.land/std@$STD_VERSION/bytes/mod.ts";
const dest = new Uint8Array(4);
const src = Uint8Array.of(1, 2, 3, 4);
const len = copy(src, dest); // returns len = 4
```

View File

@ -1,190 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/** Find first index of binary pattern from source. If not found, then return -1
* @param source source array
* @param pat pattern to find in source array
* @param start the index to start looking in the source
*/
export function indexOf(
source: Uint8Array,
pat: Uint8Array,
start = 0,
): number {
if (start >= source.length) {
return -1;
}
if (start < 0) {
start = 0;
}
const s = pat[0];
for (let i = start; i < source.length; i++) {
if (source[i] !== s) continue;
const pin = i;
let matched = 1;
let j = i;
while (matched < pat.length) {
j++;
if (source[j] !== pat[j - pin]) {
break;
}
matched++;
}
if (matched === pat.length) {
return pin;
}
}
return -1;
}
/** Find last index of binary pattern from source. If not found, then return -1.
* @param source source array
* @param pat pattern to find in source array
* @param start the index to start looking in the source
*/
export function lastIndexOf(
source: Uint8Array,
pat: Uint8Array,
start = source.length - 1,
): number {
if (start < 0) {
return -1;
}
if (start >= source.length) {
start = source.length - 1;
}
const e = pat[pat.length - 1];
for (let i = start; i >= 0; i--) {
if (source[i] !== e) continue;
const pin = i;
let matched = 1;
let j = i;
while (matched < pat.length) {
j--;
if (source[j] !== pat[pat.length - 1 - (pin - j)]) {
break;
}
matched++;
}
if (matched === pat.length) {
return pin - pat.length + 1;
}
}
return -1;
}
/** Check whether binary arrays are equal to each other.
* @param a first array to check equality
* @param b second array to check equality
*/
export function equals(a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < b.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
/** Check whether binary array starts with prefix.
* @param source source array
* @param prefix prefix array to check in source
*/
export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean {
for (let i = 0, max = prefix.length; i < max; i++) {
if (source[i] !== prefix[i]) return false;
}
return true;
}
/** Check whether binary array ends with suffix.
* @param source source array
* @param suffix suffix array to check in source
*/
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
for (
let srci = source.length - 1, sfxi = suffix.length - 1;
sfxi >= 0;
srci--, sfxi--
) {
if (source[srci] !== suffix[sfxi]) return false;
}
return true;
}
/** Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
* @param origin The origin bytes
* @param count The count you want to repeat.
* @throws `RangeError` When count is negative
*/
export function repeat(origin: Uint8Array, count: number): Uint8Array {
if (count === 0) {
return new Uint8Array();
}
if (count < 0) {
throw new RangeError("bytes: negative repeat count");
} else if ((origin.length * count) / count !== origin.length) {
throw new Error("bytes: repeat count causes overflow");
}
const int = Math.floor(count);
if (int !== count) {
throw new Error("bytes: repeat count must be an integer");
}
const nb = new Uint8Array(origin.length * count);
let bp = copy(origin, nb);
for (; bp < nb.length; bp *= 2) {
copy(nb.slice(0, bp), nb, bp);
}
return nb;
}
/** Concatenate multiple binary arrays and return new one.
* @param buf binary arrays to concatenate
*/
export function concat(...buf: Uint8Array[]): Uint8Array {
let length = 0;
for (const b of buf) {
length += b.length;
}
const output = new Uint8Array(length);
let index = 0;
for (const b of buf) {
output.set(b, index);
index += b.length;
}
return output;
}
/** Check source array contains pattern array.
* @param source source array
* @param pat patter array
*/
export function contains(source: Uint8Array, pat: Uint8Array): boolean {
return indexOf(source, pat) != -1;
}
/**
* Copy bytes from one Uint8Array to another. Bytes from `src` which don't fit
* into `dst` will not be copied.
*
* @param src Source byte array
* @param dst Destination byte array
* @param off Offset into `dst` at which to begin writing values from `src`.
* @return number of bytes copied
*/
export function copy(src: Uint8Array, dst: Uint8Array, off = 0): number {
off = Math.max(0, Math.min(off, dst.byteLength));
const dstBytesAvailable = dst.byteLength - off;
if (src.byteLength > dstBytesAvailable) {
src = src.subarray(0, dstBytesAvailable);
}
dst.set(src, off);
return src.byteLength;
}

View File

@ -1,213 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
concat,
contains,
copy,
endsWith,
equals,
indexOf,
lastIndexOf,
repeat,
startsWith,
} from "./mod.ts";
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import { decode, encode } from "../encoding/utf8.ts";
Deno.test("[bytes] indexOf1", () => {
const i = indexOf(
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]),
);
assertEquals(i, 2);
});
Deno.test("[bytes] indexOf2", () => {
const i = indexOf(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
assertEquals(i, 1);
});
Deno.test("[bytes] indexOf3", () => {
const i = indexOf(encode("Deno"), encode("D"));
assertEquals(i, 0);
});
Deno.test("[bytes] indexOf4", () => {
const i = indexOf(new Uint8Array(), new Uint8Array([0, 1]));
assertEquals(i, -1);
});
Deno.test("[bytes] indexOf with start index", () => {
const i = indexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
1,
);
assertEquals(i, 3);
});
Deno.test("[bytes] indexOf with start index 2", () => {
const i = indexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
7,
);
assertEquals(i, -1);
});
Deno.test("[bytes] lastIndexOf1", () => {
const i = lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
new Uint8Array([0, 1, 2]),
);
assertEquals(i, 3);
});
Deno.test("[bytes] lastIndexOf2", () => {
const i = lastIndexOf(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]));
assertEquals(i, 0);
});
Deno.test("[bytes] lastIndexOf3", () => {
const i = lastIndexOf(new Uint8Array(), new Uint8Array([0, 1]));
assertEquals(i, -1);
});
Deno.test("[bytes] lastIndexOf with start index", () => {
const i = lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
2,
);
assertEquals(i, 0);
});
Deno.test("[bytes] lastIndexOf with start index 2", () => {
const i = lastIndexOf(
new Uint8Array([0, 1, 2, 0, 1, 2]),
new Uint8Array([0, 1]),
-1,
);
assertEquals(i, -1);
});
Deno.test("[bytes] equals", () => {
const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
assertEquals(v, true);
});
Deno.test("[bytes] startsWith", () => {
const v = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
assertEquals(v, true);
});
Deno.test("[bytes] endsWith", () => {
const v = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2]));
assertEquals(v, true);
});
Deno.test("[bytes] repeat", () => {
// input / output / count / error message
const repeatTestCase = [
["", "", 0],
["", "", 1],
["", "", 1.1, "bytes: repeat count must be an integer"],
["", "", 2],
["", "", 0],
["-", "", 0],
["-", "-", -1, "bytes: negative repeat count"],
["-", "----------", 10],
["abc ", "abc abc abc ", 3],
];
for (const [input, output, count, errMsg] of repeatTestCase) {
if (errMsg) {
assertThrows(
(): void => {
repeat(new TextEncoder().encode(input as string), count as number);
},
Error,
errMsg as string,
);
} else {
const newBytes = repeat(
new TextEncoder().encode(input as string),
count as number,
);
assertEquals(new TextDecoder().decode(newBytes), output);
}
}
});
Deno.test("[bytes] concat", () => {
const u1 = encode("Hello ");
const u2 = encode("World");
const joined = concat(u1, u2);
assertEquals(decode(joined), "Hello World");
assert(u1 !== joined);
assert(u2 !== joined);
});
Deno.test("[bytes] concat empty arrays", () => {
const u1 = new Uint8Array();
const u2 = new Uint8Array();
const joined = concat(u1, u2);
assertEquals(joined.byteLength, 0);
assert(u1 !== joined);
assert(u2 !== joined);
});
Deno.test("[bytes] concat multiple arrays", () => {
const u1 = encode("Hello ");
const u2 = encode("W");
const u3 = encode("o");
const u4 = encode("r");
const u5 = encode("l");
const u6 = encode("d");
const joined = concat(u1, u2, u3, u4, u5, u6);
assertEquals(decode(joined), "Hello World");
assert(u1 !== joined);
assert(u2 !== joined);
});
Deno.test("[bytes] contains", () => {
const source = encode("deno.land");
const pattern = encode("deno");
assert(contains(source, pattern));
assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
});
Deno.test("[bytes] copy", function (): void {
const dst = new Uint8Array(4);
dst.fill(0);
let src = Uint8Array.of(1, 2);
let len = copy(src, dst, 0);
assert(len === 2);
assertEquals(dst, Uint8Array.of(1, 2, 0, 0));
dst.fill(0);
src = Uint8Array.of(1, 2);
len = copy(src, dst, 1);
assert(len === 2);
assertEquals(dst, Uint8Array.of(0, 1, 2, 0));
dst.fill(0);
src = Uint8Array.of(1, 2, 3, 4, 5);
len = copy(src, dst);
assert(len === 4);
assertEquals(dst, Uint8Array.of(1, 2, 3, 4));
dst.fill(0);
src = Uint8Array.of(1, 2);
len = copy(src, dst, 100);
assert(len === 0);
assertEquals(dst, Uint8Array.of(0, 0, 0, 0));
dst.fill(0);
src = Uint8Array.of(3, 4);
len = copy(src, dst, -2);
assert(len === 2);
assertEquals(dst, Uint8Array.of(3, 4, 0, 0));
});

View File

@ -1,188 +0,0 @@
# datetime
Simple helper to help parse date strings into `Date`, with additional functions.
## Usage
The following symbols from
[unicode LDML](http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
are supported:
- `yyyy` - numeric year.
- `yy` - 2-digit year.
- `M` - numeric month.
- `MM` - 2-digit month.
- `d` - numeric day.
- `dd` - 2-digit day.
- `H` - numeric hour (0-23 hours).
- `HH` - 2-digit hour (00-23 hours).
- `h` - numeric hour (1-12 hours).
- `hh` - 2-digit hour (01-12 hours).
- `m` - numeric minute.
- `mm` - 2-digit minute.
- `s` - numeric second.
- `ss` - 2-digit second.
- `S` - 1-digit fractionalSecond.
- `SS` - 2-digit fractionalSecond.
- `SSS` - 3-digit fractionalSecond.
- `a` - dayPeriod, either `AM` or `PM`.
- `'foo'` - quoted literal.
- `./-` - unquoted literal.
## Methods
### parse
Takes an input `string` and a `formatString` to parse to a `date`.
```ts
import { parse } from 'https://deno.land/std@$STD_VERSION/datetime/mod.ts'
parse("20-01-2019", "dd-MM-yyyy") // output : new Date(2019, 0, 20)
parse("2019-01-20", "yyyy-MM-dd") // output : new Date(2019, 0, 20)
parse("20.01.2019", "dd.MM.yyyy") // output : new Date(2019, 0, 20)
parse("01-20-2019 16:34", "MM-dd-yyyy HH:mm") // output : new Date(2019, 0, 20, 16, 34)
parse("01-20-2019 04:34 PM", "MM-dd-yyyy hh:mm a") // output : new Date(2019, 0, 20, 16, 34)
parse("16:34 01-20-2019", "HH:mm MM-dd-yyyy") // output : new Date(2019, 0, 20, 16, 34)
parse("01-20-2019 16:34:23.123", "MM-dd-yyyy HH:mm:ss.SSS") // output : new Date(2019, 0, 20, 16, 34, 23, 123)
...
```
### format
Takes an input `date` and a `formatString` to format to a `string`.
```ts
import { format } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
format(new Date(2019, 0, 20), "dd-MM-yyyy"); // output : "20-01-2019"
format(new Date(2019, 0, 20), "yyyy-MM-dd"); // output : "2019-01-20"
format(new Date(2019, 0, 20), "dd.MM.yyyy"); // output : "2019-01-20"
format(new Date(2019, 0, 20, 16, 34), "MM-dd-yyyy HH:mm"); // output : "01-20-2019 16:34"
format(new Date(2019, 0, 20, 16, 34), "MM-dd-yyyy hh:mm a"); // output : "01-20-2019 04:34 PM"
format(new Date(2019, 0, 20, 16, 34), "HH:mm MM-dd-yyyy"); // output : "16:34 01-20-2019"
format(new Date(2019, 0, 20, 16, 34, 23, 123), "MM-dd-yyyy HH:mm:ss.SSS"); // output : "01-20-2019 16:34:23.123"
format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd"); // output : "today: 2019-01-20"
```
### dayOfYear
Returns the number of the day in the year.
```ts
import { dayOfYear } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
dayOfYear(new Date("2019-03-11T03:24:00")); // output: 70
```
### weekOfYear
Returns the ISO week number of the provided date (1-53).
```ts
import { weekOfYear } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
weekOfYear(new Date("2020-12-28T03:24:00")); // Returns 53
```
### toIMF
Formats the given date to IMF date time format. (Reference:
https://tools.ietf.org/html/rfc7231#section-7.1.1.1 )
```js
import { toIMF } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
toIMF(new Date(0)); // => returns "Thu, 01 Jan 1970 00:00:00 GMT"
```
### isLeap
Returns true if the given date or year (in number) is a leap year. Returns false
otherwise.
```js
import { isLeap } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
isLeap(new Date("1970-01-01")); // => returns false
isLeap(new Date("1972-01-01")); // => returns true
isLeap(new Date("2000-01-01")); // => returns true
isLeap(new Date("2100-01-01")); // => returns false
isLeap(1972); // => returns true
```
### difference
Returns the difference of the 2 given dates in the given units. If the units are
omitted, it returns the difference in the all available units.
Available units: "milliseconds", "seconds", "minutes", "hours", "days", "weeks",
"months", "quarters", "years"
```js
import { difference } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
const date0 = new Date("2018-05-14");
const date1 = new Date("2020-05-13");
difference(date0, date1, { units: ["days", "months", "years"] });
// => returns { days: 730, months: 23, years: 1 }
difference(date0, date1);
// => returns {
// milliseconds: 63072000000,
// seconds: 63072000,
// minutes: 1051200,
// hours: 17520,
// days: 730,
// weeks: 104,
// months: 23,
// quarters: 5,
// years: 1
// }
```
## Constants
### SECOND
```
import { SECOND } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
console.log(SECOND); // => 1000
```
### MINUTE
```
import { MINUTE } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
console.log(MINUTE); // => 60000 (60 * 1000)
```
### HOUR
```
import { HOUR } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
console.log(HOUR); // => 3600000 (60 * 60 * 1000)
```
### DAY
```
import { DAY } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
console.log(DAY); // => 86400000 (24 * 60 * 60 * 1000)
```
### WEEK
```
import { WEEK } from "https://deno.land/std@$STD_VERSION/datetime/mod.ts";
console.log(WEEK); // => 604800000 (7 * 24 * 60 * 60 * 1000)
```

View File

@ -1,594 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
CallbackResult,
ReceiverResult,
Rule,
TestFunction,
TestResult,
Tokenizer,
} from "./tokenizer.ts";
function digits(value: string | number, count = 2): string {
return String(value).padStart(count, "0");
}
// as declared as in namespace Intl
type DateTimeFormatPartTypes =
| "day"
| "dayPeriod"
// | "era"
| "hour"
| "literal"
| "minute"
| "month"
| "second"
| "timeZoneName"
// | "weekday"
| "year"
| "fractionalSecond";
interface DateTimeFormatPart {
type: DateTimeFormatPartTypes;
value: string;
}
type TimeZone = "UTC";
interface Options {
timeZone?: TimeZone;
}
function createLiteralTestFunction(value: string): TestFunction {
return (string: string): TestResult => {
return string.startsWith(value)
? { value, length: value.length }
: undefined;
};
}
function createMatchTestFunction(match: RegExp): TestFunction {
return (string: string): TestResult => {
const result = match.exec(string);
if (result) return { value: result, length: result[0].length };
};
}
// according to unicode symbols (http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
const defaultRules = [
{
test: createLiteralTestFunction("yyyy"),
fn: (): CallbackResult => ({ type: "year", value: "numeric" }),
},
{
test: createLiteralTestFunction("yy"),
fn: (): CallbackResult => ({ type: "year", value: "2-digit" }),
},
{
test: createLiteralTestFunction("MM"),
fn: (): CallbackResult => ({ type: "month", value: "2-digit" }),
},
{
test: createLiteralTestFunction("M"),
fn: (): CallbackResult => ({ type: "month", value: "numeric" }),
},
{
test: createLiteralTestFunction("dd"),
fn: (): CallbackResult => ({ type: "day", value: "2-digit" }),
},
{
test: createLiteralTestFunction("d"),
fn: (): CallbackResult => ({ type: "day", value: "numeric" }),
},
{
test: createLiteralTestFunction("HH"),
fn: (): CallbackResult => ({ type: "hour", value: "2-digit" }),
},
{
test: createLiteralTestFunction("H"),
fn: (): CallbackResult => ({ type: "hour", value: "numeric" }),
},
{
test: createLiteralTestFunction("hh"),
fn: (): CallbackResult => ({
type: "hour",
value: "2-digit",
hour12: true,
}),
},
{
test: createLiteralTestFunction("h"),
fn: (): CallbackResult => ({
type: "hour",
value: "numeric",
hour12: true,
}),
},
{
test: createLiteralTestFunction("mm"),
fn: (): CallbackResult => ({ type: "minute", value: "2-digit" }),
},
{
test: createLiteralTestFunction("m"),
fn: (): CallbackResult => ({ type: "minute", value: "numeric" }),
},
{
test: createLiteralTestFunction("ss"),
fn: (): CallbackResult => ({ type: "second", value: "2-digit" }),
},
{
test: createLiteralTestFunction("s"),
fn: (): CallbackResult => ({ type: "second", value: "numeric" }),
},
{
test: createLiteralTestFunction("SSS"),
fn: (): CallbackResult => ({ type: "fractionalSecond", value: 3 }),
},
{
test: createLiteralTestFunction("SS"),
fn: (): CallbackResult => ({ type: "fractionalSecond", value: 2 }),
},
{
test: createLiteralTestFunction("S"),
fn: (): CallbackResult => ({ type: "fractionalSecond", value: 1 }),
},
{
test: createLiteralTestFunction("a"),
fn: (value: unknown): CallbackResult => ({
type: "dayPeriod",
value: value as string,
}),
},
// quoted literal
{
test: createMatchTestFunction(/^(')(?<value>\\.|[^\']*)\1/),
fn: (match: unknown): CallbackResult => ({
type: "literal",
value: (match as RegExpExecArray).groups!.value as string,
}),
},
// literal
{
test: createMatchTestFunction(/^.+?\s*/),
fn: (match: unknown): CallbackResult => ({
type: "literal",
value: (match as RegExpExecArray)[0],
}),
},
];
type FormatPart = {
type: DateTimeFormatPartTypes;
value: string | number;
hour12?: boolean;
};
type Format = FormatPart[];
export class DateTimeFormatter {
#format: Format;
constructor(formatString: string, rules: Rule[] = defaultRules) {
const tokenizer = new Tokenizer(rules);
this.#format = tokenizer.tokenize(
formatString,
({ type, value, hour12 }) => {
const result = {
type,
value,
} as unknown as ReceiverResult;
if (hour12) result.hour12 = hour12 as boolean;
return result;
},
) as Format;
}
format(date: Date, options: Options = {}): string {
let string = "";
const utc = options.timeZone === "UTC";
for (const token of this.#format) {
const type = token.type;
switch (type) {
case "year": {
const value = utc ? date.getUTCFullYear() : date.getFullYear();
switch (token.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2).slice(-2);
break;
}
default:
throw Error(
`FormatterError: value "${token.value}" is not supported`,
);
}
break;
}
case "month": {
const value = (utc ? date.getUTCMonth() : date.getMonth()) + 1;
switch (token.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw Error(
`FormatterError: value "${token.value}" is not supported`,
);
}
break;
}
case "day": {
const value = utc ? date.getUTCDate() : date.getDate();
switch (token.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw Error(
`FormatterError: value "${token.value}" is not supported`,
);
}
break;
}
case "hour": {
let value = utc ? date.getUTCHours() : date.getHours();
value -= token.hour12 && date.getHours() > 12 ? 12 : 0;
switch (token.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw Error(
`FormatterError: value "${token.value}" is not supported`,
);
}
break;
}
case "minute": {
const value = utc ? date.getUTCMinutes() : date.getMinutes();
switch (token.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw Error(
`FormatterError: value "${token.value}" is not supported`,
);
}
break;
}
case "second": {
const value = utc ? date.getUTCSeconds() : date.getSeconds();
switch (token.value) {
case "numeric": {
string += value;
break;
}
case "2-digit": {
string += digits(value, 2);
break;
}
default:
throw Error(
`FormatterError: value "${token.value}" is not supported`,
);
}
break;
}
case "fractionalSecond": {
const value = utc
? date.getUTCMilliseconds()
: date.getMilliseconds();
string += digits(value, Number(token.value));
break;
}
// FIXME(bartlomieju)
case "timeZoneName": {
// string += utc ? "Z" : token.value
break;
}
case "dayPeriod": {
string += token.value ? (date.getHours() >= 12 ? "PM" : "AM") : "";
break;
}
case "literal": {
string += token.value;
break;
}
default:
throw Error(`FormatterError: { ${token.type} ${token.value} }`);
}
}
return string;
}
parseToParts(string: string): DateTimeFormatPart[] {
const parts: DateTimeFormatPart[] = [];
for (const token of this.#format) {
const type = token.type;
let value = "";
switch (token.type) {
case "year": {
switch (token.value) {
case "numeric": {
value = /^\d{1,4}/.exec(string)?.[0] as string;
break;
}
case "2-digit": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
break;
}
}
break;
}
case "month": {
switch (token.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
break;
}
case "narrow": {
value = /^[a-zA-Z]+/.exec(string)?.[0] as string;
break;
}
case "short": {
value = /^[a-zA-Z]+/.exec(string)?.[0] as string;
break;
}
case "long": {
value = /^[a-zA-Z]+/.exec(string)?.[0] as string;
break;
}
default:
throw Error(
`ParserError: value "${token.value}" is not supported`,
);
}
break;
}
case "day": {
switch (token.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
break;
}
default:
throw Error(
`ParserError: value "${token.value}" is not supported`,
);
}
break;
}
case "hour": {
switch (token.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
if (token.hour12 && parseInt(value) > 12) {
console.error(
`Trying to parse hour greater than 12. Use 'H' instead of 'h'.`,
);
}
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
if (token.hour12 && parseInt(value) > 12) {
console.error(
`Trying to parse hour greater than 12. Use 'HH' instead of 'hh'.`,
);
}
break;
}
default:
throw Error(
`ParserError: value "${token.value}" is not supported`,
);
}
break;
}
case "minute": {
switch (token.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
break;
}
default:
throw Error(
`ParserError: value "${token.value}" is not supported`,
);
}
break;
}
case "second": {
switch (token.value) {
case "numeric": {
value = /^\d{1,2}/.exec(string)?.[0] as string;
break;
}
case "2-digit": {
value = /^\d{2}/.exec(string)?.[0] as string;
break;
}
default:
throw Error(
`ParserError: value "${token.value}" is not supported`,
);
}
break;
}
case "fractionalSecond": {
value = new RegExp(`^\\d{${token.value}}`).exec(string)
?.[0] as string;
break;
}
case "timeZoneName": {
value = token.value as string;
break;
}
case "dayPeriod": {
value = /^(A|P)M/.exec(string)?.[0] as string;
break;
}
case "literal": {
if (!string.startsWith(token.value as string)) {
throw Error(
`Literal "${token.value}" not found "${string.slice(0, 25)}"`,
);
}
value = token.value as string;
break;
}
default:
throw Error(`${token.type} ${token.value}`);
}
if (!value) {
throw Error(
`value not valid for token { ${type} ${value} } ${
string.slice(
0,
25,
)
}`,
);
}
parts.push({ type, value });
string = string.slice(value.length);
}
if (string.length) {
throw Error(
`datetime string was not fully parsed! ${string.slice(0, 25)}`,
);
}
return parts;
}
/** sort & filter dateTimeFormatPart */
sortDateTimeFormatPart(parts: DateTimeFormatPart[]): DateTimeFormatPart[] {
let result: DateTimeFormatPart[] = [];
const typeArray = [
"year",
"month",
"day",
"hour",
"minute",
"second",
"fractionalSecond",
];
for (const type of typeArray) {
const current = parts.findIndex((el) => el.type === type);
if (current !== -1) {
result = result.concat(parts.splice(current, 1));
}
}
result = result.concat(parts);
return result;
}
partsToDate(parts: DateTimeFormatPart[]): Date {
const date = new Date();
const utc = parts.find(
(part) => part.type === "timeZoneName" && part.value === "UTC",
);
utc ? date.setUTCHours(0, 0, 0, 0) : date.setHours(0, 0, 0, 0);
for (const part of parts) {
switch (part.type) {
case "year": {
const value = Number(part.value.padStart(4, "20"));
utc ? date.setUTCFullYear(value) : date.setFullYear(value);
break;
}
case "month": {
const value = Number(part.value) - 1;
utc ? date.setUTCMonth(value) : date.setMonth(value);
break;
}
case "day": {
const value = Number(part.value);
utc ? date.setUTCDate(value) : date.setDate(value);
break;
}
case "hour": {
let value = Number(part.value);
const dayPeriod = parts.find(
(part: DateTimeFormatPart) => part.type === "dayPeriod",
);
if (dayPeriod?.value === "PM") value += 12;
utc ? date.setUTCHours(value) : date.setHours(value);
break;
}
case "minute": {
const value = Number(part.value);
utc ? date.setUTCMinutes(value) : date.setMinutes(value);
break;
}
case "second": {
const value = Number(part.value);
utc ? date.setUTCSeconds(value) : date.setSeconds(value);
break;
}
case "fractionalSecond": {
const value = Number(part.value);
utc ? date.setUTCMilliseconds(value) : date.setMilliseconds(value);
break;
}
}
}
return date;
}
parse(string: string): Date {
const parts = this.parseToParts(string);
const sortParts = this.sortDateTimeFormatPart(parts);
return this.partsToDate(sortParts);
}
}

View File

@ -1,249 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { DateTimeFormatter } from "./formatter.ts";
export const SECOND = 1e3;
export const MINUTE = SECOND * 60;
export const HOUR = MINUTE * 60;
export const DAY = HOUR * 24;
export const WEEK = DAY * 7;
const DAYS_PER_WEEK = 7;
enum Day {
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
}
/**
* Parse date from string using format string
* @param dateString Date string
* @param format Format string
* @return Parsed date
*/
export function parse(dateString: string, formatString: string): Date {
const formatter = new DateTimeFormatter(formatString);
const parts = formatter.parseToParts(dateString);
const sortParts = formatter.sortDateTimeFormatPart(parts);
return formatter.partsToDate(sortParts);
}
/**
* Format date using format string
* @param date Date
* @param format Format string
* @return formatted date string
*/
export function format(date: Date, formatString: string): string {
const formatter = new DateTimeFormatter(formatString);
return formatter.format(date);
}
/**
* Get number of the day in the year
* @return Number of the day in year
*/
export function dayOfYear(date: Date): number {
// Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date)
// Using setFullYear as a workaround
const yearStart = new Date(date);
yearStart.setUTCFullYear(date.getUTCFullYear(), 0, 0);
const diff = date.getTime() -
yearStart.getTime() +
(yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
return Math.floor(diff / DAY);
}
/**
* Get number of the week in the year (ISO-8601)
* @return Number of the week in year
*/
export function weekOfYear(date: Date): number {
const workingDate = new Date(
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),
);
const day = workingDate.getUTCDay();
const nearestThursday = workingDate.getUTCDate() +
Day.Thu -
(day === Day.Sun ? DAYS_PER_WEEK : day);
workingDate.setUTCDate(nearestThursday);
// Get first day of year
const yearStart = new Date(Date.UTC(workingDate.getUTCFullYear(), 0, 1));
// return the calculated full weeks to nearest Thursday
return Math.ceil((workingDate.getTime() - yearStart.getTime() + DAY) / WEEK);
}
/**
* Parse a date to return a IMF formatted string date
* RFC: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
* IMF is the time format to use when generating times in HTTP
* headers. The time being formatted must be in UTC for Format to
* generate the correct format.
* @param date Date to parse
* @return IMF date formatted string
*/
export function toIMF(date: Date): string {
function dtPad(v: string, lPad = 2): string {
return v.padStart(lPad, "0");
}
const d = dtPad(date.getUTCDate().toString());
const h = dtPad(date.getUTCHours().toString());
const min = dtPad(date.getUTCMinutes().toString());
const s = dtPad(date.getUTCSeconds().toString());
const y = date.getUTCFullYear();
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
return `${days[date.getUTCDay()]}, ${d} ${
months[date.getUTCMonth()]
} ${y} ${h}:${min}:${s} GMT`;
}
/**
* Check given year is a leap year or not.
* based on : https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year
* @param year year in number or Date format
*/
export function isLeap(year: Date | number): boolean {
const yearNumber = year instanceof Date ? year.getFullYear() : year;
return (
(yearNumber % 4 === 0 && yearNumber % 100 !== 0) || yearNumber % 400 === 0
);
}
export type Unit =
| "milliseconds"
| "seconds"
| "minutes"
| "hours"
| "days"
| "weeks"
| "months"
| "quarters"
| "years";
export type DifferenceFormat = Partial<Record<Unit, number>>;
export type DifferenceOptions = {
units?: Unit[];
};
/**
* Calculate difference between two dates.
* @param from Year to calculate difference
* @param to Year to calculate difference with
* @param options Options for determining how to respond
*
* example :
*
* ```typescript
* datetime.difference(new Date("2020/1/1"),new Date("2020/2/2"),{ units : ["days","months"] })
* ```
*/
export function difference(
from: Date,
to: Date,
options?: DifferenceOptions,
): DifferenceFormat {
const uniqueUnits = options?.units ? [...new Set(options?.units)] : [
"milliseconds",
"seconds",
"minutes",
"hours",
"days",
"weeks",
"months",
"quarters",
"years",
];
const bigger = Math.max(from.getTime(), to.getTime());
const smaller = Math.min(from.getTime(), to.getTime());
const differenceInMs = bigger - smaller;
const differences: DifferenceFormat = {};
for (const uniqueUnit of uniqueUnits) {
switch (uniqueUnit) {
case "milliseconds":
differences.milliseconds = differenceInMs;
break;
case "seconds":
differences.seconds = Math.floor(differenceInMs / SECOND);
break;
case "minutes":
differences.minutes = Math.floor(differenceInMs / MINUTE);
break;
case "hours":
differences.hours = Math.floor(differenceInMs / HOUR);
break;
case "days":
differences.days = Math.floor(differenceInMs / DAY);
break;
case "weeks":
differences.weeks = Math.floor(differenceInMs / WEEK);
break;
case "months":
differences.months = calculateMonthsDifference(bigger, smaller);
break;
case "quarters":
differences.quarters = Math.floor(
(typeof differences.months !== "undefined" &&
differences.months / 4) ||
calculateMonthsDifference(bigger, smaller) / 4,
);
break;
case "years":
differences.years = Math.floor(
(typeof differences.months !== "undefined" &&
differences.months / 12) ||
calculateMonthsDifference(bigger, smaller) / 12,
);
break;
}
}
return differences;
}
function calculateMonthsDifference(bigger: number, smaller: number): number {
const biggerDate = new Date(bigger);
const smallerDate = new Date(smaller);
const yearsDiff = biggerDate.getFullYear() - smallerDate.getFullYear();
const monthsDiff = biggerDate.getMonth() - smallerDate.getMonth();
const calendarDifferences = Math.abs(yearsDiff * 12 + monthsDiff);
const compareResult = biggerDate > smallerDate ? 1 : -1;
biggerDate.setMonth(
biggerDate.getMonth() - compareResult * calendarDifferences,
);
const isLastMonthNotFull = biggerDate > smallerDate
? 1
: -1 === -compareResult
? 1
: 0;
const months = compareResult * (calendarDifferences - isLastMonthNotFull);
return months === 0 ? 0 : months;
}

View File

@ -1,393 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import * as datetime from "./mod.ts";
Deno.test({
name: "[std/datetime] parse",
fn: () => {
assertEquals(
datetime.parse("01-03-2019 16:30", "MM-dd-yyyy HH:mm"),
new Date(2019, 0, 3, 16, 30),
);
assertEquals(
datetime.parse("01.03.2019 16:30", "MM.dd.yyyy HH:mm"),
new Date(2019, 0, 3, 16, 30),
);
assertEquals(
datetime.parse("01.03.2019 16:30", "MM.dd.yyyy HH:mm"),
new Date(2019, 0, 3, 16, 30),
);
assertEquals(
datetime.parse("03-01-2019 16:31", "dd-MM-yyyy HH:mm"),
new Date(2019, 0, 3, 16, 31),
);
assertEquals(
datetime.parse("2019-01-03 16:32", "yyyy-MM-dd HH:mm"),
new Date(2019, 0, 3, 16, 32),
);
assertEquals(
datetime.parse("16:33 01-03-2019", "HH:mm MM-dd-yyyy"),
new Date(2019, 0, 3, 16, 33),
);
assertEquals(
datetime.parse("01-03-2019 16:33:23.123", "MM-dd-yyyy HH:mm:ss.SSS"),
new Date(2019, 0, 3, 16, 33, 23, 123),
);
assertEquals(
datetime.parse("01-03-2019 09:33 PM", "MM-dd-yyyy HH:mm a"),
new Date(2019, 0, 3, 21, 33),
);
assertEquals(
datetime.parse("16:34 03-01-2019", "HH:mm dd-MM-yyyy"),
new Date(2019, 0, 3, 16, 34),
);
assertEquals(
datetime.parse("16:35 2019-01-03", "HH:mm yyyy-MM-dd"),
new Date(2019, 0, 3, 16, 35),
);
assertEquals(
datetime.parse("01-03-2019", "MM-dd-yyyy"),
new Date(2019, 0, 3),
);
assertEquals(
datetime.parse("03-01-2019", "dd-MM-yyyy"),
new Date(2019, 0, 3),
);
assertEquals(
datetime.parse("31-10-2019", "dd-MM-yyyy"),
new Date(2019, 9, 31),
);
assertEquals(
datetime.parse("2019-01-03", "yyyy-MM-dd"),
new Date(2019, 0, 3),
);
},
});
Deno.test({
name: "[std/datetime] invalidParseDateTimeFormatThrows",
fn: () => {
assertThrows((): void => {
// deno-lint-ignore no-explicit-any
(datetime as any).parse("2019-01-01 00:00", "x-y-z");
}, Error);
assertThrows((): void => {
// deno-lint-ignore no-explicit-any
(datetime as any).parse("2019-01-01", "x-y-z");
}, Error);
},
});
Deno.test({
name: "[std/datetime] format",
fn: () => {
// 00 hours
assertEquals(
"01:00:00",
datetime.format(new Date("2019-01-01T01:00:00"), "HH:mm:ss"),
);
assertEquals(
"13:00:00",
datetime.format(new Date("2019-01-01T13:00:00"), "HH:mm:ss"),
);
// 12 hours
assertEquals(
"01:00:00",
datetime.format(new Date("2019-01-01T01:00:00"), "hh:mm:ss"),
);
assertEquals(
"01:00:00",
datetime.format(new Date("2019-01-01T13:00:00"), "hh:mm:ss"),
);
// milliseconds
assertEquals(
"13:00:00.000",
datetime.format(new Date("2019-01-01T13:00:00"), "HH:mm:ss.SSS"),
);
assertEquals(
"13:00:00.000",
datetime.format(new Date("2019-01-01T13:00:00.000"), "HH:mm:ss.SSS"),
);
assertEquals(
"13:00:00.123",
datetime.format(new Date("2019-01-01T13:00:00.123"), "HH:mm:ss.SSS"),
);
// day period
assertEquals(
"01:00:00 AM",
datetime.format(new Date("2019-01-01T01:00:00"), "HH:mm:ss a"),
);
assertEquals(
"01:00:00 AM",
datetime.format(new Date("2019-01-01T01:00:00"), "hh:mm:ss a"),
);
assertEquals(
"01:00:00 PM",
datetime.format(new Date("2019-01-01T13:00:00"), "hh:mm:ss a"),
);
assertEquals(
"21:00:00 PM",
datetime.format(new Date("2019-01-01T21:00:00"), "HH:mm:ss a"),
);
assertEquals(
"09:00:00 PM",
datetime.format(new Date("2019-01-01T21:00:00"), "hh:mm:ss a"),
);
// quoted literal
assertEquals(
datetime.format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd"),
"today: 2019-01-20",
);
},
});
Deno.test({
name: "[std/datetime] dayOfYear",
fn: () => {
// from https://golang.org/src/time/time_test.go
// Test YearDay in several different scenarios
// and corner cases
// Non-leap-year tests
assertEquals(datetime.dayOfYear(new Date("2007-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("2007-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("2007-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("2007-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("2007-03-01T00:00:00.000Z")), 60);
assertEquals(datetime.dayOfYear(new Date("2007-03-15T00:00:00.000Z")), 74);
assertEquals(datetime.dayOfYear(new Date("2007-04-01T00:00:00.000Z")), 91);
assertEquals(datetime.dayOfYear(new Date("2007-12-31T00:00:00.000Z")), 365);
// Leap-year tests
assertEquals(datetime.dayOfYear(new Date("2008-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("2008-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("2008-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("2008-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("2008-03-01T00:00:00.000Z")), 61);
assertEquals(datetime.dayOfYear(new Date("2008-03-15T00:00:00.000Z")), 75);
assertEquals(datetime.dayOfYear(new Date("2008-04-01T00:00:00.000Z")), 92);
assertEquals(datetime.dayOfYear(new Date("2008-12-31T00:00:00.000Z")), 366);
// Looks like leap-year (but isn't) tests
assertEquals(datetime.dayOfYear(new Date("1900-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("1900-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("1900-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("1900-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("1900-03-01T00:00:00.000Z")), 60);
assertEquals(datetime.dayOfYear(new Date("1900-03-15T00:00:00.000Z")), 74);
assertEquals(datetime.dayOfYear(new Date("1900-04-01T00:00:00.000Z")), 91);
assertEquals(datetime.dayOfYear(new Date("1900-12-31T00:00:00.000Z")), 365);
// Year one tests (non-leap)
assertEquals(datetime.dayOfYear(new Date("0001-01-01T00:00:00.000Z")), 1);
assertEquals(datetime.dayOfYear(new Date("0001-01-15T00:00:00.000Z")), 15);
assertEquals(datetime.dayOfYear(new Date("0001-02-01T00:00:00.000Z")), 32);
assertEquals(datetime.dayOfYear(new Date("0001-02-15T00:00:00.000Z")), 46);
assertEquals(datetime.dayOfYear(new Date("0001-03-01T00:00:00.000Z")), 60);
assertEquals(datetime.dayOfYear(new Date("0001-03-15T00:00:00.000Z")), 74);
assertEquals(datetime.dayOfYear(new Date("0001-04-01T00:00:00.000Z")), 91);
assertEquals(datetime.dayOfYear(new Date("0001-12-31T00:00:00.000Z")), 365);
// Year minus one tests (non-leap)
assertEquals(
datetime.dayOfYear(new Date("-000001-01-01T00:00:00.000Z")),
1,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-01-15T00:00:00.000Z")),
15,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-02-01T00:00:00.000Z")),
32,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-02-15T00:00:00.000Z")),
46,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-03-01T00:00:00.000Z")),
60,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-03-15T00:00:00.000Z")),
74,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-04-01T00:00:00.000Z")),
91,
);
assertEquals(
datetime.dayOfYear(new Date("-000001-12-31T00:00:00.000Z")),
365,
);
// 400 BC tests (leap-year)
assertEquals(
datetime.dayOfYear(new Date("-000400-01-01T00:00:00.000Z")),
1,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-01-15T00:00:00.000Z")),
15,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-02-01T00:00:00.000Z")),
32,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-02-15T00:00:00.000Z")),
46,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-03-01T00:00:00.000Z")),
61,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-03-15T00:00:00.000Z")),
75,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-04-01T00:00:00.000Z")),
92,
);
assertEquals(
datetime.dayOfYear(new Date("-000400-12-31T00:00:00.000Z")),
366,
);
// Special Cases
// Gregorian calendar change (no effect)
assertEquals(datetime.dayOfYear(new Date("1582-10-04T03:24:00.000Z")), 277);
assertEquals(datetime.dayOfYear(new Date("1582-10-15T03:24:00.000Z")), 288);
},
});
Deno.test({
name: "[std/datetime] weekOfYear",
fn: () => {
assertEquals(datetime.weekOfYear(new Date("2020-01-05T03:00:00.000Z")), 1);
assertEquals(datetime.weekOfYear(new Date("2020-06-28T03:00:00.000Z")), 26);
// iso weeks year starting sunday
assertEquals(datetime.weekOfYear(new Date(2012, 0, 1)), 52);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 2)), 1);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 8)), 1);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 9)), 2);
assertEquals(datetime.weekOfYear(new Date(2012, 0, 15)), 2);
// iso weeks year starting monday
assertEquals(datetime.weekOfYear(new Date(2007, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 7)), 1);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 8)), 2);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 14)), 2);
assertEquals(datetime.weekOfYear(new Date(2007, 0, 15)), 3);
// iso weeks year starting tuesday
assertEquals(datetime.weekOfYear(new Date(2007, 11, 31)), 1);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 6)), 1);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 7)), 2);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 13)), 2);
assertEquals(datetime.weekOfYear(new Date(2008, 0, 14)), 3);
// iso weeks year starting wednesday
assertEquals(datetime.weekOfYear(new Date(2002, 11, 30)), 1);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 5)), 1);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 6)), 2);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 12)), 2);
assertEquals(datetime.weekOfYear(new Date(2003, 0, 13)), 3);
// iso weeks year starting thursday
assertEquals(datetime.weekOfYear(new Date(2008, 11, 29)), 1);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 1)), 1);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 4)), 1);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 5)), 2);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 11)), 2);
assertEquals(datetime.weekOfYear(new Date(2009, 0, 13)), 3);
// iso weeks year starting friday
assertEquals(datetime.weekOfYear(new Date(2009, 11, 28)), 53);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 1)), 53);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 3)), 53);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 4)), 1);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 10)), 1);
assertEquals(datetime.weekOfYear(new Date(2010, 0, 11)), 2);
// iso weeks year starting saturday
assertEquals(datetime.weekOfYear(new Date(2010, 11, 27)), 52);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 1)), 52);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 2)), 52);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 3)), 1);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 9)), 1);
assertEquals(datetime.weekOfYear(new Date(2011, 0, 10)), 2);
},
});
Deno.test({
name: "[std/datetime] to IMF",
fn(): void {
const actual = datetime.toIMF(new Date(Date.UTC(1994, 3, 5, 15, 32)));
const expected = "Tue, 05 Apr 1994 15:32:00 GMT";
assertEquals(actual, expected);
},
});
Deno.test({
name: "[std/datetime] to IMF 0",
fn(): void {
const actual = datetime.toIMF(new Date(0));
const expected = "Thu, 01 Jan 1970 00:00:00 GMT";
assertEquals(actual, expected);
},
});
Deno.test({
name: "[std/datetime] isLeap",
fn(): void {
assert(datetime.isLeap(1992));
assert(datetime.isLeap(2000));
assert(!datetime.isLeap(2003));
assert(!datetime.isLeap(2007));
},
});
Deno.test({
name: "[std/datetime] difference",
fn(): void {
const denoInit = new Date("2018/5/14");
const denoReleaseV1 = new Date("2020/5/13");
let difference = datetime.difference(denoReleaseV1, denoInit, {
units: ["days", "months", "years"],
});
assertEquals(difference.days, 730);
assertEquals(difference.months, 23);
assertEquals(difference.years, 1);
const birth = new Date("1998/2/23 10:10:10");
const old = new Date("1998/2/23 11:11:11");
difference = datetime.difference(birth, old, {
units: ["milliseconds", "minutes", "seconds", "hours"],
});
assertEquals(difference.milliseconds, 3661000);
assertEquals(difference.seconds, 3661);
assertEquals(difference.minutes, 61);
assertEquals(difference.hours, 1);
},
});
Deno.test({
name: "[std/datetime] constants",
fn(): void {
assertEquals(datetime.SECOND, 1e3);
assertEquals(datetime.MINUTE, datetime.SECOND * 60);
assertEquals(datetime.HOUR, datetime.MINUTE * 60);
assertEquals(datetime.DAY, datetime.HOUR * 24);
assertEquals(datetime.WEEK, datetime.DAY * 7);
},
});

View File

@ -1,76 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export type Token = {
type: string;
value: string | number;
index: number;
[key: string]: unknown;
};
export interface ReceiverResult {
[name: string]: string | number | unknown;
}
export type CallbackResult = {
type: string;
value: string | number;
[key: string]: unknown;
};
type CallbackFunction = (value: unknown) => CallbackResult;
export type TestResult = { value: unknown; length: number } | undefined;
export type TestFunction = (
string: string,
) => TestResult | undefined;
export interface Rule {
test: TestFunction;
fn: CallbackFunction;
}
export class Tokenizer {
rules: Rule[];
constructor(rules: Rule[] = []) {
this.rules = rules;
}
addRule(test: TestFunction, fn: CallbackFunction): Tokenizer {
this.rules.push({ test, fn });
return this;
}
tokenize(
string: string,
receiver = (token: Token): ReceiverResult => token,
): ReceiverResult[] {
function* generator(rules: Rule[]): IterableIterator<ReceiverResult> {
let index = 0;
for (const rule of rules) {
const result = rule.test(string);
if (result) {
const { value, length } = result;
index += length;
string = string.slice(length);
const token = { ...rule.fn(value), index };
yield receiver(token);
yield* generator(rules);
}
}
}
const tokenGenerator = generator(this.rules);
const tokens: ReceiverResult[] = [];
for (const token of tokenGenerator) {
tokens.push(token);
}
if (string.length) {
throw new Error(
`parser error: string not fully parsed! ${string.slice(0, 25)}`,
);
}
return tokens;
}
}

View File

@ -1,575 +0,0 @@
# encoding
Helper module for dealing with external data structures.
- [`ascii85`](#ascii85)
- [`base32`](#base32)
- [`binary`](#binary)
- [`csv`](#csv)
- [`toml`](#toml)
- [`yaml`](#yaml)
## Binary
Implements equivalent methods to Go's `encoding/binary` package.
Available Functions:
```typescript
sizeof(dataType: RawTypes): number
getNBytes(r: Deno.Reader, n: number): Promise<Uint8Array>
varnum(b: Uint8Array, o: VarnumOptions = {}): number | null
varbig(b: Uint8Array, o: VarbigOptions = {}): bigint | null
putVarnum(b: Uint8Array, x: number, o: VarnumOptions = {}): number
putVarbig(b: Uint8Array, x: bigint, o: VarbigOptions = {}): number
readVarnum(r: Deno.Reader, o: VarnumOptions = {}): Promise<number>
readVarbig(r: Deno.Reader, o: VarbigOptions = {}): Promise<bigint>
writeVarnum(w: Deno.Writer, x: number, o: VarnumOptions = {}): Promise<number>
writeVarbig(w: Deno.Writer, x: bigint, o: VarbigOptions = {}): Promise<number>
```
## CSV
### API
#### `readMatrix`
```ts
(reader: BufReader, opt: ReadOptions = {
comma: ",",
trimLeadingSpace: false,
lazyQuotes: false,
}): Promise<string[][]>
```
Parse the CSV from the `reader` with the options provided and return
`string[][]`.
#### `parse`
```ts
(input: string | BufReader, opt: ParseOptions = { skipFirstRow: false }): Promise<unknown[]>
```
Parse the CSV string/buffer with the options provided. The result of this
function is as follows:
- If you don't provide `opt.skipFirstRow`, `opt.parse`, and `opt.columns`, it
returns `string[][]`.
- If you provide `opt.skipFirstRow` or `opt.columns` but not `opt.parse`, it
returns `object[]`.
- If you provide `opt.parse`, it returns an array where each element is the
value returned from `opt.parse`.
##### `ParseOptions`
- **`skipFirstRow: boolean;`**: If you provide `skipFirstRow: true` and
`columns`, the first line will be skipped. If you provide `skipFirstRow: true`
but not `columns`, the first line will be skipped and used as header
definitions.
- **`columns: string[] | HeaderOptions[];`**: If you provide `string[]` or
`ColumnOptions[]`, 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
`skipFirstRow`, `columns`, and `parse` function, input will be `string[]`.
##### `HeaderOptions`
- **`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.
##### `ReadOptions`
- **`comma?: string;`**: Character which separates values. Default: `","`.
- **`comment?: string;`**: Character to start a comment. Default: `"#"`.
- **`trimLeadingSpace?: boolean;`**: Flag to trim the leading space of the
value. Default: `false`.
- **`lazyQuotes?: boolean;`**: Allow unquoted quote in a quoted field or non
double quoted quotes in quoted field. Default: `false`.
- **`fieldsPerRecord?`**: Enabling the check of fields for each row. If == 0,
first row is used as referral for the number of fields.
#### `stringify`
```ts
(data: DataItem[], columns: Column[], options?: StringifyOptions): Promise<string>
```
- **`data`** is the source data to stringify. It's an array of items which are
plain objects or arrays.
`DataItem: Record<string, unknown> | unknown[]`
```ts
const data = [
{
name: "Deno",
repo: { org: "denoland", name: "deno" },
runsOn: ["Rust", "TypeScript"],
},
];
```
- **`columns`** is a list of instructions for how to target and transform the
data for each column of output. This is also where you can provide an explicit
header name for the column.
`Column`:
- The most essential aspect of a column is accessing the property holding the
data for that column on each object in the data array. If that member is at
the top level, `Column` can simply be a property accessor, which is either a
`string` (if it's a plain object) or a `number` (if it's an array).
```ts
const columns = [
"name",
];
```
Each property accessor will be used as the header for the column:
| name |
| :--: |
| Deno |
- If the required data is not at the top level (it's nested in other
objects/arrays), then a simple property accessor won't work, so an array of
them will be required.
```ts
const columns = [
["repo", "name"],
["repo", "org"],
];
```
When using arrays of property accessors, the header names inherit the value
of the last accessor in each array:
| name | org |
| :--: | :------: |
| deno | denoland |
- If the data is not already in the required output format, or a different
column header is desired, then a `ColumnDetails` object type can be used for
each column:
- **`fn?: (value: any) => string | Promise<string>`** is an optional
function to transform the targeted data into the desired format
- **`header?: string`** is the optional value to use for the column header
name
- **`prop: PropertyAccessor | PropertyAccessor[]`** is the property accessor
(`string` or `number`) or array of property accessors used to access the
data on each object
```ts
const columns = [
"name",
{
prop: ["runsOn", 0],
header: "language 1",
fn: (str: string) => str.toLowerCase(),
},
{
prop: ["runsOn", 1],
header: "language 2",
fn: (str: string) => str.toLowerCase(),
},
];
```
| name | language 1 | language 2 |
| :--: | :--------: | :--------: |
| Deno | rust | typescript |
- **`options`** are options for the delimiter-separated output.
- **`headers?: boolean`**: Whether or not to include the row of headers.
Default: `true`
- **`separator?: string`**: Delimiter used to separate values. Examples:
- `","` _comma_ (Default)
- `"\t"` _tab_
- `"|"` _pipe_
- etc.
### Basic Usage
```ts
import { parse } from "https://deno.land/std@$STD_VERSION/encoding/csv.ts";
const string = "a,b,c\nd,e,f";
console.log(
await parse(string, {
skipFirstRow: false,
}),
);
// output:
// [["a", "b", "c"], ["d", "e", "f"]]
```
```ts
import {
Column,
stringify,
} from "https://deno.land/std@$STD_VERSION/encoding/csv.ts";
type Character = {
age: number;
name: {
first: string;
last: string;
};
};
const data: Character[] = [
{
age: 70,
name: {
first: "Rick",
last: "Sanchez",
},
},
{
age: 14,
name: {
first: "Morty",
last: "Smith",
},
},
];
let columns: Column[] = [
["name", "first"],
"age",
];
console.log(await stringify(data, columns));
// first,age
// Rick,70
// Morty,14
//
columns = [
{
prop: "name",
fn: (name: Character["name"]) => `${name.first} ${name.last}`,
},
{
prop: "age",
header: "is_adult",
fn: (age: Character["age"]) => String(age >= 18),
},
];
console.log(await stringify(data, columns, { separator: "\t" }));
// name is_adult
// Rick Sanchez true
// Morty Smith false
//
```
## TOML
This module parse TOML files. It follows as much as possible the
[TOML specs](https://toml.io/en/latest). 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
- :heavy_check_mark: [Keys](https://toml.io/en/latest#keys)
- :exclamation: [String](https://toml.io/en/latest#string)
- :heavy_check_mark: [Multiline String](https://toml.io/en/latest#string)
- :heavy_check_mark: [Literal String](https://toml.io/en/latest#string)
- :exclamation: [Integer](https://toml.io/en/latest#integer)
- :heavy_check_mark: [Float](https://toml.io/en/latest#float)
- :heavy_check_mark: [Boolean](https://toml.io/en/latest#boolean)
- :heavy_check_mark:
[Offset Date-time](https://toml.io/en/latest#offset-date-time)
- :heavy_check_mark:
[Local Date-time](https://toml.io/en/latest#local-date-time)
- :heavy_check_mark: [Local Date](https://toml.io/en/latest#local-date)
- :exclamation: [Local Time](https://toml.io/en/latest#local-time)
- :heavy_check_mark: [Table](https://toml.io/en/latest#table)
- :heavy_check_mark: [Inline Table](https://toml.io/en/latest#inline-table)
- :exclamation: [Array of Tables](https://toml.io/en/latest#array-of-tables)
:exclamation: _Supported with warnings see [Warning](#Warning)._
#### :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:
```toml
animal = { type = { name = "pug" } }
## Output { animal: { type: { name: "pug" } } }
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:
```toml
[[bin]]
name = "deno"
path = "cli/main.rs"
[[bin]]
name = "deno_core"
path = "src/foo.rs"
[[nib]]
name = "node"
path = "not_found"
```
will output:
```json
{
"bin": [
{ "name": "deno", "path": "cli/main.rs" },
{ "name": "deno_core", "path": "src/foo.rs" }
],
"nib": [{ "name": "node", "path": "not_found" }]
}
```
### Basic usage
```ts
import {
parse,
stringify,
} from "https://deno.land/std@$STD_VERSION/encoding/toml.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);
console.log(tomlString);
// =>
// [[bin]]
// name = "deno"
// path = "cli/main.rs"
// [[bin]]
// name = "deno_core"
// path = "src/foo.rs"
// [[nib]]
// name = "node"
// path = "not_found"
const tomlObject = parse(tomlString);
console.log(tomlObject);
// =>
// {
// bin: [
// { name: "deno", path: "cli/main.rs" },
// { name: "deno_core", path: "src/foo.rs" }
// ],
// nib: [ { name: "node", path: "not_found" } ]
// }
```
## YAML
YAML parser / dumper for Deno.
Heavily inspired from [`js-yaml`](https://github.com/nodeca/js-yaml).
### Basic usage
`parse` parses the yaml string, and `stringify` dumps the given object to YAML
string.
```ts
import {
parse,
stringify,
} from "https://deno.land/std@$STD_VERSION/encoding/yaml.ts";
const data = parse(`
foo: bar
baz:
- qux
- quux
`);
console.log(data);
// => { foo: "bar", baz: [ "qux", "quux" ] }
const yaml = stringify({ foo: "bar", baz: ["qux", "quux"] });
console.log(yaml);
// =>
// foo: bar
// baz:
// - qux
// - quux
```
If your YAML contains multiple documents in it, you can use `parseAll` for
handling it.
```ts
import { parseAll } from "https://deno.land/std@$STD_VERSION/encoding/yaml.ts";
const data = parseAll(`
---
id: 1
name: Alice
---
id: 2
name: Bob
---
id: 3
name: Eve
`);
console.log(data);
// => [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Eve" } ]
```
### API
#### `parse(str: string, opts?: ParserOption): unknown`
Parses the YAML string with a single document.
#### `parseAll(str: string, iterator?: Function, opts?: ParserOption): unknown`
Parses the YAML string with multiple documents. If the iterator is given, it's
applied to every document instead of returning the array of parsed objects.
#### `stringify(obj: object, opts?: DumpOption): string`
Serializes `object` as a YAML document.
### :warning: Limitations
- `binary` type is currently not stable.
- `function`, `regexp`, and `undefined` type are currently not supported.
### More example
See: https://github.com/nodeca/js-yaml
## base32
[RFC4648 base32](https://tools.ietf.org/html/rfc4648#section-6) encoder/decoder
for Deno.
### Basic usage
`encode` encodes a `Uint8Array` to RFC4648 base32 representation, and `decode`
decodes the given RFC4648 base32 representation to a `Uint8Array`.
```ts
import {
decode,
encode,
} from "https://deno.land/std@$STD_VERSION/encoding/base32.ts";
const b32Repr = "RC2E6GA=";
const binaryData = decode(b32Repr);
console.log(binaryData);
// => Uint8Array [ 136, 180, 79, 24 ]
console.log(encode(binaryData));
// => RC2E6GA=
```
## ascii85
Ascii85/base85 encoder and decoder with support for multiple standards.
### Basic usage
`encode` encodes a `Uint8Array` to a ascii85 representation, and `decode`
decodes the given ascii85 representation to a `Uint8Array`.
```ts
import {
decode,
encode,
} from "https://deno.land/std@$STD_VERSION/encoding/ascii85.ts";
const a85Repr = "LpTqp";
const binaryData = decode(a85Repr);
console.log(binaryData);
// => Uint8Array [ 136, 180, 79, 24 ]
console.log(encode(binaryData));
// => LpTqp
```
### Specifying a standard and delimiter
By default all functions are using the most popular Adobe version of ascii85 and
not adding any delimiter. However, there are three more standards supported -
btoa (different delimiter and additional compression of 4 bytes equal to 32),
[Z85](https://rfc.zeromq.org/spec/32/) and
[RFC 1924](https://tools.ietf.org/html/rfc1924). It's possible to use a
different encoding by specifying it in `options` object as a second parameter.
Similarly, it's possible to make `encode` add a delimiter (`<~` and `~>` for
Adobe, `xbtoa Begin` and `xbtoa End` with newlines between the delimiters and
encoded data for btoa. Checksums for btoa are not supported. Delimiters are not
supported by other encodings.)
encoding examples:
```ts
import {
decode,
encode,
} from "https://deno.land/std@$STD_VERSION/encoding/ascii85.ts";
const binaryData = new Uint8Array([136, 180, 79, 24]);
console.log(encode(binaryData));
// => LpTqp
console.log(encode(binaryData, { standard: "Adobe", delimiter: true }));
// => <~LpTqp~>
console.log(encode(binaryData, { standard: "btoa", delimiter: true }));
/* => xbtoa Begin
LpTqp
xbtoa End */
console.log(encode(binaryData, { standard: "RFC 1924" }));
// => h_p`_
console.log(encode(binaryData, { standard: "Z85" }));
// => H{P}{
```

View File

@ -1,896 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { YAMLError } from "../error.ts";
import type { RepresentFn, StyleVariant, Type } from "../type.ts";
import * as common from "../utils.ts";
import { DumperState, DumperStateOptions } from "./dumper_state.ts";
type Any = common.Any;
type ArrayObject<T = Any> = common.ArrayObject<T>;
const _toString = Object.prototype.toString;
const _hasOwnProperty = Object.prototype.hasOwnProperty;
const CHAR_TAB = 0x09; /* Tab */
const CHAR_LINE_FEED = 0x0a; /* LF */
const CHAR_SPACE = 0x20; /* Space */
const CHAR_EXCLAMATION = 0x21; /* ! */
const CHAR_DOUBLE_QUOTE = 0x22; /* " */
const CHAR_SHARP = 0x23; /* # */
const CHAR_PERCENT = 0x25; /* % */
const CHAR_AMPERSAND = 0x26; /* & */
const CHAR_SINGLE_QUOTE = 0x27; /* ' */
const CHAR_ASTERISK = 0x2a; /* * */
const CHAR_COMMA = 0x2c; /* , */
const CHAR_MINUS = 0x2d; /* - */
const CHAR_COLON = 0x3a; /* : */
const CHAR_GREATER_THAN = 0x3e; /* > */
const CHAR_QUESTION = 0x3f; /* ? */
const CHAR_COMMERCIAL_AT = 0x40; /* @ */
const CHAR_LEFT_SQUARE_BRACKET = 0x5b; /* [ */
const CHAR_RIGHT_SQUARE_BRACKET = 0x5d; /* ] */
const CHAR_GRAVE_ACCENT = 0x60; /* ` */
const CHAR_LEFT_CURLY_BRACKET = 0x7b; /* { */
const CHAR_VERTICAL_LINE = 0x7c; /* | */
const CHAR_RIGHT_CURLY_BRACKET = 0x7d; /* } */
const ESCAPE_SEQUENCES: { [char: number]: string } = {};
ESCAPE_SEQUENCES[0x00] = "\\0";
ESCAPE_SEQUENCES[0x07] = "\\a";
ESCAPE_SEQUENCES[0x08] = "\\b";
ESCAPE_SEQUENCES[0x09] = "\\t";
ESCAPE_SEQUENCES[0x0a] = "\\n";
ESCAPE_SEQUENCES[0x0b] = "\\v";
ESCAPE_SEQUENCES[0x0c] = "\\f";
ESCAPE_SEQUENCES[0x0d] = "\\r";
ESCAPE_SEQUENCES[0x1b] = "\\e";
ESCAPE_SEQUENCES[0x22] = '\\"';
ESCAPE_SEQUENCES[0x5c] = "\\\\";
ESCAPE_SEQUENCES[0x85] = "\\N";
ESCAPE_SEQUENCES[0xa0] = "\\_";
ESCAPE_SEQUENCES[0x2028] = "\\L";
ESCAPE_SEQUENCES[0x2029] = "\\P";
const DEPRECATED_BOOLEANS_SYNTAX = [
"y",
"Y",
"yes",
"Yes",
"YES",
"on",
"On",
"ON",
"n",
"N",
"no",
"No",
"NO",
"off",
"Off",
"OFF",
];
function encodeHex(character: number): string {
const string = character.toString(16).toUpperCase();
let handle: string;
let length: number;
if (character <= 0xff) {
handle = "x";
length = 2;
} else if (character <= 0xffff) {
handle = "u";
length = 4;
} else if (character <= 0xffffffff) {
handle = "U";
length = 8;
} else {
throw new YAMLError(
"code point within a string may not be greater than 0xFFFFFFFF",
);
}
return `\\${handle}${common.repeat("0", length - string.length)}${string}`;
}
// Indents every line in a string. Empty lines (\n only) are not indented.
function indentString(string: string, spaces: number): string {
const ind = common.repeat(" ", spaces),
length = string.length;
let position = 0,
next = -1,
result = "",
line: string;
while (position < length) {
next = string.indexOf("\n", position);
if (next === -1) {
line = string.slice(position);
position = length;
} else {
line = string.slice(position, next + 1);
position = next + 1;
}
if (line.length && line !== "\n") result += ind;
result += line;
}
return result;
}
function generateNextLine(state: DumperState, level: number): string {
return `\n${common.repeat(" ", state.indent * level)}`;
}
function testImplicitResolving(state: DumperState, str: string): boolean {
let type: Type;
for (
let index = 0, length = state.implicitTypes.length;
index < length;
index += 1
) {
type = state.implicitTypes[index];
if (type.resolve(str)) {
return true;
}
}
return false;
}
// [33] s-white ::= s-space | s-tab
function isWhitespace(c: number): boolean {
return c === CHAR_SPACE || c === CHAR_TAB;
}
// Returns true if the character can be printed without escaping.
// From YAML 1.2: "any allowed characters known to be non-printable
// should also be escaped. [However,] This isnt mandatory"
// Derived from nb-char - \t - #x85 - #xA0 - #x2028 - #x2029.
function isPrintable(c: number): boolean {
return (
(0x00020 <= c && c <= 0x00007e) ||
(0x000a1 <= c && c <= 0x00d7ff && c !== 0x2028 && c !== 0x2029) ||
(0x0e000 <= c && c <= 0x00fffd && c !== 0xfeff) /* BOM */ ||
(0x10000 <= c && c <= 0x10ffff)
);
}
// Simplified test for values allowed after the first character in plain style.
function isPlainSafe(c: number): boolean {
// Uses a subset of nb-char - c-flow-indicator - ":" - "#"
// where nb-char ::= c-printable - b-char - c-byte-order-mark.
return (
isPrintable(c) &&
c !== 0xfeff &&
// - c-flow-indicator
c !== CHAR_COMMA &&
c !== CHAR_LEFT_SQUARE_BRACKET &&
c !== CHAR_RIGHT_SQUARE_BRACKET &&
c !== CHAR_LEFT_CURLY_BRACKET &&
c !== CHAR_RIGHT_CURLY_BRACKET &&
// - ":" - "#"
c !== CHAR_COLON &&
c !== CHAR_SHARP
);
}
// Simplified test for values allowed as the first character in plain style.
function isPlainSafeFirst(c: number): boolean {
// Uses a subset of ns-char - c-indicator
// where ns-char = nb-char - s-white.
return (
isPrintable(c) &&
c !== 0xfeff &&
!isWhitespace(c) && // - s-white
// - (c-indicator ::=
// “-” | “?” | “:” | “,” | “[” | “]” | “{” | “}”
c !== CHAR_MINUS &&
c !== CHAR_QUESTION &&
c !== CHAR_COLON &&
c !== CHAR_COMMA &&
c !== CHAR_LEFT_SQUARE_BRACKET &&
c !== CHAR_RIGHT_SQUARE_BRACKET &&
c !== CHAR_LEFT_CURLY_BRACKET &&
c !== CHAR_RIGHT_CURLY_BRACKET &&
// | “#” | “&” | “*” | “!” | “|” | “>” | “'” | “"”
c !== CHAR_SHARP &&
c !== CHAR_AMPERSAND &&
c !== CHAR_ASTERISK &&
c !== CHAR_EXCLAMATION &&
c !== CHAR_VERTICAL_LINE &&
c !== CHAR_GREATER_THAN &&
c !== CHAR_SINGLE_QUOTE &&
c !== CHAR_DOUBLE_QUOTE &&
// | “%” | “@” | “`”)
c !== CHAR_PERCENT &&
c !== CHAR_COMMERCIAL_AT &&
c !== CHAR_GRAVE_ACCENT
);
}
// Determines whether block indentation indicator is required.
function needIndentIndicator(string: string): boolean {
const leadingSpaceRe = /^\n* /;
return leadingSpaceRe.test(string);
}
const STYLE_PLAIN = 1,
STYLE_SINGLE = 2,
STYLE_LITERAL = 3,
STYLE_FOLDED = 4,
STYLE_DOUBLE = 5;
// Determines which scalar styles are possible and returns the preferred style.
// lineWidth = -1 => no limit.
// Pre-conditions: str.length > 0.
// Post-conditions:
// STYLE_PLAIN or STYLE_SINGLE => no \n are in the string.
// STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1).
// STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1).
function chooseScalarStyle(
string: string,
singleLineOnly: boolean,
indentPerLevel: number,
lineWidth: number,
testAmbiguousType: (...args: Any[]) => Any,
): number {
const shouldTrackWidth = lineWidth !== -1;
let hasLineBreak = false,
hasFoldableLine = false, // only checked if shouldTrackWidth
previousLineBreak = -1, // count the first line correctly
plain = isPlainSafeFirst(string.charCodeAt(0)) &&
!isWhitespace(string.charCodeAt(string.length - 1));
let char: number, i: number;
if (singleLineOnly) {
// Case: no block styles.
// Check for disallowed characters to rule out plain and single.
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char);
}
} else {
// Case: block styles permitted.
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (char === CHAR_LINE_FEED) {
hasLineBreak = true;
// Check if any line can be folded.
if (shouldTrackWidth) {
hasFoldableLine = hasFoldableLine ||
// Foldable line = too long, and not more-indented.
(i - previousLineBreak - 1 > lineWidth &&
string[previousLineBreak + 1] !== " ");
previousLineBreak = i;
}
} else if (!isPrintable(char)) {
return STYLE_DOUBLE;
}
plain = plain && isPlainSafe(char);
}
// in case the end is missing a \n
hasFoldableLine = hasFoldableLine ||
(shouldTrackWidth &&
i - previousLineBreak - 1 > lineWidth &&
string[previousLineBreak + 1] !== " ");
}
// Although every style can represent \n without escaping, prefer block styles
// for multiline, since they're more readable and they don't add empty lines.
// Also prefer folding a super-long line.
if (!hasLineBreak && !hasFoldableLine) {
// Strings interpretable as another type have to be quoted;
// e.g. the string 'true' vs. the boolean true.
return plain && !testAmbiguousType(string) ? STYLE_PLAIN : STYLE_SINGLE;
}
// Edge case: block indentation indicator can only have one digit.
if (indentPerLevel > 9 && needIndentIndicator(string)) {
return STYLE_DOUBLE;
}
// At this point we know block styles are valid.
// Prefer literal style unless we want to fold.
return hasFoldableLine ? STYLE_FOLDED : STYLE_LITERAL;
}
// Greedy line breaking.
// Picks the longest line under the limit each time,
// otherwise settles for the shortest line over the limit.
// NB. More-indented lines *cannot* be folded, as that would add an extra \n.
function foldLine(line: string, width: number): string {
if (line === "" || line[0] === " ") return line;
// Since a more-indented line adds a \n, breaks can't be followed by a space.
const breakRe = / [^ ]/g; // note: the match index will always be <= length-2.
let match;
// start is an inclusive index. end, curr, and next are exclusive.
let start = 0,
end,
curr = 0,
next = 0;
let result = "";
// Invariants: 0 <= start <= length-1.
// 0 <= curr <= next <= max(0, length-2). curr - start <= width.
// Inside the loop:
// A match implies length >= 2, so curr and next are <= length-2.
// tslint:disable-next-line:no-conditional-assignment
while ((match = breakRe.exec(line))) {
next = match.index;
// maintain invariant: curr - start <= width
if (next - start > width) {
end = curr > start ? curr : next; // derive end <= length-2
result += `\n${line.slice(start, end)}`;
// skip the space that was output as \n
start = end + 1; // derive start <= length-1
}
curr = next;
}
// By the invariants, start <= length-1, so there is something left over.
// It is either the whole string or a part starting from non-whitespace.
result += "\n";
// Insert a break if the remainder is too long and there is a break available.
if (line.length - start > width && curr > start) {
result += `${line.slice(start, curr)}\n${line.slice(curr + 1)}`;
} else {
result += line.slice(start);
}
return result.slice(1); // drop extra \n joiner
}
// (See the note for writeScalar.)
function dropEndingNewline(string: string): string {
return string[string.length - 1] === "\n" ? string.slice(0, -1) : string;
}
// Note: a long line without a suitable break point will exceed the width limit.
// Pre-conditions: every char in str isPrintable, str.length > 0, width > 0.
function foldString(string: string, width: number): string {
// In folded style, $k$ consecutive newlines output as $k+1$ newlines—
// unless they're before or after a more-indented line, or at the very
// beginning or end, in which case $k$ maps to $k$.
// Therefore, parse each chunk as newline(s) followed by a content line.
const lineRe = /(\n+)([^\n]*)/g;
// first line (possibly an empty line)
let result = ((): string => {
let nextLF = string.indexOf("\n");
nextLF = nextLF !== -1 ? nextLF : string.length;
lineRe.lastIndex = nextLF;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return foldLine(string.slice(0, nextLF), width);
})();
// If we haven't reached the first content line yet, don't add an extra \n.
let prevMoreIndented = string[0] === "\n" || string[0] === " ";
let moreIndented;
// rest of the lines
let match;
// tslint:disable-next-line:no-conditional-assignment
while ((match = lineRe.exec(string))) {
const prefix = match[1],
line = match[2];
moreIndented = line[0] === " ";
result += prefix +
(!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") +
// eslint-disable-next-line @typescript-eslint/no-use-before-define
foldLine(line, width);
prevMoreIndented = moreIndented;
}
return result;
}
// Escapes a double-quoted string.
function escapeString(string: string): string {
let result = "";
let char, nextChar;
let escapeSeq;
for (let i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
// Check for surrogate pairs (reference Unicode 3.0 section "3.7 Surrogates").
if (char >= 0xd800 && char <= 0xdbff /* high surrogate */) {
nextChar = string.charCodeAt(i + 1);
if (nextChar >= 0xdc00 && nextChar <= 0xdfff /* low surrogate */) {
// Combine the surrogate pair and store it escaped.
result += encodeHex(
(char - 0xd800) * 0x400 + nextChar - 0xdc00 + 0x10000,
);
// Advance index one extra since we already used that char here.
i++;
continue;
}
}
escapeSeq = ESCAPE_SEQUENCES[char];
result += !escapeSeq && isPrintable(char)
? string[i]
: escapeSeq || encodeHex(char);
}
return result;
}
// Pre-conditions: string is valid for a block scalar, 1 <= indentPerLevel <= 9.
function blockHeader(string: string, indentPerLevel: number): string {
const indentIndicator = needIndentIndicator(string)
? String(indentPerLevel)
: "";
// note the special case: the string '\n' counts as a "trailing" empty line.
const clip = string[string.length - 1] === "\n";
const keep = clip && (string[string.length - 2] === "\n" || string === "\n");
const chomp = keep ? "+" : clip ? "" : "-";
return `${indentIndicator}${chomp}\n`;
}
// Note: line breaking/folding is implemented for only the folded style.
// NB. We drop the last trailing newline (if any) of a returned block scalar
// since the dumper adds its own newline. This always works:
// • No ending newline => unaffected; already using strip "-" chomping.
// • Ending newline => removed then restored.
// Importantly, this keeps the "+" chomp indicator from gaining an extra line.
function writeScalar(
state: DumperState,
string: string,
level: number,
iskey: boolean,
): void {
state.dump = ((): string => {
if (string.length === 0) {
return "''";
}
if (
!state.noCompatMode &&
DEPRECATED_BOOLEANS_SYNTAX.indexOf(string) !== -1
) {
return `'${string}'`;
}
const indent = state.indent * Math.max(1, level); // no 0-indent scalars
// As indentation gets deeper, let the width decrease monotonically
// to the lower bound min(state.lineWidth, 40).
// Note that this implies
// state.lineWidth ≤ 40 + state.indent: width is fixed at the lower bound.
// state.lineWidth > 40 + state.indent: width decreases until the lower
// bound.
// This behaves better than a constant minimum width which disallows
// narrower options, or an indent threshold which causes the width
// to suddenly increase.
const lineWidth = state.lineWidth === -1
? -1
: Math.max(Math.min(state.lineWidth, 40), state.lineWidth - indent);
// Without knowing if keys are implicit/explicit,
// assume implicit for safety.
const singleLineOnly = iskey ||
// No block styles in flow mode.
(state.flowLevel > -1 && level >= state.flowLevel);
function testAmbiguity(str: string): boolean {
return testImplicitResolving(state, str);
}
switch (
chooseScalarStyle(
string,
singleLineOnly,
state.indent,
lineWidth,
testAmbiguity,
)
) {
case STYLE_PLAIN:
return string;
case STYLE_SINGLE:
return `'${string.replace(/'/g, "''")}'`;
case STYLE_LITERAL:
return `|${blockHeader(string, state.indent)}${
dropEndingNewline(
indentString(string, indent),
)
}`;
case STYLE_FOLDED:
return `>${blockHeader(string, state.indent)}${
dropEndingNewline(
indentString(foldString(string, lineWidth), indent),
)
}`;
case STYLE_DOUBLE:
return `"${escapeString(string)}"`;
default:
throw new YAMLError("impossible error: invalid scalar style");
}
})();
}
function writeFlowSequence(
state: DumperState,
level: number,
object: Any,
): void {
let _result = "";
const _tag = state.tag;
for (let index = 0, length = object.length; index < length; index += 1) {
// Write only valid elements.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (writeNode(state, level, object[index], false, false)) {
if (index !== 0) _result += `,${!state.condenseFlow ? " " : ""}`;
_result += state.dump;
}
}
state.tag = _tag;
state.dump = `[${_result}]`;
}
function writeBlockSequence(
state: DumperState,
level: number,
object: Any,
compact = false,
): void {
let _result = "";
const _tag = state.tag;
for (let index = 0, length = object.length; index < length; index += 1) {
// Write only valid elements.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (writeNode(state, level + 1, object[index], true, true)) {
if (!compact || index !== 0) {
_result += generateNextLine(state, level);
}
if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
_result += "-";
} else {
_result += "- ";
}
_result += state.dump;
}
}
state.tag = _tag;
state.dump = _result || "[]"; // Empty sequence if no valid values.
}
function writeFlowMapping(
state: DumperState,
level: number,
object: Any,
): void {
let _result = "";
const _tag = state.tag,
objectKeyList = Object.keys(object);
let pairBuffer: string, objectKey: string, objectValue: Any;
for (
let index = 0, length = objectKeyList.length;
index < length;
index += 1
) {
pairBuffer = state.condenseFlow ? '"' : "";
if (index !== 0) pairBuffer += ", ";
objectKey = objectKeyList[index];
objectValue = object[objectKey];
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (!writeNode(state, level, objectKey, false, false)) {
continue; // Skip this pair because of invalid key;
}
if (state.dump.length > 1024) pairBuffer += "? ";
pairBuffer += `${state.dump}${state.condenseFlow ? '"' : ""}:${
state.condenseFlow ? "" : " "
}`;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (!writeNode(state, level, objectValue, false, false)) {
continue; // Skip this pair because of invalid value.
}
pairBuffer += state.dump;
// Both key and value are valid.
_result += pairBuffer;
}
state.tag = _tag;
state.dump = `{${_result}}`;
}
function writeBlockMapping(
state: DumperState,
level: number,
object: Any,
compact = false,
): void {
const _tag = state.tag,
objectKeyList = Object.keys(object);
let _result = "";
// Allow sorting keys so that the output file is deterministic
if (state.sortKeys === true) {
// Default sorting
objectKeyList.sort();
} else if (typeof state.sortKeys === "function") {
// Custom sort function
objectKeyList.sort(state.sortKeys);
} else if (state.sortKeys) {
// Something is wrong
throw new YAMLError("sortKeys must be a boolean or a function");
}
let pairBuffer = "",
objectKey: string,
objectValue: Any,
explicitPair: boolean;
for (
let index = 0, length = objectKeyList.length;
index < length;
index += 1
) {
pairBuffer = "";
if (!compact || index !== 0) {
pairBuffer += generateNextLine(state, level);
}
objectKey = objectKeyList[index];
objectValue = object[objectKey];
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (!writeNode(state, level + 1, objectKey, true, true, true)) {
continue; // Skip this pair because of invalid key.
}
explicitPair = (state.tag !== null && state.tag !== "?") ||
(state.dump && state.dump.length > 1024);
if (explicitPair) {
if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
pairBuffer += "?";
} else {
pairBuffer += "? ";
}
}
pairBuffer += state.dump;
if (explicitPair) {
pairBuffer += generateNextLine(state, level);
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (!writeNode(state, level + 1, objectValue, true, explicitPair)) {
continue; // Skip this pair because of invalid value.
}
if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
pairBuffer += ":";
} else {
pairBuffer += ": ";
}
pairBuffer += state.dump;
// Both key and value are valid.
_result += pairBuffer;
}
state.tag = _tag;
state.dump = _result || "{}"; // Empty mapping if no valid pairs.
}
function detectType(
state: DumperState,
object: Any,
explicit = false,
): boolean {
const typeList = explicit ? state.explicitTypes : state.implicitTypes;
let type: Type;
let style: StyleVariant;
let _result: string;
for (let index = 0, length = typeList.length; index < length; index += 1) {
type = typeList[index];
if (
(type.instanceOf || type.predicate) &&
(!type.instanceOf ||
(typeof object === "object" && object instanceof type.instanceOf)) &&
(!type.predicate || type.predicate(object))
) {
state.tag = explicit ? type.tag : "?";
if (type.represent) {
style = state.styleMap[type.tag] || type.defaultStyle;
if (_toString.call(type.represent) === "[object Function]") {
_result = (type.represent as RepresentFn)(object, style);
} else if (_hasOwnProperty.call(type.represent, style)) {
_result = (type.represent as ArrayObject<RepresentFn>)[style](
object,
style,
);
} else {
throw new YAMLError(
`!<${type.tag}> tag resolver accepts not "${style}" style`,
);
}
state.dump = _result;
}
return true;
}
}
return false;
}
// Serializes `object` and writes it to global `result`.
// Returns true on success, or false on invalid object.
//
function writeNode(
state: DumperState,
level: number,
object: Any,
block: boolean,
compact: boolean,
iskey = false,
): boolean {
state.tag = null;
state.dump = object;
if (!detectType(state, object, false)) {
detectType(state, object, true);
}
const type = _toString.call(state.dump);
if (block) {
block = state.flowLevel < 0 || state.flowLevel > level;
}
const objectOrArray = type === "[object Object]" || type === "[object Array]";
let duplicateIndex = -1;
let duplicate = false;
if (objectOrArray) {
duplicateIndex = state.duplicates.indexOf(object);
duplicate = duplicateIndex !== -1;
}
if (
(state.tag !== null && state.tag !== "?") ||
duplicate ||
(state.indent !== 2 && level > 0)
) {
compact = false;
}
if (duplicate && state.usedDuplicates[duplicateIndex]) {
state.dump = `*ref_${duplicateIndex}`;
} else {
if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) {
state.usedDuplicates[duplicateIndex] = true;
}
if (type === "[object Object]") {
if (block && Object.keys(state.dump).length !== 0) {
writeBlockMapping(state, level, state.dump, compact);
if (duplicate) {
state.dump = `&ref_${duplicateIndex}${state.dump}`;
}
} else {
writeFlowMapping(state, level, state.dump);
if (duplicate) {
state.dump = `&ref_${duplicateIndex} ${state.dump}`;
}
}
} else if (type === "[object Array]") {
const arrayLevel = state.noArrayIndent && level > 0 ? level - 1 : level;
if (block && state.dump.length !== 0) {
writeBlockSequence(state, arrayLevel, state.dump, compact);
if (duplicate) {
state.dump = `&ref_${duplicateIndex}${state.dump}`;
}
} else {
writeFlowSequence(state, arrayLevel, state.dump);
if (duplicate) {
state.dump = `&ref_${duplicateIndex} ${state.dump}`;
}
}
} else if (type === "[object String]") {
if (state.tag !== "?") {
writeScalar(state, state.dump, level, iskey);
}
} else {
if (state.skipInvalid) return false;
throw new YAMLError(`unacceptable kind of an object to dump ${type}`);
}
if (state.tag !== null && state.tag !== "?") {
state.dump = `!<${state.tag}> ${state.dump}`;
}
}
return true;
}
function inspectNode(
object: Any,
objects: Any[],
duplicatesIndexes: number[],
): void {
if (object !== null && typeof object === "object") {
const index = objects.indexOf(object);
if (index !== -1) {
if (duplicatesIndexes.indexOf(index) === -1) {
duplicatesIndexes.push(index);
}
} else {
objects.push(object);
if (Array.isArray(object)) {
for (let idx = 0, length = object.length; idx < length; idx += 1) {
inspectNode(object[idx], objects, duplicatesIndexes);
}
} else {
const objectKeyList = Object.keys(object);
for (
let idx = 0, length = objectKeyList.length;
idx < length;
idx += 1
) {
inspectNode(object[objectKeyList[idx]], objects, duplicatesIndexes);
}
}
}
}
}
function getDuplicateReferences(
object: Record<string, unknown>,
state: DumperState,
): void {
const objects: Any[] = [],
duplicatesIndexes: number[] = [];
inspectNode(object, objects, duplicatesIndexes);
const length = duplicatesIndexes.length;
for (let index = 0; index < length; index += 1) {
state.duplicates.push(objects[duplicatesIndexes[index]]);
}
state.usedDuplicates = new Array(length);
}
export function dump(input: Any, options?: DumperStateOptions): string {
options = options || {};
const state = new DumperState(options);
if (!state.noRefs) getDuplicateReferences(input, state);
if (writeNode(state, 0, input, true, true)) return `${state.dump}\n`;
return "";
}

View File

@ -1,141 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import type { Schema, SchemaDefinition } from "../schema.ts";
import { State } from "../state.ts";
import type { StyleVariant, Type } from "../type.ts";
import type { Any, ArrayObject } from "../utils.ts";
const _hasOwnProperty = Object.prototype.hasOwnProperty;
function compileStyleMap(
schema: Schema,
map?: ArrayObject<StyleVariant> | null,
): ArrayObject<StyleVariant> {
if (typeof map === "undefined" || map === null) return {};
let type: Type;
const result: ArrayObject<StyleVariant> = {};
const keys = Object.keys(map);
let tag: string, style: StyleVariant;
for (let index = 0, length = keys.length; index < length; index += 1) {
tag = keys[index];
style = String(map[tag]) as StyleVariant;
if (tag.slice(0, 2) === "!!") {
tag = `tag:yaml.org,2002:${tag.slice(2)}`;
}
type = schema.compiledTypeMap.fallback[tag];
if (
type &&
typeof type.styleAliases !== "undefined" &&
_hasOwnProperty.call(type.styleAliases, style)
) {
style = type.styleAliases[style];
}
result[tag] = style;
}
return result;
}
export interface DumperStateOptions {
/** indentation width to use (in spaces). */
indent?: number;
/** when true, will not add an indentation level to array elements */
noArrayIndent?: boolean;
/**
* do not throw on invalid types (like function in the safe schema)
* and skip pairs and single values with such types.
*/
skipInvalid?: boolean;
/**
* specifies level of nesting, when to switch from
* block to flow style for collections. -1 means block style everywhere
*/
flowLevel?: number;
/** Each tag may have own set of styles. - "tag" => "style" map. */
styles?: ArrayObject<StyleVariant> | null;
/** specifies a schema to use. */
schema?: SchemaDefinition;
/**
* If true, sort keys when dumping YAML in ascending, ASCII character order.
* If a function, use the function to sort the keys. (default: false)
* If a function is specified, the function must return a negative value
* if first argument is less than second argument, zero if they're equal
* and a positive value otherwise.
*/
sortKeys?: boolean | ((a: string, b: string) => number);
/** set max line width. (default: 80) */
lineWidth?: number;
/**
* if true, don't convert duplicate objects
* into references (default: false)
*/
noRefs?: boolean;
/**
* if true don't try to be compatible with older yaml versions.
* Currently: don't quote "yes", "no" and so on,
* as required for YAML 1.1 (default: false)
*/
noCompatMode?: boolean;
/**
* if true flow sequences will be condensed, omitting the
* space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`.
* Can be useful when using yaml for pretty URL query params
* as spaces are %-encoded. (default: false).
*/
condenseFlow?: boolean;
}
export class DumperState extends State {
public indent: number;
public noArrayIndent: boolean;
public skipInvalid: boolean;
public flowLevel: number;
public sortKeys: boolean | ((a: Any, b: Any) => number);
public lineWidth: number;
public noRefs: boolean;
public noCompatMode: boolean;
public condenseFlow: boolean;
public implicitTypes: Type[];
public explicitTypes: Type[];
public tag: string | null = null;
public result = "";
public duplicates: Any[] = [];
public usedDuplicates: Any[] = []; // changed from null to []
public styleMap: ArrayObject<StyleVariant>;
public dump: Any;
constructor({
schema,
indent = 2,
noArrayIndent = false,
skipInvalid = false,
flowLevel = -1,
styles = null,
sortKeys = false,
lineWidth = 80,
noRefs = false,
noCompatMode = false,
condenseFlow = false,
}: DumperStateOptions) {
super(schema);
this.indent = Math.max(1, indent);
this.noArrayIndent = noArrayIndent;
this.skipInvalid = skipInvalid;
this.flowLevel = flowLevel;
this.styleMap = compileStyleMap(this.schema as Schema, styles);
this.sortKeys = sortKeys;
this.lineWidth = lineWidth;
this.noRefs = noRefs;
this.noCompatMode = noCompatMode;
this.condenseFlow = condenseFlow;
this.implicitTypes = (this.schema as Schema).compiledImplicit;
this.explicitTypes = (this.schema as Schema).compiledExplicit;
}
}

View File

@ -1,20 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import type { Mark } from "./mark.ts";
export class YAMLError extends Error {
constructor(
message = "(unknown reason)",
protected mark: Mark | string = "",
) {
super(`${message} ${mark}`);
this.name = this.constructor.name;
}
public toString(_compact: boolean): string {
return `${this.name}: ${this.message} ${this.mark}`;
}
}

View File

@ -1,22 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { stringify } from "../../yaml.ts";
console.log(
stringify({
foo: {
bar: true,
test: [
"a",
"b",
{
a: false,
},
{
a: false,
},
],
},
test: "foobar",
}),
);

View File

@ -1,27 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { parse, stringify } from "../../yaml.ts";
const test = {
foo: {
bar: true,
test: [
"a",
"b",
{
a: false,
},
{
a: false,
},
],
},
test: "foobar",
};
const string = stringify(test);
if (Deno.inspect(test) === Deno.inspect(parse(string))) {
console.log("In-Out as expected.");
} else {
console.log("Something went wrong.");
}

View File

@ -1,19 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { parse } from "../../yaml.ts";
const result = parse(`
test: toto
foo:
bar: True
baz: 1
qux: ~
`);
console.log(result);
const expected = '{ test: "toto", foo: { bar: true, baz: 1, qux: null } }';
if (Deno.inspect(result) === expected) {
console.log("Output is as expected.");
} else {
console.error("Error during parse. Output is not as expect.", expected);
}

View File

@ -1,21 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { parse } from "../../yaml.ts";
(() => {
const yml = Deno.readFileSync(`${Deno.cwd()}/example/sample_document.yml`);
const document = new TextDecoder().decode(yml);
// deno-lint-ignore no-explicit-any
const obj = parse(document) as Record<string, any>;
console.log(obj);
let i = 0;
for (const o of Object.values(obj)) {
console.log(`======${i}`);
for (const [key, value] of Object.entries(o)) {
console.log(key, value);
}
i++;
}
})();

View File

@ -1,197 +0,0 @@
---
# Collection Types #############################################################
################################################################################
# http://yaml.org/type/map.html -----------------------------------------------#
map:
# Unordered set of key: value pairs.
Block style: !!map
Clark : Evans
Ingy : döt Net
Oren : Ben-Kiki
Flow style: !!map { Clark: Evans, Ingy: döt Net, Oren: Ben-Kiki }
# http://yaml.org/type/omap.html ----------------------------------------------#
omap:
# Explicitly typed ordered map (dictionary).
Bestiary: !!omap
- aardvark: African pig-like ant eater. Ugly.
- anteater: South-American ant eater. Two species.
- anaconda: South-American constrictor snake. Scaly.
# Etc.
# Flow style
Numbers: !!omap [ one: 1, two: 2, three : 3 ]
# http://yaml.org/type/pairs.html ---------------------------------------------#
pairs:
# Explicitly typed pairs.
Block tasks: !!pairs
- meeting: with team.
- meeting: with boss.
- break: lunch.
- meeting: with client.
Flow tasks: !!pairs [ meeting: with team, meeting: with boss ]
# http://yaml.org/type/set.html -----------------------------------------------#
set:
# Explicitly typed set.
baseball players: !!set
? Mark McGwire
? Sammy Sosa
? Ken Griffey
# Flow style
baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees }
# http://yaml.org/type/seq.html -----------------------------------------------#
seq:
# Ordered sequence of nodes
Block style: !!seq
- Mercury # Rotates - no light/dark sides.
- Venus # Deadliest. Aptly named.
- Earth # Mostly dirt.
- Mars # Seems empty.
- Jupiter # The king.
- Saturn # Pretty.
- Uranus # Where the sun hardly shines.
- Neptune # Boring. No rings.
- Pluto # You call this a planet?
Flow style: !!seq [ Mercury, Venus, Earth, Mars, # Rocks
Jupiter, Saturn, Uranus, Neptune, # Gas
Pluto ] # Overrated
# Scalar Types #################################################################
################################################################################
# http://yaml.org/type/binary.html --------------------------------------------#
binary:
canonical: !!binary "\
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
generic: !!binary |
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
description:
The binary value above is a tiny arrow encoded as a gif image.
# http://yaml.org/type/bool.html ----------------------------------------------#
bool:
- true
- True
- TRUE
- false
- False
- FALSE
# http://yaml.org/type/float.html ---------------------------------------------#
float:
canonical: 6.8523015e+5
exponential: 685.230_15e+03
fixed: 685_230.15
sexagesimal: 190:20:30.15
negative infinity: -.inf
not a number: .NaN
# http://yaml.org/type/int.html -----------------------------------------------#
int:
canonical: 685230
decimal: +685_230
octal: 02472256
hexadecimal: 0x_0A_74_AE
binary: 0b1010_0111_0100_1010_1110
sexagesimal: 190:20:30
# http://yaml.org/type/merge.html ---------------------------------------------#
merge:
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }
# All the following maps are equal:
- # Explicit keys
x: 1
y: 2
r: 10
label: nothing
- # Merge one map
<< : *CENTER
r: 10
label: center
- # Merge multiple maps
<< : [ *CENTER, *BIG ]
label: center/big
- # Override
<< : [ *BIG, *LEFT, *SMALL ]
x: 1
label: big/left/small
# http://yaml.org/type/null.html ----------------------------------------------#
null:
# This mapping has four keys,
# one has a value.
empty:
canonical: ~
english: null
~: null key
# This sequence has five
# entries, two have values.
sparse:
- ~
- 2nd entry
-
- 4th entry
- Null
# http://yaml.org/type/str.html -----------------------------------------------#
string: abcd
# http://yaml.org/type/timestamp.html -----------------------------------------#
timestamp:
canonical: 2001-12-15T02:59:43.1Z
valid iso8601: 2001-12-14t21:59:43.10-05:00
space separated: 2001-12-14 21:59:43.10 -5
no time zone (Z): 2001-12-15 2:59:43.10
date (00:00:00Z): 2002-12-14
# JavaScript Specific Types ####################################################
################################################################################
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
# regexp:
# simple: !!js/regexp foobar
# modifiers: !!js/regexp /foobar/mi
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined
# undefined: !!js/undefined ~
# https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function
# function: !!js/function >
# function foobar() {
# return 'Wow! JS-YAML Rocks!';
# }

File diff suppressed because it is too large Load Diff

View File

@ -1,75 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import type { YAMLError } from "../error.ts";
import type { Schema, SchemaDefinition, TypeMap } from "../schema.ts";
import { State } from "../state.ts";
import type { Type } from "../type.ts";
import type { Any, ArrayObject } from "../utils.ts";
export interface LoaderStateOptions {
legacy?: boolean;
listener?: ((...args: Any[]) => void) | null;
/** string to be used as a file path in error/warning messages. */
filename?: string;
/** specifies a schema to use. */
schema?: SchemaDefinition;
/** compatibility with JSON.parse behaviour. */
json?: boolean;
/** function to call on warning messages. */
onWarning?(this: null, e?: YAMLError): void;
}
// deno-lint-ignore no-explicit-any
export type ResultType = any[] | Record<string, any> | string;
export class LoaderState extends State {
public documents: Any[] = [];
public length: number;
public lineIndent = 0;
public lineStart = 0;
public position = 0;
public line = 0;
public filename?: string;
public onWarning?: (...args: Any[]) => void;
public legacy: boolean;
public json: boolean;
public listener?: ((...args: Any[]) => void) | null;
public implicitTypes: Type[];
public typeMap: TypeMap;
public version?: string | null;
public checkLineBreaks?: boolean;
public tagMap?: ArrayObject;
public anchorMap?: ArrayObject;
public tag?: string | null;
public anchor?: string | null;
public kind?: string | null;
public result: ResultType | null = "";
constructor(
public input: string,
{
filename,
schema,
onWarning,
legacy = false,
json = false,
listener = null,
}: LoaderStateOptions,
) {
super(schema);
this.filename = filename;
this.onWarning = onWarning;
this.legacy = legacy;
this.json = json;
this.listener = listener;
this.implicitTypes = (this.schema as Schema).compiledImplicit;
this.typeMap = (this.schema as Schema).compiledTypeMap;
this.length = input.length;
}
}

View File

@ -1,79 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { repeat } from "./utils.ts";
export class Mark {
constructor(
public name: string,
public buffer: string,
public position: number,
public line: number,
public column: number,
) {}
public getSnippet(indent = 4, maxLength = 75): string | null {
if (!this.buffer) return null;
let head = "";
let start = this.position;
while (
start > 0 &&
"\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(start - 1)) === -1
) {
start -= 1;
if (this.position - start > maxLength / 2 - 1) {
head = " ... ";
start += 5;
break;
}
}
let tail = "";
let end = this.position;
while (
end < this.buffer.length &&
"\x00\r\n\x85\u2028\u2029".indexOf(this.buffer.charAt(end)) === -1
) {
end += 1;
if (end - this.position > maxLength / 2 - 1) {
tail = " ... ";
end -= 5;
break;
}
}
const snippet = this.buffer.slice(start, end);
return `${repeat(" ", indent)}${head}${snippet}${tail}\n${
repeat(
" ",
indent + this.position - start + head.length,
)
}^`;
}
public toString(compact?: boolean): string {
let snippet,
where = "";
if (this.name) {
where += `in "${this.name}" `;
}
where += `at line ${this.line + 1}, column ${this.column + 1}`;
if (!compact) {
snippet = this.getSnippet();
if (snippet) {
where += `:\n${snippet}`;
}
}
return where;
}
}

View File

@ -1,32 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { CbFunction, load, loadAll } from "./loader/loader.ts";
import type { LoaderStateOptions } from "./loader/loader_state.ts";
export type ParseOptions = LoaderStateOptions;
/**
* Parses `content` as single YAML document.
*
* Returns a JavaScript object or throws `YAMLException` on error.
* By default, does not support regexps, functions and undefined. This method is safe for untrusted data.
*
*/
export function parse(content: string, options?: ParseOptions): unknown {
return load(content, options);
}
/**
* Same as `parse()`, but understands multi-document sources.
* Applies iterator to each document if specified, or returns array of documents.
*/
export function parseAll(
content: string,
iterator?: CbFunction,
options?: ParseOptions,
): unknown {
return loadAll(content, iterator, options);
}

View File

@ -1,56 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { parse, parseAll } from "./parse.ts";
import { assertEquals } from "../../testing/asserts.ts";
Deno.test({
name: "`parse` parses single document yaml string",
fn(): void {
const yaml = `
test: toto
foo:
bar: True
baz: 1
qux: ~
`;
const expected = { test: "toto", foo: { bar: true, baz: 1, qux: null } };
assertEquals(parse(yaml), expected);
},
});
Deno.test({
name: "`parseAll` parses the yaml string with multiple documents",
fn(): void {
const yaml = `
---
id: 1
name: Alice
---
id: 2
name: Bob
---
id: 3
name: Eve
`;
const expected = [
{
id: 1,
name: "Alice",
},
{
id: 2,
name: "Bob",
},
{
id: 3,
name: "Eve",
},
];
assertEquals(parseAll(yaml), expected);
},
});

View File

@ -1,101 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { YAMLError } from "./error.ts";
import type { KindType, Type } from "./type.ts";
import type { Any, ArrayObject } from "./utils.ts";
function compileList(
schema: Schema,
name: "implicit" | "explicit",
result: Type[],
): Type[] {
const exclude: number[] = [];
for (const includedSchema of schema.include) {
result = compileList(includedSchema, name, result);
}
for (const currentType of schema[name]) {
for (
let previousIndex = 0;
previousIndex < result.length;
previousIndex++
) {
const previousType = result[previousIndex];
if (
previousType.tag === currentType.tag &&
previousType.kind === currentType.kind
) {
exclude.push(previousIndex);
}
}
result.push(currentType);
}
return result.filter((type, index): unknown => !exclude.includes(index));
}
export type TypeMap = { [k in KindType | "fallback"]: ArrayObject<Type> };
function compileMap(...typesList: Type[][]): TypeMap {
const result: TypeMap = {
fallback: {},
mapping: {},
scalar: {},
sequence: {},
};
for (const types of typesList) {
for (const type of types) {
if (type.kind !== null) {
result[type.kind][type.tag] = result["fallback"][type.tag] = type;
}
}
}
return result;
}
export class Schema implements SchemaDefinition {
public static SCHEMA_DEFAULT?: Schema;
public implicit: Type[];
public explicit: Type[];
public include: Schema[];
public compiledImplicit: Type[];
public compiledExplicit: Type[];
public compiledTypeMap: TypeMap;
constructor(definition: SchemaDefinition) {
this.explicit = definition.explicit || [];
this.implicit = definition.implicit || [];
this.include = definition.include || [];
for (const type of this.implicit) {
if (type.loadKind && type.loadKind !== "scalar") {
throw new YAMLError(
// eslint-disable-next-line max-len
"There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported.",
);
}
}
this.compiledImplicit = compileList(this, "implicit", []);
this.compiledExplicit = compileList(this, "explicit", []);
this.compiledTypeMap = compileMap(
this.compiledImplicit,
this.compiledExplicit,
);
}
public static create(): void {}
}
export interface SchemaDefinition {
implicit?: Any[];
explicit?: Type[];
include?: Schema[];
}

View File

@ -1,13 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Schema } from "../schema.ts";
import { json } from "./json.ts";
// Standard YAML's Core schema.
// http://www.yaml.org/spec/1.2/spec.html#id2804923
export const core = new Schema({
include: [json],
});

View File

@ -1,16 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Schema } from "../schema.ts";
import { binary, merge, omap, pairs, set, timestamp } from "../type/mod.ts";
import { core } from "./core.ts";
// JS-YAML's default schema for `safeLoad` function.
// It is not described in the YAML specification.
export const def = new Schema({
explicit: [binary, omap, pairs, set],
implicit: [timestamp, merge],
include: [core],
});

View File

@ -1,13 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Schema } from "../schema.ts";
import { map, seq, str } from "../type/mod.ts";
// Standard YAML's Failsafe schema.
// http://www.yaml.org/spec/1.2/spec.html#id2802346
export const failsafe = new Schema({
explicit: [str, seq, map],
});

View File

@ -1,15 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Schema } from "../schema.ts";
import { bool, float, int, nil } from "../type/mod.ts";
import { failsafe } from "./failsafe.ts";
// Standard YAML's JSON schema.
// http://www.yaml.org/spec/1.2/spec.html#id2803231
export const json = new Schema({
implicit: [nil, bool, int, float],
include: [failsafe],
});

View File

@ -1,9 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export { core as CORE_SCHEMA } from "./core.ts";
export { def as DEFAULT_SCHEMA } from "./default.ts";
export { failsafe as FAILSAFE_SCHEMA } from "./failsafe.ts";
export { json as JSON_SCHEMA } from "./json.ts";

View File

@ -1,11 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import type { SchemaDefinition } from "./schema.ts";
import { DEFAULT_SCHEMA } from "./schema/mod.ts";
export abstract class State {
constructor(public schema: SchemaDefinition = DEFAULT_SCHEMA) {}
}

View File

@ -1,21 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { dump } from "./dumper/dumper.ts";
import type { DumperStateOptions } from "./dumper/dumper_state.ts";
export type DumpOptions = DumperStateOptions;
/**
* Serializes `object` as a YAML document.
*
* You can disable exceptions by setting the skipInvalid option to true.
*/
export function stringify(
obj: Record<string, unknown>,
options?: DumpOptions,
): string {
return dump(obj, options);
}

View File

@ -1,41 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../../testing/asserts.ts";
import { stringify } from "./stringify.ts";
Deno.test({
name: "stringified correctly",
fn(): void {
const FIXTURE = {
foo: {
bar: true,
test: [
"a",
"b",
{
a: false,
},
{
a: false,
},
],
},
test: "foobar",
};
const ASSERTS = `foo:
bar: true
test:
- a
- b
- a: false
- a: false
test: foobar
`;
assertEquals(stringify(FIXTURE), ASSERTS);
},
});

View File

@ -1,55 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import type { Any, ArrayObject } from "./utils.ts";
export type KindType = "sequence" | "scalar" | "mapping";
export type StyleVariant = "lowercase" | "uppercase" | "camelcase" | "decimal";
export type RepresentFn = (data: Any, style?: StyleVariant) => Any;
const DEFAULT_RESOLVE = (): boolean => true;
const DEFAULT_CONSTRUCT = (data: Any): Any => data;
interface TypeOptions {
kind: KindType;
resolve?: (data: Any) => boolean;
construct?: (data: string) => Any;
instanceOf?: Any;
predicate?: (data: Record<string, unknown>) => boolean;
represent?: RepresentFn | ArrayObject<RepresentFn>;
defaultStyle?: StyleVariant;
styleAliases?: ArrayObject;
}
function checkTagFormat(tag: string): string {
return tag;
}
export class Type {
public tag: string;
public kind: KindType | null = null;
public instanceOf: Any;
public predicate?: (data: Record<string, unknown>) => boolean;
public represent?: RepresentFn | ArrayObject<RepresentFn>;
public defaultStyle?: StyleVariant;
public styleAliases?: ArrayObject;
public loadKind?: KindType;
constructor(tag: string, options?: TypeOptions) {
this.tag = checkTagFormat(tag);
if (options) {
this.kind = options.kind;
this.resolve = options.resolve || DEFAULT_RESOLVE;
this.construct = options.construct || DEFAULT_CONSTRUCT;
this.instanceOf = options.instanceOf;
this.predicate = options.predicate;
this.represent = options.represent;
this.defaultStyle = options.defaultStyle;
this.styleAliases = options.styleAliases;
}
}
public resolve: (data?: Any) => boolean = (): boolean => true;
public construct: (data?: Any) => Any = (data): Any => data;
}

View File

@ -1,136 +0,0 @@
// Ported from js-yaml v3.13.1:
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import type { Any } from "../utils.ts";
// [ 64, 65, 66 ] -> [ padding, CR, LF ]
const BASE64_MAP =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r";
function resolveYamlBinary(data: Any): boolean {
if (data === null) return false;
let code: number;
let bitlen = 0;
const max = data.length;
const map = BASE64_MAP;
// Convert one by one.
for (let idx = 0; idx < max; idx++) {
code = map.indexOf(data.charAt(idx));
// Skip CR/LF
if (code > 64) continue;
// Fail on illegal characters
if (code < 0) return false;
bitlen += 6;
}
// If there are any bits left, source was corrupted
return bitlen % 8 === 0;
}
function constructYamlBinary(data: string): Deno.Buffer {
// remove CR/LF & padding to simplify scan
const input = data.replace(/[\r\n=]/g, "");
const max = input.length;
const map = BASE64_MAP;
// Collect by 6*4 bits (3 bytes)
const result = [];
let bits = 0;
for (let idx = 0; idx < max; idx++) {
if (idx % 4 === 0 && idx) {
result.push((bits >> 16) & 0xff);
result.push((bits >> 8) & 0xff);
result.push(bits & 0xff);
}
bits = (bits << 6) | map.indexOf(input.charAt(idx));
}
// Dump tail
const tailbits = (max % 4) * 6;
if (tailbits === 0) {
result.push((bits >> 16) & 0xff);
result.push((bits >> 8) & 0xff);
result.push(bits & 0xff);
} else if (tailbits === 18) {
result.push((bits >> 10) & 0xff);
result.push((bits >> 2) & 0xff);
} else if (tailbits === 12) {
result.push((bits >> 4) & 0xff);
}
return new Deno.Buffer(new Uint8Array(result));
}
function representYamlBinary(object: Uint8Array): string {
const max = object.length;
const map = BASE64_MAP;
// Convert every three bytes to 4 ASCII characters.
let result = "";
let bits = 0;
for (let idx = 0; idx < max; idx++) {
if (idx % 3 === 0 && idx) {
result += map[(bits >> 18) & 0x3f];
result += map[(bits >> 12) & 0x3f];
result += map[(bits >> 6) & 0x3f];
result += map[bits & 0x3f];
}
bits = (bits << 8) + object[idx];
}
// Dump tail
const tail = max % 3;
if (tail === 0) {
result += map[(bits >> 18) & 0x3f];
result += map[(bits >> 12) & 0x3f];
result += map[(bits >> 6) & 0x3f];
result += map[bits & 0x3f];
} else if (tail === 2) {
result += map[(bits >> 10) & 0x3f];
result += map[(bits >> 4) & 0x3f];
result += map[(bits << 2) & 0x3f];
result += map[64];
} else if (tail === 1) {
result += map[(bits >> 2) & 0x3f];
result += map[(bits << 4) & 0x3f];
result += map[64];
result += map[64];
}
return result;
}
function isBinary(obj: Any): obj is Deno.Buffer {
const buf = new Deno.Buffer();
try {
if (0 > buf.readFromSync(obj as Deno.Buffer)) return true;
return false;
} catch {
return false;
} finally {
buf.reset();
}
}
export const binary = new Type("tag:yaml.org,2002:binary", {
construct: constructYamlBinary,
kind: "scalar",
predicate: isBinary,
represent: representYamlBinary,
resolve: resolveYamlBinary,
});

View File

@ -1,39 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import { isBoolean } from "../utils.ts";
function resolveYamlBoolean(data: string): boolean {
const max = data.length;
return (
(max === 4 && (data === "true" || data === "True" || data === "TRUE")) ||
(max === 5 && (data === "false" || data === "False" || data === "FALSE"))
);
}
function constructYamlBoolean(data: string): boolean {
return data === "true" || data === "True" || data === "TRUE";
}
export const bool = new Type("tag:yaml.org,2002:bool", {
construct: constructYamlBoolean,
defaultStyle: "lowercase",
kind: "scalar",
predicate: isBoolean,
represent: {
lowercase(object: boolean): string {
return object ? "true" : "false";
},
uppercase(object: boolean): string {
return object ? "TRUE" : "FALSE";
},
camelcase(object: boolean): string {
return object ? "True" : "False";
},
},
resolve: resolveYamlBoolean,
});

View File

@ -1,125 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { StyleVariant, Type } from "../type.ts";
import { Any, isNegativeZero } from "../utils.ts";
const YAML_FLOAT_PATTERN = new RegExp(
// 2.5e4, 2.5 and integers
"^(?:[-+]?(?:0|[1-9][0-9_]*)(?:\\.[0-9_]*)?(?:[eE][-+]?[0-9]+)?" +
// .2e4, .2
// special case, seems not from spec
"|\\.[0-9_]+(?:[eE][-+]?[0-9]+)?" +
// 20:59
"|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*" +
// .inf
"|[-+]?\\.(?:inf|Inf|INF)" +
// .nan
"|\\.(?:nan|NaN|NAN))$",
);
function resolveYamlFloat(data: string): boolean {
if (
!YAML_FLOAT_PATTERN.test(data) ||
// Quick hack to not allow integers end with `_`
// Probably should update regexp & check speed
data[data.length - 1] === "_"
) {
return false;
}
return true;
}
function constructYamlFloat(data: string): number {
let value = data.replace(/_/g, "").toLowerCase();
const sign = value[0] === "-" ? -1 : 1;
const digits: number[] = [];
if ("+-".indexOf(value[0]) >= 0) {
value = value.slice(1);
}
if (value === ".inf") {
return sign === 1 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
}
if (value === ".nan") {
return NaN;
}
if (value.indexOf(":") >= 0) {
value.split(":").forEach((v): void => {
digits.unshift(parseFloat(v));
});
let valueNb = 0.0;
let base = 1;
digits.forEach((d): void => {
valueNb += d * base;
base *= 60;
});
return sign * valueNb;
}
return sign * parseFloat(value);
}
const SCIENTIFIC_WITHOUT_DOT = /^[-+]?[0-9]+e/;
function representYamlFloat(object: Any, style?: StyleVariant): Any {
if (isNaN(object)) {
switch (style) {
case "lowercase":
return ".nan";
case "uppercase":
return ".NAN";
case "camelcase":
return ".NaN";
}
} else if (Number.POSITIVE_INFINITY === object) {
switch (style) {
case "lowercase":
return ".inf";
case "uppercase":
return ".INF";
case "camelcase":
return ".Inf";
}
} else if (Number.NEGATIVE_INFINITY === object) {
switch (style) {
case "lowercase":
return "-.inf";
case "uppercase":
return "-.INF";
case "camelcase":
return "-.Inf";
}
} else if (isNegativeZero(object)) {
return "-0.0";
}
const res = object.toString(10);
// JS stringifier can build scientific format without dots: 5e-100,
// while YAML requires dot: 5.e-100. Fix it with simple hack
return SCIENTIFIC_WITHOUT_DOT.test(res) ? res.replace("e", ".e") : res;
}
function isFloat(object: Any): boolean {
return (
Object.prototype.toString.call(object) === "[object Number]" &&
(object % 1 !== 0 || isNegativeZero(object))
);
}
export const float = new Type("tag:yaml.org,2002:float", {
construct: constructYamlFloat,
defaultStyle: "lowercase",
kind: "scalar",
predicate: isFloat,
represent: representYamlFloat,
resolve: resolveYamlFloat,
});

View File

@ -1,188 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import { Any, isNegativeZero } from "../utils.ts";
function isHexCode(c: number): boolean {
return (
(0x30 <= /* 0 */ c && c <= 0x39) /* 9 */ ||
(0x41 <= /* A */ c && c <= 0x46) /* F */ ||
(0x61 <= /* a */ c && c <= 0x66) /* f */
);
}
function isOctCode(c: number): boolean {
return 0x30 <= /* 0 */ c && c <= 0x37 /* 7 */;
}
function isDecCode(c: number): boolean {
return 0x30 <= /* 0 */ c && c <= 0x39 /* 9 */;
}
function resolveYamlInteger(data: string): boolean {
const max = data.length;
let index = 0;
let hasDigits = false;
if (!max) return false;
let ch = data[index];
// sign
if (ch === "-" || ch === "+") {
ch = data[++index];
}
if (ch === "0") {
// 0
if (index + 1 === max) return true;
ch = data[++index];
// base 2, base 8, base 16
if (ch === "b") {
// base 2
index++;
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (ch !== "0" && ch !== "1") return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}
if (ch === "x") {
// base 16
index++;
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (!isHexCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}
// base 8
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (!isOctCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}
// base 10 (except 0) or base 60
// value should not start with `_`;
if (ch === "_") return false;
for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (ch === ":") break;
if (!isDecCode(data.charCodeAt(index))) {
return false;
}
hasDigits = true;
}
// Should have digits and should not end with `_`
if (!hasDigits || ch === "_") return false;
// if !base60 - done;
if (ch !== ":") return true;
// base60 almost not used, no needs to optimize
return /^(:[0-5]?[0-9])+$/.test(data.slice(index));
}
function constructYamlInteger(data: string): number {
let value = data;
const digits: number[] = [];
if (value.indexOf("_") !== -1) {
value = value.replace(/_/g, "");
}
let sign = 1;
let ch = value[0];
if (ch === "-" || ch === "+") {
if (ch === "-") sign = -1;
value = value.slice(1);
ch = value[0];
}
if (value === "0") return 0;
if (ch === "0") {
if (value[1] === "b") return sign * parseInt(value.slice(2), 2);
if (value[1] === "x") return sign * parseInt(value, 16);
return sign * parseInt(value, 8);
}
if (value.indexOf(":") !== -1) {
value.split(":").forEach((v): void => {
digits.unshift(parseInt(v, 10));
});
let valueInt = 0;
let base = 1;
digits.forEach((d): void => {
valueInt += d * base;
base *= 60;
});
return sign * valueInt;
}
return sign * parseInt(value, 10);
}
function isInteger(object: Any): boolean {
return (
Object.prototype.toString.call(object) === "[object Number]" &&
object % 1 === 0 &&
!isNegativeZero(object)
);
}
export const int = new Type("tag:yaml.org,2002:int", {
construct: constructYamlInteger,
defaultStyle: "decimal",
kind: "scalar",
predicate: isInteger,
represent: {
binary(obj: number): string {
return obj >= 0
? `0b${obj.toString(2)}`
: `-0b${obj.toString(2).slice(1)}`;
},
octal(obj: number): string {
return obj >= 0 ? `0${obj.toString(8)}` : `-0${obj.toString(8).slice(1)}`;
},
decimal(obj: number): string {
return obj.toString(10);
},
hexadecimal(obj: number): string {
return obj >= 0
? `0x${obj.toString(16).toUpperCase()}`
: `-0x${obj.toString(16).toUpperCase().slice(1)}`;
},
},
resolve: resolveYamlInteger,
styleAliases: {
binary: [2, "bin"],
decimal: [10, "dec"],
hexadecimal: [16, "hex"],
octal: [8, "oct"],
},
});

View File

@ -1,14 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import type { Any } from "../utils.ts";
export const map = new Type("tag:yaml.org,2002:map", {
construct(data): Any {
return data !== null ? data : {};
},
kind: "mapping",
});

View File

@ -1,15 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
function resolveYamlMerge(data: string): boolean {
return data === "<<" || data === null;
}
export const merge = new Type("tag:yaml.org,2002:merge", {
kind: "scalar",
resolve: resolveYamlMerge,
});

View File

@ -1,18 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export { binary } from "./binary.ts";
export { bool } from "./bool.ts";
export { float } from "./float.ts";
export { int } from "./int.ts";
export { map } from "./map.ts";
export { merge } from "./merge.ts";
export { nil } from "./nil.ts";
export { omap } from "./omap.ts";
export { pairs } from "./pairs.ts";
export { seq } from "./seq.ts";
export { set } from "./set.ts";
export { str } from "./str.ts";
export { timestamp } from "./timestamp.ts";

View File

@ -1,45 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
function resolveYamlNull(data: string): boolean {
const max = data.length;
return (
(max === 1 && data === "~") ||
(max === 4 && (data === "null" || data === "Null" || data === "NULL"))
);
}
function constructYamlNull(): null {
return null;
}
function isNull(object: unknown): object is null {
return object === null;
}
export const nil = new Type("tag:yaml.org,2002:null", {
construct: constructYamlNull,
defaultStyle: "lowercase",
kind: "scalar",
predicate: isNull,
represent: {
canonical(): string {
return "~";
},
lowercase(): string {
return "null";
},
uppercase(): string {
return "NULL";
},
camelcase(): string {
return "Null";
},
},
resolve: resolveYamlNull,
});

View File

@ -1,46 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import type { Any } from "../utils.ts";
const _hasOwnProperty = Object.prototype.hasOwnProperty;
const _toString = Object.prototype.toString;
function resolveYamlOmap(data: Any): boolean {
const objectKeys: string[] = [];
let pairKey = "";
let pairHasKey = false;
for (const pair of data) {
pairHasKey = false;
if (_toString.call(pair) !== "[object Object]") return false;
for (pairKey in pair) {
if (_hasOwnProperty.call(pair, pairKey)) {
if (!pairHasKey) pairHasKey = true;
else return false;
}
}
if (!pairHasKey) return false;
if (objectKeys.indexOf(pairKey) === -1) objectKeys.push(pairKey);
else return false;
}
return true;
}
function constructYamlOmap(data: Any): Any {
return data !== null ? data : [];
}
export const omap = new Type("tag:yaml.org,2002:omap", {
construct: constructYamlOmap,
kind: "sequence",
resolve: resolveYamlOmap,
});

View File

@ -1,49 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import type { Any } from "../utils.ts";
const _toString = Object.prototype.toString;
function resolveYamlPairs(data: Any[][]): boolean {
const result = new Array(data.length);
for (let index = 0; index < data.length; index++) {
const pair = data[index];
if (_toString.call(pair) !== "[object Object]") return false;
const keys = Object.keys(pair);
if (keys.length !== 1) return false;
result[index] = [keys[0], pair[keys[0] as Any]];
}
return true;
}
function constructYamlPairs(data: string): Any[] {
if (data === null) return [];
const result = new Array(data.length);
for (let index = 0; index < data.length; index += 1) {
const pair = data[index];
const keys = Object.keys(pair);
result[index] = [keys[0], pair[keys[0] as Any]];
}
return result;
}
export const pairs = new Type("tag:yaml.org,2002:pairs", {
construct: constructYamlPairs,
kind: "sequence",
resolve: resolveYamlPairs,
});

View File

@ -1,14 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import type { Any } from "../utils.ts";
export const seq = new Type("tag:yaml.org,2002:seq", {
construct(data): Any {
return data !== null ? data : [];
},
kind: "sequence",
});

View File

@ -1,31 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
import type { Any } from "../utils.ts";
const _hasOwnProperty = Object.prototype.hasOwnProperty;
function resolveYamlSet(data: Any): boolean {
if (data === null) return true;
for (const key in data) {
if (_hasOwnProperty.call(data, key)) {
if (data[key] !== null) return false;
}
}
return true;
}
function constructYamlSet(data: string): Any {
return data !== null ? data : {};
}
export const set = new Type("tag:yaml.org,2002:set", {
construct: constructYamlSet,
kind: "mapping",
resolve: resolveYamlSet,
});

View File

@ -1,12 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
export const str = new Type("tag:yaml.org,2002:str", {
construct(data): string {
return data !== null ? data : "";
},
kind: "scalar",
});

View File

@ -1,96 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { Type } from "../type.ts";
const YAML_DATE_REGEXP = new RegExp(
"^([0-9][0-9][0-9][0-9])" + // [1] year
"-([0-9][0-9])" + // [2] month
"-([0-9][0-9])$", // [3] day
);
const YAML_TIMESTAMP_REGEXP = new RegExp(
"^([0-9][0-9][0-9][0-9])" + // [1] year
"-([0-9][0-9]?)" + // [2] month
"-([0-9][0-9]?)" + // [3] day
"(?:[Tt]|[ \\t]+)" + // ...
"([0-9][0-9]?)" + // [4] hour
":([0-9][0-9])" + // [5] minute
":([0-9][0-9])" + // [6] second
"(?:\\.([0-9]*))?" + // [7] fraction
"(?:[ \\t]*(Z|([-+])([0-9][0-9]?)" + // [8] tz [9] tz_sign [10] tz_hour
"(?::([0-9][0-9]))?))?$", // [11] tz_minute
);
function resolveYamlTimestamp(data: string): boolean {
if (data === null) return false;
if (YAML_DATE_REGEXP.exec(data) !== null) return true;
if (YAML_TIMESTAMP_REGEXP.exec(data) !== null) return true;
return false;
}
function constructYamlTimestamp(data: string): Date {
let match = YAML_DATE_REGEXP.exec(data);
if (match === null) match = YAML_TIMESTAMP_REGEXP.exec(data);
if (match === null) throw new Error("Date resolve error");
// match: [1] year [2] month [3] day
const year = +match[1];
const month = +match[2] - 1; // JS month starts with 0
const day = +match[3];
if (!match[4]) {
// no hour
return new Date(Date.UTC(year, month, day));
}
// match: [4] hour [5] minute [6] second [7] fraction
const hour = +match[4];
const minute = +match[5];
const second = +match[6];
let fraction = 0;
if (match[7]) {
let partFraction = match[7].slice(0, 3);
while (partFraction.length < 3) {
// milli-seconds
partFraction += "0";
}
fraction = +partFraction;
}
// match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute
let delta = null;
if (match[9]) {
const tzHour = +match[10];
const tzMinute = +(match[11] || 0);
delta = (tzHour * 60 + tzMinute) * 60000; // delta in milli-seconds
if (match[9] === "-") delta = -delta;
}
const date = new Date(
Date.UTC(year, month, day, hour, minute, second, fraction),
);
if (delta) date.setTime(date.getTime() - delta);
return date;
}
function representYamlTimestamp(date: Date): string {
return date.toISOString();
}
export const timestamp = new Type("tag:yaml.org,2002:timestamp", {
construct: constructYamlTimestamp,
instanceOf: Date,
kind: "scalar",
represent: representYamlTimestamp,
resolve: resolveYamlTimestamp,
});

View File

@ -1,80 +0,0 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore no-explicit-any
export type Any = any;
export function isNothing(subject: unknown): subject is never {
return typeof subject === "undefined" || subject === null;
}
export function isArray(value: unknown): value is Any[] {
return Array.isArray(value);
}
export function isBoolean(value: unknown): value is boolean {
return typeof value === "boolean" || value instanceof Boolean;
}
export function isNull(value: unknown): value is null {
return value === null;
}
export function isNumber(value: unknown): value is number {
return typeof value === "number" || value instanceof Number;
}
export function isString(value: unknown): value is string {
return typeof value === "string" || value instanceof String;
}
export function isSymbol(value: unknown): value is symbol {
return typeof value === "symbol";
}
export function isUndefined(value: unknown): value is undefined {
return value === undefined;
}
export function isObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object";
}
export function isError(e: unknown): boolean {
return e instanceof Error;
}
export function isFunction(value: unknown): value is () => void {
return typeof value === "function";
}
export function isRegExp(value: unknown): value is RegExp {
return value instanceof RegExp;
}
export function toArray<T>(sequence: T): T | [] | [T] {
if (isArray(sequence)) return sequence;
if (isNothing(sequence)) return [];
return [sequence];
}
export function repeat(str: string, count: number): string {
let result = "";
for (let cycle = 0; cycle < count; cycle++) {
result += str;
}
return result;
}
export function isNegativeZero(i: number): boolean {
return i === 0 && Number.NEGATIVE_INFINITY === 1 / i;
}
export interface ArrayObject<T = Any> {
[P: string]: T;
}

View File

@ -1,133 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
export type Ascii85Standard = "Adobe" | "btoa" | "RFC 1924" | "Z85";
/**
* encoding/decoding options
* @property standard - characterset and delimiter (if supported and used). Defaults to Adobe
* @property delimiter - whether to use a delimiter (if supported) - "<~" and "~>" by default
*/
export interface Ascii85Options {
standard?: Ascii85Standard;
delimiter?: boolean;
}
const rfc1924 =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
const Z85 =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
/**
* Encodes a given Uint8Array into ascii85, supports multiple standards
* @param uint8 input to encode
* @param [options] encoding options
* @param [options.standard=Adobe] encoding standard (Adobe, btoa, RFC 1924 or Z85)
* @param [options.delimiter] whether to use a delimiter, if supported by encoding standard
*/
export function encode(uint8: Uint8Array, options?: Ascii85Options): string {
const standard = options?.standard ?? "Adobe";
let output: string[] = [],
v: number,
n = 0,
difference = 0;
if (uint8.length % 4 !== 0) {
const tmp = uint8;
difference = 4 - (tmp.length % 4);
uint8 = new Uint8Array(tmp.length + difference);
uint8.set(tmp);
}
const view = new DataView(uint8.buffer);
for (let i = 0, len = uint8.length; i < len; i += 4) {
v = view.getUint32(i);
// Adobe and btoa standards compress 4 zeroes to single "z" character
if (
(standard === "Adobe" || standard === "btoa") &&
v === 0 &&
i < len - difference - 3
) {
output[n++] = "z";
continue;
}
// btoa compresses 4 spaces - that is, bytes equal to 32 - into single "y" character
if (standard === "btoa" && v === 538976288) {
output[n++] = "y";
continue;
}
for (let j = 4; j >= 0; j--) {
output[n + j] = String.fromCharCode((v % 85) + 33);
v = Math.trunc(v / 85);
}
n += 5;
}
switch (standard) {
case "Adobe":
if (options?.delimiter) {
return `<~${output.slice(0, output.length - difference).join("")}~>`;
}
break;
case "btoa":
if (options?.delimiter) {
return `xbtoa Begin\n${
output
.slice(0, output.length - difference)
.join("")
}\nxbtoa End`;
}
break;
case "RFC 1924":
output = output.map((val) => rfc1924[val.charCodeAt(0) - 33]);
break;
case "Z85":
output = output.map((val) => Z85[val.charCodeAt(0) - 33]);
break;
}
return output.slice(0, output.length - difference).join("");
}
/**
* Decodes a given ascii85 encoded string.
* @param ascii85 input to decode
* @param [options] decoding options
* @param [options.standard=Adobe] encoding standard used in the input string (Adobe, btoa, RFC 1924 or Z85)
*/
export function decode(ascii85: string, options?: Ascii85Options): Uint8Array {
const encoding = options?.standard ?? "Adobe";
// translate all encodings to most basic adobe/btoa one and decompress some special characters ("z" and "y")
switch (encoding) {
case "Adobe":
ascii85 = ascii85.replaceAll(/(<~|~>)/g, "").replaceAll("z", "!!!!!");
break;
case "btoa":
ascii85 = ascii85
.replaceAll(/(xbtoa Begin|xbtoa End|\n)/g, "")
.replaceAll("z", "!!!!!")
.replaceAll("y", "+<VdL");
break;
case "RFC 1924":
ascii85 = ascii85.replaceAll(
/./g,
(match) => String.fromCharCode(rfc1924.indexOf(match) + 33),
);
break;
case "Z85":
ascii85 = ascii85.replaceAll(
/./g,
(match) => String.fromCharCode(Z85.indexOf(match) + 33),
);
break;
}
//remove all invalid characters
ascii85 = ascii85.replaceAll(/[^!-u]/g, "");
const len = ascii85.length,
output = new Uint8Array(len + 4 - (len % 4));
const view = new DataView(output.buffer);
let v = 0,
n = 0,
max = 0;
for (let i = 0; i < len;) {
for (max += 5; i < max; i++) {
v = v * 85 + (i < len ? ascii85.charCodeAt(i) : 117) - 33;
}
view.setUint32(n, v);
v = 0;
n += 4;
}
return output.slice(0, Math.trunc(len * 0.8));
}

View File

@ -1,178 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { Ascii85Standard, decode, encode } from "./ascii85.ts";
type TestCases = Partial<{ [index in Ascii85Standard]: string[][] }>;
const utf8encoder = new TextEncoder();
const testCasesNoDelimiter: TestCases = {
Adobe: [
["test", "FCfN8"],
["ascii85", "@<5pmBfIs"],
["Hello world!", "87cURD]j7BEbo80"],
//wikipedia example
[
"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
"9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c",
],
["", ""],
["\0", "!!"],
["\0\0", "!!!"],
["\0\0\0", "!!!!"],
//special Adobe and btoa test cases - 4 bytes equal to 0 should become a "z"
["\0\0\0\0", "z"],
["\0\0\0\0\0", "z!!"],
[" ", "+<VdL"],
],
btoa: [
["test", "FCfN8"],
["ascii85", "@<5pmBfIs"],
["Hello world!", "87cURD]j7BEbo80"],
//wikipedia example
[
"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
"9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c",
],
["", ""],
["\0", "!!"],
["\0\0", "!!!"],
["\0\0\0", "!!!!"],
//special Adobe and btoa test cases - 4 bytes equal to 0 should become a "z"
["\0\0\0\0", "z"],
["\0\0\0\0\0", "z!!"],
//special btoa test case - 4 spaces should become "y"
[" ", "y"],
],
"RFC 1924": [
["test", "bY*jN"],
["ascii85", "VRK_?X*e|"],
["Hello world!", "NM&qnZy<MXa%^NF"],
//wikipedia example
[
"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
"O<`^zX>%ZCX>)XGZfA9Ab7*B`EFf-gbRchTY<VDJc_3(Mb0BhMVRLV8EFfZabRc4RAarPHb0BkRZfA9DVR9gFVRLh7Z*CxFa&K)QZ**v7av))DX>DO_b1WctXlY|;AZc?TVIXXEb95kYW*~HEWgu;7Ze%PVbZB98AYyqSVIXj2a&u*NWpZI|V`U(3W*}r`Y-wj`bRcPNAarPDAY*TCbZKsNWn>^>Ze$>7Ze(R<VRUI{VPb4$AZKN6WpZJ3X>V>IZ)PBCZf|#NWn^b%EFfigV`XJzb0BnRWgv5CZ*p`Xc4cT~ZDnp_Wgu^6AYpEKAY);2ZeeU7aBO8^b9HiME&",
],
["", ""],
["\0", "00"],
["\0\0", "000"],
["\0\0\0", "0000"],
["\0\0\0\0", "00000"],
["\0\0\0\0\0", "0000000"],
[" ", "ARr(h"],
],
Z85: [
["test", "By/Jn"],
["ascii85", "vrk{)x/E%"],
["Hello world!", "nm=QNzY<mxA+]nf"],
//wikipedia example
[
"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
"o<}]Zx(+zcx(!xgzFa9aB7/b}efF?GBrCHty<vdjC{3^mB0bHmvrlv8efFzABrC4raARphB0bKrzFa9dvr9GfvrlH7z/cXfA=k!qz//V7AV!!dx(do{B1wCTxLy%&azC)tvixxeB95Kyw/#hewGU&7zE+pvBzb98ayYQsvixJ2A=U/nwPzi%v}u^3w/$R}y?WJ}BrCpnaARpday/tcBzkSnwN(](zE:(7zE^r<vrui@vpB4:azkn6wPzj3x(v(iz!pbczF%-nwN]B+efFIGv}xjZB0bNrwGV5cz/P}xC4Ct#zdNP{wGU]6ayPekay!&2zEEu7Abo8]B9hIme=",
],
["", ""],
["\0", "00"],
["\0\0", "000"],
["\0\0\0", "0000"],
["\0\0\0\0", "00000"],
["\0\0\0\0\0", "0000000"],
[" ", "arR^H"],
],
};
const testCasesDelimiter: TestCases = {
Adobe: [
["test", "<~FCfN8~>"],
["ascii85", "<~@<5pmBfIs~>"],
["Hello world!", "<~87cURD]j7BEbo80~>"],
//wikipedia example
[
"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
"<~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c~>",
],
["", "<~~>"],
["\0", "<~!!~>"],
["\0\0", "<~!!!~>"],
["\0\0\0", "<~!!!!~>"],
//special Adobe and btoa test cases - 4 bytes equal to 0 should become a "z"
["\0\0\0\0", "<~z~>"],
["\0\0\0\0\0", "<~z!!~>"],
[" ", "<~+<VdL~>"],
],
btoa: [
["test", "xbtoa Begin\nFCfN8\nxbtoa End"],
["ascii85", "xbtoa Begin\n@<5pmBfIs\nxbtoa End"],
["Hello world!", "xbtoa Begin\n87cURD]j7BEbo80\nxbtoa End"],
//wikipedia example
[
"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
"xbtoa Begin\n9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\nxbtoa End",
],
["", "xbtoa Begin\n\nxbtoa End"],
["\0", "xbtoa Begin\n!!\nxbtoa End"],
["\0\0", "xbtoa Begin\n!!!\nxbtoa End"],
["\0\0\0", "xbtoa Begin\n!!!!\nxbtoa End"],
//special Adobe and btoa test cases - 4 bytes equal to 0 should become a "z"
["\0\0\0\0", "xbtoa Begin\nz\nxbtoa End"],
["\0\0\0\0\0", "xbtoa Begin\nz!!\nxbtoa End"],
//special btoa test case - 4 spaces should become "y"
[" ", "xbtoa Begin\ny\nxbtoa End"],
],
};
for (const [standard, tests] of Object.entries(testCasesNoDelimiter)) {
if (tests === undefined) continue;
Deno.test({
name: `[encoding/ascii85] encode ${standard}`,
fn(): void {
for (const [bin, b85] of tests) {
assertEquals(
encode(utf8encoder.encode(bin), {
standard: standard as Ascii85Standard,
}),
b85,
);
}
},
});
Deno.test({
name: `[encoding/ascii85] decode ${standard}`,
fn(): void {
for (const [bin, b85] of tests) {
assertEquals(
decode(b85, { standard: standard as Ascii85Standard }),
utf8encoder.encode(bin),
);
}
},
});
}
for (const [standard, tests] of Object.entries(testCasesDelimiter)) {
if (tests === undefined) continue;
Deno.test({
name: `[encoding/ascii85] encode ${standard} with delimiter`,
fn(): void {
for (const [bin, b85] of tests) {
assertEquals(
encode(utf8encoder.encode(bin), {
standard: standard as Ascii85Standard,
delimiter: true,
}),
b85,
);
}
},
});
Deno.test({
name: `[encoding/ascii85] decode ${standard} with delimiter`,
fn(): void {
for (const [bin, b85] of tests) {
assertEquals(
decode(b85, {
standard: standard as Ascii85Standard,
delimiter: true,
}),
utf8encoder.encode(bin),
);
}
},
});
}

View File

@ -1,207 +0,0 @@
// Modified from https://github.com/beatgammit/base64-js
// Copyright (c) 2014 Jameson Little. MIT License.
const lookup: string[] = [];
const revLookup: number[] = [];
// RFC4648 base32
const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
for (let i = 0, len = code.length; i < len; ++i) {
lookup[i] = code[i];
revLookup[code.charCodeAt(i)] = i;
}
const placeHolderPadLookup = [0, 1, , 2, 3, , 4];
function _getPadLen(placeHoldersLen: number): number {
const maybeLen = placeHolderPadLookup[placeHoldersLen];
if (typeof maybeLen !== "number") {
throw new Error("Invalid pad length");
}
return maybeLen;
}
function getLens(b32: string): [number, number] {
const len = b32.length;
if (len % 8 > 0) {
throw new Error("Invalid string. Length must be a multiple of 8");
}
let validLen = b32.indexOf("=");
if (validLen === -1) validLen = len;
const placeHoldersLen = validLen === len ? 0 : 8 - (validLen % 8);
return [validLen, placeHoldersLen];
}
/**
* Returns number of bytes encoded in the given RFC4648 base32 string input.
* @param b32
*/
export function byteLength(b32: string): number {
const [validLen, placeHoldersLen] = getLens(b32);
return _byteLength(validLen, placeHoldersLen);
}
function _byteLength(validLen: number, placeHoldersLen: number): number {
return ((validLen + placeHoldersLen) * 5) / 8 - _getPadLen(placeHoldersLen);
}
/**
* Decodes a given RFC4648 base32 encoded string.
* @param b32
*/
export function decode(b32: string): Uint8Array {
let tmp: number;
const [validLen, placeHoldersLen] = getLens(b32);
const arr = new Uint8Array(_byteLength(validLen, placeHoldersLen));
let curByte = 0;
// if there are placeholders, only get up to the last complete 8 chars
const len = placeHoldersLen > 0 ? validLen - 8 : validLen;
let i: number;
for (i = 0; i < len; i += 8) {
tmp = (revLookup[b32.charCodeAt(i)] << 20) |
(revLookup[b32.charCodeAt(i + 1)] << 15) |
(revLookup[b32.charCodeAt(i + 2)] << 10) |
(revLookup[b32.charCodeAt(i + 3)] << 5) |
revLookup[b32.charCodeAt(i + 4)];
arr[curByte++] = (tmp >> 17) & 0xff;
arr[curByte++] = (tmp >> 9) & 0xff;
arr[curByte++] = (tmp >> 1) & 0xff;
tmp = ((tmp & 1) << 15) |
(revLookup[b32.charCodeAt(i + 5)] << 10) |
(revLookup[b32.charCodeAt(i + 6)] << 5) |
revLookup[b32.charCodeAt(i + 7)];
arr[curByte++] = (tmp >> 8) & 0xff;
arr[curByte++] = tmp & 0xff;
}
if (placeHoldersLen === 1) {
tmp = (revLookup[b32.charCodeAt(i)] << 20) |
(revLookup[b32.charCodeAt(i + 1)] << 15) |
(revLookup[b32.charCodeAt(i + 2)] << 10) |
(revLookup[b32.charCodeAt(i + 3)] << 5) |
revLookup[b32.charCodeAt(i + 4)];
arr[curByte++] = (tmp >> 17) & 0xff;
arr[curByte++] = (tmp >> 9) & 0xff;
arr[curByte++] = (tmp >> 1) & 0xff;
tmp = ((tmp & 1) << 7) |
(revLookup[b32.charCodeAt(i + 5)] << 2) |
(revLookup[b32.charCodeAt(i + 6)] >> 3);
arr[curByte++] = tmp & 0xff;
} else if (placeHoldersLen === 3) {
tmp = (revLookup[b32.charCodeAt(i)] << 19) |
(revLookup[b32.charCodeAt(i + 1)] << 14) |
(revLookup[b32.charCodeAt(i + 2)] << 9) |
(revLookup[b32.charCodeAt(i + 3)] << 4) |
(revLookup[b32.charCodeAt(i + 4)] >> 1);
arr[curByte++] = (tmp >> 16) & 0xff;
arr[curByte++] = (tmp >> 8) & 0xff;
arr[curByte++] = tmp & 0xff;
} else if (placeHoldersLen === 4) {
tmp = (revLookup[b32.charCodeAt(i)] << 11) |
(revLookup[b32.charCodeAt(i + 1)] << 6) |
(revLookup[b32.charCodeAt(i + 2)] << 1) |
(revLookup[b32.charCodeAt(i + 3)] >> 4);
arr[curByte++] = (tmp >> 8) & 0xff;
arr[curByte++] = tmp & 0xff;
} else if (placeHoldersLen === 6) {
tmp = (revLookup[b32.charCodeAt(i)] << 3) |
(revLookup[b32.charCodeAt(i + 1)] >> 2);
arr[curByte++] = tmp & 0xff;
}
return arr;
}
function encodeChunk(uint8: Uint8Array, start: number, end: number): string {
let tmp: number;
const output = [];
for (let i = start; i < end; i += 5) {
tmp = ((uint8[i] << 16) & 0xff0000) |
((uint8[i + 1] << 8) & 0xff00) |
(uint8[i + 2] & 0xff);
output.push(lookup[(tmp >> 19) & 0x1f]);
output.push(lookup[(tmp >> 14) & 0x1f]);
output.push(lookup[(tmp >> 9) & 0x1f]);
output.push(lookup[(tmp >> 4) & 0x1f]);
tmp = ((tmp & 0xf) << 16) |
((uint8[i + 3] << 8) & 0xff00) |
(uint8[i + 4] & 0xff);
output.push(lookup[(tmp >> 15) & 0x1f]);
output.push(lookup[(tmp >> 10) & 0x1f]);
output.push(lookup[(tmp >> 5) & 0x1f]);
output.push(lookup[tmp & 0x1f]);
}
return output.join("");
}
/**
* Encodes a given Uint8Array into RFC4648 base32 representation
* @param uint8
*/
export function encode(uint8: Uint8Array): string {
let tmp: number;
const len = uint8.length;
const extraBytes = len % 5;
const parts = [];
const maxChunkLength = 16385; // must be multiple of 5
const len2 = len - extraBytes;
// go through the array every 5 bytes, we'll deal with trailing stuff later
for (let i = 0; i < len2; i += maxChunkLength) {
parts.push(
encodeChunk(
uint8,
i,
i + maxChunkLength > len2 ? len2 : i + maxChunkLength,
),
);
}
// pad the end with zeros, but make sure to not forget the extra bytes
if (extraBytes === 4) {
tmp = ((uint8[len2] & 0xff) << 16) |
((uint8[len2 + 1] & 0xff) << 8) |
(uint8[len2 + 2] & 0xff);
parts.push(lookup[(tmp >> 19) & 0x1f]);
parts.push(lookup[(tmp >> 14) & 0x1f]);
parts.push(lookup[(tmp >> 9) & 0x1f]);
parts.push(lookup[(tmp >> 4) & 0x1f]);
tmp = ((tmp & 0xf) << 11) | (uint8[len2 + 3] << 3);
parts.push(lookup[(tmp >> 10) & 0x1f]);
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("=");
} else if (extraBytes === 3) {
tmp = ((uint8[len2] & 0xff) << 17) |
((uint8[len2 + 1] & 0xff) << 9) |
((uint8[len2 + 2] & 0xff) << 1);
parts.push(lookup[(tmp >> 20) & 0x1f]);
parts.push(lookup[(tmp >> 15) & 0x1f]);
parts.push(lookup[(tmp >> 10) & 0x1f]);
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("===");
} else if (extraBytes === 2) {
tmp = ((uint8[len2] & 0xff) << 12) | ((uint8[len2 + 1] & 0xff) << 4);
parts.push(lookup[(tmp >> 15) & 0x1f]);
parts.push(lookup[(tmp >> 10) & 0x1f]);
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("====");
} else if (extraBytes === 1) {
tmp = (uint8[len2] & 0xff) << 2;
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("======");
}
return parts.join("");
}

Some files were not shown because too many files have changed in this diff Show More