From 41e9072d579d93ab7fdf52b168be2353c9e189ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 16:34:26 +0100 Subject: [PATCH 01/13] Restructure base64URL module --- encoding/base64url.js | 66 ------------------- encoding/base64url.jsdom.test.ts | 9 ++- encoding/base64url.json | 12 ---- encoding/base64url.md | 34 ---------- encoding/base64url.test.ts | 31 +++++---- encoding/base64url.ts | 99 ----------------------------- encoding/base64url/DecodeContext.ts | 6 ++ encoding/base64url/EncodeContext.ts | 6 ++ encoding/base64url/decode.ts | 20 ++++++ encoding/base64url/decodeBytes.ts | 10 +++ encoding/base64url/encode.ts | 25 ++++++++ encoding/base64url/encodeBytes.ts | 10 +++ encoding/base64url/fromBase64.ts | 2 + encoding/base64url/toBase64.ts | 2 + encoding/byteString/from.ts | 2 + encoding/byteString/to.ts | 2 + encoding/index.js | 5 -- encoding/index.ts | 5 -- 18 files changed, 107 insertions(+), 239 deletions(-) delete mode 100644 encoding/base64url.js delete mode 100644 encoding/base64url.json delete mode 100644 encoding/base64url.md delete mode 100644 encoding/base64url.ts create mode 100644 encoding/base64url/DecodeContext.ts create mode 100644 encoding/base64url/EncodeContext.ts create mode 100644 encoding/base64url/decode.ts create mode 100644 encoding/base64url/decodeBytes.ts create mode 100644 encoding/base64url/encode.ts create mode 100644 encoding/base64url/encodeBytes.ts create mode 100644 encoding/base64url/fromBase64.ts create mode 100644 encoding/base64url/toBase64.ts create mode 100644 encoding/byteString/from.ts create mode 100644 encoding/byteString/to.ts delete mode 100644 encoding/index.js delete mode 100644 encoding/index.ts diff --git a/encoding/base64url.js b/encoding/base64url.js deleted file mode 100644 index 6c6d1a70..00000000 --- a/encoding/base64url.js +++ /dev/null @@ -1,66 +0,0 @@ -/* eslint-env browser, node */ -const toArray = typedArray => [...typedArray]; - -export const toByteString = bytes => - bytes.map(_ => String.fromCharCode(_)).join(""); - -export const fromByteString = byteString => - [...byteString].map(_ => _.codePointAt(0) || 0); - -const ENCODING = "utf-8"; - -const btoaImplementation = ( - text, - context = typeof window !== "undefined" ? window : undefined -) => - context - ? context.btoa( - toByteString(toArray(new context.TextEncoder().encode(text))) - ) - : Buffer.from(text, ENCODING).toString("base64"); - -const atobImplementation = ( - text, - context = typeof window !== "undefined" ? window : undefined -) => - context - ? new context.TextDecoder(ENCODING).decode( - new Uint8Array(fromByteString(context.atob(text))) - ) - : Buffer.from(text, "base64").toString(ENCODING); - -export const encode = (text, context) => - btoaImplementation(text, context) - .replace(/=/g, "") - .replace(/\+/g, "-") - .replace(/\//g, "_"); - -export const decode = (text, context) => - atobImplementation(text.replace(/-/g, "+").replace(/_/g, "/"), context); - -export const toBase64Url = base64 => - base64.replace(/\+/g, "-").replace(/\//g, "_"); - -export const fromBase64Url = base64 => - base64.replace(/-/g, "+").replace(/_/g, "/"); - -export const encodeBytes = (bytes, context) => { - const sourceText = toByteString(bytes); - - return encode(sourceText, context); -}; - -export const decodeBytes = (text, context) => { - const decoded = decode(text, context); - - return fromByteString(decoded); -}; - -export default { - decode, - decodeBytes, - encode, - encodeBytes, - fromByteString, - toByteString -}; diff --git a/encoding/base64url.jsdom.test.ts b/encoding/base64url.jsdom.test.ts index 29ad1ea7..006b02e8 100644 --- a/encoding/base64url.jsdom.test.ts +++ b/encoding/base64url.jsdom.test.ts @@ -1,9 +1,8 @@ /* eslint-env jest, node */ -import { - decode, - encode - // @ts-ignore ambiguous import -} from "./base64url.ts"; +// @ts-ignore ambiguous import +import encode from "./base64url/encode.ts"; +// @ts-ignore ambiguous import +import decode from "./base64url/decode.ts"; const unicodeText = "Zombies everywhere 🧟"; diff --git a/encoding/base64url.json b/encoding/base64url.json deleted file mode 100644 index 34c52133..00000000 --- a/encoding/base64url.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "base64url", - "description": "Provides a way to encode strings and bytes from and into Base64URL.", - "signature": "{\n decode: (text: string, context?: DecodeContext) => string;\n decodeBytes: (\n text: string,\n context?: {\n atob: (byteString: string) => string;\n TextDecoder: new (encoding: string) => {\n decode: (input?: Uint8Array) => string;\n };\n }\n ) => number[];\n encode: (\n text: string,\n context?: {\n btoa: (byteString: string) => string;\n TextEncoder: new () => {\n encode: (input?: string) => Uint8Array;\n };\n }\n ) => string;\n encodeBytes: (bytes: number[], context?: EncodeContext) => string;\n fromByteString: (byteString: string) => number[];\n toByteString: (bytes: number[]) => string;\n}", - "examples": [ - { - "language": "javascript", - "content": "base64url(); // β‡’ TODO" - } - ], - "questions": ["TODO: List questions that may this function answers."] -} diff --git a/encoding/base64url.md b/encoding/base64url.md deleted file mode 100644 index 78bb2f76..00000000 --- a/encoding/base64url.md +++ /dev/null @@ -1,34 +0,0 @@ -# base64url - -Provides a way to encode strings and bytes from and into Base64URL. - -## Type signature - - -```typescript -{ - decode: (text: string, context?: DecodeContext) => string; - decodeBytes: ( - text: string, - context?: { - atob: (byteString: string) => string; - TextDecoder: new (encoding: string) => { - decode: (input?: Uint8Array) => string; - }; - } - ) => number[]; - encode: ( - text: string, - context?: { - btoa: (byteString: string) => string; - TextEncoder: new () => { - encode: (input?: string) => Uint8Array; - }; - } - ) => string; - encodeBytes: (bytes: number[], context?: EncodeContext) => string; - fromByteString: (byteString: string) => number[]; - toByteString: (bytes: number[]) => string; -} -``` - diff --git a/encoding/base64url.test.ts b/encoding/base64url.test.ts index f6a3c99c..2f4679f1 100644 --- a/encoding/base64url.test.ts +++ b/encoding/base64url.test.ts @@ -1,15 +1,20 @@ /* eslint-env jest, node */ -import { - decode, - decodeBytes, - encode, - encodeBytes, - fromBase64Url, - fromByteString, - toBase64Url, - toByteString - // @ts-ignore ambiguous import -} from "./base64url.ts"; +// @ts-ignore ambiguous import +import encode from "./base64url/encode.ts"; +// @ts-ignore ambiguous import +import decode from "./base64url/decode.ts"; +// @ts-ignore ambiguous import +import encodeBytes from "./base64url/encodeBytes.ts"; +// @ts-ignore ambiguous import +import decodeBytes from "./base64url/decodeBytes.ts"; +// @ts-ignore ambiguous import +import toBase64 from "./base64url/fromBase64.ts"; +// @ts-ignore ambiguous import +import fromBase64 from "./base64url/toBase64.ts"; +// @ts-ignore ambiguous import +import fromByteString from "./byteString/from.ts"; +// @ts-ignore ambiguous import +import toByteString from "./byteString/to.ts"; // @ts-ignore ambiguous import import range from "../array/range.ts"; @@ -99,7 +104,7 @@ describe("base64url", () => { it("converts base64 to base64URL", () => { const text = toByteString(range(256)); - expect(toBase64Url(Buffer.from(text, "utf-8").toString("base64"))).toEqual( + expect(fromBase64(Buffer.from(text, "utf-8").toString("base64"))).toEqual( encode(text) ); }); @@ -107,7 +112,7 @@ describe("base64url", () => { it("converts base64URL to base64", () => { const text = toByteString(range(256)); - expect(fromBase64Url(encode(text))).toEqual( + expect(toBase64(encode(text))).toEqual( Buffer.from(text, "utf-8").toString("base64") ); }); diff --git a/encoding/base64url.ts b/encoding/base64url.ts deleted file mode 100644 index 98c75bc4..00000000 --- a/encoding/base64url.ts +++ /dev/null @@ -1,99 +0,0 @@ -/* eslint-env browser, node */ - -export interface EncodeContext { - btoa: (byteString: string) => string; - TextEncoder: new () => { - encode: { (input?: string): Uint8Array }; - }; -} - -export interface DecodeContext { - atob: (byteString: string) => string; - TextDecoder: new (encoding: string) => { - decode: (input?: Uint8Array) => string; - }; -} - -const toArray = (typedArray: Uint8Array): number[] => [...typedArray]; - -export const toByteString = (bytes: number[]) => - bytes.map(_ => String.fromCharCode(_)).join(""); - -export const fromByteString = (byteString: string): number[] => - [...byteString].map(_ => _.codePointAt(0) || 0); - -const ENCODING = "utf-8"; - -const btoaImplementation = ( - text: string, - context: EncodeContext | undefined = typeof window !== "undefined" - ? window - : undefined -) => - context - ? context.btoa( - toByteString(toArray(new context.TextEncoder().encode(text))) - ) - : Buffer.from(text, ENCODING).toString("base64"); - -const atobImplementation = ( - text: string, - context: DecodeContext | undefined = typeof window !== "undefined" - ? window - : undefined -) => - context - ? new context.TextDecoder(ENCODING).decode( - new Uint8Array(fromByteString(context.atob(text))) - ) - : Buffer.from(text, "base64").toString(ENCODING); - -export const encode = ( - text: string, - context?: { - btoa: (byteString: string) => string; - TextEncoder: new () => { encode: (input?: string) => Uint8Array }; - } -) => - btoaImplementation(text, context) - .replace(/=/g, "") - .replace(/\+/g, "-") - .replace(/\//g, "_"); - -export const decode = (text: string, context?: DecodeContext) => - atobImplementation(text.replace(/-/g, "+").replace(/_/g, "/"), context); - -export const toBase64Url = (base64: string) => - base64.replace(/\+/g, "-").replace(/\//g, "_"); - -export const fromBase64Url = (base64: string) => - base64.replace(/-/g, "+").replace(/_/g, "/"); - -export const encodeBytes = (bytes: number[], context?: EncodeContext) => { - const sourceText = toByteString(bytes); - - return encode(sourceText, context); -}; - -export const decodeBytes = ( - text: string, - context?: { - atob: (byteString: string) => string; - TextDecoder: new (encoding: string) => { - decode: (input?: Uint8Array) => string; - }; - } -) => { - const decoded = decode(text, context); - - return fromByteString(decoded); -}; - -export default { - decode, - decodeBytes, - encode, - encodeBytes, - fromByteString, - toByteString -}; diff --git a/encoding/base64url/DecodeContext.ts b/encoding/base64url/DecodeContext.ts new file mode 100644 index 00000000..a477cedd --- /dev/null +++ b/encoding/base64url/DecodeContext.ts @@ -0,0 +1,6 @@ +export interface DecodeContext { + atob: (byteString: string) => string; + TextDecoder: new (encoding: string) => { + decode: (input?: Uint8Array) => string; + }; +} diff --git a/encoding/base64url/EncodeContext.ts b/encoding/base64url/EncodeContext.ts new file mode 100644 index 00000000..fa791926 --- /dev/null +++ b/encoding/base64url/EncodeContext.ts @@ -0,0 +1,6 @@ +export interface EncodeContext { + btoa: (byteString: string) => string; + TextEncoder: new () => { + encode: { (input?: string): Uint8Array }; + }; +} diff --git a/encoding/base64url/decode.ts b/encoding/base64url/decode.ts new file mode 100644 index 00000000..225da26b --- /dev/null +++ b/encoding/base64url/decode.ts @@ -0,0 +1,20 @@ +/* eslint-env browser, node */ +import fromByteString from "../byteString/from"; +import { DecodeContext } from "./DecodeContext"; + +const ENCODING = "utf-8"; + +const atobImplementation = ( + text: string, + context: DecodeContext | undefined = typeof window !== "undefined" + ? window + : undefined +) => + context + ? new context.TextDecoder(ENCODING).decode( + new Uint8Array(fromByteString(context.atob(text))) + ) + : Buffer.from(text, "base64").toString(ENCODING); + +export default (text: string, context?: DecodeContext) => + atobImplementation(text.replace(/-/g, "+").replace(/_/g, "/"), context); diff --git a/encoding/base64url/decodeBytes.ts b/encoding/base64url/decodeBytes.ts new file mode 100644 index 00000000..d2e1bc50 --- /dev/null +++ b/encoding/base64url/decodeBytes.ts @@ -0,0 +1,10 @@ +/* eslint-env browser, node */ +import decode from "./decode"; +import fromByteString from "../byteString/from"; +import { DecodeContext } from "./DecodeContext"; + +export const decodeBytes = (text: string, context?: DecodeContext) => { + const decoded = decode(text, context); + + return fromByteString(decoded); +}; diff --git a/encoding/base64url/encode.ts b/encoding/base64url/encode.ts new file mode 100644 index 00000000..aa63cc00 --- /dev/null +++ b/encoding/base64url/encode.ts @@ -0,0 +1,25 @@ +/* eslint-env browser, node */ +import toByteString from "../byteString/to"; +import { EncodeContext } from "./EncodeContext"; + +const toArray = (typedArray: Uint8Array): number[] => [...typedArray]; + +const ENCODING = "utf-8"; + +const btoaImplementation = ( + text: string, + context: EncodeContext | undefined = typeof window !== "undefined" + ? window + : undefined +) => + context + ? context.btoa( + toByteString(toArray(new context.TextEncoder().encode(text))) + ) + : Buffer.from(text, ENCODING).toString("base64"); + +export default (text: string, context?: EncodeContext) => + btoaImplementation(text, context) + .replace(/=/g, "") + .replace(/\+/g, "-") + .replace(/\//g, "_"); diff --git a/encoding/base64url/encodeBytes.ts b/encoding/base64url/encodeBytes.ts new file mode 100644 index 00000000..53bb08e9 --- /dev/null +++ b/encoding/base64url/encodeBytes.ts @@ -0,0 +1,10 @@ +/* eslint-env browser, node */ +import encode from "./encode"; +import toByteString from "../byteString/to"; +import { EncodeContext } from "./EncodeContext"; + +export const encodeBytes = (bytes: number[], context?: EncodeContext) => { + const sourceText = toByteString(bytes); + + return encode(sourceText, context); +}; diff --git a/encoding/base64url/fromBase64.ts b/encoding/base64url/fromBase64.ts new file mode 100644 index 00000000..2c5e41cd --- /dev/null +++ b/encoding/base64url/fromBase64.ts @@ -0,0 +1,2 @@ +export default (base64: string) => + base64.replace(/\+/g, "-").replace(/\//g, "_"); diff --git a/encoding/base64url/toBase64.ts b/encoding/base64url/toBase64.ts new file mode 100644 index 00000000..9e980362 --- /dev/null +++ b/encoding/base64url/toBase64.ts @@ -0,0 +1,2 @@ +export default (base64Url: string) => + base64Url.replace(/-/g, "+").replace(/_/g, "/"); diff --git a/encoding/byteString/from.ts b/encoding/byteString/from.ts new file mode 100644 index 00000000..c0ebd1b3 --- /dev/null +++ b/encoding/byteString/from.ts @@ -0,0 +1,2 @@ +export default (byteString: string): number[] => + [...byteString].map(_ => _.codePointAt(0) || 0); diff --git a/encoding/byteString/to.ts b/encoding/byteString/to.ts new file mode 100644 index 00000000..7066f842 --- /dev/null +++ b/encoding/byteString/to.ts @@ -0,0 +1,2 @@ +export default (bytes: number[]) => + bytes.map(_ => String.fromCharCode(_)).join(""); diff --git a/encoding/index.js b/encoding/index.js deleted file mode 100644 index 78553ab2..00000000 --- a/encoding/index.js +++ /dev/null @@ -1,5 +0,0 @@ -import base64url from "./base64url.js"; - -export { base64url }; - -export default { base64url }; diff --git a/encoding/index.ts b/encoding/index.ts deleted file mode 100644 index 347392d6..00000000 --- a/encoding/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import base64url from "./base64url"; - -export { base64url }; - -export default { base64url }; From 4b449e09a65208b8dea1e733b70c24274ce9907f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 17:43:19 +0100 Subject: [PATCH 02/13] Disable conficting eslint rule for now --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index c058c631..d31ecc3a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,6 +12,7 @@ module.exports = { rules: { "arrow-parens": ["error", "as-needed"], "no-sparse-arrays": 0, + "no-unused-vars": 0, "padding-line-between-statements": [ "error", { blankLine: "always", prev: "*", next: "export" }, From dcbe502681a18a038a1d2891a38f9882e5452998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 17:43:47 +0100 Subject: [PATCH 03/13] Enable custom interfaces to be commited --- .gitignore | 1 + encoding/base64url/DecodeContext.interface.d.ts | 6 ++++++ encoding/base64url/EncodeContext.interface.d.ts | 6 ++++++ 3 files changed, 13 insertions(+) create mode 100644 encoding/base64url/DecodeContext.interface.d.ts create mode 100644 encoding/base64url/EncodeContext.interface.d.ts diff --git a/.gitignore b/.gitignore index 8ea881d8..60b08df6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ index.umd.js **/*.d.ts .coverage docs/dist +!*.interface.d.ts diff --git a/encoding/base64url/DecodeContext.interface.d.ts b/encoding/base64url/DecodeContext.interface.d.ts new file mode 100644 index 00000000..a477cedd --- /dev/null +++ b/encoding/base64url/DecodeContext.interface.d.ts @@ -0,0 +1,6 @@ +export interface DecodeContext { + atob: (byteString: string) => string; + TextDecoder: new (encoding: string) => { + decode: (input?: Uint8Array) => string; + }; +} diff --git a/encoding/base64url/EncodeContext.interface.d.ts b/encoding/base64url/EncodeContext.interface.d.ts new file mode 100644 index 00000000..fa791926 --- /dev/null +++ b/encoding/base64url/EncodeContext.interface.d.ts @@ -0,0 +1,6 @@ +export interface EncodeContext { + btoa: (byteString: string) => string; + TextEncoder: new () => { + encode: { (input?: string): Uint8Array }; + }; +} From e316d61042849f749403c50086beb829a580b550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 17:46:00 +0100 Subject: [PATCH 04/13] Adjust interface imports and remove eslint false positive skip comments --- docs/scripts/docs.js | 2 -- encoding/base64url/DecodeContext.ts | 6 ------ encoding/base64url/EncodeContext.ts | 6 ------ encoding/base64url/decode.ts | 2 +- encoding/base64url/decodeBytes.ts | 4 ++-- encoding/base64url/encode.ts | 2 +- encoding/base64url/encodeBytes.ts | 4 ++-- 7 files changed, 6 insertions(+), 20 deletions(-) delete mode 100644 encoding/base64url/DecodeContext.ts delete mode 100644 encoding/base64url/EncodeContext.ts diff --git a/docs/scripts/docs.js b/docs/scripts/docs.js index 8f2ad7e2..713716f1 100644 --- a/docs/scripts/docs.js +++ b/docs/scripts/docs.js @@ -1,6 +1,5 @@ /* eslint-env browser */ -// eslint-disable-next-line no-unused-vars function tryInREPL(event, scope) { var target = event.target; var isReplRun = target.matches(".btn-repl"); @@ -25,7 +24,6 @@ function tryInREPL(event, scope) { }); } -// eslint-disable-next-line no-unused-vars function toggleTableOfContents() { var className = "toc-active"; diff --git a/encoding/base64url/DecodeContext.ts b/encoding/base64url/DecodeContext.ts deleted file mode 100644 index a477cedd..00000000 --- a/encoding/base64url/DecodeContext.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface DecodeContext { - atob: (byteString: string) => string; - TextDecoder: new (encoding: string) => { - decode: (input?: Uint8Array) => string; - }; -} diff --git a/encoding/base64url/EncodeContext.ts b/encoding/base64url/EncodeContext.ts deleted file mode 100644 index fa791926..00000000 --- a/encoding/base64url/EncodeContext.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface EncodeContext { - btoa: (byteString: string) => string; - TextEncoder: new () => { - encode: { (input?: string): Uint8Array }; - }; -} diff --git a/encoding/base64url/decode.ts b/encoding/base64url/decode.ts index 225da26b..a6de6290 100644 --- a/encoding/base64url/decode.ts +++ b/encoding/base64url/decode.ts @@ -1,6 +1,6 @@ /* eslint-env browser, node */ import fromByteString from "../byteString/from"; -import { DecodeContext } from "./DecodeContext"; +import { DecodeContext } from "./DecodeContext.interface"; const ENCODING = "utf-8"; diff --git a/encoding/base64url/decodeBytes.ts b/encoding/base64url/decodeBytes.ts index d2e1bc50..249ab5ae 100644 --- a/encoding/base64url/decodeBytes.ts +++ b/encoding/base64url/decodeBytes.ts @@ -1,9 +1,9 @@ /* eslint-env browser, node */ import decode from "./decode"; import fromByteString from "../byteString/from"; -import { DecodeContext } from "./DecodeContext"; +import { DecodeContext } from "./DecodeContext.interface"; -export const decodeBytes = (text: string, context?: DecodeContext) => { +export default (text: string, context?: DecodeContext) => { const decoded = decode(text, context); return fromByteString(decoded); diff --git a/encoding/base64url/encode.ts b/encoding/base64url/encode.ts index aa63cc00..d32c8285 100644 --- a/encoding/base64url/encode.ts +++ b/encoding/base64url/encode.ts @@ -1,6 +1,6 @@ /* eslint-env browser, node */ import toByteString from "../byteString/to"; -import { EncodeContext } from "./EncodeContext"; +import { EncodeContext } from "./EncodeContext.interface"; const toArray = (typedArray: Uint8Array): number[] => [...typedArray]; diff --git a/encoding/base64url/encodeBytes.ts b/encoding/base64url/encodeBytes.ts index 53bb08e9..833e1e8c 100644 --- a/encoding/base64url/encodeBytes.ts +++ b/encoding/base64url/encodeBytes.ts @@ -1,9 +1,9 @@ /* eslint-env browser, node */ import encode from "./encode"; import toByteString from "../byteString/to"; -import { EncodeContext } from "./EncodeContext"; +import { EncodeContext } from "./EncodeContext.interface"; -export const encodeBytes = (bytes: number[], context?: EncodeContext) => { +export default (bytes: number[], context?: EncodeContext) => { const sourceText = toByteString(bytes); return encode(sourceText, context); From 6ce16718a68ae77bf4c41c91ea9e5c04e21eeae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 17:50:30 +0100 Subject: [PATCH 05/13] Generalize reserved keyword name mappings --- .npmignore | 1 + document.js | 7 ++----- ignore.js | 1 + indexReadme.js | 7 ++----- mappings.js | 4 ++++ regenerate.js | 7 ++----- 6 files changed, 12 insertions(+), 15 deletions(-) create mode 100644 mappings.js diff --git a/.npmignore b/.npmignore index b48a1bc6..843307fb 100644 --- a/.npmignore +++ b/.npmignore @@ -21,6 +21,7 @@ document.js ignore.js indexReadme.js jest.config.js +mappings.js normalizeLineEndings.js package-lock.json regenerate.js diff --git a/document.js b/document.js index 28fce76b..d8e97902 100644 --- a/document.js +++ b/document.js @@ -4,6 +4,7 @@ import { promises, existsSync } from "fs"; import path from "path"; import ignored from "./ignore.js"; +import mappings from "./mappings.js"; const template = ({ name, description, signature, examples, questions }) => { let content = `# ${name} @@ -82,11 +83,7 @@ const { const [, , cwd = process.cwd()] = process.argv; -const mapping = { - function: "_function" -}; - -const identifier = name => mapping[name] || name; +const identifier = name => mappings[name] || name; // Do not match type definition files *.d.ts but match *.ts: // https://stackoverflow.com/a/43493203/1384679 diff --git a/ignore.js b/ignore.js index a45af36b..7a1e106f 100644 --- a/ignore.js +++ b/ignore.js @@ -21,6 +21,7 @@ const ignoredFiles = [ "indexReadme.js", "jest.config.js", "LICENSE", + "mappings.js", "normalizeLineEndings.js", "package-lock.json", "package.json", diff --git a/indexReadme.js b/indexReadme.js index 546bf64b..1a6feb2f 100644 --- a/indexReadme.js +++ b/indexReadme.js @@ -4,6 +4,7 @@ import { promises, existsSync } from "fs"; import path from "path"; import ignored from "./ignore.js"; +import mappings from "./mappings.js"; const [ignoredFiles, ignoredDirectories] = ignored; @@ -15,11 +16,7 @@ const { const [, , cwd = process.cwd()] = process.argv; -const mapping = { - function: "_function" -}; - -const identifier = name => mapping[name] || name; +const identifier = name => mappings[name] || name; // Do not match type definition files *.d.ts but match *.ts: // https://stackoverflow.com/a/43493203/1384679 diff --git a/mappings.js b/mappings.js new file mode 100644 index 00000000..bdf8100f --- /dev/null +++ b/mappings.js @@ -0,0 +1,4 @@ +export default { + from: "_from", + function: "_function" +}; diff --git a/regenerate.js b/regenerate.js index 99391995..2cab411c 100644 --- a/regenerate.js +++ b/regenerate.js @@ -4,6 +4,7 @@ import { promises } from "fs"; import path from "path"; import ignored from "./ignore.js"; +import mappings from "./mappings.js"; const [ignoredFiles, ignoredDirectories] = ignored; @@ -11,11 +12,7 @@ const { readdir: readDirectoryAsync, writeFile: writeFileAsync } = promises; const [, , cwd = process.cwd()] = process.argv; -const mapping = { - function: "_function" -}; - -const identifier = name => mapping[name] || name; +const identifier = name => mappings[name] || name; // Do not match type definition files *.d.ts but match *.ts: // https://stackoverflow.com/a/43493203/1384679 From 1f4fde87472ce1d2d7aaf29292a63056ba0dd13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 18:03:55 +0100 Subject: [PATCH 06/13] Regenerate code for base64URL module --- README.md | 101 +++++++++++++++++++------- encoding/README.md | 109 ++++++++++++++++++++-------- encoding/base64url/README.md | 59 +++++++++++++++ encoding/base64url/decode.js | 17 +++++ encoding/base64url/decode.json | 12 +++ encoding/base64url/decode.md | 9 +++ encoding/base64url/decodeBytes.js | 9 +++ encoding/base64url/decodeBytes.json | 12 +++ encoding/base64url/decodeBytes.md | 9 +++ encoding/base64url/encode.js | 21 ++++++ encoding/base64url/encode.json | 12 +++ encoding/base64url/encode.md | 9 +++ encoding/base64url/encodeBytes.js | 9 +++ encoding/base64url/encodeBytes.json | 12 +++ encoding/base64url/encodeBytes.md | 9 +++ encoding/base64url/fromBase64.js | 1 + encoding/base64url/fromBase64.json | 12 +++ encoding/base64url/fromBase64.md | 9 +++ encoding/base64url/index.js | 17 +++++ encoding/base64url/index.ts | 17 +++++ encoding/base64url/toBase64.js | 1 + encoding/base64url/toBase64.json | 12 +++ encoding/base64url/toBase64.md | 9 +++ encoding/byteString/README.md | 19 +++++ encoding/byteString/from.js | 1 + encoding/byteString/from.json | 12 +++ encoding/byteString/from.md | 9 +++ encoding/byteString/index.js | 6 ++ encoding/byteString/index.ts | 6 ++ encoding/byteString/to.js | 1 + encoding/byteString/to.json | 12 +++ encoding/byteString/to.md | 9 +++ encoding/index.js | 6 ++ encoding/index.ts | 6 ++ 34 files changed, 518 insertions(+), 56 deletions(-) create mode 100644 encoding/base64url/README.md create mode 100644 encoding/base64url/decode.js create mode 100644 encoding/base64url/decode.json create mode 100644 encoding/base64url/decode.md create mode 100644 encoding/base64url/decodeBytes.js create mode 100644 encoding/base64url/decodeBytes.json create mode 100644 encoding/base64url/decodeBytes.md create mode 100644 encoding/base64url/encode.js create mode 100644 encoding/base64url/encode.json create mode 100644 encoding/base64url/encode.md create mode 100644 encoding/base64url/encodeBytes.js create mode 100644 encoding/base64url/encodeBytes.json create mode 100644 encoding/base64url/encodeBytes.md create mode 100644 encoding/base64url/fromBase64.js create mode 100644 encoding/base64url/fromBase64.json create mode 100644 encoding/base64url/fromBase64.md create mode 100644 encoding/base64url/index.js create mode 100644 encoding/base64url/index.ts create mode 100644 encoding/base64url/toBase64.js create mode 100644 encoding/base64url/toBase64.json create mode 100644 encoding/base64url/toBase64.md create mode 100644 encoding/byteString/README.md create mode 100644 encoding/byteString/from.js create mode 100644 encoding/byteString/from.json create mode 100644 encoding/byteString/from.md create mode 100644 encoding/byteString/index.js create mode 100644 encoding/byteString/index.ts create mode 100644 encoding/byteString/to.js create mode 100644 encoding/byteString/to.json create mode 100644 encoding/byteString/to.md create mode 100644 encoding/index.js create mode 100644 encoding/index.ts diff --git a/README.md b/README.md index 3a77485d..5fa2a371 100644 --- a/README.md +++ b/README.md @@ -1113,36 +1113,85 @@ Computes a difference between two objects. #### base64url -Provides a way to encode strings and bytes from and into Base64URL. +##### decode -##### Type signature +###### Type signature ```typescript -{ - decode: (text: string, context?: DecodeContext) => string; - decodeBytes: ( - text: string, - context?: { - atob: (byteString: string) => string; - TextDecoder: new (encoding: string) => { - decode: (input?: Uint8Array) => string; - }; - } - ) => number[]; - encode: ( - text: string, - context?: { - btoa: (byteString: string) => string; - TextEncoder: new () => { - encode: (input?: string) => Uint8Array; - }; - } - ) => string; - encodeBytes: (bytes: number[], context?: EncodeContext) => string; - fromByteString: (byteString: string) => number[]; - toByteString: (bytes: number[]) => string; -} +(text: string, context?: DecodeContext) => string +``` + + +##### decodeBytes + +###### Type signature + + +```typescript +(text: string, context?: DecodeContext) => number[] +``` + + +##### encode + +###### Type signature + + +```typescript +(text: string, context?: EncodeContext) => string +``` + + +##### encodeBytes + +###### Type signature + + +```typescript +(bytes: number[], context?: EncodeContext) => string +``` + + +##### fromBase64 + +###### Type signature + + +```typescript +(base64: string) => string +``` + + +##### toBase64 + +###### Type signature + + +```typescript +(base64Url: string) => string +``` + + +#### byteString + +##### from + +###### Type signature + + +```typescript +(byteString: string) => number[] +``` + + +##### to + +###### Type signature + + +```typescript +(bytes: number[]) => string ``` diff --git a/encoding/README.md b/encoding/README.md index 78bb2f76..19ea0f6e 100644 --- a/encoding/README.md +++ b/encoding/README.md @@ -1,34 +1,83 @@ # base64url -Provides a way to encode strings and bytes from and into Base64URL. - -## Type signature - - -```typescript -{ - decode: (text: string, context?: DecodeContext) => string; - decodeBytes: ( - text: string, - context?: { - atob: (byteString: string) => string; - TextDecoder: new (encoding: string) => { - decode: (input?: Uint8Array) => string; - }; - } - ) => number[]; - encode: ( - text: string, - context?: { - btoa: (byteString: string) => string; - TextEncoder: new () => { - encode: (input?: string) => Uint8Array; - }; - } - ) => string; - encodeBytes: (bytes: number[], context?: EncodeContext) => string; - fromByteString: (byteString: string) => number[]; - toByteString: (bytes: number[]) => string; -} +## decode + +### Type signature + + +```typescript +(text: string, context?: DecodeContext) => string +``` + + +## decodeBytes + +### Type signature + + +```typescript +(text: string, context?: DecodeContext) => number[] +``` + + +## encode + +### Type signature + + +```typescript +(text: string, context?: EncodeContext) => string +``` + + +## encodeBytes + +### Type signature + + +```typescript +(bytes: number[], context?: EncodeContext) => string +``` + + +## fromBase64 + +### Type signature + + +```typescript +(base64: string) => string +``` + + +## toBase64 + +### Type signature + + +```typescript +(base64Url: string) => string +``` + + +# byteString + +## from + +### Type signature + + +```typescript +(byteString: string) => number[] +``` + + +## to + +### Type signature + + +```typescript +(bytes: number[]) => string ``` diff --git a/encoding/base64url/README.md b/encoding/base64url/README.md new file mode 100644 index 00000000..4dbe4908 --- /dev/null +++ b/encoding/base64url/README.md @@ -0,0 +1,59 @@ +# decode + +## Type signature + + +```typescript +(text: string, context?: DecodeContext) => string +``` + + +# decodeBytes + +## Type signature + + +```typescript +(text: string, context?: DecodeContext) => number[] +``` + + +# encode + +## Type signature + + +```typescript +(text: string, context?: EncodeContext) => string +``` + + +# encodeBytes + +## Type signature + + +```typescript +(bytes: number[], context?: EncodeContext) => string +``` + + +# fromBase64 + +## Type signature + + +```typescript +(base64: string) => string +``` + + +# toBase64 + +## Type signature + + +```typescript +(base64Url: string) => string +``` + diff --git a/encoding/base64url/decode.js b/encoding/base64url/decode.js new file mode 100644 index 00000000..4c4f663a --- /dev/null +++ b/encoding/base64url/decode.js @@ -0,0 +1,17 @@ +/* eslint-env browser, node */ +import fromByteString from "../byteString/from.js"; + +const ENCODING = "utf-8"; + +const atobImplementation = ( + text, + context = typeof window !== "undefined" ? window : undefined +) => + context + ? new context.TextDecoder(ENCODING).decode( + new Uint8Array(fromByteString(context.atob(text))) + ) + : Buffer.from(text, "base64").toString(ENCODING); + +export default (text, context) => + atobImplementation(text.replace(/-/g, "+").replace(/_/g, "/"), context); diff --git a/encoding/base64url/decode.json b/encoding/base64url/decode.json new file mode 100644 index 00000000..49e95fdb --- /dev/null +++ b/encoding/base64url/decode.json @@ -0,0 +1,12 @@ +{ + "name": "decode", + "description": "TODO: Fill short description here.", + "signature": "(text: string, context?: DecodeContext) => string", + "examples": [ + { + "language": "javascript", + "content": "decode(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/base64url/decode.md b/encoding/base64url/decode.md new file mode 100644 index 00000000..98e9faa8 --- /dev/null +++ b/encoding/base64url/decode.md @@ -0,0 +1,9 @@ +# decode + +## Type signature + + +```typescript +(text: string, context?: DecodeContext) => string +``` + diff --git a/encoding/base64url/decodeBytes.js b/encoding/base64url/decodeBytes.js new file mode 100644 index 00000000..7328e957 --- /dev/null +++ b/encoding/base64url/decodeBytes.js @@ -0,0 +1,9 @@ +/* eslint-env browser, node */ +import decode from "./decode.js"; +import fromByteString from "../byteString/from.js"; + +export default (text, context) => { + const decoded = decode(text, context); + + return fromByteString(decoded); +}; diff --git a/encoding/base64url/decodeBytes.json b/encoding/base64url/decodeBytes.json new file mode 100644 index 00000000..a14a51ef --- /dev/null +++ b/encoding/base64url/decodeBytes.json @@ -0,0 +1,12 @@ +{ + "name": "decodeBytes", + "description": "TODO: Fill short description here.", + "signature": "(text: string, context?: DecodeContext) => number[]", + "examples": [ + { + "language": "javascript", + "content": "decodeBytes(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/base64url/decodeBytes.md b/encoding/base64url/decodeBytes.md new file mode 100644 index 00000000..f7b8e8e7 --- /dev/null +++ b/encoding/base64url/decodeBytes.md @@ -0,0 +1,9 @@ +# decodeBytes + +## Type signature + + +```typescript +(text: string, context?: DecodeContext) => number[] +``` + diff --git a/encoding/base64url/encode.js b/encoding/base64url/encode.js new file mode 100644 index 00000000..7876e8a8 --- /dev/null +++ b/encoding/base64url/encode.js @@ -0,0 +1,21 @@ +/* eslint-env browser, node */ +import toByteString from "../byteString/to.js"; + +const toArray = typedArray => [...typedArray]; +const ENCODING = "utf-8"; + +const btoaImplementation = ( + text, + context = typeof window !== "undefined" ? window : undefined +) => + context + ? context.btoa( + toByteString(toArray(new context.TextEncoder().encode(text))) + ) + : Buffer.from(text, ENCODING).toString("base64"); + +export default (text, context) => + btoaImplementation(text, context) + .replace(/=/g, "") + .replace(/\+/g, "-") + .replace(/\//g, "_"); diff --git a/encoding/base64url/encode.json b/encoding/base64url/encode.json new file mode 100644 index 00000000..a65e7eae --- /dev/null +++ b/encoding/base64url/encode.json @@ -0,0 +1,12 @@ +{ + "name": "encode", + "description": "TODO: Fill short description here.", + "signature": "(text: string, context?: EncodeContext) => string", + "examples": [ + { + "language": "javascript", + "content": "encode(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/base64url/encode.md b/encoding/base64url/encode.md new file mode 100644 index 00000000..4ecd5deb --- /dev/null +++ b/encoding/base64url/encode.md @@ -0,0 +1,9 @@ +# encode + +## Type signature + + +```typescript +(text: string, context?: EncodeContext) => string +``` + diff --git a/encoding/base64url/encodeBytes.js b/encoding/base64url/encodeBytes.js new file mode 100644 index 00000000..e4935e7b --- /dev/null +++ b/encoding/base64url/encodeBytes.js @@ -0,0 +1,9 @@ +/* eslint-env browser, node */ +import encode from "./encode.js"; +import toByteString from "../byteString/to.js"; + +export default (bytes, context) => { + const sourceText = toByteString(bytes); + + return encode(sourceText, context); +}; diff --git a/encoding/base64url/encodeBytes.json b/encoding/base64url/encodeBytes.json new file mode 100644 index 00000000..08aeb97e --- /dev/null +++ b/encoding/base64url/encodeBytes.json @@ -0,0 +1,12 @@ +{ + "name": "encodeBytes", + "description": "TODO: Fill short description here.", + "signature": "(bytes: number[], context?: EncodeContext) => string", + "examples": [ + { + "language": "javascript", + "content": "encodeBytes(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/base64url/encodeBytes.md b/encoding/base64url/encodeBytes.md new file mode 100644 index 00000000..3d1cd4de --- /dev/null +++ b/encoding/base64url/encodeBytes.md @@ -0,0 +1,9 @@ +# encodeBytes + +## Type signature + + +```typescript +(bytes: number[], context?: EncodeContext) => string +``` + diff --git a/encoding/base64url/fromBase64.js b/encoding/base64url/fromBase64.js new file mode 100644 index 00000000..fe45357d --- /dev/null +++ b/encoding/base64url/fromBase64.js @@ -0,0 +1 @@ +export default base64 => base64.replace(/\+/g, "-").replace(/\//g, "_"); diff --git a/encoding/base64url/fromBase64.json b/encoding/base64url/fromBase64.json new file mode 100644 index 00000000..6c36d5ca --- /dev/null +++ b/encoding/base64url/fromBase64.json @@ -0,0 +1,12 @@ +{ + "name": "fromBase64", + "description": "TODO: Fill short description here.", + "signature": "(base64: string) => string", + "examples": [ + { + "language": "javascript", + "content": "fromBase64(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/base64url/fromBase64.md b/encoding/base64url/fromBase64.md new file mode 100644 index 00000000..c8c7be7d --- /dev/null +++ b/encoding/base64url/fromBase64.md @@ -0,0 +1,9 @@ +# fromBase64 + +## Type signature + + +```typescript +(base64: string) => string +``` + diff --git a/encoding/base64url/index.js b/encoding/base64url/index.js new file mode 100644 index 00000000..5b34a5d6 --- /dev/null +++ b/encoding/base64url/index.js @@ -0,0 +1,17 @@ +import decode from "./decode.js"; +import decodeBytes from "./decodeBytes.js"; +import encode from "./encode.js"; +import encodeBytes from "./encodeBytes.js"; +import fromBase64 from "./fromBase64.js"; +import toBase64 from "./toBase64.js"; + +export { decode, decodeBytes, encode, encodeBytes, fromBase64, toBase64 }; + +export default { + decode, + decodeBytes, + encode, + encodeBytes, + fromBase64, + toBase64 +}; diff --git a/encoding/base64url/index.ts b/encoding/base64url/index.ts new file mode 100644 index 00000000..51e49552 --- /dev/null +++ b/encoding/base64url/index.ts @@ -0,0 +1,17 @@ +import decode from "./decode"; +import decodeBytes from "./decodeBytes"; +import encode from "./encode"; +import encodeBytes from "./encodeBytes"; +import fromBase64 from "./fromBase64"; +import toBase64 from "./toBase64"; + +export { decode, decodeBytes, encode, encodeBytes, fromBase64, toBase64 }; + +export default { + decode, + decodeBytes, + encode, + encodeBytes, + fromBase64, + toBase64 +}; diff --git a/encoding/base64url/toBase64.js b/encoding/base64url/toBase64.js new file mode 100644 index 00000000..a2f668d2 --- /dev/null +++ b/encoding/base64url/toBase64.js @@ -0,0 +1 @@ +export default base64Url => base64Url.replace(/-/g, "+").replace(/_/g, "/"); diff --git a/encoding/base64url/toBase64.json b/encoding/base64url/toBase64.json new file mode 100644 index 00000000..96d766ee --- /dev/null +++ b/encoding/base64url/toBase64.json @@ -0,0 +1,12 @@ +{ + "name": "toBase64", + "description": "TODO: Fill short description here.", + "signature": "(base64Url: string) => string", + "examples": [ + { + "language": "javascript", + "content": "toBase64(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/base64url/toBase64.md b/encoding/base64url/toBase64.md new file mode 100644 index 00000000..b01b09d2 --- /dev/null +++ b/encoding/base64url/toBase64.md @@ -0,0 +1,9 @@ +# toBase64 + +## Type signature + + +```typescript +(base64Url: string) => string +``` + diff --git a/encoding/byteString/README.md b/encoding/byteString/README.md new file mode 100644 index 00000000..ad182df4 --- /dev/null +++ b/encoding/byteString/README.md @@ -0,0 +1,19 @@ +# from + +## Type signature + + +```typescript +(byteString: string) => number[] +``` + + +# to + +## Type signature + + +```typescript +(bytes: number[]) => string +``` + diff --git a/encoding/byteString/from.js b/encoding/byteString/from.js new file mode 100644 index 00000000..dac94828 --- /dev/null +++ b/encoding/byteString/from.js @@ -0,0 +1 @@ +export default byteString => [...byteString].map(_ => _.codePointAt(0) || 0); diff --git a/encoding/byteString/from.json b/encoding/byteString/from.json new file mode 100644 index 00000000..fbd0d1a4 --- /dev/null +++ b/encoding/byteString/from.json @@ -0,0 +1,12 @@ +{ + "name": "from", + "description": "TODO: Fill short description here.", + "signature": "(byteString: string) => number[]", + "examples": [ + { + "language": "javascript", + "content": "from(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/byteString/from.md b/encoding/byteString/from.md new file mode 100644 index 00000000..3fa5afe1 --- /dev/null +++ b/encoding/byteString/from.md @@ -0,0 +1,9 @@ +# from + +## Type signature + + +```typescript +(byteString: string) => number[] +``` + diff --git a/encoding/byteString/index.js b/encoding/byteString/index.js new file mode 100644 index 00000000..8d0e9003 --- /dev/null +++ b/encoding/byteString/index.js @@ -0,0 +1,6 @@ +import _from from "./from.js"; +import to from "./to.js"; + +export { _from, to }; + +export default { _from, to }; diff --git a/encoding/byteString/index.ts b/encoding/byteString/index.ts new file mode 100644 index 00000000..0efce719 --- /dev/null +++ b/encoding/byteString/index.ts @@ -0,0 +1,6 @@ +import _from from "./from"; +import to from "./to"; + +export { _from, to }; + +export default { _from, to }; diff --git a/encoding/byteString/to.js b/encoding/byteString/to.js new file mode 100644 index 00000000..b4a4302d --- /dev/null +++ b/encoding/byteString/to.js @@ -0,0 +1 @@ +export default bytes => bytes.map(_ => String.fromCharCode(_)).join(""); diff --git a/encoding/byteString/to.json b/encoding/byteString/to.json new file mode 100644 index 00000000..8adc1bc3 --- /dev/null +++ b/encoding/byteString/to.json @@ -0,0 +1,12 @@ +{ + "name": "to", + "description": "TODO: Fill short description here.", + "signature": "(bytes: number[]) => string", + "examples": [ + { + "language": "javascript", + "content": "to(); // β‡’ TODO" + } + ], + "questions": ["TODO: List questions that may this function answers."] +} diff --git a/encoding/byteString/to.md b/encoding/byteString/to.md new file mode 100644 index 00000000..df2cf7a9 --- /dev/null +++ b/encoding/byteString/to.md @@ -0,0 +1,9 @@ +# to + +## Type signature + + +```typescript +(bytes: number[]) => string +``` + diff --git a/encoding/index.js b/encoding/index.js new file mode 100644 index 00000000..48b2fa76 --- /dev/null +++ b/encoding/index.js @@ -0,0 +1,6 @@ +import base64url from "./base64url/index.js"; +import byteString from "./byteString/index.js"; + +export { base64url, byteString }; + +export default { base64url, byteString }; diff --git a/encoding/index.ts b/encoding/index.ts new file mode 100644 index 00000000..6de7c628 --- /dev/null +++ b/encoding/index.ts @@ -0,0 +1,6 @@ +import base64url from "./base64url/index"; +import byteString from "./byteString/index"; + +export { base64url, byteString }; + +export default { base64url, byteString }; From 729d555bb566d31ff9413a16856d094541af2bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 18:06:27 +0100 Subject: [PATCH 07/13] Fix mixed imports --- encoding/base64url.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encoding/base64url.test.ts b/encoding/base64url.test.ts index 2f4679f1..0b9fc157 100644 --- a/encoding/base64url.test.ts +++ b/encoding/base64url.test.ts @@ -8,9 +8,9 @@ import encodeBytes from "./base64url/encodeBytes.ts"; // @ts-ignore ambiguous import import decodeBytes from "./base64url/decodeBytes.ts"; // @ts-ignore ambiguous import -import toBase64 from "./base64url/fromBase64.ts"; +import toBase64 from "./base64url/toBase64.ts"; // @ts-ignore ambiguous import -import fromBase64 from "./base64url/toBase64.ts"; +import fromBase64 from "./base64url/fromBase64.ts"; // @ts-ignore ambiguous import import fromByteString from "./byteString/from.ts"; // @ts-ignore ambiguous import From 0e966d22a60a17a483ace056e983c3fa8e70c808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Thu, 9 Jan 2020 18:11:53 +0100 Subject: [PATCH 08/13] Fix TypeScript issues within base64URL tests --- encoding/base64url.jsdom.test.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/encoding/base64url.jsdom.test.ts b/encoding/base64url.jsdom.test.ts index 006b02e8..adf0a7eb 100644 --- a/encoding/base64url.jsdom.test.ts +++ b/encoding/base64url.jsdom.test.ts @@ -77,7 +77,13 @@ describe("base64url", () => { let out = ""; for (let i = 0; i < s.length; i += 3) { - const groupsOfSix = [undefined, undefined, undefined, undefined]; + const groupsOfSix: [ + number | undefined, + number | undefined, + number | undefined, + number | undefined + ] = [undefined, undefined, undefined, undefined]; + groupsOfSix[0] = s.charCodeAt(i) >> 2; groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4; @@ -87,15 +93,17 @@ describe("base64url", () => { } if (s.length > i + 2) { - groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6; + groupsOfSix[2] = (groupsOfSix[2] || 0) | (s.charCodeAt(i + 2) >> 6); groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f; } for (let j = 0; j < groupsOfSix.length; j++) { - if (typeof groupsOfSix[j] === "undefined") { + const x = groupsOfSix[j]; + + if (x === undefined) { out += "="; } else { - out += btoaLookup(groupsOfSix[j]); + out += btoaLookup(x); } } } @@ -119,19 +127,21 @@ describe("base64url", () => { throw new RangeError("Index out of range."); }; + const globalAny: any = global; + const context = { atob, btoa, - TextEncoder: global["TextEncoder"], - TextDecoder: global["TextDecoder"] + TextEncoder: globalAny["TextEncoder"], + TextDecoder: globalAny["TextDecoder"] }; expect(decode(encode(unicodeText, context), context)).toEqual(unicodeText); - global["window"] = context; + globalAny["window"] = context; expect(decode(encode(unicodeText))).toEqual(unicodeText); - delete global["window"]; + delete globalAny["window"]; }); }); From 6582a42d57d729f38a8b0c20efe9d5cf87346160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Fri, 17 Jan 2020 15:56:01 +0100 Subject: [PATCH 09/13] Extend tests and descriptions of the module --- encoding/base64url.test.ts | 49 +++++++++++++++++++++++++++-- encoding/base64url/decode.json | 4 +-- encoding/base64url/decodeBytes.json | 4 +-- encoding/base64url/encode.json | 4 +-- encoding/base64url/encodeBytes.json | 4 +-- encoding/base64url/fromBase64.json | 4 +-- encoding/base64url/fromBase64.ts | 5 ++- encoding/base64url/toBase64.json | 2 +- encoding/base64url/toBase64.ts | 5 ++- encoding/byteString/from.json | 4 +-- encoding/byteString/to.json | 4 +-- 11 files changed, 69 insertions(+), 20 deletions(-) diff --git a/encoding/base64url.test.ts b/encoding/base64url.test.ts index 0b9fc157..ebb2b981 100644 --- a/encoding/base64url.test.ts +++ b/encoding/base64url.test.ts @@ -67,6 +67,14 @@ describe("base64url", () => { expect(encode(text)).toEqual("QmFzZTY0VVJMIGVuY29kZS9kZWNvZGUgdGVzdA"); }); + it("properly replaces non-URL-safe characters", () => { + expect(encode("<>")).toEqual("PDw_Pz8-Pg"); + expect(Buffer.from("<>", "utf-8").toString("base64")).toEqual( + "PDw/Pz8+Pg==" + ); + expect(decode(encode("<>"))).toEqual("<>"); + }); + it("is does not include padding characters", () => { const text = "ZaΕΌΓ³Ε‚Δ‡ gΔ™Ε›lΔ… jaΕΊΕ„"; @@ -93,6 +101,15 @@ describe("base64url", () => { expect(decode(encode(text))).toEqual(text); }); + it("encodes/decodes bytes", () => { + const bytes = [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]; + + const encoded = "w4Jnw6vCp20-bBsQfA"; + + expect(encodeBytes(bytes)).toEqual(encoded); + expect(decodeBytes(encoded)).toEqual(bytes); + }); + it("handles Unicode", () => { expect(decode(encode(unicodeText))).toEqual(unicodeText); }); @@ -111,9 +128,35 @@ describe("base64url", () => { it("converts base64URL to base64", () => { const text = toByteString(range(256)); + const base64url = encode(text); + const encoded = toBase64(base64url); + const expected = Buffer.from(text, "utf-8").toString("base64"); - expect(toBase64(encode(text))).toEqual( - Buffer.from(text, "utf-8").toString("base64") - ); + expect(encoded).toEqual(expected); + }); + + it("fromBase64 strips padding characters", () => { + expect(fromBase64("PDw/Pz8+Pg==")).toEqual("PDw_Pz8-Pg"); + }); + + it("toBase64 adds padding characters when needed", () => { + expect(toBase64("PDw_Pz8-Pg")).toEqual("PDw/Pz8+Pg=="); + }); + + it("converts base64 to base64URL and vice versa", () => { + const base64 = "PDw/Pz8+Pg=="; + const base64url = "PDw_Pz8-Pg"; + + expect(fromBase64(base64)).toEqual(base64url); + expect(toBase64(base64url)).toEqual(base64); + }); + + it("encodes/decodes bytes to and from string", () => { + const bytes = [0x50, 0x51, 0x52]; + + const encoded = "PQR"; + + expect(toByteString(bytes)).toEqual(encoded); + expect(fromByteString(encoded)).toEqual(bytes); }); }); diff --git a/encoding/base64url/decode.json b/encoding/base64url/decode.json index 49e95fdb..53086a6c 100644 --- a/encoding/base64url/decode.json +++ b/encoding/base64url/decode.json @@ -1,11 +1,11 @@ { "name": "decode", - "description": "TODO: Fill short description here.", + "description": "Decodes the given Base64URL back into string.", "signature": "(text: string, context?: DecodeContext) => string", "examples": [ { "language": "javascript", - "content": "decode(); // β‡’ TODO" + "content": "decode(\"PDw_Pz8-Pg\"); // β‡’ \"<>\"" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/base64url/decodeBytes.json b/encoding/base64url/decodeBytes.json index a14a51ef..9630f2f7 100644 --- a/encoding/base64url/decodeBytes.json +++ b/encoding/base64url/decodeBytes.json @@ -1,11 +1,11 @@ { "name": "decodeBytes", - "description": "TODO: Fill short description here.", + "description": "Decodes the given Base64URL back into byte array.", "signature": "(text: string, context?: DecodeContext) => number[]", "examples": [ { "language": "javascript", - "content": "decodeBytes(); // β‡’ TODO" + "content": "decodeBytes(\"w4Jnw6vCp20-bBsQfA\");\n// β‡’ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/base64url/encode.json b/encoding/base64url/encode.json index a65e7eae..e63c07d4 100644 --- a/encoding/base64url/encode.json +++ b/encoding/base64url/encode.json @@ -1,11 +1,11 @@ { "name": "encode", - "description": "TODO: Fill short description here.", + "description": "Encodes the given string into Base64URL.", "signature": "(text: string, context?: EncodeContext) => string", "examples": [ { "language": "javascript", - "content": "encode(); // β‡’ TODO" + "content": "encode(\"<>\"); // β‡’ \"PDw_Pz8-Pg\"" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/base64url/encodeBytes.json b/encoding/base64url/encodeBytes.json index 08aeb97e..1f00edd7 100644 --- a/encoding/base64url/encodeBytes.json +++ b/encoding/base64url/encodeBytes.json @@ -1,11 +1,11 @@ { "name": "encodeBytes", - "description": "TODO: Fill short description here.", + "description": "Encodes the given bytes into Base64URL.", "signature": "(bytes: number[], context?: EncodeContext) => string", "examples": [ { "language": "javascript", - "content": "encodeBytes(); // β‡’ TODO" + "content": "encodeBytes([0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]);\n// β‡’ \"w4Jnw6vCp20-bBsQfA\"" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/base64url/fromBase64.json b/encoding/base64url/fromBase64.json index 6c36d5ca..fdd321e1 100644 --- a/encoding/base64url/fromBase64.json +++ b/encoding/base64url/fromBase64.json @@ -1,11 +1,11 @@ { "name": "fromBase64", - "description": "TODO: Fill short description here.", + "description": "Converts Base64 string into Base64URL one.", "signature": "(base64: string) => string", "examples": [ { "language": "javascript", - "content": "fromBase64(); // β‡’ TODO" + "content": "fromBase64(\"PDw/Pz8+Pg==\"); // β‡’ \"PDw_Pz8-Pg\"" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/base64url/fromBase64.ts b/encoding/base64url/fromBase64.ts index 2c5e41cd..f5b9f97c 100644 --- a/encoding/base64url/fromBase64.ts +++ b/encoding/base64url/fromBase64.ts @@ -1,2 +1,5 @@ export default (base64: string) => - base64.replace(/\+/g, "-").replace(/\//g, "_"); + base64 + .replace(/\+/g, "-") + .replace(/\//g, "_") + .replace(/=/g, ""); diff --git a/encoding/base64url/toBase64.json b/encoding/base64url/toBase64.json index 96d766ee..9bcf29cf 100644 --- a/encoding/base64url/toBase64.json +++ b/encoding/base64url/toBase64.json @@ -5,7 +5,7 @@ "examples": [ { "language": "javascript", - "content": "toBase64(); // β‡’ TODO" + "content": "toBase64(\"PDw_Pz8-Pg\"); // β‡’ \"PDw/Pz8+Pg==\"" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/base64url/toBase64.ts b/encoding/base64url/toBase64.ts index 9e980362..146ef611 100644 --- a/encoding/base64url/toBase64.ts +++ b/encoding/base64url/toBase64.ts @@ -1,2 +1,5 @@ export default (base64Url: string) => - base64Url.replace(/-/g, "+").replace(/_/g, "/"); + base64Url + .replace(/-/g, "+") + .replace(/_/g, "/") + .padEnd(Math.ceil(base64Url.length / 4) * 4, "="); diff --git a/encoding/byteString/from.json b/encoding/byteString/from.json index fbd0d1a4..fee05323 100644 --- a/encoding/byteString/from.json +++ b/encoding/byteString/from.json @@ -1,11 +1,11 @@ { "name": "from", - "description": "TODO: Fill short description here.", + "description": "Converts string to byte array.", "signature": "(byteString: string) => number[]", "examples": [ { "language": "javascript", - "content": "from(); // β‡’ TODO" + "content": "from(\"PQR\"); // β‡’ [80, 81, 82]" } ], "questions": ["TODO: List questions that may this function answers."] diff --git a/encoding/byteString/to.json b/encoding/byteString/to.json index 8adc1bc3..c63fe0c8 100644 --- a/encoding/byteString/to.json +++ b/encoding/byteString/to.json @@ -1,11 +1,11 @@ { "name": "to", - "description": "TODO: Fill short description here.", + "description": "Coverts byte array to string.", "signature": "(bytes: number[]) => string", "examples": [ { "language": "javascript", - "content": "to(); // β‡’ TODO" + "content": "to([0x50, 0x51, 0x52]); // β‡’ \"PQR\"" } ], "questions": ["TODO: List questions that may this function answers."] From 1d119b1bb0b150d19ba23441425917c0d643fb31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Fri, 17 Jan 2020 19:12:10 +0100 Subject: [PATCH 10/13] Update docs and generated code --- README.md | 80 +++++++++++++++++++++++++++++++ encoding/README.md | 80 +++++++++++++++++++++++++++++++ encoding/base64url/README.md | 60 +++++++++++++++++++++++ encoding/base64url/decode.md | 10 ++++ encoding/base64url/decodeBytes.md | 11 +++++ encoding/base64url/encode.md | 10 ++++ encoding/base64url/encodeBytes.md | 11 +++++ encoding/base64url/fromBase64.js | 6 ++- encoding/base64url/fromBase64.md | 10 ++++ encoding/base64url/toBase64.js | 6 ++- encoding/base64url/toBase64.md | 8 ++++ encoding/byteString/README.md | 20 ++++++++ encoding/byteString/from.md | 10 ++++ encoding/byteString/to.md | 10 ++++ 14 files changed, 330 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5fa2a371..0765410c 100644 --- a/README.md +++ b/README.md @@ -1115,6 +1115,8 @@ Computes a difference between two objects. ##### decode +Decodes the given Base64URL back into string. + ###### Type signature @@ -1123,8 +1125,18 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +decode("PDw_Pz8-Pg"); // β‡’ "<>" +``` + + ##### decodeBytes +Decodes the given Base64URL back into byte array. + ###### Type signature @@ -1133,8 +1145,19 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +decodeBytes("w4Jnw6vCp20-bBsQfA"); +// β‡’ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c] +``` + + ##### encode +Encodes the given string into Base64URL. + ###### Type signature @@ -1143,8 +1166,18 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +encode("<>"); // β‡’ "PDw_Pz8-Pg" +``` + + ##### encodeBytes +Encodes the given bytes into Base64URL. + ###### Type signature @@ -1153,8 +1186,19 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +encodeBytes([0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]); +// β‡’ "w4Jnw6vCp20-bBsQfA" +``` + + ##### fromBase64 +Converts Base64 string into Base64URL one. + ###### Type signature @@ -1163,6 +1207,14 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" +``` + + ##### toBase64 ###### Type signature @@ -1173,10 +1225,20 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +toBase64("PDw_Pz8-Pg"); // β‡’ "PDw/Pz8+Pg==" +``` + + #### byteString ##### from +Converts string to byte array. + ###### Type signature @@ -1185,8 +1247,18 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +from("PQR"); // β‡’ [80, 81, 82] +``` + + ##### to +Coverts byte array to string. + ###### Type signature @@ -1195,6 +1267,14 @@ Computes a difference between two objects. ``` +###### Examples + + +```javascript +to([0x50, 0x51, 0x52]); // β‡’ "PQR" +``` + + ### file #### validName diff --git a/encoding/README.md b/encoding/README.md index 19ea0f6e..18aa9319 100644 --- a/encoding/README.md +++ b/encoding/README.md @@ -2,6 +2,8 @@ ## decode +Decodes the given Base64URL back into string. + ### Type signature @@ -10,8 +12,18 @@ ``` +### Examples + + +```javascript +decode("PDw_Pz8-Pg"); // β‡’ "<>" +``` + + ## decodeBytes +Decodes the given Base64URL back into byte array. + ### Type signature @@ -20,8 +32,19 @@ ``` +### Examples + + +```javascript +decodeBytes("w4Jnw6vCp20-bBsQfA"); +// β‡’ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c] +``` + + ## encode +Encodes the given string into Base64URL. + ### Type signature @@ -30,8 +53,18 @@ ``` +### Examples + + +```javascript +encode("<>"); // β‡’ "PDw_Pz8-Pg" +``` + + ## encodeBytes +Encodes the given bytes into Base64URL. + ### Type signature @@ -40,8 +73,19 @@ ``` +### Examples + + +```javascript +encodeBytes([0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]); +// β‡’ "w4Jnw6vCp20-bBsQfA" +``` + + ## fromBase64 +Converts Base64 string into Base64URL one. + ### Type signature @@ -50,6 +94,14 @@ ``` +### Examples + + +```javascript +fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" +``` + + ## toBase64 ### Type signature @@ -60,10 +112,20 @@ ``` +### Examples + + +```javascript +toBase64("PDw_Pz8-Pg"); // β‡’ "PDw/Pz8+Pg==" +``` + + # byteString ## from +Converts string to byte array. + ### Type signature @@ -72,8 +134,18 @@ ``` +### Examples + + +```javascript +from("PQR"); // β‡’ [80, 81, 82] +``` + + ## to +Coverts byte array to string. + ### Type signature @@ -81,3 +153,11 @@ (bytes: number[]) => string ``` + +### Examples + + +```javascript +to([0x50, 0x51, 0x52]); // β‡’ "PQR" +``` + diff --git a/encoding/base64url/README.md b/encoding/base64url/README.md index 4dbe4908..a22a6e00 100644 --- a/encoding/base64url/README.md +++ b/encoding/base64url/README.md @@ -1,5 +1,7 @@ # decode +Decodes the given Base64URL back into string. + ## Type signature @@ -8,8 +10,18 @@ ``` +## Examples + + +```javascript +decode("PDw_Pz8-Pg"); // β‡’ "<>" +``` + + # decodeBytes +Decodes the given Base64URL back into byte array. + ## Type signature @@ -18,8 +30,19 @@ ``` +## Examples + + +```javascript +decodeBytes("w4Jnw6vCp20-bBsQfA"); +// β‡’ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c] +``` + + # encode +Encodes the given string into Base64URL. + ## Type signature @@ -28,8 +51,18 @@ ``` +## Examples + + +```javascript +encode("<>"); // β‡’ "PDw_Pz8-Pg" +``` + + # encodeBytes +Encodes the given bytes into Base64URL. + ## Type signature @@ -38,8 +71,19 @@ ``` +## Examples + + +```javascript +encodeBytes([0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]); +// β‡’ "w4Jnw6vCp20-bBsQfA" +``` + + # fromBase64 +Converts Base64 string into Base64URL one. + ## Type signature @@ -48,6 +92,14 @@ ``` +## Examples + + +```javascript +fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" +``` + + # toBase64 ## Type signature @@ -57,3 +109,11 @@ (base64Url: string) => string ``` + +## Examples + + +```javascript +toBase64("PDw_Pz8-Pg"); // β‡’ "PDw/Pz8+Pg==" +``` + diff --git a/encoding/base64url/decode.md b/encoding/base64url/decode.md index 98e9faa8..a75a8ad2 100644 --- a/encoding/base64url/decode.md +++ b/encoding/base64url/decode.md @@ -1,5 +1,7 @@ # decode +Decodes the given Base64URL back into string. + ## Type signature @@ -7,3 +9,11 @@ (text: string, context?: DecodeContext) => string ``` + +## Examples + + +```javascript +decode("PDw_Pz8-Pg"); // β‡’ "<>" +``` + diff --git a/encoding/base64url/decodeBytes.md b/encoding/base64url/decodeBytes.md index f7b8e8e7..37a827af 100644 --- a/encoding/base64url/decodeBytes.md +++ b/encoding/base64url/decodeBytes.md @@ -1,5 +1,7 @@ # decodeBytes +Decodes the given Base64URL back into byte array. + ## Type signature @@ -7,3 +9,12 @@ (text: string, context?: DecodeContext) => number[] ``` + +## Examples + + +```javascript +decodeBytes("w4Jnw6vCp20-bBsQfA"); +// β‡’ [0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c] +``` + diff --git a/encoding/base64url/encode.md b/encoding/base64url/encode.md index 4ecd5deb..7d24bf7d 100644 --- a/encoding/base64url/encode.md +++ b/encoding/base64url/encode.md @@ -1,5 +1,7 @@ # encode +Encodes the given string into Base64URL. + ## Type signature @@ -7,3 +9,11 @@ (text: string, context?: EncodeContext) => string ``` + +## Examples + + +```javascript +encode("<>"); // β‡’ "PDw_Pz8-Pg" +``` + diff --git a/encoding/base64url/encodeBytes.md b/encoding/base64url/encodeBytes.md index 3d1cd4de..2015a68c 100644 --- a/encoding/base64url/encodeBytes.md +++ b/encoding/base64url/encodeBytes.md @@ -1,5 +1,7 @@ # encodeBytes +Encodes the given bytes into Base64URL. + ## Type signature @@ -7,3 +9,12 @@ (bytes: number[], context?: EncodeContext) => string ``` + +## Examples + + +```javascript +encodeBytes([0xc2, 0x67, 0xeb, 0xa7, 0x6d, 0x3e, 0x6c, 0x1b, 0x10, 0x7c]); +// β‡’ "w4Jnw6vCp20-bBsQfA" +``` + diff --git a/encoding/base64url/fromBase64.js b/encoding/base64url/fromBase64.js index fe45357d..a03f49f6 100644 --- a/encoding/base64url/fromBase64.js +++ b/encoding/base64url/fromBase64.js @@ -1 +1,5 @@ -export default base64 => base64.replace(/\+/g, "-").replace(/\//g, "_"); +export default base64 => + base64 + .replace(/\+/g, "-") + .replace(/\//g, "_") + .replace(/=/g, ""); diff --git a/encoding/base64url/fromBase64.md b/encoding/base64url/fromBase64.md index c8c7be7d..fa877d00 100644 --- a/encoding/base64url/fromBase64.md +++ b/encoding/base64url/fromBase64.md @@ -1,5 +1,7 @@ # fromBase64 +Converts Base64 string into Base64URL one. + ## Type signature @@ -7,3 +9,11 @@ (base64: string) => string ``` + +## Examples + + +```javascript +fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" +``` + diff --git a/encoding/base64url/toBase64.js b/encoding/base64url/toBase64.js index a2f668d2..ef94bd62 100644 --- a/encoding/base64url/toBase64.js +++ b/encoding/base64url/toBase64.js @@ -1 +1,5 @@ -export default base64Url => base64Url.replace(/-/g, "+").replace(/_/g, "/"); +export default base64Url => + base64Url + .replace(/-/g, "+") + .replace(/_/g, "/") + .padEnd(Math.ceil(base64Url.length / 4) * 4, "="); diff --git a/encoding/base64url/toBase64.md b/encoding/base64url/toBase64.md index b01b09d2..53ba6aef 100644 --- a/encoding/base64url/toBase64.md +++ b/encoding/base64url/toBase64.md @@ -7,3 +7,11 @@ (base64Url: string) => string ``` + +## Examples + + +```javascript +toBase64("PDw_Pz8-Pg"); // β‡’ "PDw/Pz8+Pg==" +``` + diff --git a/encoding/byteString/README.md b/encoding/byteString/README.md index ad182df4..78f90898 100644 --- a/encoding/byteString/README.md +++ b/encoding/byteString/README.md @@ -1,5 +1,7 @@ # from +Converts string to byte array. + ## Type signature @@ -8,8 +10,18 @@ ``` +## Examples + + +```javascript +from("PQR"); // β‡’ [80, 81, 82] +``` + + # to +Coverts byte array to string. + ## Type signature @@ -17,3 +29,11 @@ (bytes: number[]) => string ``` + +## Examples + + +```javascript +to([0x50, 0x51, 0x52]); // β‡’ "PQR" +``` + diff --git a/encoding/byteString/from.md b/encoding/byteString/from.md index 3fa5afe1..64050ff5 100644 --- a/encoding/byteString/from.md +++ b/encoding/byteString/from.md @@ -1,5 +1,7 @@ # from +Converts string to byte array. + ## Type signature @@ -7,3 +9,11 @@ (byteString: string) => number[] ``` + +## Examples + + +```javascript +from("PQR"); // β‡’ [80, 81, 82] +``` + diff --git a/encoding/byteString/to.md b/encoding/byteString/to.md index df2cf7a9..ff0c0a8c 100644 --- a/encoding/byteString/to.md +++ b/encoding/byteString/to.md @@ -1,5 +1,7 @@ # to +Coverts byte array to string. + ## Type signature @@ -7,3 +9,11 @@ (bytes: number[]) => string ``` + +## Examples + + +```javascript +to([0x50, 0x51, 0x52]); // β‡’ "PQR" +``` + From 03ecf7ef73569826265960e51416f833ff5fd1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Tue, 21 Jan 2020 11:11:50 +0100 Subject: [PATCH 11/13] Add missing encoding/base64url/toBase64 description --- encoding/base64url/toBase64.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encoding/base64url/toBase64.json b/encoding/base64url/toBase64.json index 9bcf29cf..2b7631e6 100644 --- a/encoding/base64url/toBase64.json +++ b/encoding/base64url/toBase64.json @@ -1,6 +1,6 @@ { "name": "toBase64", - "description": "TODO: Fill short description here.", + "description": "Converts Base64URL string into Base64 one.", "signature": "(base64Url: string) => string", "examples": [ { From 7cebd6e859bf434e15058d584292a688d14c4c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Tue, 21 Jan 2020 11:12:25 +0100 Subject: [PATCH 12/13] Remove from from reserved keywords list --- compile.js | 4 ++-- mappings.js | 1 - normalizeLineEndings.js | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/compile.js b/compile.js index 88cb1f3f..83536e3a 100644 --- a/compile.js +++ b/compile.js @@ -7,14 +7,14 @@ import path from "path"; import os from "os"; import pQueue from "p-queue"; +import ignored from "./ignore.js"; + const CONCURRENCY = Math.max(1, os.cpus().length - 1); const { default: PQueue } = pQueue; const execAsync = promisify(exec); -import ignored from "./ignore.js"; - const [sourceIgnoredFiles, ignoredDirectories] = ignored; const ignoredFiles = sourceIgnoredFiles.filter(x => x !== "index.ts"); diff --git a/mappings.js b/mappings.js index bdf8100f..0901159d 100644 --- a/mappings.js +++ b/mappings.js @@ -1,4 +1,3 @@ export default { - from: "_from", function: "_function" }; diff --git a/normalizeLineEndings.js b/normalizeLineEndings.js index 02ee26dd..49fef276 100644 --- a/normalizeLineEndings.js +++ b/normalizeLineEndings.js @@ -5,12 +5,12 @@ import path from "path"; import os from "os"; import pQueue from "p-queue"; +import ignored from "./ignore.js"; + const CONCURRENCY = Math.max(1, os.cpus().length - 1); const { default: PQueue } = pQueue; -import ignored from "./ignore.js"; - const [, ignoredDirectories] = ignored; const { From e8add3e344649ebebdc4bbfa0be0abc402c1865b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Zalewski?= Date: Tue, 21 Jan 2020 12:24:33 +0100 Subject: [PATCH 13/13] Regenerate code and docs --- README.md | 2 ++ encoding/README.md | 2 ++ encoding/base64url/README.md | 2 ++ encoding/base64url/toBase64.md | 2 ++ encoding/byteString/index.js | 6 +++--- encoding/byteString/index.ts | 6 +++--- 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0765410c..efbdf1b6 100644 --- a/README.md +++ b/README.md @@ -1217,6 +1217,8 @@ fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" ##### toBase64 +Converts Base64URL string into Base64 one. + ###### Type signature diff --git a/encoding/README.md b/encoding/README.md index 18aa9319..170cac8b 100644 --- a/encoding/README.md +++ b/encoding/README.md @@ -104,6 +104,8 @@ fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" ## toBase64 +Converts Base64URL string into Base64 one. + ### Type signature diff --git a/encoding/base64url/README.md b/encoding/base64url/README.md index a22a6e00..e02753f8 100644 --- a/encoding/base64url/README.md +++ b/encoding/base64url/README.md @@ -102,6 +102,8 @@ fromBase64("PDw/Pz8+Pg=="); // β‡’ "PDw_Pz8-Pg" # toBase64 +Converts Base64URL string into Base64 one. + ## Type signature diff --git a/encoding/base64url/toBase64.md b/encoding/base64url/toBase64.md index 53ba6aef..5856bf2d 100644 --- a/encoding/base64url/toBase64.md +++ b/encoding/base64url/toBase64.md @@ -1,5 +1,7 @@ # toBase64 +Converts Base64URL string into Base64 one. + ## Type signature diff --git a/encoding/byteString/index.js b/encoding/byteString/index.js index 8d0e9003..fa795a66 100644 --- a/encoding/byteString/index.js +++ b/encoding/byteString/index.js @@ -1,6 +1,6 @@ -import _from from "./from.js"; +import from from "./from.js"; import to from "./to.js"; -export { _from, to }; +export { from, to }; -export default { _from, to }; +export default { from, to }; diff --git a/encoding/byteString/index.ts b/encoding/byteString/index.ts index 0efce719..f1f3d34f 100644 --- a/encoding/byteString/index.ts +++ b/encoding/byteString/index.ts @@ -1,6 +1,6 @@ -import _from from "./from"; +import from from "./from"; import to from "./to"; -export { _from, to }; +export { from, to }; -export default { _from, to }; +export default { from, to };