multiformats
Advanced tools
Comparing version
@@ -136,2 +136,6 @@ 'use strict'; | ||
testThrow(() => base.decode(b64), msg); | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt2.decode(b64), bytes.fromString('test')); | ||
const baseExt3 = base.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt3.decode(b64), bytes.fromString('test')); | ||
}); | ||
@@ -138,0 +142,0 @@ test('truncated data', () => { |
@@ -136,2 +136,6 @@ 'use strict'; | ||
testThrow(() => base.decode(b64), msg); | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt2.decode(b64), bytes.fromString('test')); | ||
const baseExt3 = base.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt3.decode(b64), bytes.fromString('test')); | ||
}); | ||
@@ -138,0 +142,0 @@ test('truncated data', () => { |
@@ -43,7 +43,3 @@ 'use strict'; | ||
or(decoder) { | ||
const decoders = { | ||
[this.prefix]: this, | ||
...decoder.decoders || { [decoder.prefix]: decoder } | ||
}; | ||
return new ComposedDecoder(decoders); | ||
return or(this, decoder); | ||
} | ||
@@ -56,7 +52,3 @@ } | ||
or(decoder) { | ||
const other = decoder.decoders || { [decoder.prefix]: decoder }; | ||
return new ComposedDecoder({ | ||
...this.decoders, | ||
...other | ||
}); | ||
return or(this, decoder); | ||
} | ||
@@ -73,2 +65,6 @@ decode(input) { | ||
} | ||
const or = (left, right) => new ComposedDecoder({ | ||
...left.decoders || { [left.prefix]: left }, | ||
...right.decoders || { [right.prefix]: right } | ||
}); | ||
class Codec { | ||
@@ -170,2 +166,3 @@ constructor(name, prefix, baseEncode, baseDecode) { | ||
exports.from = from; | ||
exports.or = or; | ||
exports.rfc4648 = rfc4648; |
@@ -129,2 +129,6 @@ import * as bytes from '../src/bytes.js'; | ||
testThrow(() => base.decode(b64), msg); | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt2.decode(b64), bytes.fromString('test')); | ||
const baseExt3 = base.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt3.decode(b64), bytes.fromString('test')); | ||
}); | ||
@@ -131,0 +135,0 @@ test('truncated data', () => { |
@@ -129,2 +129,6 @@ import * as bytes from '../src/bytes.js'; | ||
testThrow(() => base.decode(b64), msg); | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt2.decode(b64), bytes.fromString('test')); | ||
const baseExt3 = base.or(base64.decoder.or(base16.decoder)); | ||
same(baseExt3.decode(b64), bytes.fromString('test')); | ||
}); | ||
@@ -131,0 +135,0 @@ test('truncated data', () => { |
@@ -38,7 +38,3 @@ import basex from '../../vendor/base-x.js'; | ||
or(decoder) { | ||
const decoders = { | ||
[this.prefix]: this, | ||
...decoder.decoders || { [decoder.prefix]: decoder } | ||
}; | ||
return new ComposedDecoder(decoders); | ||
return or(this, decoder); | ||
} | ||
@@ -51,7 +47,3 @@ } | ||
or(decoder) { | ||
const other = decoder.decoders || { [decoder.prefix]: decoder }; | ||
return new ComposedDecoder({ | ||
...this.decoders, | ||
...other | ||
}); | ||
return or(this, decoder); | ||
} | ||
@@ -68,2 +60,6 @@ decode(input) { | ||
} | ||
export const or = (left, right) => new ComposedDecoder({ | ||
...left.decoders || { [left.prefix]: left }, | ||
...right.decoders || { [right.prefix]: right } | ||
}); | ||
export class Codec { | ||
@@ -70,0 +66,0 @@ constructor(name, prefix, baseEncode, baseDecode) { |
{ | ||
"name": "multiformats", | ||
"version": "9.4.10", | ||
"version": "9.4.11", | ||
"description": "Interface for multihash, multicodec, multibase and CID", | ||
@@ -146,3 +146,3 @@ "main": "./cjs/src/index.js", | ||
"standard": "^16.0.3", | ||
"typescript": "^4.4.2" | ||
"typescript": "~4.4.2" | ||
}, | ||
@@ -149,0 +149,0 @@ "standard": { |
@@ -114,9 +114,3 @@ import basex from '../../vendor/base-x.js' | ||
or (decoder) { | ||
/** @type {Decoders<Prefix|OtherPrefix>} */ | ||
const decoders = ({ | ||
[this.prefix]: this, | ||
...decoder.decoders || ({ [decoder.prefix]: decoder }) | ||
}) | ||
return new ComposedDecoder(decoders) | ||
return or(this, decoder) | ||
} | ||
@@ -154,8 +148,3 @@ } | ||
or (decoder) { | ||
/** @type {Decoders<OtherPrefix>} */ | ||
const other = (decoder.decoders || { [decoder.prefix]: decoder }) | ||
return new ComposedDecoder({ | ||
...this.decoders, | ||
...other | ||
}) | ||
return or(this, decoder) | ||
} | ||
@@ -179,2 +168,14 @@ | ||
/** | ||
* @template {string} L | ||
* @template {string} R | ||
* @param {UnibaseDecoder<L>|CombobaseDecoder<L>} left | ||
* @param {UnibaseDecoder<R>|CombobaseDecoder<R>} right | ||
* @returns {ComposedDecoder<L|R>} | ||
*/ | ||
export const or = (left, right) => new ComposedDecoder(/** @type {Decoders<L|R>} */({ | ||
...(left.decoders || { [/** @type UnibaseDecoder<L> */(left).prefix]: left }), | ||
...(right.decoders || { [/** @type UnibaseDecoder<R> */(right).prefix]: right }) | ||
})) | ||
/** | ||
* @template T | ||
@@ -181,0 +182,0 @@ * @typedef {import('./interface').MultibaseCodec<T>} MultibaseCodec |
@@ -133,2 +133,10 @@ /* globals describe, it */ | ||
testThrow(() => base.decode(b64), msg) | ||
// non-composed combined with composed | ||
const baseExt2 = base32.decoder.or(base64.decoder.or(base16.decoder)) | ||
same(baseExt2.decode(b64), bytes.fromString('test')) | ||
// composed combined with composed | ||
const baseExt3 = base.or(base64.decoder.or(base16.decoder)) | ||
same(baseExt3.decode(b64), bytes.fromString('test')) | ||
}) | ||
@@ -135,0 +143,0 @@ |
@@ -0,1 +1,2 @@ | ||
export function or<L extends string, R extends string>(left: import("./interface").UnibaseDecoder<L> | import("./interface").CombobaseDecoder<L>, right: import("./interface").UnibaseDecoder<R> | import("./interface").CombobaseDecoder<R>): ComposedDecoder<L | R>; | ||
/** | ||
@@ -67,2 +68,33 @@ * @template T | ||
/** | ||
* @template {string} Prefix | ||
* @typedef {import('./interface').CombobaseDecoder<Prefix>} CombobaseDecoder | ||
*/ | ||
/** | ||
* @template {string} Prefix | ||
* @typedef {Record<Prefix, UnibaseDecoder<Prefix>>} Decoders | ||
*/ | ||
/** | ||
* @template {string} Prefix | ||
* @implements {MultibaseDecoder<Prefix>} | ||
* @implements {CombobaseDecoder<Prefix>} | ||
*/ | ||
declare class ComposedDecoder<Prefix extends string> implements MultibaseDecoder<Prefix>, CombobaseDecoder<Prefix> { | ||
/** | ||
* @param {Record<Prefix, UnibaseDecoder<Prefix>>} decoders | ||
*/ | ||
constructor(decoders: Record<Prefix, UnibaseDecoder<Prefix>>); | ||
decoders: Record<Prefix, import("./interface").UnibaseDecoder<Prefix>>; | ||
/** | ||
* @template {string} OtherPrefix | ||
* @param {UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder | ||
* @returns {ComposedDecoder<Prefix|OtherPrefix>} | ||
*/ | ||
or<OtherPrefix extends string>(decoder: import("./interface").UnibaseDecoder<OtherPrefix> | ComposedDecoder<OtherPrefix>): ComposedDecoder<Prefix | OtherPrefix>; | ||
/** | ||
* @param {string} input | ||
* @returns {Uint8Array} | ||
*/ | ||
decode(input: string): Uint8Array; | ||
} | ||
/** | ||
* @typedef {import('./interface').BaseEncoder} BaseEncoder | ||
@@ -149,34 +181,3 @@ * @typedef {import('./interface').BaseDecoder} BaseDecoder | ||
} | ||
/** | ||
* @template {string} Prefix | ||
* @typedef {import('./interface').CombobaseDecoder<Prefix>} CombobaseDecoder | ||
*/ | ||
/** | ||
* @template {string} Prefix | ||
* @typedef {Record<Prefix, UnibaseDecoder<Prefix>>} Decoders | ||
*/ | ||
/** | ||
* @template {string} Prefix | ||
* @implements {MultibaseDecoder<Prefix>} | ||
* @implements {CombobaseDecoder<Prefix>} | ||
*/ | ||
declare class ComposedDecoder<Prefix extends string> implements MultibaseDecoder<Prefix>, CombobaseDecoder<Prefix> { | ||
/** | ||
* @param {Record<Prefix, UnibaseDecoder<Prefix>>} decoders | ||
*/ | ||
constructor(decoders: Record<Prefix, UnibaseDecoder<Prefix>>); | ||
decoders: Record<Prefix, import("./interface").UnibaseDecoder<Prefix>>; | ||
/** | ||
* @template {string} OtherPrefix | ||
* @param {UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder | ||
* @returns {ComposedDecoder<Prefix|OtherPrefix>} | ||
*/ | ||
or<OtherPrefix extends string>(decoder: import("./interface").UnibaseDecoder<OtherPrefix> | ComposedDecoder<OtherPrefix>): ComposedDecoder<Prefix | OtherPrefix>; | ||
/** | ||
* @param {string} input | ||
* @returns {Uint8Array} | ||
*/ | ||
decode(input: string): Uint8Array; | ||
} | ||
export {}; | ||
//# sourceMappingURL=base.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
435446
0.36%13541
0.13%