Skip to content

Commit

Permalink
refactor: all testing + encoding submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
nberlette committed Oct 19, 2022
1 parent 075ab98 commit fa9783d
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 172 deletions.
59 changes: 36 additions & 23 deletions encoding/base64.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

import { base64 as $base64 } from "../deps.ts";

const utf8decoder = new TextDecoder();
const utf8 = new TextDecoder();

const base64: base64 = {
encode: (data) => $base64.encode(data),
decode: (b64) => utf8decoder.decode($base64.decode(b64)),
decodeBytes: (b64) => $base64.decode(b64),
declare type base64 = {
/**
* Encodes a string or ArrayBuffer into a base64 string.
* @param data
* @returns RFC4648 base64-encoded string
*/
encode(data: ArrayBuffer | string): string;
/**
* Decodes a given RFC4648 base64-encoded string into its original value.
* @param b64
* @returns original decoded value as a Uint8Array
*/
decodeBytes(b64: string): Uint8Array;
/**
* Decodes a given RFC4648 base64-encoded string into its original value.
* @param b64
* @returns original decoded value as a string
*/
decode(b64: string): string;
};

Object.assign(globalThis, { base64 });

declare global {
interface base64 {
encode(data: ArrayBuffer | string): string;
/**
* Decodes a given RFC4648 base64 encoded string, returning a Uint8Array.
* @param b64
*/
decodeBytes(b64: string): Uint8Array;
/**
* Decodes a given RFC4648 base64 encoded string, returning a string.
* @param b64
*/
decode(b64: string): string;
}
namespace globalThis {
const base64: base64;
}
const base64: base64;
}

const base64 = Object.defineProperties<base64>({
encode: (data) => $base64.encode(data),
decode: (b64) => utf8.decode($base64.decode(b64)),
decodeBytes: (b64) => $base64.decode(b64),
}, {
encode: { enumerable: true },
decode: { enumerable: true },
decodeBytes: { enumerable: true },
[Symbol.toStringTag]: { value: "base64" },
}) as base64;

Object.assign(globalThis, { base64 });
58 changes: 33 additions & 25 deletions encoding/base64url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

import { base64url as $base64url } from "../deps.ts";
Expand All @@ -10,31 +12,37 @@ const base64url: base64url = {
decodeBytes: (b64url) => $base64url.decode(b64url),
};

Object.assign(globalThis, { base64url });
interface base64url {
/**
* Encodes a given ArrayBuffer or string into a base64url representation
* @param data
*/
encode(data: ArrayBuffer | string): string;
/**
* Converts given base64url encoded data back to original string.
* @param b64url
* @see {@link base64url.encode}
* @see {@link base64url.decodeBytes}
*/
decode(b64url: string): string;
/**
* Converts given base64url encoded data back to original TypedArray.
* @param b64url
* @see {@link base64url.encode}
* @see {@link base64url.decode}
*/
decodeBytes(b64url: string): Uint8Array;
}

declare global {
interface base64url {
/**
* Encodes a given ArrayBuffer or string into a base64url representation
* @param data
*/
encode(data: ArrayBuffer | string): string;
/**
* Converts given base64url encoded data back to original string.
* @param b64url
* @see {@link base64url.encode}
* @see {@link base64url.decodeBytes}
*/
decode(b64url: string): string;
/**
* Converts given base64url encoded data back to original TypedArray.
* @param b64url
* @see {@link base64url.encode}
* @see {@link base64url.decode}
*/
decodeBytes(b64url: string): Uint8Array;
}
namespace globalThis {
const base64url: base64url;
}
const base64url: base64url;
}

Object.assign(globalThis, {
base64url: Object.defineProperty({ ...base64url }, Symbol.toStringTag, {
value: "base64url",
writable: false,
enumerable: false,
configurable: false,
}),
});
20 changes: 17 additions & 3 deletions encoding/binary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { binary as $binary } from "../deps.ts";
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

Object.assign(globalThis, { binary: $binary });
import { binary } from "../deps.ts";
import type { Merge } from "../_internal.ts";

type binary = Merge<typeof binary>;

declare global {
const binary: typeof $binary;
const binary: binary;
}

Object.assign(globalThis, {
binary: Object.defineProperty({ ...binary }, Symbol.toStringTag, {
value: "binary",
writable: false,
enumerable: false,
configurable: false,
}),
});
20 changes: 17 additions & 3 deletions encoding/csv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { CSV as $CSV } from "../deps.ts";
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

Object.assign(globalThis, { CSV: $CSV });
import { CSV } from "../deps.ts";
import type { Merge } from "../_internal.ts";

type CSV = Merge<typeof CSV>;

declare global {
const CSV: typeof $CSV;
const CSV: CSV;
}

Object.assign(globalThis, {
CSV: Object.defineProperty<CSV>({ ...CSV }, Symbol.toStringTag, {
value: "CSV",
writable: false,
enumerable: false,
configurable: false,
}),
});
20 changes: 17 additions & 3 deletions encoding/front_matter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { front_matter as $front_matter } from "../deps.ts";
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

Object.assign(globalThis, { front_matter: $front_matter });
import { FrontMatter } from "../deps.ts";
import type { Merge } from "../_internal.ts";

type FrontMatter = Merge<typeof FrontMatter>;

declare global {
const front_matter: typeof $front_matter;
const FrontMatter: FrontMatter;
}

Object.assign(globalThis, {
FrontMatter: Object.defineProperty({ ...FrontMatter }, Symbol.toStringTag, {
value: "FrontMatter",
writable: false,
enumerable: false,
configurable: false,
}),
});
22 changes: 17 additions & 5 deletions encoding/hex.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { hex as $hex } from "../deps.ts";
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

import { hex as Hex } from "../deps.ts";
import type { Merge } from "../_internal.ts";

type Hex = Merge<typeof Hex>;

declare global {
namespace globalThis {
const hex: typeof $hex;
}
const Hex: Hex;
}

Object.assign(globalThis, { hex: $hex });
Object.assign(globalThis, {
Hex: Object.defineProperty({ ...Hex }, Symbol.toStringTag, {
value: "Hex",
writable: false,
enumerable: false,
configurable: false,
}),
});
98 changes: 93 additions & 5 deletions encoding/json5.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,97 @@
import { JSON5 as $JSON5 } from "../deps.ts";
/// <reference no-default-lib="true" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.window" />

import type {
JSONReplacerFn,
JSONReviverFn,
JSONValue,
Merge,
} from "../_internal.ts";
import { JSON5 as JSON5_MOD } from "../deps.ts";

/**
* Converts a JSON5 string into an object.
* @template T Type of return value.
* @param text A valid JSON string.
* @param reviver A function that transforms the results. This function is
* called for each member of the object. If a member contains nested objects,
* the nested objects are transformed before the parent object is.
*/
function parse<T extends any = JSONValue>(
text: string,
reviver?: JSONReviverFn,
): T {
return JSON5_MOD.parse(text, reviver);
}

/**
* Converts a JavaScript value to a JSON5 string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
function stringify(
value: JSONValue,
replacer?: JSONReplacerFn,
space?: string | number,
): string {
return JSON5_MOD.stringify(
value,
replacer,
space ?? 0,
);
}

/**
* Loads JSON5 from file synchronously
* @param path File path or url
*/
function require<T extends any = JSONValue>(
path: string | URL,
reviver?: JSONReviverFn,
): T {
const raw = Deno.readTextFileSync(path);
return parse<T>(raw, reviver);
}

/**
* Loads JSON5 from file asynchronously
* @param path File path or url
*/
async function requireAsync<T extends any = JSONValue>(
path: string | URL,
reviver?: JSONReviverFn,
): Promise<T> {
const raw = await Deno.readTextFile(path);
return parse<T>(raw, reviver);
}

// defaults
const JSON5 = { parse, stringify, require, requireAsync };

const descriptor = {
enumerable: true,
writable: false,
configurable: false,
};

Object.defineProperties(JSON5, {
parse: descriptor,
stringify: descriptor,
require: descriptor,
requireAsync: descriptor,
[Symbol.toStringTag]: {
value: "JSON5",
},
});

type JSON5 = Merge<typeof JSON5>;

declare global {
namespace globalThis {
const JSON5: typeof $JSON5;
}
const JSON5: JSON5;
}

Object.assign(globalThis, { JSON5: $JSON5 });
Object.freeze(JSON5);

Object.assign(globalThis, { JSON5 });
Loading

0 comments on commit fa9783d

Please sign in to comment.