@ctrl/ts-base32 2.1.3 → 4.0.0

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
@@ -1,4 +1,4 @@
1
- # ts-base32 [![npm](https://badgen.net/npm/v/@ctrl/ts-base32)](https://www.npmjs.com/package/@ctrl/ts-base32) [![CircleCI](https://badgen.net/circleci/github/scttcper/ts-base32)](https://circleci.com/gh/scttcper/ts-base32) [![coverage](https://badgen.net/codecov/c/github/scttcper/ts-base32)](https://codecov.io/gh/scttcper/ts-base32) [![bundlesize](https://badgen.net/bundlephobia/min/@ctrl/ts-base32)](https://bundlephobia.com/result?p=@ctrl/ts-base32)
1
+ # ts-base32 [![npm](https://badgen.net/npm/v/@ctrl/ts-base32)](https://www.npmjs.com/package/@ctrl/ts-base32) [![coverage](https://badgen.net/codecov/c/github/scttcper/ts-base32)](https://codecov.io/gh/scttcper/ts-base32) [![bundlesize](https://badgen.net/bundlephobia/min/@ctrl/ts-base32)](https://bundlephobia.com/result?p=@ctrl/ts-base32)
2
2
 
3
3
  Base32 encode and decode in typescript exported as both commonjs and tree shakeable modules. Support for RFC4648, RFC4648_HEX, and CROCKFORD base32 encoding. Mostly directly taken from LinusU's packages.
4
4
 
@@ -14,17 +14,18 @@ npm install @ctrl/ts-base32
14
14
 
15
15
  ```ts
16
16
  import { base32Encode, base32Decode } from '@ctrl/ts-base32';
17
+ import { stringToUint8Array, uint8ArrayToString } from 'uint8array-extras';
17
18
 
18
- console.log(base32Encode(Buffer.from('a')));
19
+ console.log(base32Encode(stringToUint8Array('a')));
19
20
  // 'ME======'
20
21
 
21
- console.log(base32Encode(Buffer.from('a'), { padding: false }));
22
+ console.log(base32Encode(stringToUint8Array('a'), { padding: false }));
22
23
  // 'ME'
23
24
 
24
25
  console.log(base32Decode('ME======'));
25
- // ArrayBuffer { byteLength: 1 }
26
+ // Uint8Array
26
27
 
27
- console.log(Buffer.from(base32Decode('ME======')).toString());
28
+ console.log(uint8ArrayToString(base32Decode('ME======'))
28
29
  // 'a'
29
30
  ```
30
31
 
@@ -32,4 +33,4 @@ console.log(Buffer.from(base32Decode('ME======')).toString());
32
33
 
33
34
  base32-encode - https://github.com/LinusU/base32-encode
34
35
  base32-decode - https://github.com/LinusU/base32-decode
35
- hex-to-array-buffer - https://github.com/LinusU/hex-to-array-buffer
36
+ uint8array-extras - https://github.com/sindresorhus/uint8array-extras
@@ -1,10 +1,10 @@
1
1
  type Variant = 'RFC3548' | 'RFC4648' | 'RFC4648-HEX' | 'Crockford';
