Skip to content

Commit

Permalink
refactor(base-n): remove deps, minor internal updates
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 11, 2023
1 parent 3e11441 commit c675cba
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/base-n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ For Node.js REPL:
const baseN = await import("@thi.ng/base-n");
```

Package sizes (brotli'd, pre-treeshake): ESM: 931 bytes
Package sizes (brotli'd, pre-treeshake): ESM: 944 bytes

## Dependencies

- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
None

## API

Expand Down
3 changes: 0 additions & 3 deletions packages/base-n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
"pub": "yarn npm publish --access public",
"test": "testament test"
},
"dependencies": {
"@thi.ng/hex": "^2.3.14"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.36.3",
"@thi.ng/testament": "^0.3.20",
Expand Down
4 changes: 2 additions & 2 deletions packages/base-n/src/16.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defBase } from "./base.js";
import { __B16_LC_CHARS, defBase } from "./base.js";

export const B16_UC_CHARS = "0123456789ABCDEF";

export const B16_LC_CHARS = "0123456789abcdef";
export const B16_LC_CHARS = __B16_LC_CHARS;

/**
* Alias for {@link B16_LC_CHARS}
Expand Down
29 changes: 19 additions & 10 deletions packages/base-n/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { U8 } from "@thi.ng/hex";
import type { IBase } from "./api.js";

export const defBase = (chars: string) => new BaseN(chars);

const ZERO = BigInt(0);
const MASK = BigInt(255);
const SHIFT = BigInt(8);

/** @internal */
export const __B16_LC_CHARS = "0123456789abcdef";

export class BaseN implements IBase {
readonly N: number;
readonly index: Record<string, number>;
Expand All @@ -29,20 +35,20 @@ export class BaseN implements IBase {
encodeBigInt(x: bigint, size = 0) {
if (x < BigInt(2 ** 53)) return this.encode(Number(x), size);
const { base, N } = this;
if (x === BigInt(0)) return __pad(base[0], size, base[0]);
if (x === ZERO) return __pad(base[0], size, base[0]);
const NN = BigInt(N);
let res = "";
while (x > 0) {
while (x > ZERO) {
res = base[Number(x % NN)] + res;
x /= NN;
}
return __pad(res, size, base[0]);
}

encodeBytes(buf: Uint8Array, size = 0) {
let hex = "";
for (let i = 0, n = buf.length; i < n; i++) hex += U8(buf[i]);
return this.encodeBigInt(BigInt(`0x${hex}`), size);
let hex = "0x";
for (let i = 0, n = buf.length; i < n; i++) hex += __u8(buf[i]);
return this.encodeBigInt(BigInt(hex), size);
}

decode(x: string) {
Expand All @@ -57,7 +63,7 @@ export class BaseN implements IBase {
decodeBigInt(x: string): bigint {
const { index, N } = this;
const NN = BigInt(N);
let res = BigInt(0);
let res = ZERO;
for (let n = x.length - 1, i = 0; i <= n; i++) {
res += BigInt(index[x[i]]) * NN ** BigInt(n - i);
}
Expand All @@ -66,10 +72,8 @@ export class BaseN implements IBase {

decodeBytes(x: string, buf: Uint8Array): Uint8Array {
let y = this.decodeBigInt(x);
const M = BigInt(255);
const SHIFT = BigInt(8);
for (let i = buf.length; i-- > 0; ) {
buf[i] = Number(y & M);
buf[i] = Number(y & MASK);
y >>= SHIFT;
}
return buf;
Expand All @@ -84,5 +88,10 @@ export class BaseN implements IBase {
}
}

/** @internal */
const __pad = (x: string, size: number, fill: string) =>
size - x.length > 0 ? fill.repeat(size - x.length) + x : x;

/** @internal */
const __u8 = (x: number) =>
__B16_LC_CHARS[(x >>> 4) & 0xf] + __B16_LC_CHARS[x & 0xf];
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2614,7 +2614,6 @@ __metadata:
resolution: "@thi.ng/base-n@workspace:packages/base-n"
dependencies:
"@microsoft/api-extractor": ^7.36.3
"@thi.ng/hex": ^2.3.14
"@thi.ng/testament": ^0.3.20
rimraf: ^5.0.1
tools: "workspace:^"
Expand Down

0 comments on commit c675cba

Please sign in to comment.