cafe-utility 10.18.0 → 10.19.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.
Files changed (3) hide show
  1. package/index.d.ts +5 -1
  2. package/index.js +46 -3
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -132,7 +132,7 @@ interface ParsedFilename {
132
132
  filename: string;
133
133
  }
134
134
  declare function parseFilename(string: string): ParsedFilename;
135
- declare function randomize(string: string): string;
135
+ declare function randomize(string: string, generator?: () => number): string;
136
136
  declare function expand(input: string): string[];
137
137
  declare function shrinkTrim(string: string): string;
138
138
  declare function capitalize(string: string): string;
@@ -167,6 +167,8 @@ declare type StringSegment = {
167
167
  string: string;
168
168
  };
169
169
  declare function segmentizeString(string: string, symbol: string): StringSegment[];
170
+ declare function base64ToUint8Array(base64: string): Uint8Array;
171
+ declare function hexToUint8Array(hex: string): Uint8Array;
170
172
  declare function parseHtmlAttributes(string: string): Record<string, string>;
171
173
  declare function readNextWord(string: string, index: number, allowedCharacters?: string[]): string;
172
174
  declare function resolveVariables(string: string, variables: Record<string, string>, prefix?: string, separator?: string): string;
@@ -617,6 +619,8 @@ export declare const Strings: {
617
619
  isBalanced: typeof isBalanced;
618
620
  textToFormat: typeof textToFormat;
619
621
  segmentize: typeof segmentizeString;
622
+ hexToUint8Array: typeof hexToUint8Array;
623
+ base64ToUint8Array: typeof base64ToUint8Array;
620
624
  };
621
625
  export declare const Assertions: {
622
626
  asEqual: typeof asEqual;
package/index.js CHANGED
@@ -944,8 +944,8 @@ function parseFilename(string) {
944
944
  }
945
945
  }
946
946
 
947
- function randomize(string) {
948
- return string.replace(/\{(.+?)\}/g, (_, group) => pick(group.split('|')))
947
+ function randomize(string, generator = Math.random) {
948
+ return string.replace(/\{(.+?)\}/g, (_, group) => pick(group.split('|'), generator))
949
949
  }
950
950
 
951
951
  function expand(input) {
@@ -1152,6 +1152,47 @@ function segmentizeString(string, symbol) {
1152
1152
  return segments
1153
1153
  }
1154
1154
 
1155
+ function base64ToUint8Array(base64) {
1156
+ const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
1157
+ let padding = 0
1158
+ if (base64.charAt(base64.length - 1) === '=') {
1159
+ padding++
1160
+ if (base64.charAt(base64.length - 2) === '=') {
1161
+ padding++
1162
+ }
1163
+ }
1164
+ const length = (base64.length * 6) / 8 - padding
1165
+ const output = new Uint8Array(length)
1166
+ let p = 0
1167
+ let q = 0
1168
+ while (p < base64.length) {
1169
+ const w = BASE64_CHARS.indexOf(base64.charAt(p++))
1170
+ const x = BASE64_CHARS.indexOf(base64.charAt(p++))
1171
+ const y = BASE64_CHARS.indexOf(base64.charAt(p++))
1172
+ const z = BASE64_CHARS.indexOf(base64.charAt(p++))
1173
+ const first = (w << 2) | (x >> 4)
1174
+ const second = ((x & 15) << 4) | (y >> 2)
1175
+ const third = ((y & 3) << 6) | z
1176
+ output[q++] = first
1177
+ if (q < length) output[q++] = second
1178
+ if (q < length) output[q++] = third
1179
+ }
1180
+ return output
1181
+ }
1182
+
1183
+ function hexToUint8Array(hex) {
1184
+ if (hex.startsWith('0x')) {
1185
+ hex = hex.slice(2)
1186
+ }
1187
+ const arrayLength = hex.length / 2
1188
+ const result = new Uint8Array(arrayLength)
1189
+ for (let i = 0; i < arrayLength; i++) {
1190
+ const byteValue = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
1191
+ result[i] = byteValue
1192
+ }
1193
+ return result
1194
+ }
1195
+
1155
1196
  function parseHtmlAttributes(string) {
1156
1197
  const attributes = {}
1157
1198
  const matches = string.match(/([a-z\-]+)="([^"]+)"/g)
@@ -2881,7 +2922,9 @@ exports.Strings = {
2881
2922
  describeMarkdown,
2882
2923
  isBalanced,
2883
2924
  textToFormat,
2884
- segmentize: segmentizeString
2925
+ segmentize: segmentizeString,
2926
+ hexToUint8Array,
2927
+ base64ToUint8Array
2885
2928
  }
2886
2929
 
2887
2930
  exports.Assertions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.18.0",
3
+ "version": "10.19.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {