@hpcc-js/util 3.4.2 → 3.4.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hpcc-js/util",
3
- "version": "3.4.2",
3
+ "version": "3.4.4",
4
4
  "description": "hpcc-js - Utilities",
5
5
  "type": "module",
6
6
  "main": "./dist/index.umd.cjs",
@@ -41,7 +41,7 @@
41
41
  "@xmldom/xmldom": "0.9.8"
42
42
  },
43
43
  "devDependencies": {
44
- "@hpcc-js/esbuild-plugins": "^1.7.0"
44
+ "@hpcc-js/esbuild-plugins": "^1.8.0"
45
45
  },
46
46
  "repository": {
47
47
  "type": "git",
@@ -54,5 +54,5 @@
54
54
  "url": "https://github.com/hpcc-systems/Visualization/issues"
55
55
  },
56
56
  "homepage": "https://github.com/hpcc-systems/Visualization/tree/main/packages/util",
57
- "gitHead": "5e5fc8d746e6a42c58da2ec4f55f2f7cbaeff611"
57
+ "gitHead": "70194f16ed27ea1167af126b35cbf4af5f181be9"
58
58
  }
package/src/index.ts CHANGED
@@ -19,3 +19,4 @@ export * from "./stack.ts";
19
19
  export * from "./stateful.ts";
20
20
  export * from "./string.ts";
21
21
  export * from "./url.ts";
22
+ export * from "./utf8ToBase64.ts";
@@ -0,0 +1,47 @@
1
+ const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2
+
3
+ function toUTF8Bytes(value: string): Uint8Array {
4
+ if (typeof TextEncoder !== "undefined") {
5
+ return new TextEncoder().encode(value);
6
+ }
7
+
8
+ const encoded = encodeURIComponent(value);
9
+ const bytes: number[] = [];
10
+ for (let i = 0; i < encoded.length; ++i) {
11
+ if (encoded[i] === "%") {
12
+ bytes.push(parseInt(encoded.substring(i + 1, i + 3), 16));
13
+ i += 2;
14
+ } else {
15
+ bytes.push(encoded.charCodeAt(i));
16
+ }
17
+ }
18
+ return Uint8Array.from(bytes);
19
+ }
20
+
21
+ function bytesToBase64(bytes: Uint8Array): string {
22
+ let output = "";
23
+ for (let i = 0; i < bytes.length; i += 3) {
24
+ const byte1 = bytes[i];
25
+ const hasByte2 = i + 1 < bytes.length;
26
+ const hasByte3 = i + 2 < bytes.length;
27
+ const byte2 = hasByte2 ? bytes[i + 1] : 0;
28
+ const byte3 = hasByte3 ? bytes[i + 2] : 0;
29
+
30
+ output += BASE64_ALPHABET[byte1 >> 2];
31
+ output += BASE64_ALPHABET[((byte1 & 0x03) << 4) | (byte2 >> 4)];
32
+ output += hasByte2 ? BASE64_ALPHABET[((byte2 & 0x0f) << 2) | (byte3 >> 6)] : "=";
33
+ output += hasByte3 ? BASE64_ALPHABET[byte3 & 0x3f] : "=";
34
+ }
35
+ return output;
36
+ }
37
+
38
+ export function utf8ToBase64(value: string = ""): string {
39
+ const normalized = value == null ? "" : String(value);
40
+
41
+ const maybeBuffer = (globalThis as any)?.Buffer;
42
+ if (maybeBuffer?.from) {
43
+ return maybeBuffer.from(normalized, "utf8").toString("base64");
44
+ }
45
+
46
+ return bytesToBase64(toUTF8Bytes(normalized));
47
+ }
package/types/index.d.ts CHANGED
@@ -19,3 +19,4 @@ export * from "./stack.ts";
19
19
  export * from "./stateful.ts";
20
20
  export * from "./string.ts";
21
21
  export * from "./url.ts";
22
+ export * from "./utf8ToBase64.ts";
@@ -0,0 +1 @@
1
+ export declare function utf8ToBase64(value?: string): string;