2
- export declare function base32Encode(buffer: ArrayBuffer, variant?: Variant, options?: Partial<{
2
+ export declare function base32Encode(input: Uint8Array, variant?: Variant, options?: Partial<{
3
3
  padding: boolean;
4
4
  }>): string;
5
- export declare function base32Decode(input: string, variant?: Variant): ArrayBuffer;
5
+ export declare function base32Decode(input: string, variant?: Variant): Uint8Array;
6
6
  /**
7
- * Turn a string of hexadecimal characters into an ArrayBuffer
7
+ * Turn a string of hexadecimal characters into an Uint8Array
8
8
  */
9
- export declare function hexToArrayBuffer(hex: string): ArrayBuffer;
9
+ export declare function hexToUint8Array(hex: string): Uint8Array;
10
10
  export {};
package/dist/src/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
2
2
  const RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
3
3
  const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
4
- export function base32Encode(buffer, variant = 'RFC4648', options = {}) {
4
+ export function base32Encode(input, variant = 'RFC4648', options = {}) {
5
5
  let alphabet;
6
6
  let defaultPadding;
7
7
  switch (variant) {
@@ -18,12 +18,13 @@ export function base32Encode(buffer, variant = 'RFC4648', options = {}) {
18
18
  alphabet = CROCKFORD;
19
19
  defaultPadding = false;
20
20
  break;
21
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
21
22
  default:
22
23
  throw new Error(`Unknown base32 variant: ${variant}`);
23
24
  }
24
25
  const padding = options.padding ?? defaultPadding;
25
- const length = buffer.byteLength;
26
- const view = new Uint8Array(buffer);
26
+ const length = input.byteLength;
27
+ const view = new Uint8Array(input);
27
28
  let bits = 0;
28
29
  let value = 0;
29
30
  let output = '';
@@ -69,6 +70,7 @@ export function base32Decode(input, variant = 'RFC4648') {
69
70
  alphabet = CROCKFORD;
70
71
  cleanedInput = input.toUpperCase().replace(/O/g, '0').replace(/[IL]/g, '1');
71
72
  break;
73
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
72
74
  default:
73
75
  throw new Error(`Unknown base32 variant: ${variant}`);
74
76
  }
@@ -85,12 +87,12 @@ export function base32Decode(input, variant = 'RFC4648') {
85
87
  bits -= 8;
86
88
  }
87
89
  }
88
- return output.buffer;
90
+ return output;
89
91
  }
90
92
  /**
91
- * Turn a string of hexadecimal characters into an ArrayBuffer
93
+ * Turn a string of hexadecimal characters into an Uint8Array
92
94
  */
93
- export function hexToArrayBuffer(hex) {
95
+ export function hexToUint8Array(hex) {
94
96
  if (hex.length % 2 !== 0) {
95
97
  throw new RangeError('Expected string to be an even number of characters');
96
98
  }
@@ -98,5 +100,5 @@ export function hexToArrayBuffer(hex) {
98
100
  for (let i = 0; i < hex.length; i += 2) {
99
101
  view[i / 2] = parseInt(hex.substring(i, i + 2), 16);
100
102
  }
101
- return view.buffer;
103
+ return view;
102
104
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl/ts-base32",
3
- "version": "2.1.3",
3
+ "version": "4.0.0",
4
4
  "description": "Base32 encoder/decoder with support for multiple variants",
5
5
  "author": "Scott Cooper <scttcper@gmail.com>",
6
6
  "license": "MIT",
@@ -21,10 +21,14 @@
21
21
  ],
22
22
  "sideEffects": false,
23
23
  "scripts": {
24
- "demo:build": "npm run build --workspace=demo",
25
- "demo:watch": "npm run dev --workspace=demo",
26
- "lint": "eslint --ext .ts .",
27
- "lint:fix": "eslint --fix --ext .ts .",
24
+ "demo:build": "pnpm run -r build",
25
+ "demo:watch": "pnpm run -r dev",
26
+ "lint": "pnpm run '/^(lint:biome|lint:eslint)$/'",
27
+ "lint:biome": "biome check .",
28
+ "lint:eslint": "eslint --ext .ts,.tsx .",
29
+ "lint:fix": "pnpm run '/^(lint:biome|lint:eslint):fix$/'",
30
+ "lint:eslint:fix": "eslint --ext .ts,.tsx . --fix",
31
+ "lint:biome:fix": "biome check . --apply",
28
32
  "prepare": "npm run build",
29
33
  "build": "tsc",
30
34
  "test": "vitest run",
@@ -32,18 +36,15 @@
32
36
  "test:ci": "vitest run --coverage --reporter=junit --outputFile=./junit.xml"
33
37
  },
34
38
  "devDependencies": {
35
- "@ctrl/eslint-config": "3.7.0",
36
- "@sindresorhus/tsconfig": "3.0.1",
37
- "@types/node": "20.1.0",
38
- "@vitest/coverage-c8": "0.31.0",
39
- "buffer": "6.0.3",
40
- "c8": "7.13.0",
41
- "typescript": "5.0.4",
42
- "vitest": "0.31.0"
39
+ "@biomejs/biome": "1.5.3",
40
+ "@ctrl/eslint-config-biome": "2.0.9",
41
+ "@sindresorhus/tsconfig": "5.0.0",
42
+ "@types/node": "20.11.24",
43
+ "@vitest/coverage-v8": "1.3.1",
44
+ "typescript": "5.3.3",
45
+ "uint8array-extras": "^1.1.0",
46
+ "vitest": "1.3.1"
43
47
  },
44
- "workspaces": [
45
- "demo"
46
- ],
47
48
  "publishConfig": {
48
49
  "access": "public",
49
50
  "provenance": true
@@ -54,6 +55,6 @@
54
55
  ]
55
56
  },
56
57
  "engines": {
57
- "node": ">=14.16"
58
+ "node": ">=18"
58
59
  }
59
60
  }