Skip to content

Commit

Permalink
Rename coders: number => numberBigint
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed May 16, 2024
1 parent e55ac61 commit d32d7e7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ assertType<P.CoderType<{ TAG: 'a'; data: string } | { TAG: 'b'; data: boolean }>
);
// NOTE: U256 is bigint by default, but we can cast it to number
assertType<P.CoderType<{ TAG: 1; data: string } | { TAG: 2; data: boolean }>>(
P.tag(P.apply(P.U256BE, P.coders.number), { 1: P.cstring, 2: P.bool })
P.tag(P.apply(P.U256BE, P.coders.numberBigint), { 1: P.cstring, 2: P.bool })
);
// MappedTag
assertType<P.CoderType<{ TAG: 'a'; data: string } | { TAG: 'b'; data: boolean }>>(
Expand Down Expand Up @@ -113,7 +113,7 @@ assertType<P.CoderType<'a' | 'b' | 'c'>>(e);

// match
assertType<base.Coder<bigint | [string, unknown][], number | Record<string, unknown>>>(
P.coders.match([P.coders.number, P.coders.dict()])
P.coders.match([P.coders.numberBigint, P.coders.dict()])
);

const m1: base.Coder<number | undefined, string> = 1 as any;
Expand Down
17 changes: 3 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import type { Coder as BaseCoder } from '@scure/base';
import { hex as baseHex, utf8 } from '@scure/base';

// copied from scure-base
// export interface BCoder<F, T> {
// encode(from: F): T;
// decode(to: T): F;
// }
// declare const TextEncoder: any;
// declare const TextDecoder: any;
// const utf8: BytesCoder<string> = {
// encode: (data) => new TextDecoder().decode(data),
// decode: (str) => new TextEncoder().encode(str),
// };

/**
* Define complex binary structures using composable primitives.
* Main ideas:
Expand All @@ -35,6 +23,8 @@ import { hex as baseHex, utf8 } from '@scure/base';
* });
*/

// TODO: remove dependency on scure-base & inline?

/**
* Shortcut to zero-length (empty) byte array
*/
Expand Down Expand Up @@ -104,7 +94,6 @@ export const utils = {
equalBytes,
isBytes,
isCoder,
checkBounds,
concatBytes,
createView,
isPlainObject,
Expand Down Expand Up @@ -1186,7 +1175,7 @@ export const int = (size: number, le = false, signed = false, sized = true): Cod
if (typeof sized !== 'boolean')
throw new Error(`int/sized: expected boolean, got ${typeof sized}`);
if (size > 6) throw new Error('int supports size up to 6 bytes (48 bits): use bigints instead');
return apply(bigint(size, le, signed, sized), coders.number);
return apply(bigint(size, le, signed, sized), coders.numberBigint);
};

type ViewCoder = {
Expand Down
2 changes: 1 addition & 1 deletion test/debugger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ should('PSBT1', () => {
return num;
},
});
const CompactSizeLen = P.apply(CompactSize, P.coders.number);
const CompactSizeLen = P.apply(CompactSize, P.coders.numberBigint);
const PKey = P.struct({ type: CompactSizeLen, data: P.bytes(null) });
const PSBTKeyPair = P.array(
P.NULL,
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,9 +1121,9 @@ describe('structures', () => {

describe('coders', () => {
should('number', () => {
deepStrictEqual(P.coders.number.encode(1000n), 1000);
deepStrictEqual(P.coders.number.encode(9007199254740991n), 9007199254740991);
throws(() => P.coders.number.encode(9007199254740992n));
deepStrictEqual(P.coders.numberBigint.encode(1000n), 1000);
deepStrictEqual(P.coders.numberBigint.encode(9007199254740991n), 9007199254740991);
throws(() => P.coders.numberBigint.encode(9007199254740992n));
});

should('decimal', () => {
Expand Down

0 comments on commit d32d7e7

Please sign in to comment.