@thi.ng/base-n
Advanced tools
Comparing version 2.5.19 to 2.5.20
32
16.js
import { __B16_LC_CHARS, defBase } from "./base.js"; | ||
export const B16_UC_CHARS = "0123456789ABCDEF"; | ||
export const B16_LC_CHARS = __B16_LC_CHARS; | ||
/** | ||
* Alias for {@link B16_LC_CHARS} | ||
*/ | ||
export const B16_CHARS = B16_LC_CHARS; | ||
/** | ||
* Digits: 0-9 A-F | ||
*/ | ||
export const BASE16_UC = defBase(B16_UC_CHARS); | ||
/** | ||
* Digits: 0-9 a-f | ||
*/ | ||
export const BASE16_LC = defBase(B16_LC_CHARS); | ||
/** | ||
* Alias for {@link BASE16_LC} | ||
*/ | ||
export const BASE16 = BASE16_LC; | ||
const B16_UC_CHARS = "0123456789ABCDEF"; | ||
const B16_LC_CHARS = __B16_LC_CHARS; | ||
const B16_CHARS = B16_LC_CHARS; | ||
const BASE16_UC = defBase(B16_UC_CHARS); | ||
const BASE16_LC = defBase(B16_LC_CHARS); | ||
const BASE16 = BASE16_LC; | ||
export { | ||
B16_CHARS, | ||
B16_LC_CHARS, | ||
B16_UC_CHARS, | ||
BASE16, | ||
BASE16_LC, | ||
BASE16_UC | ||
}; |
35
32.js
import { defBase } from "./base.js"; | ||
/** | ||
* Digits: A-Z 2-7 | ||
* | ||
* @remarks | ||
* Reference: https://tools.ietf.org/html/rfc4648 | ||
*/ | ||
export const BASE32_RFC4648 = defBase("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"); | ||
/** | ||
* Alias for {@link BASE32_RFC4648} | ||
*/ | ||
export const BASE32 = BASE32_RFC4648; | ||
/** | ||
* Digits: 0-9 A-V | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Base32#base32hex | ||
*/ | ||
export const BASE32_HEX = defBase("0123456789ABCDEFGHIJKLMNOPQRSTUV"); | ||
/** | ||
* Digits: 0-9 A-Z (excl. I,L,O,U) | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Base32#Crockford's_Base32 | ||
*/ | ||
export const BASE32_CROCKFORD = defBase("0123456789ABCDEFGHJKMNPQRSTVWXYZ"); | ||
const BASE32_RFC4648 = defBase("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"); | ||
const BASE32 = BASE32_RFC4648; | ||
const BASE32_HEX = defBase("0123456789ABCDEFGHIJKLMNOPQRSTUV"); | ||
const BASE32_CROCKFORD = defBase("0123456789ABCDEFGHJKMNPQRSTVWXYZ"); | ||
export { | ||
BASE32, | ||
BASE32_CROCKFORD, | ||
BASE32_HEX, | ||
BASE32_RFC4648 | ||
}; |
14
36.js
import { defBase } from "./base.js"; | ||
export const B36_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
/** | ||
* Digits: 0-9 A-Z | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Base36 | ||
*/ | ||
export const BASE36 = defBase(B36_CHARS); | ||
const B36_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
const BASE36 = defBase(B36_CHARS); | ||
export { | ||
B36_CHARS, | ||
BASE36 | ||
}; |
29
58.js
@@ -5,16 +5,15 @@ import { defBase } from "./base.js"; | ||
const L = "abcdefghijkmnopqrstuvwxyz"; | ||
export const B58_CHARS_UC = D + U + L; | ||
export const B58_CHARS_LC = D + L + U; | ||
export const B58_CHARS = B58_CHARS_UC; | ||
/** | ||
* Reference: https://en.wikipedia.org/wiki/Binary-to-text_encoding | ||
*/ | ||
export const BASE58_UC = defBase(B58_CHARS_UC); | ||
/** | ||
* Alias for {@link BASE58_UC}. | ||
*/ | ||
export const BASE58 = BASE58_UC; | ||
/** | ||
* Alt version of Base58 which uses {@link B58_CHARS_LC}. | ||
*/ | ||
export const BASE58_LC = defBase(B58_CHARS_LC); | ||
const B58_CHARS_UC = D + U + L; | ||
const B58_CHARS_LC = D + L + U; | ||
const B58_CHARS = B58_CHARS_UC; | ||
const BASE58_UC = defBase(B58_CHARS_UC); | ||
const BASE58 = BASE58_UC; | ||
const BASE58_LC = defBase(B58_CHARS_LC); | ||
export { | ||
B58_CHARS, | ||
B58_CHARS_LC, | ||
B58_CHARS_UC, | ||
BASE58, | ||
BASE58_LC, | ||
BASE58_UC | ||
}; |
14
62.js
import { B36_CHARS } from "./36.js"; | ||
import { defBase } from "./base.js"; | ||
export const B62_CHARS = B36_CHARS + "abcdefghijklmnopqrstuvwxyz"; | ||
/** | ||
* Digits: 0-9 A-Z a-z | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Base62 | ||
*/ | ||
export const BASE62 = defBase(B62_CHARS); | ||
const B62_CHARS = B36_CHARS + "abcdefghijklmnopqrstuvwxyz"; | ||
const BASE62 = defBase(B62_CHARS); | ||
export { | ||
B62_CHARS, | ||
BASE62 | ||
}; |
16
64.js
import { B62_CHARS } from "./62.js"; | ||
import { defBase } from "./base.js"; | ||
export const B64_CHARS = B62_CHARS + "+/"; | ||
/** | ||
* Digits: 0-9 A-Z a-z + / | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/Base64 | ||
* | ||
* Note: This encoder does NOT perform automatic padding (i.e. `=`-suffixes) | ||
*/ | ||
export const BASE64 = defBase(B64_CHARS); | ||
const B64_CHARS = B62_CHARS + "+/"; | ||
const BASE64 = defBase(B64_CHARS); | ||
export { | ||
B64_CHARS, | ||
BASE64 | ||
}; |
8
8.js
import { defBase } from "./base.js"; | ||
/** | ||
* Digits: 0-7 | ||
*/ | ||
export const BASE8 = defBase("01234567"); | ||
const BASE8 = defBase("01234567"); | ||
export { | ||
BASE8 | ||
}; |
11
85.js
import { B62_CHARS } from "./62.js"; | ||
import { defBase } from "./base.js"; | ||
export const B85_CHARS = B62_CHARS + "!#$%&()*+-;<=>?@^_`{|}~"; | ||
/** | ||
* Reference: https://en.wikipedia.org/wiki/Ascii85 | ||
*/ | ||
export const BASE85 = defBase(B85_CHARS); | ||
const B85_CHARS = B62_CHARS + "!#$%&()*+-;<=>?@^_`{|}~"; | ||
const BASE85 = defBase(B85_CHARS); | ||
export { | ||
B85_CHARS, | ||
BASE85 | ||
}; |
152
base.js
@@ -1,2 +0,2 @@ | ||
export const defBase = (chars) => new BaseN(chars); | ||
const defBase = (chars) => new BaseN(chars); | ||
const ZERO = BigInt(0); | ||
@@ -6,82 +6,86 @@ const MASK = BigInt(255); | ||
const MAX_SAFE_INT = BigInt(2 ** 53 - 1); | ||
/** @internal */ | ||
export const __B16_LC_CHARS = "0123456789abcdef"; | ||
export class BaseN { | ||
base; | ||
N; | ||
index; | ||
constructor(base) { | ||
this.base = base; | ||
this.N = base.length; | ||
this.index = [...base].reduce((acc, x, i) => ((acc[x] = i), acc), {}); | ||
const __B16_LC_CHARS = "0123456789abcdef"; | ||
class BaseN { | ||
constructor(base) { | ||
this.base = base; | ||
this.N = base.length; | ||
this.index = [...base].reduce( | ||
(acc, x, i) => (acc[x] = i, acc), | ||
{} | ||
); | ||
} | ||
N; | ||
index; | ||
encode(x, size = 0) { | ||
const { base, N } = this; | ||
if (x === 0) | ||
return __pad(base[0], size, base[0]); | ||
let res = ""; | ||
while (x > 0) { | ||
res = base[x % N] + res; | ||
x = Math.floor(x / N); | ||
} | ||
encode(x, size = 0) { | ||
const { base, N } = this; | ||
if (x === 0) | ||
return __pad(base[0], size, base[0]); | ||
let res = ""; | ||
while (x > 0) { | ||
res = base[x % N] + res; | ||
x = Math.floor(x / N); | ||
} | ||
return __pad(res, size, base[0]); | ||
return __pad(res, size, base[0]); | ||
} | ||
encodeBigInt(x, size = 0) { | ||
if (x <= MAX_SAFE_INT) | ||
return this.encode(Number(x), size); | ||
const { base, N } = this; | ||
if (x === ZERO) | ||
return __pad(base[0], size, base[0]); | ||
const NN = BigInt(N); | ||
let res = ""; | ||
while (x > ZERO) { | ||
res = base[Number(x % NN)] + res; | ||
x /= NN; | ||
} | ||
encodeBigInt(x, size = 0) { | ||
if (x <= MAX_SAFE_INT) | ||
return this.encode(Number(x), size); | ||
const { base, N } = this; | ||
if (x === ZERO) | ||
return __pad(base[0], size, base[0]); | ||
const NN = BigInt(N); | ||
let res = ""; | ||
while (x > ZERO) { | ||
res = base[Number(x % NN)] + res; | ||
x /= NN; | ||
} | ||
return __pad(res, size, base[0]); | ||
return __pad(res, size, base[0]); | ||
} | ||
encodeBytes(buf, size = 0) { | ||
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) { | ||
const { index, N } = this; | ||
let res = 0; | ||
for (let n = x.length - 1, i = 0; i <= n; i++) { | ||
res += index[x[i]] * N ** (n - i); | ||
} | ||
encodeBytes(buf, size = 0) { | ||
let hex = "0x"; | ||
for (let i = 0, n = buf.length; i < n; i++) | ||
hex += __u8(buf[i]); | ||
return this.encodeBigInt(BigInt(hex), size); | ||
return res; | ||
} | ||
decodeBigInt(x) { | ||
const { index, N } = this; | ||
const NN = BigInt(N); | ||
let res = ZERO; | ||
for (let n = x.length - 1, i = 0; i <= n; i++) { | ||
res += BigInt(index[x[i]]) * NN ** BigInt(n - i); | ||
} | ||
decode(x) { | ||
const { index, N } = this; | ||
let res = 0; | ||
for (let n = x.length - 1, i = 0; i <= n; i++) { | ||
res += index[x[i]] * N ** (n - i); | ||
} | ||
return res; | ||
return res; | ||
} | ||
decodeBytes(x, buf) { | ||
let y = this.decodeBigInt(x); | ||
for (let i = buf.length; i-- > 0; ) { | ||
buf[i] = Number(y & MASK); | ||
y >>= SHIFT; | ||
} | ||
decodeBigInt(x) { | ||
const { index, N } = this; | ||
const NN = BigInt(N); | ||
let res = ZERO; | ||
for (let n = x.length - 1, i = 0; i <= n; i++) { | ||
res += BigInt(index[x[i]]) * NN ** BigInt(n - i); | ||
} | ||
return res; | ||
} | ||
decodeBytes(x, buf) { | ||
let y = this.decodeBigInt(x); | ||
for (let i = buf.length; i-- > 0;) { | ||
buf[i] = Number(y & MASK); | ||
y >>= SHIFT; | ||
} | ||
return buf; | ||
} | ||
validate(x) { | ||
return new RegExp(`^[${this.base}]+$`).test(x); | ||
} | ||
size(x) { | ||
return Math.ceil(Math.log(x) / Math.log(this.N)); | ||
} | ||
return buf; | ||
} | ||
validate(x) { | ||
return new RegExp(`^[${this.base}]+$`).test(x); | ||
} | ||
size(x) { | ||
return Math.ceil(Math.log(x) / Math.log(this.N)); | ||
} | ||
} | ||
/** @internal */ | ||
const __pad = (x, size, fill) => { | ||
const d = size - x.length; | ||
return d > 0 ? fill.repeat(d) + x : x; | ||
const d = size - x.length; | ||
return d > 0 ? fill.repeat(d) + x : x; | ||
}; | ||
/** @internal */ | ||
const __u8 = (x) => __B16_LC_CHARS[(x >>> 4) & 0xf] + __B16_LC_CHARS[x & 0xf]; | ||
const __u8 = (x) => __B16_LC_CHARS[x >>> 4 & 15] + __B16_LC_CHARS[x & 15]; | ||
export { | ||
BaseN, | ||
__B16_LC_CHARS, | ||
defBase | ||
}; |
# Change Log | ||
- **Last updated**: 2023-12-09T19:12:03Z | ||
- **Last updated**: 2023-12-11T10:07:09Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -5,0 +5,0 @@ |
{ | ||
"name": "@thi.ng/base-n", | ||
"version": "2.5.19", | ||
"version": "2.5.20", | ||
"description": "Arbitrary base-n conversions w/ presets for base8/16/32/36/58/62/64/85, support for bigints and encoding/decoding of byte arrays", | ||
@@ -27,3 +27,5 @@ "type": "module", | ||
"scripts": { | ||
"build": "yarn clean && tsc --declaration", | ||
"build": "yarn build:esbuild && yarn build:decl", | ||
"build:decl": "tsc --declaration --emitDeclarationOnly", | ||
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts", | ||
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc", | ||
@@ -38,2 +40,3 @@ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"@microsoft/api-extractor": "^7.38.3", | ||
"esbuild": "^0.19.8", | ||
"rimraf": "^5.0.5", | ||
@@ -110,3 +113,3 @@ "tools": "^0.0.1", | ||
}, | ||
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n" | ||
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n" | ||
} |
@@ -54,3 +54,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 1.02 KB | ||
Package sizes (brotli'd, pre-treeshake): ESM: 1.01 KB | ||
@@ -57,0 +57,0 @@ ## Dependencies |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101
1
3
1
39
31439
6
355