@natoboram/based.ts 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,22 @@
2
2
 
3
3
  [![GitHub Pages](https://github.com/NatoBoram/based.ts/actions/workflows/github-pages.yaml/badge.svg)](https://github.com/NatoBoram/based.ts/actions/workflows/github-pages.yaml) [![Node.js CI](https://github.com/NatoBoram/based.ts/actions/workflows/node.js.yaml/badge.svg)](https://github.com/NatoBoram/based.ts/actions/workflows/node.js.yaml)
4
4
 
5
- Change between bases.
5
+ A TypeScript library for working with arbitrary bases.
6
+
7
+ ## Installation
8
+
9
+ It can be installed globally if you want to generate base36-encoded UUIDs.
10
+
11
+ ```sh
12
+ pnpm install --global @natoboram/based.ts
13
+ basedts
14
+ ```
15
+
16
+ ```log
17
+ Base 36 UUID: 2pcugbwbg50o24pnu8h3u1f0b
18
+ ```
19
+
20
+ Proper CLI options are planned for the future, but for now that's all I needed this package for.
6
21
 
7
22
  ## Usage
8
23
 
@@ -11,8 +26,19 @@ pnpm i -D @natoboram/based.ts
11
26
  ```
12
27
 
13
28
  ```ts
14
- import { base36Uuid } from "@natoboram/based.ts"
29
+ import {
30
+ basedToBigInt,
31
+ bytesToBigInt,
32
+ getRandomBytes,
33
+ toBase,
34
+ } from "@natoboram/based.ts"
35
+
36
+ // Generate a base36-encoded UUID
37
+ const bytes = getRandomBytes()
38
+ const bigInt = bytesToBigInt(bytes)
39
+ const base36 = toBase(bigInt, 36n)
40
+ console.log("Base 36 UUID:", base36)
15
41
 
16
- const uuid = base36Uuid()
17
- console.log("Base 36 UUID:", uuid)
42
+ // Convert between two bases
43
+ const base64 = toBase(basedToBigInt("20zsnycqen1k898slr7xgnc9t", 36n), 64n)
18
44
  ```
@@ -0,0 +1,14 @@
1
+ /** Base-encoded string. Use this class if you convert from and to different
2
+ * bases often. Otherwise, just use the provided utilities. You can provide a custom number's space */
3
+ export declare class Based {
4
+ readonly value: string;
5
+ readonly base: bigint;
6
+ readonly space: string;
7
+ constructor(value: string, base: bigint, space?: string);
8
+ divide(based: Based): Based;
9
+ minus(based: Based): Based;
10
+ multiply(based: Based): Based;
11
+ plus(based: Based): Based;
12
+ to(base: bigint, space?: string): Based;
13
+ }
14
+ //# sourceMappingURL=based.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"based.d.ts","sourceRoot":"","sources":["../src/based.ts"],"names":[],"mappings":"AAIA;sGACsG;AACtG,qBAAa,KAAK;IAEhB,QAAQ,CAAC,KAAK,EAAE,MAAM;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,QAAQ,CAAC,KAAK;gBAFL,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,SAAc;IAG7B,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAO3B,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAO1B,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAO7B,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK;IAOzB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK;CAKvC"}
package/dist/based.js ADDED
@@ -0,0 +1,45 @@
1
+ import { base64Space } from "./space.js";
2
+ import { toBase } from "./to_base.js";
3
+ import { basedToBigInt } from "./to_bigint.js";
4
+ /** Base-encoded string. Use this class if you convert from and to different
5
+ * bases often. Otherwise, just use the provided utilities. You can provide a custom number's space */
6
+ export class Based {
7
+ value;
8
+ base;
9
+ space;
10
+ constructor(value, base, space = base64Space) {
11
+ this.value = value;
12
+ this.base = base;
13
+ this.space = space;
14
+ }
15
+ divide(based) {
16
+ const dividend = basedToBigInt(this.value, this.base, this.space);
17
+ const divisor = basedToBigInt(based.value, based.base, based.space);
18
+ const quotient = dividend / divisor;
19
+ return new Based(toBase(quotient, this.base, this.space), this.base);
20
+ }
21
+ minus(based) {
22
+ const minuend = basedToBigInt(this.value, this.base, this.space);
23
+ const subtrahend = basedToBigInt(based.value, based.base, based.space);
24
+ const difference = minuend - subtrahend;
25
+ return new Based(toBase(difference, this.base, this.space), this.base);
26
+ }
27
+ multiply(based) {
28
+ const multiplier = basedToBigInt(this.value, this.base, this.space);
29
+ const multiplicand = basedToBigInt(based.value, based.base, based.space);
30
+ const product = multiplier * multiplicand;
31
+ return new Based(toBase(product, this.base, this.space), this.base);
32
+ }
33
+ plus(based) {
34
+ const augend = basedToBigInt(this.value, this.base, this.space);
35
+ const addend = basedToBigInt(based.value, based.base, based.space);
36
+ const sum = augend + addend;
37
+ return new Based(toBase(sum, this.base, this.space), this.base);
38
+ }
39
+ to(base, space) {
40
+ const bigInt = basedToBigInt(this.value, this.base, this.space);
41
+ const value = toBase(bigInt, base, space);
42
+ return new Based(value, base, space);
43
+ }
44
+ }
45
+ //# sourceMappingURL=based.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"based.js","sourceRoot":"","sources":["../src/based.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAE9C;sGACsG;AACtG,MAAM,OAAO,KAAK;IAEP;IACA;IACA;IAHV,YACU,KAAa,EACb,IAAY,EACZ,QAAQ,WAAW;QAFnB,UAAK,GAAL,KAAK,CAAQ;QACb,SAAI,GAAJ,IAAI,CAAQ;QACZ,UAAK,GAAL,KAAK,CAAc;IAC1B,CAAC;IAEJ,MAAM,CAAC,KAAY;QAClB,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QACjE,MAAM,OAAO,GAAG,aAAa,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CA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IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QAC/D,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,CAAA;QAClE,MAAM,GAAG,GAAG,MAAM,GAAG,MAAM,CAAA;QAC3B,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IAChE,CAAC;IAED,EAAE,CAAC,IAAY,EAAE,KAAc;QAC9B,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QAC/D,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;QACzC,OAAO,IAAI,KAAK,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;IACrC,CAAC;CACD"}
@@ -0,0 +1,4 @@
1
+ /** Get a randomized selection of bytes from `crypto`. By default, get 128 bits,
2
+ * a UUID's worth of bytes. */
3
+ export declare function getRandomBytes(length?: number): Uint8ClampedArray;
4
+ //# sourceMappingURL=bytes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAAA;8BAC8B;AAC9B,wBAAgB,cAAc,CAAC,MAAM,SAAK,qBAIzC"}
package/dist/bytes.js ADDED
@@ -0,0 +1,8 @@
1
+ /** Get a randomized selection of bytes from `crypto`. By default, get 128 bits,
2
+ * a UUID's worth of bytes. */
3
+ export function getRandomBytes(length = 16) {
4
+ const bytes = new Uint8ClampedArray(length);
5
+ crypto.getRandomValues(bytes);
6
+ return bytes;
7
+ }
8
+ //# sourceMappingURL=bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../src/bytes.ts"],"names":[],"mappings":"AAAA;8BAC8B;AAC9B,MAAM,UAAU,cAAc,CAAC,MAAM,GAAG,EAAE;IACzC,MAAM,KAAK,GAAG,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAA;IAC3C,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;IAC7B,OAAO,KAAK,CAAA;AACb,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,8 @@
1
- export * from "./base36-uuid.js";
1
+ export * from "./based.js";
2
+ export * from "./bytes.js";
3
+ export * from "./space.js";
4
+ export * from "./to_base.js";
5
+ export * from "./to_bigint.js";
6
+ export * from "./to_bytes.js";
7
+ export * from "./typed_array.js";
2
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA"}
package/dist/index.js CHANGED
@@ -1,2 +1,8 @@
1
- export * from "./base36-uuid.js";
1
+ export * from "./based.js";
2
+ export * from "./bytes.js";
3
+ export * from "./space.js";
4
+ export * from "./to_base.js";
5
+ export * from "./to_bigint.js";
6
+ export * from "./to_bytes.js";
7
+ export * from "./typed_array.js";
2
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA"}
package/dist/main.js CHANGED
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import { base36Uuid } from "./base36-uuid.js";
3
- const uuid = base36Uuid();
4
- console.log("Base 36 UUID:", uuid);
2
+ import { bytesToBigInt, getRandomBytes, toBase } from "./index.js";
3
+ const bytes = getRandomBytes();
4
+ const bigInt = bytesToBigInt(bytes);
5
+ const base36 = toBase(bigInt, 36n);
6
+ console.log("Base 36 UUID:", base36);
5
7
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,MAAM,IAAI,GAAG,UAAU,EAAE,CAAA;AACzB,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,IAAI,CAAC,CAAA"}
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAElE,MAAM,KAAK,GAAG,cAAc,EAAE,CAAA;AAC9B,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAA;AACnC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAElC,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,MAAM,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ /** Default number space for this package. */
2
+ export declare const base64Space = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
3
+ //# sourceMappingURL=space.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"space.d.ts","sourceRoot":"","sources":["../src/space.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,eAAO,MAAM,WAAW,qEAC2C,CAAA"}
package/dist/space.js ADDED
@@ -0,0 +1,3 @@
1
+ /** Default number space for this package. */
2
+ export const base64Space = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
3
+ //# sourceMappingURL=space.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"space.js","sourceRoot":"","sources":["../src/space.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,CAAC,MAAM,WAAW,GACvB,kEAAkE,CAAA"}
@@ -0,0 +1,3 @@
1
+ /** Convert a number to a string in a given base. */
2
+ export declare function toBase(value: bigint, base: bigint, space?: string): string;
3
+ //# sourceMappingURL=to_base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_base.d.ts","sourceRoot":"","sources":["../src/to_base.ts"],"names":[],"mappings":"AAEA,oDAAoD;AACpD,wBAAgB,MAAM,CACrB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,SAAc,GACjB,MAAM,CAgBR"}
@@ -0,0 +1,18 @@
1
+ import { base64Space } from "./space.js";
2
+ /** Convert a number to a string in a given base. */
3
+ export function toBase(value, base, space = base64Space) {
4
+ if (space.length < Number(base))
5
+ throw new Error("Invalid space for this base", {
6
+ cause: { value, base, space },
7
+ });
8
+ if (value < 0n)
9
+ return `-${toBase(-value, base, space)}`;
10
+ let result = "";
11
+ while (value) {
12
+ const digit = Number(value % base);
13
+ value /= base;
14
+ result = space[digit] + result;
15
+ }
16
+ return result;
17
+ }
18
+ //# sourceMappingURL=to_base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_base.js","sourceRoot":"","sources":["../src/to_base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAExC,oDAAoD;AACpD,MAAM,UAAU,MAAM,CACrB,KAAa,EACb,IAAY,EACZ,KAAK,GAAG,WAAW;IAEnB,IAAI,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,6BAA6B,EAAE;YAC9C,KAAK,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE;SAC7B,CAAC,CAAA;IAEH,IAAI,KAAK,GAAG,EAAE;QAAE,OAAO,IAAI,MAAM,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,CAAA;IAExD,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,OAAO,KAAK,EAAE,CAAC;QACd,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;QAClC,KAAK,IAAI,IAAI,CAAA;QACb,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,MAAM,CAAA;IAC/B,CAAC;IAED,OAAO,MAAM,CAAA;AACd,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { TypedUintArray } from "./typed_array.js";
2
+ /** Turns a `string` in a given `base` into a `bigint` by multiplying each digit
3
+ * by the `base` raised to the power of its position in the `string`. */
4
+ export declare function basedToBigInt(based: string, base: bigint, space?: string): bigint;
5
+ /** Turns a typed `uint` array into a `bigint` by converting each `uint` into a
6
+ * hexadecimal string then concatenating the resulting digits using JavaScript's
7
+ * `0x` notation. */
8
+ export declare function bytesToBigInt(typedArray: TypedUintArray): bigint;
9
+ //# sourceMappingURL=to_bigint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_bigint.d.ts","sourceRoot":"","sources":["../src/to_bigint.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAEtD;wEACwE;AACxE,wBAAgB,aAAa,CAC5B,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,SAAc,GACjB,MAAM,CAuBR;AAED;;oBAEoB;AACpB,wBAAgB,aAAa,CAAC,UAAU,EAAE,cAAc,GAAG,MAAM,CAUhE"}
@@ -0,0 +1,32 @@
1
+ import { base64Space } from "./space.js";
2
+ /** Turns a `string` in a given `base` into a `bigint` by multiplying each digit
3
+ * by the `base` raised to the power of its position in the `string`. */
4
+ export function basedToBigInt(based, base, space = base64Space) {
5
+ space = space.slice(0, Number(base));
6
+ if (space.length < Number(base))
7
+ throw new Error("Invalid space for this base", {
8
+ cause: { based, base, space },
9
+ });
10
+ if (base <= 36)
11
+ based = based.toLowerCase();
12
+ if (based.startsWith("-"))
13
+ return -basedToBigInt(based.slice(1), base, space);
14
+ return Array.from(based).reduce((result, digit, digitIndex) => {
15
+ const spaceIndex = space.indexOf(digit);
16
+ if (spaceIndex == -1)
17
+ throw new Error("Invalid digit for this space", {
18
+ cause: { based, base, space, result, digit, digitIndex, spaceIndex },
19
+ });
20
+ return (result +
21
+ BigInt(spaceIndex) * base ** BigInt(based.length - 1 - digitIndex));
22
+ }, 0n);
23
+ }
24
+ /** Turns a typed `uint` array into a `bigint` by converting each `uint` into a
25
+ * hexadecimal string then concatenating the resulting digits using JavaScript's
26
+ * `0x` notation. */
27
+ export function bytesToBigInt(typedArray) {
28
+ const maxLength = typedArray.BYTES_PER_ELEMENT * 2;
29
+ const hexes = Array.from(typedArray).reduce((hexes, value) => (hexes.push(value.toString(16).padStart(maxLength, "0")), hexes), new Array(typedArray.length));
30
+ return BigInt(`0x${hexes.join("")}`);
31
+ }
32
+ //# sourceMappingURL=to_bigint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_bigint.js","sourceRoot":"","sources":["../src/to_bigint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAGxC;wEACwE;AACxE,MAAM,UAAU,aAAa,CAC5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,CAAC,CAAA;IAClD,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,CAC1C,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CACjB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,EAAE,KAAK,CAC9D,EACD,IAAI,KAAK,CAAS,UAAU,CAAC,MAAM,CAAC,CACpC,CAAA;IAED,OAAO,MAAM,CAAC,KAAK,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA;AACrC,CAAC"}
@@ -0,0 +1,4 @@
1
+ /** Turns a `bigint` into a `Uint8ClampedArray` by converting it to a
2
+ * hexadecimal string then turning each digit into a pair of bytes. */
3
+ export declare function bigIntToBytes(bigInt: bigint): Uint8ClampedArray;
4
+ //# sourceMappingURL=to_bytes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_bytes.d.ts","sourceRoot":"","sources":["../src/to_bytes.ts"],"names":[],"mappings":"AAAA;sEACsE;AACtE,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,iBAAiB,CAa/D"}
@@ -0,0 +1,15 @@
1
+ /** Turns a `bigint` into a `Uint8ClampedArray` by converting it to a
2
+ * hexadecimal string then turning each digit into a pair of bytes. */
3
+ export function bigIntToBytes(bigInt) {
4
+ let hexes = bigInt.toString(16);
5
+ if (hexes.length % 2)
6
+ hexes = `0${hexes}`;
7
+ const length = hexes.length / 2;
8
+ const bytes = new Uint8ClampedArray(length);
9
+ for (let index = 0; index < length; index++) {
10
+ const hex = hexes.slice(index * 2, index * 2 + 2);
11
+ bytes[index] = parseInt(hex, 16);
12
+ }
13
+ return bytes;
14
+ }
15
+ //# sourceMappingURL=to_bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to_bytes.js","sourceRoot":"","sources":["../src/to_bytes.ts"],"names":[],"mappings":"AAAA;sEACsE;AACtE,MAAM,UAAU,aAAa,CAAC,MAAc;IAC3C,IAAI,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;IAC/B,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,KAAK,GAAG,IAAI,KAAK,EAAE,CAAA;IAEzC,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAA;IAC/B,MAAM,KAAK,GAAG,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAA;IAE3C,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;QACjD,KAAK,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IACjC,CAAC;IAED,OAAO,KAAK,CAAA;AACb,CAAC"}
@@ -0,0 +1,2 @@
1
+ export type TypedUintArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array;
2
+ //# sourceMappingURL=typed_array.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typed_array.d.ts","sourceRoot":"","sources":["../src/typed_array.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GACvB,UAAU,GACV,iBAAiB,GACjB,WAAW,GACX,WAAW,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=typed_array.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typed_array.js","sourceRoot":"","sources":["../src/typed_array.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@natoboram/based.ts",
3
- "version": "0.0.1",
4
- "description": "Change between bases",
3
+ "version": "0.0.2",
4
+ "description": "A TypeScript library for working with arbitrary bases",
5
5
  "keywords": [
6
+ "base2",
7
+ "base10",
8
+ "base16",
6
9
  "base36",
10
+ "base62",
11
+ "base64",
7
12
  "uuid"
8
13
  ],
9
14
  "homepage": "https://github.com/NatoBoram/based.ts",
@@ -1,8 +0,0 @@
1
- export declare function stringToBits(str: string): Uint8Array;
2
- export declare function get128Bits(): Uint8ClampedArray;
3
- export declare function bytesToBigInt(bytes: Uint8ClampedArray): bigint;
4
- export declare function bigIntToBytes(bn: bigint): Uint8ClampedArray;
5
- export declare function bigIntToBase36(bigInt: bigint): string;
6
- export declare function base36ToBigInt(base36: string): bigint;
7
- export declare function base36Uuid(): string;
8
- //# sourceMappingURL=base36-uuid.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base36-uuid.d.ts","sourceRoot":"","sources":["../src/base36-uuid.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,cAEvC;AAED,wBAAgB,UAAU,sBAIzB;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,iBAAiB,UAWrD;AAED,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,qBAavC;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,UAE5C;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,UAK5C;AAED,wBAAgB,UAAU,WAKzB"}
@@ -1,43 +0,0 @@
1
- export function stringToBits(str) {
2
- return new TextEncoder().encode(str);
3
- }
4
- export function get128Bits() {
5
- const bytes = new Uint8ClampedArray(16);
6
- crypto.getRandomValues(bytes);
7
- return bytes;
8
- }
9
- export function bytesToBigInt(bytes) {
10
- const hexes = bytes.reduce((hexes, byte) => {
11
- /** A single hexadecimal number. Because the byte is 8 bits, the hex ranges
12
- * from `00` to `FF`. */
13
- const hex = byte.toString(16).padStart(2, "0");
14
- hexes.push(hex);
15
- return hexes;
16
- }, new Array(16));
17
- return BigInt(`0x${hexes.join("")}`);
18
- }
19
- export function bigIntToBytes(bn) {
20
- let hexes = bn.toString(16);
21
- if (hexes.length % 2)
22
- hexes = `0${hexes}`;
23
- const length = hexes.length / 2;
24
- const bytes = new Uint8ClampedArray(length);
25
- for (let index = 0; index < length; index++) {
26
- const hex = hexes.slice(index * 2, index * 2 + 2);
27
- bytes[index] = parseInt(hex, 16);
28
- }
29
- return bytes;
30
- }
31
- export function bigIntToBase36(bigInt) {
32
- return bigInt.toString(36);
33
- }
34
- export function base36ToBigInt(base36) {
35
- return Array.from(base36).reduce((bigInt, digit) => bigInt * 36n + BigInt(parseInt(digit, 36)), 0n);
36
- }
37
- export function base36Uuid() {
38
- const bytes = get128Bits();
39
- const bigInt = bytesToBigInt(bytes);
40
- const base36 = bigIntToBase36(bigInt);
41
- return base36;
42
- }
43
- //# sourceMappingURL=base36-uuid.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base36-uuid.js","sourceRoot":"","sources":["../src/base36-uuid.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAC,GAAW;IACvC,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACrC,CAAC;AAED,MAAM,UAAU,UAAU;IACzB,MAAM,KAAK,GAAG,IAAI,iBAAiB,CAAC,EAAE,CAAC,CAAA;IACvC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;IAC7B,OAAO,KAAK,CAAA;AACb,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAwB;IACrD,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QAC1C;gCACwB;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;QAE9C,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QACf,OAAO,KAAK,CAAA;IACb,CAAC,EAAE,IAAI,KAAK,CAA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