@choksheak/ts-utils 0.1.6 → 0.1.8

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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Encode an input ArrayBuffer into a hex string.
3
+ */
4
+ export declare function arrayBufferToHex(buffer: ArrayBuffer): string;
5
+ /**
6
+ * Encode an input ArrayBuffer into a base64 string.
7
+ */
8
+ export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
package/arrayBuffer.js ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/arrayBuffer.ts
21
+ var arrayBuffer_exports = {};
22
+ __export(arrayBuffer_exports, {
23
+ arrayBufferToBase64: () => arrayBufferToBase64,
24
+ arrayBufferToHex: () => arrayBufferToHex
25
+ });
26
+ module.exports = __toCommonJS(arrayBuffer_exports);
27
+ function arrayBufferToHex(buffer) {
28
+ const byteArray = new Uint8Array(buffer);
29
+ return Array.from(byteArray).map((byte) => byte.toString(16).padStart(2, "0")).join("");
30
+ }
31
+ function arrayBufferToBase64(buffer) {
32
+ const byteArray = new Uint8Array(buffer);
33
+ const binaryString = Array.from(byteArray).map((byte) => String.fromCodePoint(byte)).join("");
34
+ return btoa(binaryString);
35
+ }
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ arrayBufferToBase64,
39
+ arrayBufferToHex
40
+ });
41
+ //# sourceMappingURL=arrayBuffer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/arrayBuffer.ts"],"sourcesContent":["/**\n * Encode an input ArrayBuffer into a hex string.\n */\nexport function arrayBufferToHex(buffer: ArrayBuffer): string {\n // Create a Uint8Array view of the ArrayBuffer\n const byteArray = new Uint8Array(buffer);\n\n // Convert each byte to a two-character hexadecimal string\n return Array.from(byteArray)\n .map((byte) => byte.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Encode an input ArrayBuffer into a base64 string.\n */\nexport function arrayBufferToBase64(buffer: ArrayBuffer): string {\n // Convert the ArrayBuffer to a Uint8Array\n const byteArray = new Uint8Array(buffer);\n\n // Create a binary string from the byte array\n const binaryString = Array.from(byteArray)\n .map((byte) => String.fromCodePoint(byte))\n .join(\"\");\n\n // Encode the binary string to base64. No need to use safeBtoa because we\n // already simplified the binary input above.\n return btoa(binaryString);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,iBAAiB,QAA6B;AAE5D,QAAM,YAAY,IAAI,WAAW,MAAM;AAGvC,SAAO,MAAM,KAAK,SAAS,EACxB,IAAI,CAAC,SAAS,KAAK,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAChD,KAAK,EAAE;AACZ;AAKO,SAAS,oBAAoB,QAA6B;AAE/D,QAAM,YAAY,IAAI,WAAW,MAAM;AAGvC,QAAM,eAAe,MAAM,KAAK,SAAS,EACtC,IAAI,CAAC,SAAS,OAAO,cAAc,IAAI,CAAC,EACxC,KAAK,EAAE;AAIV,SAAO,KAAK,YAAY;AAC1B;","names":[]}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@choksheak/ts-utils",
3
3
  "license": "The Unlicense",
4
- "version": "0.1.6",
4
+ "version": "0.1.8",
5
5
  "description": "Random Typescript utilities with support for full tree-shaking",
6
6
  "private": false,
