Socket
Socket
Sign inDemoInstall

@thi.ng/base-n

Package Overview
Dependencies
Maintainers
1
Versions
99
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/base-n - npm Package Compare versions

Comparing version 2.6.4 to 2.7.0

10.d.ts

7

16.d.ts

@@ -1,8 +0,3 @@

export declare const B16_UC_CHARS = "0123456789ABCDEF";
export declare const B16_LC_CHARS = "0123456789abcdef";
export * from "./chars/16.js";
/**
* Alias for {@link B16_LC_CHARS}
*/
export declare const B16_CHARS = "0123456789abcdef";
/**
* Digits: 0-9 A-F

@@ -9,0 +4,0 @@ */

@@ -1,12 +0,8 @@

import { __B16_LC_CHARS, defBase } from "./base.js";
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);
import { defBase } from "./base.js";
import * as c from "./chars/16.js";
export * from "./chars/16.js";
const BASE16_UC = defBase(c.B16_UC_CHARS);
const BASE16_LC = defBase(c.B16_LC_CHARS);
const BASE16 = BASE16_LC;
export {
B16_CHARS,
B16_LC_CHARS,
B16_UC_CHARS,
BASE16,

@@ -13,0 +9,0 @@ BASE16_LC,

@@ -0,1 +1,2 @@

export * from "./chars/32.js";
/**

@@ -2,0 +3,0 @@ * Digits: A-Z 2-7

import { defBase } from "./base.js";
const BASE32_RFC4648 = defBase("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
import * as c from "./chars/32.js";
export * from "./chars/32.js";
const BASE32_RFC4648 = defBase(c.B32_RFC4648_CHARS);
const BASE32 = BASE32_RFC4648;
const BASE32_HEX = defBase("0123456789ABCDEFGHIJKLMNOPQRSTUV");
const BASE32_CROCKFORD = defBase("0123456789ABCDEFGHJKMNPQRSTVWXYZ");
const BASE32_HEX = defBase(c.B32_HEX_CHARS);
const BASE32_CROCKFORD = defBase(c.B32_CROCKFORD_CHARS);
export {

@@ -7,0 +9,0 @@ BASE32,

@@ -1,2 +0,2 @@

export declare const B36_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
export * from "./chars/36.js";
/**

@@ -3,0 +3,0 @@ * Digits: 0-9 A-Z

import { defBase } from "./base.js";
const B36_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const BASE36 = defBase(B36_CHARS);
import * as c from "./chars/36.js";
export * from "./chars/36.js";
const BASE36 = defBase(c.B36_CHARS);
export {
B36_CHARS,
BASE36
};

@@ -1,4 +0,2 @@

export declare const B58_CHARS_UC: string;
export declare const B58_CHARS_LC: string;
export declare const B58_CHARS: string;
export * from "./chars/58.js";
/**

@@ -5,0 +3,0 @@ * Reference: https://en.wikipedia.org/wiki/Binary-to-text_encoding

import { defBase } from "./base.js";
const D = "123456789";
const U = "ABCDEFGHJKLMNPQRSTUVWXYZ";
const L = "abcdefghijkmnopqrstuvwxyz";
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);
import * as c from "./chars/58.js";
export * from "./chars/58.js";
const BASE58_UC = defBase(c.B58_CHARS_UC);
const BASE58 = BASE58_UC;
const BASE58_LC = defBase(B58_CHARS_LC);
const BASE58_LC = defBase(c.B58_CHARS_LC);
export {
B58_CHARS,
B58_CHARS_LC,
B58_CHARS_UC,
BASE58,

@@ -16,0 +9,0 @@ BASE58_LC,

@@ -1,2 +0,2 @@

export declare const B62_CHARS: string;
export * from "./chars/62.js";
/**

@@ -3,0 +3,0 @@ * Digits: 0-9 A-Z a-z

@@ -1,8 +0,7 @@

import { B36_CHARS } from "./36.js";
import { defBase } from "./base.js";
const B62_CHARS = B36_CHARS + "abcdefghijklmnopqrstuvwxyz";
const BASE62 = defBase(B62_CHARS);
import * as c from "./chars/62.js";
export * from "./chars/62.js";
const BASE62 = defBase(c.B62_CHARS);
export {
B62_CHARS,
BASE62
};

@@ -1,2 +0,2 @@

export declare const B64_CHARS: string;
export * from "./chars/64.js";
/**

@@ -3,0 +3,0 @@ * Digits: 0-9 A-Z a-z + /

@@ -1,8 +0,7 @@

import { B62_CHARS } from "./62.js";
import { defBase } from "./base.js";
const B64_CHARS = B62_CHARS + "+/";
const BASE64 = defBase(B64_CHARS);
import * as c from "./chars/64.js";
export * from "./chars/64.js";
const BASE64 = defBase(c.B64_CHARS);
export {
B64_CHARS,
BASE64
};

@@ -1,2 +0,2 @@

export declare const B83_CHARS: string;
export * from "./chars/83.js";
/**

@@ -3,0 +3,0 @@ * Reference: https://github.com/woltapp/blurhash/blob/master/Algorithm.md#base-83

@@ -1,8 +0,7 @@

import { B62_CHARS } from "./62.js";
import { defBase } from "./base.js";
const B83_CHARS = B62_CHARS + "#$%*+,-.:;=?@[]^_{|}~";
const BASE83 = defBase(B83_CHARS);
import * as c from "./chars/83.js";
export * from "./chars/83.js";
const BASE83 = defBase(c.B83_CHARS);
export {
B83_CHARS,
BASE83
};

@@ -1,2 +0,2 @@

export declare const B85_CHARS: string;
export * from "./chars/85.js";
/**

@@ -3,0 +3,0 @@ * Reference: https://en.wikipedia.org/wiki/Ascii85

@@ -1,8 +0,7 @@

import { B62_CHARS } from "./62.js";
import { defBase } from "./base.js";
const B85_CHARS = B62_CHARS + "!#$%&()*+-;<=>?@^_`{|}~";
const BASE85 = defBase(B85_CHARS);
import * as c from "./chars/85.js";
export * from "./chars/85.js";
const BASE85 = defBase(c.B85_CHARS);
export {
B85_CHARS,
BASE85
};

@@ -1,4 +0,14 @@

export interface IBase {
export interface IBase extends BaseCommon, IBaseEncode, IBaseDecode {
}
export interface BaseCommon {
/**
* The base of this implementation.
*/
readonly N: number;
/**
* The character set (as string) used for the encoding.
*/
readonly base: string;
}
export interface IBaseEncode {
/**

@@ -32,2 +42,8 @@ * Encodes `x` into a baseN encoded string. `x` MUST be < 2^53. Use

encodeBytes(buf: Uint8Array, size?: number): string;
size(x: number): number;
}
/**
* Base-n decode-only interface.
*/
export interface IBaseDecode {
/**

@@ -34,0 +50,0 @@ * Decodes baseN encoded string `x` into a numeric value. Assumes the

import type { IBase } from "./api.js";
import { BaseNDecoder } from "./decode.js";
import { BaseNEncoder } from "./encode.js";
export declare const defBase: (chars: string) => BaseN;
/** @internal */
export declare const __B16_LC_CHARS = "0123456789abcdef";
export declare class BaseN implements IBase {
readonly base: string;
readonly N: number;
readonly index: Record<string, number>;
private __pc;
protected enc: BaseNEncoder;
protected dec: BaseNDecoder;
constructor(base: string);

@@ -20,4 +20,3 @@ clear(): void;

size(x: number): number;
private __pad;
}
//# sourceMappingURL=base.d.ts.map

@@ -0,7 +1,4 @@

import { BaseNDecoder } from "./decode.js";
import { BaseNEncoder } from "./encode.js";
const defBase = (chars) => new BaseN(chars);
const ZERO = BigInt(0);
const MASK = BigInt(255);
const SHIFT = BigInt(8);
const MAX_SAFE_INT = BigInt(2 ** 53 - 1);
const __B16_LC_CHARS = "0123456789abcdef";
class BaseN {

@@ -11,85 +8,39 @@ constructor(base) {

this.N = base.length;
this.index = [...base].reduce(
(acc, x, i) => (acc[x] = i, acc),
{}
);
this.enc = new BaseNEncoder(base);
this.dec = new BaseNDecoder(base);
}
N;
index;
__pc = [];
enc;
dec;
clear() {
this.__pc.length = 0;
this.enc.clear();
}
encode(x, size = 0) {
const { base, N } = this;
if (x === 0)
return this.__pad(base[0], size);
let res = "";
while (x > 0) {
res = base[x % N] + res;
x = Math.floor(x / N);
}
return this.__pad(res, size);
encode(x, size) {
return this.enc.encode(x, size);
}
encodeBigInt(x, size = 0) {
if (x <= MAX_SAFE_INT)
return this.encode(Number(x), size);
const { base, N } = this;
if (x === ZERO)
return this.__pad(base[0], size);
const NN = BigInt(N);
let res = "";
while (x > ZERO) {
res = base[Number(x % NN)] + res;
x /= NN;
}
return this.__pad(res, size);
encodeBigInt(x, size) {
return this.enc.encodeBigInt(x, size);
}
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);
encodeBytes(buf, size) {
return this.enc.encodeBytes(buf, 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);
}
return res;
return this.dec.decode(x);
}
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;
return this.dec.decodeBigInt(x);
}
decodeBytes(x, buf) {
let y = this.decodeBigInt(x);
for (let i = buf.length; i-- > 0; ) {
buf[i] = Number(y & MASK);
y >>= SHIFT;
}
return buf;
return this.dec.decodeBytes(x, buf);
}
validate(x) {
return new RegExp(`^[${this.base}]+$`).test(x);
return this.dec.validate(x);
}
size(x) {
return Math.ceil(Math.log(x) / Math.log(this.N));
return this.enc.size(x);
}
__pad(x, size) {
const d = size - x.length;
return d > 0 ? (this.__pc[d] || (this.__pc[d] = this.base[0].repeat(d))) + x : x;
}
}
const __u8 = (x) => __B16_LC_CHARS[x >>> 4 & 15] + __B16_LC_CHARS[x & 15];
export {
BaseN,
__B16_LC_CHARS,
defBase
};
# Change Log
- **Last updated**: 2024-01-30T21:37:18Z
- **Last updated**: 2024-02-06T23:18:11Z
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)

