From e8945a94afe60b3d39fcc446294e36ee5a6ee811 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 11 Jan 2021 11:33:24 +0000 Subject: [PATCH 1/2] fix: update aegir and fix types --- package.json | 6 +++--- src/index.js | 4 ++++ src/rfc4648.js | 1 + src/types.ts | 20 ++++++++++++-------- test/multibase.spec.js | 5 +++++ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 5d6ee5c..79f82ae 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "main": "src/index.js", "repository": "github:multiformats/js-multibase", "scripts": { - "prepare": "aegir build", + "prepare": "aegir build --no-bundle", "lint": "aegir lint", "test": "aegir test", "test:node": "aegir test -t node", @@ -35,10 +35,10 @@ }, "dependencies": { "@multiformats/base-x": "^4.0.1", - "web-encoding": "^1.0.4" + "web-encoding": "^1.0.6" }, "devDependencies": { - "aegir": "^29.0.1", + "aegir": "^30.3.0", "benchmark": "^2.1.4" }, "engines": { diff --git a/src/index.js b/src/index.js index 310e597..045fab7 100644 --- a/src/index.js +++ b/src/index.js @@ -113,9 +113,13 @@ function validEncode (name, buf) { * @throws {Error} Will throw if the encoding is not supported */ function encoding (nameOrCode) { + // @ts-ignore if (constants.names[nameOrCode]) { + // @ts-ignore return constants.names[nameOrCode] + // @ts-ignore } else if (constants.codes[nameOrCode]) { + // @ts-ignore return constants.codes[nameOrCode] } else { throw new Error(`Unsupported encoding: ${nameOrCode}`) diff --git a/src/rfc4648.js b/src/rfc4648.js index 89a1aa2..c911cc1 100644 --- a/src/rfc4648.js +++ b/src/rfc4648.js @@ -10,6 +10,7 @@ */ const decode = (string, alphabet, bitsPerChar) => { // Build the character lookup table: + /** @type {Record} */ const codes = {} for (let i = 0; i < alphabet.length; ++i) { codes[alphabet[i]] = i diff --git a/src/types.ts b/src/types.ts index 84438f8..1aec796 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,7 +24,7 @@ export type BaseCode = | 'm' | 'M' | 'u' - | 'U'; + | 'U' /** * - Names of the supported encodings @@ -52,11 +52,15 @@ export type BaseName = | 'base64' | 'base64pad' | 'base64url' - | 'base64urlpad'; + | 'base64urlpad' -export type BaseNameOrCode = BaseCode | BaseName; -export type Codec = { - encode: (buffer: Uint8Array) => string; - decode: (hash: string) => Uint8Array; -}; -export type CodecFactory = (input: string) => Codec; +export type BaseNameOrCode = BaseCode | BaseName +export interface Codec { + encode: (buffer: Uint8Array) => string + decode: (hash: string) => Uint8Array +} +export interface CodecFactory { (input: string): Codec } + +export type Foo = Codec & CodecFactory & { + someProp: string +} diff --git a/test/multibase.spec.js b/test/multibase.spec.js index 9d80cd9..31257f9 100644 --- a/test/multibase.spec.js +++ b/test/multibase.spec.js @@ -6,6 +6,7 @@ const { encodeText, decodeText } = require('../src/util') const multibase = require('../src') const constants = require('../src/constants.js') +/** @type {Array<[BaseName, string, string]>} */ const unsupportedBases = [] /** @@ -88,6 +89,10 @@ const supportedBases = [ ['base64urlpad', '÷ïÿ🥰÷ïÿ😎🥶🤯', 'Uw7fDr8O_8J-lsMO3w6_Dv_CfmI7wn6W28J-krw=='] ] +/** + * @param {string} label + * @param {(encodeText:(text: string) => Uint8Array) => void} def + */ const they = (label, def) => { it(`${label} (Uint8Array)`, def.bind(null, encodeText)) } From a6d82adaf594585153b5e049f67a465ed26d5647 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Wed, 13 Jan 2021 14:47:19 +0000 Subject: [PATCH 2/2] fix: feedback --- src/index.js | 13 +++++-------- src/types.ts | 4 ---- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 045fab7..df66bad 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,7 @@ const { encodeText, decodeText, concat } = require('./util') /** @typedef {import('./base')} Base */ /** @typedef {import("./types").BaseNameOrCode} BaseNameOrCode */ /** @typedef {import("./types").BaseCode} BaseCode */ +/** @typedef {import("./types").BaseName} BaseName */ /** * Create a new Uint8Array with the multibase varint+code. @@ -113,14 +114,10 @@ function validEncode (name, buf) { * @throws {Error} Will throw if the encoding is not supported */ function encoding (nameOrCode) { - // @ts-ignore - if (constants.names[nameOrCode]) { - // @ts-ignore - return constants.names[nameOrCode] - // @ts-ignore - } else if (constants.codes[nameOrCode]) { - // @ts-ignore - return constants.codes[nameOrCode] + if (constants.names[/** @type {BaseName} */(nameOrCode)]) { + return constants.names[/** @type {BaseName} */(nameOrCode)] + } else if (constants.codes[/** @type {BaseCode} */(nameOrCode)]) { + return constants.codes[/** @type {BaseCode} */(nameOrCode)] } else { throw new Error(`Unsupported encoding: ${nameOrCode}`) } diff --git a/src/types.ts b/src/types.ts index 1aec796..e85a720 100644 --- a/src/types.ts +++ b/src/types.ts @@ -60,7 +60,3 @@ export interface Codec { decode: (hash: string) => Uint8Array } export interface CodecFactory { (input: string): Codec } - -export type Foo = Codec & CodecFactory & { - someProp: string -}