7
7
  "scripts": {
8
8
  "lint": "eslint src/**",
9
- "format": "prettier --write \"**/*.{ts}\"",
9
+ "format": "prettier --write \"**/*.ts\"",
10
10
  "test": "npm run clean && jest",
11
11
  "test:watch": "npm run clean && jest --watch",
12
12
  "clean": "rm -rf dist",
package/safeBtoa.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Base 64 encode the given input string, but safely.
3
+ */
4
+ export declare function safeBtoa(input: string): string;
package/safeBtoa.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/safeBtoa.ts
21
+ var safeBtoa_exports = {};
22
+ __export(safeBtoa_exports, {
23
+ safeBtoa: () => safeBtoa
24
+ });
25
+ module.exports = __toCommonJS(safeBtoa_exports);
26
+ function safeBtoa(input) {
27
+ const utf8Bytes = new TextEncoder().encode(input);
28
+ const binaryString = Array.from(utf8Bytes).map((byte) => String.fromCodePoint(byte)).join("");
29
+ return btoa(binaryString);
30
+ }
31
+ // Annotate the CommonJS export names for ESM import in node:
32
+ 0 && (module.exports = {
33
+ safeBtoa
34
+ });
35
+ //# sourceMappingURL=safeBtoa.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/safeBtoa.ts"],"sourcesContent":["/**\n * Base 64 encode the given input string, but safely.\n */\nexport function safeBtoa(input: string): string {\n // Convert the string to a UTF-8 encoded binary-safe string\n const utf8Bytes = new TextEncoder().encode(input);\n\n // Convert the binary data to a string for btoa\n const binaryString = Array.from(utf8Bytes)\n .map((byte) => String.fromCodePoint(byte))\n .join(\"\");\n\n // Use btoa to encode the binary-safe string\n return btoa(binaryString);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGO,SAAS,SAAS,OAAuB;AAE9C,QAAM,YAAY,IAAI,YAAY,EAAE,OAAO,KAAK;AAGhD,QAAM,eAAe,MAAM,KAAK,SAAS,EACtC,IAAI,CAAC,SAAS,OAAO,cAAc,IAAI,CAAC,EACxC,KAAK,EAAE;AAGV,SAAO,KAAK,YAAY;AAC1B;","names":[]}
package/sha256.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * SHA-256 hash an input string into an ArrayBuffer.
3
+ */
4
+ export declare function sha256(input: string): Promise<ArrayBuffer>;
package/sha256.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __async = (__this, __arguments, generator) => {
20
+ return new Promise((resolve, reject) => {
21
+ var fulfilled = (value) => {
22
+ try {
23
+ step(generator.next(value));
24
+ } catch (e) {
25
+ reject(e);
26
+ }
27
+ };
28
+ var rejected = (value) => {
29
+ try {
30
+ step(generator.throw(value));
31
+ } catch (e) {
32
+ reject(e);
33
+ }
34
+ };
35
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
+ step((generator = generator.apply(__this, __arguments)).next());
37
+ });
38
+ };
39
+
40
+ // src/sha256.ts
41
+ var sha256_exports = {};
42
+ __export(sha256_exports, {
43
+ sha256: () => sha256
44
+ });
45
+ module.exports = __toCommonJS(sha256_exports);
46
+ function sha256(input) {
47
+ return __async(this, null, function* () {
48
+ const encoder = new TextEncoder();
49
+ const uint8Array = encoder.encode(input);
50
+ const arrayBuffer = yield crypto.subtle.digest("SHA-256", uint8Array);
51
+ return arrayBuffer;
52
+ });
53
+ }
54
+ // Annotate the CommonJS export names for ESM import in node:
55
+ 0 && (module.exports = {
56
+ sha256
57
+ });
58
+ //# sourceMappingURL=sha256.js.map
package/sha256.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sha256.ts"],"sourcesContent":["/**\n * SHA-256 hash an input string into an ArrayBuffer.\n */\nexport async function sha256(input: string): Promise<ArrayBuffer> {\n // Encode the input string as a Uint8Array\n const encoder = new TextEncoder();\n const uint8Array = encoder.encode(input);\n\n // Compute the SHA-256 hash using the SubtleCrypto API\n const arrayBuffer = await crypto.subtle.digest(\"SHA-256\", uint8Array);\n\n return arrayBuffer;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,SAAsB,OAAO,OAAqC;AAAA;AAEhE,UAAM,UAAU,IAAI,YAAY;AAChC,UAAM,aAAa,QAAQ,OAAO,KAAK;AAGvC,UAAM,cAAc,MAAM,OAAO,OAAO,OAAO,WAAW,UAAU;AAEpE,WAAO;AAAA,EACT;AAAA;","names":[]}
package/sleep.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number): Promise<void>;
package/sleep.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/sleep.ts
21
+ var sleep_exports = {};
22
+ __export(sleep_exports, {
23
+ sleep: () => sleep
24
+ });
25
+ module.exports = __toCommonJS(sleep_exports);
26
+ function sleep(ms) {
27
+ return new Promise((resolve) => setTimeout(resolve, ms));
28
+ }
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ sleep
32
+ });
33
+ //# sourceMappingURL=sleep.js.map
package/sleep.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sleep.ts"],"sourcesContent":["export function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,SAAS,MAAM,IAA2B;AAC/C,SAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AACzD;","names":[]}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Encode an input ArrayBuffer into a hex string.
3
+ */
4
+ export function arrayBufferToHex(buffer: ArrayBuffer): string {
5
+ // Create a Uint8Array view of the ArrayBuffer
6
+ const byteArray = new Uint8Array(buffer);
7
+
8
+ // Convert each byte to a two-character hexadecimal string
9
+ return Array.from(byteArray)
10
+ .map((byte) => byte.toString(16).padStart(2, "0"))
11
+ .join("");
12
+ }
13
+
14
+ /**
15
+ * Encode an input ArrayBuffer into a base64 string.
16
+ */
17
+ export function arrayBufferToBase64(buffer: ArrayBuffer): string {
18
+ // Convert the ArrayBuffer to a Uint8Array
19
+ const byteArray = new Uint8Array(buffer);
20
+
21
+ // Create a binary string from the byte array
22
+ const binaryString = Array.from(byteArray)
23
+ .map((byte) => String.fromCodePoint(byte))
24
+ .join("");
25
+
26
+ // Encode the binary string to base64. No need to use safeBtoa because we
27
+ // already simplified the binary input above.
28
+ return btoa(binaryString);
29
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Base 64 encode the given input string, but safely.
3
+ */
4
+ export function safeBtoa(input: string): string {
5
+ // Convert the string to a UTF-8 encoded binary-safe string
6
+ const utf8Bytes = new TextEncoder().encode(input);
7
+
8
+ // Convert the binary data to a string for btoa
9
+ const binaryString = Array.from(utf8Bytes)
10
+ .map((byte) => String.fromCodePoint(byte))
11
+ .join("");
12
+
13
+ // Use btoa to encode the binary-safe string
14
+ return btoa(binaryString);
15
+ }
package/src/sha256.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * SHA-256 hash an input string into an ArrayBuffer.
3
+ */
4
+ export async function sha256(input: string): Promise<ArrayBuffer> {
5
+ // Encode the input string as a Uint8Array
6
+ const encoder = new TextEncoder();
7
+ const uint8Array = encoder.encode(input);
8
+
9
+ // Compute the SHA-256 hash using the SubtleCrypto API
10
+ const arrayBuffer = await crypto.subtle.digest("SHA-256", uint8Array);
11
+
12
+ return arrayBuffer;
13
+ }
package/src/sleep.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function sleep(ms: number): Promise<void> {
2
+ return new Promise((resolve) => setTimeout(resolve, ms));
3
+ }