@@ -12,2 +12,12 @@

## [2.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/base-n@2.7.0) (2024-02-06)
#### 🚀 Features
- pkg restructure,separate decoder/encoder, add bases ([47e37bc](https://github.com/thi-ng/umbrella/commit/47e37bc))
- extract BaseNDecoder/Encoder classes
- add IBaseDecode/IBaseEncode interfaces
- migrate chatsets to own files to help w/ treeshaking
- add base10/26
## [2.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/base-n@2.6.0) (2024-01-26)

@@ -14,0 +24,0 @@

export * from "./api.js";
export * from "./base.js";
export * from "./8.js";
export * from "./10.js";
export * from "./16.js";
export * from "./26.js";
export * from "./32.js";

@@ -6,0 +8,0 @@ export * from "./36.js";

export * from "./api.js";
export * from "./base.js";
export * from "./8.js";
export * from "./10.js";
export * from "./16.js";
export * from "./26.js";
export * from "./32.js";

@@ -6,0 +8,0 @@ export * from "./36.js";

{
"name": "@thi.ng/base-n",
"version": "2.6.4",
"version": "2.7.0",
"description": "Arbitrary base-n conversions w/ presets for base8/16/32/36/58/62/64/83/85, support for bigints and encoding/decoding of byte arrays",

@@ -47,3 +47,5 @@ "type": "module",

"base8",
"base10",
"base16",
"base26",
"base32",

@@ -54,2 +56,3 @@ "base36",

"base64",
"base83",
"base85",

@@ -72,3 +75,4 @@ "bigint",

"./*.js",
"./*.d.ts"
"./*.d.ts",
"chars"
],

@@ -82,2 +86,5 @@ "exports": {

},
"./26": {
"default": "./26.js"
},
"./32": {

@@ -112,2 +119,35 @@ "default": "./32.js"

"default": "./base.js"
},
"./chars/16": {
"default": "./chars/16.js"
},
"./chars/26": {
"default": "./chars/26.js"
},
"./chars/32": {
"default": "./chars/32.js"
},
"./chars/36": {
"default": "./chars/36.js"
},
"./chars/58": {
"default": "./chars/58.js"
},
"./chars/62": {
"default": "./chars/62.js"
},
"./chars/64": {
"default": "./chars/64.js"
},
"./chars/83": {
"default": "./chars/83.js"
},
"./chars/85": {
"default": "./chars/85.js"
},
"./decode": {
"default": "./decode.js"
},
"./encode": {
"default": "./encode.js"
}

@@ -118,3 +158,3 @@ },

},
"gitHead": "a86521dfac00768f7e51a607209325eb72004730\n"
"gitHead": "ce8202c237a367c4038d41919a8acf75e1122507\n"
}

@@ -59,3 +59,3 @@ <!-- This file is generated - DO NOT EDIT! -->

Package sizes (brotli'd, pre-treeshake): ESM: 1.07 KB
Package sizes (brotli'd, pre-treeshake): ESM: 1.24 KB

@@ -62,0 +62,0 @@ ## Dependencies

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc