cafe-utility 9.3.0 → 9.4.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 +3 -1
  2. package/index.js +34 -12
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -124,7 +124,7 @@ interface ParsedFilename {
124
124
  }
125
125
  declare function parseFilename(string: string): ParsedFilename;
126
126
  declare function randomize(string: string): string;
127
- declare function expand(string: string): string[];
127
+ declare function expand(input: string): string[];
128
128
  declare function shrinkTrim(string: string): string;
129
129
  declare function capitalize(string: string): string;
130
130
  declare function decapitalize(string: string): string;
@@ -229,6 +229,7 @@ declare function diffKeys(objectA: CafeObject, objectB: CafeObject): {
229
229
  };
230
230
  declare function pickRandomKey(object: CafeObject): string;
231
231
  declare function mapRandomKey<T>(object: CafeObject<T>, mapFunction: (value: T) => T): string;
232
+ declare function fromObjectString<T>(string: string): T;
232
233
  interface NumberFormatOptions {
233
234
  precision?: number;
234
235
  longForm?: boolean;
@@ -450,6 +451,7 @@ export declare const Objects: {
450
451
  diffKeys: typeof diffKeys;
451
452
  pickRandomKey: typeof pickRandomKey;
452
453
  mapRandomKey: typeof mapRandomKey;
454
+ fromObjectString: typeof fromObjectString;
453
455
  };
454
456
  export declare const Pagination: {
455
457
  asPageNumber: typeof asPageNumber;
package/index.js CHANGED
@@ -868,17 +868,19 @@ function randomize(string) {
868
868
  return string.replace(/\{(.+?)\}/g, (_, group) => pick(group.split('|')))
869
869
  }
870
870
 
871
- function expand(string) {
872
- const matches = string.match(/\{(.+?)\}/g)
873
- if (!matches) {
874
- return [string]
875
- }
876
- const result = []
877
- for (const match of matches) {
878
- const group = match.slice(1, match.length - 1)
879
- for (const option of group.split(',')) {
880
- result.push(string.replace(match, option))
881
- }
871
+ function expand(input) {
872
+ const regex = /\{(.+?)\}/
873
+ const match = input.match(regex)
874
+ if (!match || !match.index) {
875
+ return [input]
876
+ }
877
+ const group = match[1].split(',')
878
+ const prefix = input.slice(0, match.index)
879
+ const suffix = input.slice(match.index + match[0].length)
880
+ let result = []
881
+ for (const option of group) {
882
+ const expanded = expand(prefix + option + suffix)
883
+ result = result.concat(expanded)
882
884
  }
883
885
  return result
884
886
  }
@@ -1483,6 +1485,25 @@ function mapRandomKey(object, mapFunction) {
1483
1485
  return key
1484
1486
  }
1485
1487
 
1488
+ function fromObjectString(string) {
1489
+ string = string.replace(/(\w+)\((.+)\)/g, (match, $1, $2) => {
1490
+ return `${$1}(${$2.replaceAll(',', '&comma;')})`
1491
+ })
1492
+ string = string.replace(/(,)(\s+})/g, '$2')
1493
+ string = string.replace(/\.\.\..+?,/g, '')
1494
+ string = string.replace(/(,\s+)([a-zA-Z]\w+),/g, "$1$2: '$2',")
1495
+ string = string.replace(/:(.+)\?(.+):/g, (match, $1, $2) => {
1496
+ return `: (${$1.trim()} && ${$2.trim()}) ||`
1497
+ })
1498
+ string = string.replace(/([a-zA-Z0-9]+)( ?: ?{)/g, '"$1"$2')
1499
+ string = string.replace(/([a-zA-Z0-9]+) ?: ?(.+?)(,|\n|})/g, (match, $1, $2, $3) => {
1500
+ return `"${$1}":"${$2.trim()}"${$3}`
1501
+ })
1502
+ string = string.replace(/("'|'")/g, '"')
1503
+ string = string.replaceAll('&comma;', ',')
1504
+ return JSON.parse(string)
1505
+ }
1506
+
1486
1507
  const thresholds = [1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27, 1e30, 1e33]
1487
1508
  const longNumberUnits = [
1488
1509
  'thousand',
@@ -2372,7 +2393,8 @@ exports.Objects = {
2372
2393
  createStatefulToggle,
2373
2394
  diffKeys,
2374
2395
  pickRandomKey,
2375
- mapRandomKey
2396
+ mapRandomKey,
2397
+ fromObjectString
2376
2398
  }
2377
2399
 
2378
2400
  exports.Pagination = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "9.3.0",
3
+ "version": "9.4.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {