@wtasnorg/node-lib 0.0.9 → 0.0.11

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 (55) hide show
  1. package/changelog.txt +14 -0
  2. package/dev_checklist.txt +9 -3
  3. package/docs/README.md +12 -0
  4. package/docs/docs.json +1523 -233
  5. package/docs/functions/createFindDirectories.md +1 -1
  6. package/docs/functions/decode.md +1 -1
  7. package/docs/functions/decode32.md +49 -0
  8. package/docs/functions/decode58.md +49 -0
  9. package/docs/functions/decode85.md +49 -0
  10. package/docs/functions/encode.md +1 -1
  11. package/docs/functions/encode32.md +45 -0
  12. package/docs/functions/encode58.md +45 -0
  13. package/docs/functions/encode85.md +45 -0
  14. package/docs/functions/hello.md +35 -3
  15. package/docs/functions/parseUserAgent.md +1 -1
  16. package/docs/functions/pojo.md +1 -1
  17. package/docs/interfaces/FileSystemDependencies.md +3 -3
  18. package/docs/interfaces/FindDirectoriesOptions.md +5 -5
  19. package/docs/interfaces/UserAgentInfo.md +6 -6
  20. package/docs/type-aliases/Base32CharsetType.md +13 -0
  21. package/docs/type-aliases/Base58CharsetType.md +13 -0
  22. package/docs/type-aliases/Base64CharsetType.md +1 -1
  23. package/docs/type-aliases/Base85CharsetType.md +13 -0
  24. package/docs/variables/Base32Charset.md +16 -0
  25. package/docs/variables/Base58Charset.md +16 -0
  26. package/docs/variables/Base64Charset.md +1 -1
  27. package/docs/variables/Base85Charset.md +16 -0
  28. package/package.json +44 -3
  29. package/readme.txt +12 -0
  30. package/src/base32.d.ts +58 -0
  31. package/src/base32.js +143 -0
  32. package/src/base32.test.d.ts +2 -0
  33. package/src/base32.test.js +121 -0
  34. package/src/base32.test.ts +144 -0
  35. package/src/base32.ts +169 -0
  36. package/src/base58.d.ts +58 -0
  37. package/src/base58.js +155 -0
  38. package/src/base58.test.d.ts +2 -0
  39. package/src/base58.test.js +108 -0
  40. package/src/base58.test.ts +128 -0
  41. package/src/base58.ts +177 -0
  42. package/src/base85.d.ts +58 -0
  43. package/src/base85.js +173 -0
  44. package/src/base85.test.d.ts +2 -0
  45. package/src/base85.test.js +107 -0
  46. package/src/base85.test.ts +125 -0
  47. package/src/base85.ts +199 -0
  48. package/src/hello.d.ts +27 -2
  49. package/src/hello.js +29 -4
  50. package/src/hello.test.js +11 -0
  51. package/src/hello.test.ts +13 -0
  52. package/src/hello.ts +29 -4
  53. package/src/index.d.ts +8 -2
  54. package/src/index.js +4 -1
  55. package/src/index.ts +20 -2
package/src/base85.ts ADDED
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Base85 encoding/decoding with multiple charset variants.
3
+ * Base85 provides ~25% better efficiency than Base64 (4:5 vs 3:4 ratio).
4
+ * @module base85
5
+ */
6
+
7
+ /**
8
+ * Available Base85 charset variants.
9
+ * - `ascii85`: Adobe Ascii85 (btoa format)
10
+ * - `z85`: ZeroMQ Base85 (no quotes or backslash)
11
+ * - `rfc1924`: RFC 1924 IPv6 encoding
12
+ */
13
+ const Base85Charset = ["ascii85", "z85", "rfc1924"] as const;
14
+
15
+ /**
16
+ * Base85 charset type.
17
+ */
18
+ type Base85CharsetType = (typeof Base85Charset)[number];
19
+
20
+ /**
21
+ * Charset alphabets for Base85 variants.
22
+ * Each has exactly 85 printable ASCII characters.
23
+ */
24
+ const CHARSETS: Record<Base85CharsetType, string> = {
25
+ ascii85: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu",
26
+ z85: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#",
27
+ rfc1924: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
28
+ };
29
+
30
+ /**
31
+ * Build a reverse lookup table for decoding.
32
+ * @param alphabet - The 85-character alphabet string.
33
+ * @returns Map of character to index.
34
+ */
35
+ function buildDecodeTable(alphabet: string): Map<string, number> {
36
+ const table = new Map<string, number>();
37
+ for (let i = 0; i < alphabet.length; i++) {
38
+ const char = alphabet[i];
39
+ if (char !== undefined) {
40
+ table.set(char, i);
41
+ }
42
+ }
43
+ return table;
44
+ }
45
+
46
+ /**
47
+ * Pre-built decode tables for each charset.
48
+ */
49
+ const DECODE_TABLES: Record<Base85CharsetType, Map<string, number>> = {
50
+ ascii85: buildDecodeTable(CHARSETS.ascii85),
51
+ z85: buildDecodeTable(CHARSETS.z85),
52
+ rfc1924: buildDecodeTable(CHARSETS.rfc1924)
53
+ };
54
+
55
+ /**
56
+ * Encode a string to Base85.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * import { encode85 } from "./base85.js";
61
+ *
62
+ * encode85("Hello");
63
+ * // => "87cURDZ"
64
+ *
65
+ * encode85("test", "z85");
66
+ * // => "wrx.P"
67
+ * ```
68
+ *
69
+ * @param input - The string to encode.
70
+ * @param charset - The charset variant to use (default: "ascii85").
71
+ * @returns The Base85 encoded string.
72
+ */
73
+ function encode85(input: string, charset: Base85CharsetType = "ascii85"): string {
74
+ if (input === "") {
75
+ return "";
76
+ }
77
+
78
+ const alphabet = CHARSETS[charset];
79
+ const bytes = new TextEncoder().encode(input);
80
+ let result = "";
81
+
82
+ // Process 4 bytes at a time
83
+ for (let i = 0; i < bytes.length; i += 4) {
84
+ const chunkSize = Math.min(4, bytes.length - i);
85
+
86
+ // Pack up to 4 bytes into a value using multiplication to avoid
87
+ // 32-bit signed integer overflow from bitwise operations
88
+ let value = 0;
89
+ for (let j = 0; j < chunkSize; j++) {
90
+ value = value * 256 + (bytes[i + j] ?? 0);
91
+ }
92
+ // Pad with zeros for incomplete chunks
93
+ for (let j = chunkSize; j < 4; j++) {
94
+ value = value * 256;
95
+ }
96
+
97
+ // Convert to 5 base-85 digits
98
+ const encoded: string[] = [];
99
+ for (let j = 0; j < 5; j++) {
100
+ encoded.unshift(alphabet[value % 85] ?? "");
101
+ value = Math.floor(value / 85);
102
+ }
103
+
104
+ // For the last chunk, only output as many characters as needed
105
+ // (chunkSize bytes -> chunkSize + 1 characters)
106
+ if (i + 4 > bytes.length) {
107
+ result += encoded.slice(0, chunkSize + 1).join("");
108
+ } else {
109
+ result += encoded.join("");
110
+ }
111
+ }
112
+
113
+ return result;
114
+ }
115
+
116
+ /**
117
+ * Decode a Base85 string.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * import { decode85 } from "./base85.js";
122
+ *
123
+ * decode85("87cURDZ");
124
+ * // => "Hello"
125
+ *
126
+ * decode85("wrx.P", "z85");
127
+ * // => "test"
128
+ * ```
129
+ *
130
+ * @param input - The Base85 encoded string.
131
+ * @param charset - The charset variant to use (default: "ascii85").
132
+ * @returns The decoded string.
133
+ * @throws Error if the input contains invalid characters.
134
+ */
135
+ function decode85(input: string, charset: Base85CharsetType = "ascii85"): string {
136
+ if (input === "") {
137
+ return "";
138
+ }
139
+
140
+ const decodeTable = DECODE_TABLES[charset];
141
+ const bytes: number[] = [];
142
+ const inputLen = input.length;
143
+
144
+ // Calculate expected output length
145
+ // Full groups of 5 chars -> 4 bytes each
146
+ // Partial group: n chars -> n-1 bytes
147
+ const fullGroups = Math.floor(inputLen / 5);
148
+ const remainder = inputLen % 5;
149
+ const totalBytes = fullGroups * 4 + (remainder > 0 ? remainder - 1 : 0);
150
+
151
+ // Process 5 characters at a time
152
+ let byteCount = 0;
153
+ for (let i = 0; i < inputLen; i += 5) {
154
+ const chunkSize = Math.min(5, inputLen - i);
155
+
156
+ // Decode up to 5 characters into a 32-bit value
157
+ let value = 0;
158
+ for (let j = 0; j < chunkSize; j++) {
159
+ const char = input[i + j];
160
+ if (char === undefined) {
161
+ break;
162
+ }
163
+ const digit = decodeTable.get(char);
164
+ if (digit === undefined) {
165
+ throw new Error(`Invalid Base85 character: ${char}`);
166
+ }
167
+ value = value * 85 + digit;
168
+ }
169
+
170
+ // Pad with 84 (highest digit) for incomplete chunks
171
+ for (let j = chunkSize; j < 5; j++) {
172
+ value = value * 85 + 84;
173
+ }
174
+
175
+ // Calculate how many bytes this chunk should produce
176
+ const bytesToExtract = chunkSize === 5 ? 4 : chunkSize - 1;
177
+
178
+ // Extract bytes from high to low
179
+ const extracted: number[] = [];
180
+ for (let j = 0; j < 4; j++) {
181
+ extracted.unshift(value & 0xff);
182
+ value = value >>> 8;
183
+ }
184
+
185
+ // Only push the bytes we need
186
+ for (let j = 0; j < bytesToExtract && byteCount < totalBytes; j++) {
187
+ const byte = extracted[j];
188
+ if (byte !== undefined) {
189
+ bytes.push(byte);
190
+ byteCount++;
191
+ }
192
+ }
193
+ }
194
+
195
+ return new TextDecoder().decode(new Uint8Array(bytes));
196
+ }
197
+
198
+ export { encode85, decode85, Base85Charset };
199
+ export type { Base85CharsetType };
package/src/hello.d.ts CHANGED
@@ -1,8 +1,33 @@
1
1
  /**
2
2
  * A sample function that should work to test if lib is installed correctly.
3
3
  *
4
- * @returns hello message
4
+ * @param {Console?} konsole - optional console object to log the message
5
+ * @returns {Promise<string>} `hello from @wtasnorg/node-lib`
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { hello } from "@wtasnorg/node-lib";
10
+ *
11
+ * async function main() {
12
+ * const message = await hello(console);
13
+ * console.log("Received message:", message);
14
+ * }
15
+ *
16
+ * main();
17
+ * ```
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { hello } from "@wtasnorg/node-lib";
22
+ *
23
+ * async function main() {
24
+ * const message = await hello();
25
+ * // Do something with the message
26
+ * }
27
+ *
28
+ * main();
29
+ * ```
5
30
  */
6
- declare function hello(): Promise<string>;
31
+ declare function hello(konsole?: Console): Promise<string>;
7
32
  export { hello };
8
33
  //# sourceMappingURL=hello.d.ts.map
package/src/hello.js CHANGED
@@ -1,12 +1,37 @@
1
1
  /**
2
2
  * A sample function that should work to test if lib is installed correctly.
3
3
  *
4
- * @returns hello message
4
+ * @param {Console?} konsole - optional console object to log the message
5
+ * @returns {Promise<string>} `hello from @wtasnorg/node-lib`
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { hello } from "@wtasnorg/node-lib";
10
+ *
11
+ * async function main() {
12
+ * const message = await hello(console);
13
+ * console.log("Received message:", message);
14
+ * }
15
+ *
16
+ * main();
17
+ * ```
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { hello } from "@wtasnorg/node-lib";
22
+ *
23
+ * async function main() {
24
+ * const message = await hello();
25
+ * // Do something with the message
26
+ * }
27
+ *
28
+ * main();
29
+ * ```
5
30
  */
6
- async function hello() {
31
+ async function hello(konsole) {
7
32
  const message = "hello from @wtasnorg/node-lib";
8
- if (console?.log) {
9
- console.log(message);
33
+ if (konsole?.log) {
34
+ konsole.log(message);
10
35
  }
11
36
  return message;
12
37
  }
package/src/hello.test.js CHANGED
@@ -12,5 +12,16 @@ describe("hello", () => {
12
12
  const actual = await hello();
13
13
  strictEqual(actual, expected);
14
14
  });
15
+ it("logs message when console is provided", async () => {
16
+ const spyConsole = {
17
+ logCalledWith: "",
18
+ log(message) {
19
+ this.logCalledWith = message;
20
+ }
21
+ };
22
+ const expectedMessage = "hello from @wtasnorg/node-lib";
23
+ await hello(spyConsole);
24
+ strictEqual(spyConsole.logCalledWith, expectedMessage);
25
+ });
15
26
  });
16
27
  //# sourceMappingURL=hello.test.js.map
package/src/hello.test.ts CHANGED
@@ -14,4 +14,17 @@ describe("hello", () => {
14
14
  const actual = await hello();
15
15
  strictEqual(actual, expected);
16
16
  });
17
+
18
+ it("logs message when console is provided", async () => {
19
+ const spyConsole = {
20
+ logCalledWith: "",
21
+ log(message: string) {
22
+ this.logCalledWith = message;
23
+ }
24
+ };
25
+
26
+ const expectedMessage = "hello from @wtasnorg/node-lib";
27
+ await hello(spyConsole as unknown as Console);
28
+ strictEqual(spyConsole.logCalledWith, expectedMessage);
29
+ });
17
30
  });
package/src/hello.ts CHANGED
@@ -1,12 +1,37 @@
1
1
  /**
2
2
  * A sample function that should work to test if lib is installed correctly.
3
3
  *
4
- * @returns hello message
4
+ * @param {Console?} konsole - optional console object to log the message
5
+ * @returns {Promise<string>} `hello from @wtasnorg/node-lib`
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { hello } from "@wtasnorg/node-lib";
10
+ *
11
+ * async function main() {
12
+ * const message = await hello(console);
13
+ * console.log("Received message:", message);
14
+ * }
15
+ *
16
+ * main();
17
+ * ```
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { hello } from "@wtasnorg/node-lib";
22
+ *
23
+ * async function main() {
24
+ * const message = await hello();
25
+ * // Do something with the message
26
+ * }
27
+ *
28
+ * main();
29
+ * ```
5
30
  */
6
- async function hello() {
31
+ async function hello(konsole?: Console): Promise<string> {
7
32
  const message = "hello from @wtasnorg/node-lib";
8
- if (console?.log) {
9
- console.log(message);
33
+ if (konsole?.log) {
34
+ konsole.log(message);
10
35
  }
11
36
  return message;
12
37
  }
package/src/index.d.ts CHANGED
@@ -6,6 +6,12 @@ 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
+ export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset, encode58, decode58, Base58Charset, encode85, decode85, Base85Charset, encode32, decode32, Base32Charset };
16
+ export type { FindDirectoriesOptions, FileSystemDependencies, UserAgentInfo, Base64CharsetType, Base58CharsetType, Base85CharsetType, Base32CharsetType };
11
17
  //# sourceMappingURL=index.d.ts.map
package/src/index.js CHANGED
@@ -3,5 +3,8 @@ 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
+ export { hello, pojo, createFindDirectories, parseUserAgent, encode, decode, Base64Charset, encode58, decode58, Base58Charset, encode85, decode85, Base85Charset, encode32, decode32, Base32Charset };
7
10
  //# sourceMappingURL=index.js.map
package/src/index.ts CHANGED
@@ -6,6 +6,12 @@ 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";
9
15
 
10
16
  export {
11
17
  hello,
@@ -14,12 +20,24 @@ export {
14
20
  parseUserAgent,
15
21
  encode,
16
22
  decode,
17
- Base64Charset
23
+ Base64Charset,
24
+ encode58,
25
+ decode58,
26
+ Base58Charset,
27
+ encode85,
28
+ decode85,
29
+ Base85Charset,
30
+ encode32,
31
+ decode32,
32
+ Base32Charset
18
33
  };
19
34
 
20
35
  export type {
21
36
  FindDirectoriesOptions,
22
37
  FileSystemDependencies,
23
38
  UserAgentInfo,
24
- Base64CharsetType
39
+ Base64CharsetType,
40
+ Base58CharsetType,
41
+ Base85CharsetType,
42
+ Base32CharsetType
25
43
  };