cafe-utility 10.17.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 +6 -2
  2. package/index.js +56 -9
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -22,7 +22,7 @@ declare function chance(threshold: number, generator?: () => number): boolean;
22
22
  declare function pick<T>(array: T[], generator?: () => number): T;
23
23
  declare function pickMany<T>(array: T[], count: number, generator?: () => number): T[];
24
24
  declare function pickManyUnique<T>(array: T[], count: number, equalityFunction: (a: T, b: T) => boolean, generator?: () => number): T[];
25
- declare function pickGuaranteed<T>(array: T[], include: T | null, exclude: T | null, count: number, generator?: () => number): {
25
+ declare function pickGuaranteed<T>(array: T[], include: T | null, exclude: T | null, count: number, predicate: (value: T, values: T[]) => boolean, generator?: () => number): {
26
26
  values: T[];
27
27
  indexOfGuaranteed: number;
28
28
  };
@@ -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
@@ -169,15 +169,19 @@ function pickManyUnique(array, count, equalityFunction, generator = Math.random)
169
169
  return results
170
170
  }
171
171
 
172
- function pickGuaranteed(array, include, exclude, count, generator = Math.random) {
173
- const picks = pickMany(
174
- array.filter(x => x !== include && x !== exclude),
175
- include === null ? count : count - 1,
176
- generator
177
- )
172
+ function pickGuaranteed(array, include, exclude, count, predicate, generator = Math.random) {
173
+ const choices = array.filter(x => x !== include && x !== exclude)
174
+ const picks = []
178
175
  if (include !== null) {
179
176
  picks.push(include)
180
177
  }
178
+ while (choices.length && picks.length < count) {
179
+ const index = exports.Random.intBetween(0, choices.length - 1, generator)
180
+ if (predicate(choices[index], picks)) {
181
+ picks.push(choices[index])
182
+ }
183
+ choices.splice(index, 1)
184
+ }
181
185
  shuffle(picks, generator)
182
186
  return { values: picks, indexOfGuaranteed: include !== null ? picks.indexOf(include) : -1 }
183
187
  }
@@ -940,8 +944,8 @@ function parseFilename(string) {
940
944
  }
941
945
  }
942
946
 
943
- function randomize(string) {
944
- 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))
945
949
  }
946
950
 
947
951
  function expand(input) {
@@ -1148,6 +1152,47 @@ function segmentizeString(string, symbol) {
1148
1152
  return segments
1149
1153
  }
1150
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
+
1151
1196
  function parseHtmlAttributes(string) {
1152
1197
  const attributes = {}
1153
1198
  const matches = string.match(/([a-z\-]+)="([^"]+)"/g)
@@ -2877,7 +2922,9 @@ exports.Strings = {
2877
2922
  describeMarkdown,
2878
2923
  isBalanced,
2879
2924
  textToFormat,
2880
- segmentize: segmentizeString
2925
+ segmentize: segmentizeString,
2926
+ hexToUint8Array,
2927
+ base64ToUint8Array
2881
2928
  }
2882
2929
 
2883
2930
  exports.Assertions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.17.0",
3
+ "version": "10.19.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {