@wtasnorg/node-lib 0.0.10 → 0.0.12

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.
Files changed (62) hide show
  1. package/changelog.txt +14 -0
  2. package/docs/README.md +17 -0
  3. package/docs/docs.json +2148 -413
  4. package/docs/functions/countOnes.md +31 -0
  5. package/docs/functions/countZeroes.md +32 -0
  6. package/docs/functions/countZeroesWithWidth.md +37 -0
  7. package/docs/functions/createFindDirectories.md +3 -3
  8. package/docs/functions/decode.md +3 -3
  9. package/docs/functions/decode32.md +49 -0
  10. package/docs/functions/decode58.md +49 -0
  11. package/docs/functions/decode85.md +49 -0
  12. package/docs/functions/encode.md +3 -3
  13. package/docs/functions/encode32.md +45 -0
  14. package/docs/functions/encode58.md +45 -0
  15. package/docs/functions/encode85.md +45 -0
  16. package/docs/functions/hello.md +1 -1
  17. package/docs/functions/parseUserAgent.md +1 -1
  18. package/docs/functions/pojo.md +1 -1
  19. package/docs/functions/popcount32.md +27 -0
  20. package/docs/functions/popcount64.md +31 -0
  21. package/docs/interfaces/FileSystemDependencies.md +3 -3
  22. package/docs/interfaces/FindDirectoriesOptions.md +5 -5
  23. package/docs/interfaces/UserAgentInfo.md +6 -6
  24. package/docs/type-aliases/Base32CharsetType.md +13 -0
  25. package/docs/type-aliases/Base58CharsetType.md +13 -0
  26. package/docs/type-aliases/Base64CharsetType.md +1 -1
  27. package/docs/type-aliases/Base85CharsetType.md +13 -0
  28. package/docs/variables/Base32Charset.md +16 -0
  29. package/docs/variables/Base58Charset.md +16 -0
  30. package/docs/variables/Base64Charset.md +1 -1
  31. package/docs/variables/Base85Charset.md +16 -0
  32. package/package.json +52 -8
  33. package/readme.txt +30 -4
  34. package/src/base32.d.ts +58 -0
  35. package/src/base32.js +143 -0
  36. package/src/base32.test.d.ts +2 -0
  37. package/src/base32.test.js +121 -0
  38. package/src/base32.test.ts +144 -0
  39. package/src/base32.ts +169 -0
  40. package/src/base58.d.ts +58 -0
  41. package/src/base58.js +155 -0
  42. package/src/base58.test.d.ts +2 -0
  43. package/src/base58.test.js +108 -0
  44. package/src/base58.test.ts +128 -0
  45. package/src/base58.ts +177 -0
  46. package/src/base85.d.ts +58 -0
  47. package/src/base85.js +173 -0
  48. package/src/base85.test.d.ts +2 -0
  49. package/src/base85.test.js +107 -0
  50. package/src/base85.test.ts +125 -0
  51. package/src/base85.ts +199 -0
  52. package/src/bits.d.ts +43 -0
  53. package/src/bits.js +117 -0
  54. package/src/bits.test.d.ts +2 -0
  55. package/src/bits.test.js +62 -0
  56. package/src/bits.test.ts +77 -0
  57. package/src/bits.ts +135 -0
  58. package/src/index.d.ts +9 -2
  59. package/src/index.js +5 -1
  60. package/src/index.ts +26 -2
  61. package/.github/workflows/npm-publish.yml +0 -36
  62. package/.github/workflows/npm-test-on-pr.yml +0 -30
package/src/bits.ts ADDED
@@ -0,0 +1,135 @@
1
+ const UINT64_MASK = (1n << 64n) - 1n;
2
+
3
+ /**
4
+ *
5
+ * @param {number | bigint} value
6
+ * @param {string} fnName
7
+ * @returns {bigint}
8
+ * @throws {RangeError} If `value` is negative or an unsafe integer.
9
+ * @example
10
+ * toNonNegativeBigInt(42, "exampleFunction"); // returns 42n
11
+ * toNonNegativeBigInt(-1, "exampleFunction"); // throws RangeError
12
+ */
13
+ function toNonNegativeBigInt(value: number | bigint, fnName: string): bigint {
14
+ if (typeof value === "bigint") {
15
+ if (value < 0n) {
16
+ throw new RangeError(`${fnName}: value must be non-negative`);
17
+ }
18
+ return value;
19
+ }
20
+
21
+ if (!Number.isSafeInteger(value) || value < 0) {
22
+ throw new RangeError(`${fnName}: value must be a non-negative safe integer`);
23
+ }
24
+
25
+ return BigInt(value);
26
+ }
27
+
28
+ /**
29
+ * Count set bits in a bigint.
30
+ *
31
+ * @param {bigint} value - Input value.
32
+ * @returns {number} Number of set bits.
33
+ * @example
34
+ * popcountBigInt(0b1011n); // returns 3
35
+ */
36
+ function popcountBigInt(value: bigint): number {
37
+ let count = 0;
38
+ let bits = value;
39
+ while (bits !== 0n) {
40
+ bits &= bits - 1n;
41
+ count += 1;
42
+ }
43
+ return count;
44
+ }
45
+
46
+ /**
47
+ * Count set bits in an unsigned 32-bit representation of a number.
48
+ *
49
+ * @param {number} value - Number interpreted as uint32.
50
+ * @returns {number} Number of set bits (0-32).
51
+ */
52
+ function popcount32(value: number): number {
53
+ let bits = value >>> 0;
54
+ bits -= (bits >>> 1) & 0x55555555;
55
+ bits = (bits & 0x33333333) + ((bits >>> 2) & 0x33333333);
56
+ bits = (bits + (bits >>> 4)) & 0x0f0f0f0f;
57
+ bits += bits >>> 8;
58
+ bits += bits >>> 16;
59
+ return bits & 0x3f;
60
+ }
61
+
62
+ /**
63
+ * Count set bits in the low 64 bits of a number or bigint.
64
+ *
65
+ * @param {number | bigint} value - Input value. For `number`, it must be a non-negative safe integer.
66
+ * @returns {number} Number of set bits (0-64).
67
+ * @throws {RangeError} If `value` is negative or an unsafe integer.
68
+ */
69
+ function popcount64(value: number | bigint): number {
70
+ const bits64 = toNonNegativeBigInt(value, "popcount64") & UINT64_MASK;
71
+ return popcountBigInt(bits64);
72
+ }
73
+
74
+ /**
75
+ * Count set bits in a non-negative integer.
76
+ *
77
+ * @param {number | bigint} value - Input value.
78
+ * @returns {number} Number of set bits.
79
+ * @throws {RangeError} If `value` is negative or an unsafe integer.
80
+ */
81
+ function countOnes(value: number | bigint): number {
82
+ const bits = toNonNegativeBigInt(value, "countOnes");
83
+ return popcountBigInt(bits);
84
+ }
85
+
86
+ /**
87
+ * Count zero bits within a fixed width, considering only low-order `width` bits.
88
+ *
89
+ * @param {number | bigint} value - Input value.
90
+ * @param {number} width - Bit width to inspect.
91
+ * @returns {number} Number of zero bits in the selected width.
92
+ * @throws {RangeError} If `value` is negative/unsafe or `width` is negative/unsafe.
93
+ */
94
+ function countZeroesWithWidth(value: number | bigint, width: number): number {
95
+ if (!Number.isSafeInteger(width) || width < 0) {
96
+ throw new RangeError("countZeroesWithWidth: width must be a non-negative safe integer");
97
+ }
98
+
99
+ if (width === 0) {
100
+ return 0;
101
+ }
102
+
103
+ const bits = toNonNegativeBigInt(value, "countZeroesWithWidth");
104
+ const widthBigInt = BigInt(width);
105
+ const mask = (1n << widthBigInt) - 1n;
106
+ const ones = popcountBigInt(bits & mask);
107
+ return width - ones;
108
+ }
109
+
110
+ /**
111
+ * Count zero bits from LSB up to and including the highest set bit.
112
+ * For zero input, returns 0.
113
+ *
114
+ * @param {number | bigint} value - Input value.
115
+ * @returns {number} Zero count up to the left-most set bit.
116
+ * @throws {RangeError} If `value` is negative or an unsafe integer.
117
+ */
118
+ function countZeroes(value: number | bigint): number {
119
+ const bits = toNonNegativeBigInt(value, "countZeroes");
120
+ if (bits === 0n) {
121
+ return 0;
122
+ }
123
+
124
+ const width = bits.toString(2).length;
125
+ const ones = popcountBigInt(bits);
126
+ return width - ones;
127
+ }
128
+
129
+ export {
130
+ popcount32,
131
+ popcount64,
132
+ countOnes,
133
+ countZeroesWithWidth,
134
+ countZeroes
135
+ };
package/src/index.d.ts CHANGED
@@ -6,6 +6,13 @@ import type { UserAgentInfo } from "./user-agent.js";
6
6
  import { parseUserAgent } from "./user-agent.js";
7
7
  import type { Base64CharsetType } from "./base64.js";
8
8
  import { encode, decode, Base64Charset } from "./base64.js";
9
- export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset };
10
- export type { FindDirectoriesOptions, FileSystemDependencies, UserAgentInfo, Base64CharsetType };
9
+ import type { Base58CharsetType } from "./base58.js";
10
+ import { encode58, decode58, Base58Charset } from "./base58.js";
11
+ import type { Base85CharsetType } from "./base85.js";
12
+ import { encode85, decode85, Base85Charset } from "./base85.js";
13
+ import type { Base32CharsetType } from "./base32.js";
14
+ import { encode32, decode32, Base32Charset } from "./base32.js";
15
+ import { popcount32, popcount64, countOnes, countZeroesWithWidth, countZeroes } from "./bits.js";
16
+ export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset, encode58, decode58, Base58Charset, encode85, decode85, Base85Charset, encode32, decode32, Base32Charset, popcount32, popcount64, countOnes, countZeroesWithWidth, countZeroes };
17
+ export type { FindDirectoriesOptions, FileSystemDependencies, UserAgentInfo, Base64CharsetType, Base58CharsetType, Base85CharsetType, Base32CharsetType };
11
18
  //# sourceMappingURL=index.d.ts.map
package/src/index.js CHANGED
@@ -3,5 +3,9 @@ import { pojo } from "./pojo.js";
3
3
  import { createFindDirectories } from "./find.js";
4
4
  import { parseUserAgent } from "./user-agent.js";
5
5
  import { encode, decode, Base64Charset } from "./base64.js";
6
- export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset };
6
+ import { encode58, decode58, Base58Charset } from "./base58.js";
7
+ import { encode85, decode85, Base85Charset } from "./base85.js";
8
+ import { encode32, decode32, Base32Charset } from "./base32.js";
9
+ import { popcount32, popcount64, countOnes, countZeroesWithWidth, countZeroes } from "./bits.js";
10
+ export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset, encode58, decode58, Base58Charset, encode85, decode85, Base85Charset, encode32, decode32, Base32Charset, popcount32, popcount64, countOnes, countZeroesWithWidth, countZeroes };
7
11
  //# sourceMappingURL=index.js.map
package/src/index.ts CHANGED
@@ -6,6 +6,13 @@ import type { UserAgentInfo } from "./user-agent.js";
6
6
  import { parseUserAgent } from "./user-agent.js";
7
7
  import type { Base64CharsetType } from "./base64.js";
8
8
  import { encode, decode, Base64Charset } from "./base64.js";
9
+ import type { Base58CharsetType } from "./base58.js";
10
+ import { encode58, decode58, Base58Charset } from "./base58.js";
11
+ import type { Base85CharsetType } from "./base85.js";
12
+ import { encode85, decode85, Base85Charset } from "./base85.js";
13
+ import type { Base32CharsetType } from "./base32.js";
14
+ import { encode32, decode32, Base32Charset } from "./base32.js";
15
+ import { popcount32, popcount64, countOnes, countZeroesWithWidth, countZeroes } from "./bits.js";
9
16
 
10
17
  export {
11
18
  hello,
@@ -14,12 +21,29 @@ export {
14
21
  parseUserAgent,
15
22
  encode,
16
23
  decode,
17
- Base64Charset
24
+ Base64Charset,
25
+ encode58,
26
+ decode58,
27
+ Base58Charset,
28
+ encode85,
29
+ decode85,
30
+ Base85Charset,
31
+ encode32,
32
+ decode32,
33
+ Base32Charset,
34
+ popcount32,
35
+ popcount64,
36
+ countOnes,
37
+ countZeroesWithWidth,
38
+ countZeroes
18
39
  };
19
40
 
20
41
  export type {
21
42
  FindDirectoriesOptions,
22
43
  FileSystemDependencies,
23
44
  UserAgentInfo,
24
- Base64CharsetType
45
+ Base64CharsetType,
46
+ Base58CharsetType,
47
+ Base85CharsetType,
48
+ Base32CharsetType
25
49
  };
@@ -1,36 +0,0 @@
1
- # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
- # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
-
4
- name: Node.js Package
5
-
6
- on:
7
- release:
8
- types: [created]
9
-
10
- jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
- steps:
14
- - uses: actions/checkout@v4
15
- - uses: actions/setup-node@v4
16
- with:
17
- node-version: 20
18
- - run: npm clean-install
19
- - run: npm run build
20
- - run: npm run test
21
- - run: npm run docs
22
- - run: npm run docs:json
23
-
24
- publish-npm:
25
- needs: build
26
- runs-on: ubuntu-latest
27
- steps:
28
- - uses: actions/checkout@v4
29
- - uses: actions/setup-node@v4
30
- with:
31
- node-version: 20
32
- registry-url: https://registry.npmjs.org/
33
- - run: npm clean-install
34
- - run: npm publish
35
- env:
36
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
@@ -1,30 +0,0 @@
1
- name: Run tests on pull request
2
-
3
- on:
4
- pull_request:
5
- types: [opened, reopened, synchronize]
6
-
7
- permissions:
8
- contents: read
9
-
10
- jobs:
11
- run-tests:
12
- runs-on: ubuntu-latest
13
-
14
- steps:
15
- - name: Checkout repository
16
- uses: actions/checkout@v4
17
- with:
18
- fetch-depth: 0
19
-
20
- - name: Setup Node.js
21
- uses: actions/setup-node@v4
22
- with:
23
- node-version: 20
24
- cache: npm
25
-
26
- - name: Install dependencies
27
- run: npm ci
28
-
29
- - name: Run tests
30
- run: npm test