@naturalcycles/js-lib 14.110.0 → 14.112.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.
@@ -39,7 +39,7 @@ export declare function _stringEnumEntries<T extends StringEnum>(en: T): [k: str
39
39
  */
40
40
  export declare function _numberEnumInverse<T extends NumberEnum>(en: T, v: string): T[keyof T];
41
41
  /**
42
- * _enumInverse, but allows to get/return undefined output.
42
+ * _numberEnumInverse, but allows to get/return undefined output.
43
43
  */
44
44
  export declare function _numberEnumInverseNullable<T extends NumberEnum>(en: T, v: string | undefined): T[keyof T] | undefined;
45
45
  /**
@@ -50,6 +50,14 @@ export declare function _numberEnumInverseNullable<T extends NumberEnum>(en: T,
50
50
  */
51
51
  export declare function _numberEnumNormalize<T extends NumberEnum>(en: T, v: string | number): T[keyof T];
52
52
  /**
53
- * Same as _enumNormalize, but allows to return undefined values.
53
+ * Same as _numberEnumNormalize, but allows to return undefined values.
54
54
  */
55
55
  export declare function _numberEnumNormalizeNullable<T extends NumberEnum>(en: T, v: string | number | undefined): T[keyof T] | undefined;
56
+ /**
57
+ * Returns a String key for given NumberEnum value, or undefined if not found.
58
+ */
59
+ export declare function _numberEnumKeyNullable<T extends NumberEnum>(en: T, v: T[keyof T] | undefined | null): keyof T | undefined;
60
+ /**
61
+ * Returns a String key for given NumberEnum value, throws if not found.
62
+ */
63
+ export declare function _numberEnumKey<T extends NumberEnum>(en: T, v: T[keyof T] | undefined | null): keyof T;
package/dist/enum.util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._numberEnumNormalizeNullable = exports._numberEnumNormalize = exports._numberEnumInverseNullable = exports._numberEnumInverse = exports._stringEnumEntries = exports._numberEnumEntries = exports._stringEnumValues = exports._stringEnumKeys = exports._numberEnumValues = exports._numberEnumKeys = void 0;
3
+ exports._numberEnumKey = exports._numberEnumKeyNullable = exports._numberEnumNormalizeNullable = exports._numberEnumNormalize = exports._numberEnumInverseNullable = exports._numberEnumInverse = exports._stringEnumEntries = exports._numberEnumEntries = exports._stringEnumValues = exports._stringEnumKeys = exports._numberEnumValues = exports._numberEnumKeys = void 0;
4
4
  /**
5
5
  * Returns all number keys of a number-enum.
6
6
  */
@@ -63,12 +63,12 @@ exports._stringEnumEntries = _stringEnumEntries;
63
63
  function _numberEnumInverse(en, v) {
64
64
  const r = en[v];
65
65
  if (!r)
66
- throw new Error(`enumInverse value not found for: ${v}`);
66
+ throw new Error(`_numberEnumInverse value not found for: ${v}`);
67
67
  return r;
68
68
  }
69
69
  exports._numberEnumInverse = _numberEnumInverse;
70
70
  /**
71
- * _enumInverse, but allows to get/return undefined output.
71
+ * _numberEnumInverse, but allows to get/return undefined output.
72
72
  */
73
73
  function _numberEnumInverseNullable(en, v) {
74
74
  return en[v];
@@ -83,14 +83,34 @@ exports._numberEnumInverseNullable = _numberEnumInverseNullable;
83
83
  function _numberEnumNormalize(en, v) {
84
84
  const r = _numberEnumNormalizeNullable(en, v);
85
85
  if (!r || !en[r])
86
- throw new Error(`enumNormalize value not found for: ${v}`);
86
+ throw new Error(`_numberEnumNormalize value not found for: ${v}`);
87
87
  return r;
88
88
  }
89
89
  exports._numberEnumNormalize = _numberEnumNormalize;
90
90
  /**
91
- * Same as _enumNormalize, but allows to return undefined values.
91
+ * Same as _numberEnumNormalize, but allows to return undefined values.
92
92
  */
93
93
  function _numberEnumNormalizeNullable(en, v) {
94
94
  return typeof v === 'string' ? en[v] : v;
95
95
  }
96
96
  exports._numberEnumNormalizeNullable = _numberEnumNormalizeNullable;
97
+ /**
98
+ * Returns a String key for given NumberEnum value, or undefined if not found.
99
+ */
100
+ function _numberEnumKeyNullable(en, v) {
101
+ const key = en[v];
102
+ // This prevents passing a Key (not a Value) of enum here, which returns unexpected result (number, not string)
103
+ return typeof key === 'string' ? key : undefined;
104
+ }
105
+ exports._numberEnumKeyNullable = _numberEnumKeyNullable;
106
+ /**
107
+ * Returns a String key for given NumberEnum value, throws if not found.
108
+ */
109
+ function _numberEnumKey(en, v) {
110
+ const key = en[v];
111
+ // This prevents passing a Key (not a Value) of enum here, which returns unexpected result (number, not string)
112
+ if (typeof key !== 'string')
113
+ throw new Error(`_enumKey value not found for: ${v}`);
114
+ return key;
115
+ }
116
+ exports._numberEnumKey = _numberEnumKey;
@@ -135,19 +135,19 @@ export declare function _invert<T extends AnyObject>(o: T): {
135
135
  };
136
136
  export declare function _invertMap<K, V>(m: ReadonlyMap<K, V>): Map<V, K>;
137
137
  /**
138
- * Gets the property value at path of object. If the resolved value is undefined the defaultValue is used
139
- * in its place.
138
+ * Gets the property value at path of object.
140
139
  *
141
- * @param obj The object to query.
142
- * @param path The path of the property to get.
143
- * @param def The value returned if the resolved value is undefined.
144
- * @return Returns the resolved value.
140
+ * @example
141
+ * const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
142
+ * _get(obj, 'a') // 'a'
143
+ * _get(obj, 'c.cc') // 'cc'
144
+ * _get(obj, 'c[cc]') // 'cc'
145
+ * _get(obj, 'unknown.path') // undefined
145
146
  */
146
- export declare function _get<T extends AnyObject>(obj?: T, path?: string, def?: any): any;
147
+ export declare function _get<T extends AnyObject>(obj?: T, path?: string): unknown;
147
148
  /**
148
149
  * Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
149
- * missing index properties while objects are created for all other missing properties. Use _.setWith to
150
- * customize path creation.
150
+ * missing index properties while objects are created for all other missing properties.
151
151
  *
152
152
  * @param obj The object to modify.
153
153
  * @param path The path of the property to set.
@@ -156,7 +156,7 @@ export declare function _get<T extends AnyObject>(obj?: T, path?: string, def?:
156
156
  *
157
157
  * Based on: https://stackoverflow.com/a/54733755/4919972
158
158
  */
159
- export declare function _set<IN extends AnyObject, OUT = IN>(obj: IN, path: PropertyPath, value?: any): OUT;
159
+ export declare function _set<T extends AnyObject>(obj: T, path: PropertyPath, value: any): T;
160
160
  /**
161
161
  * Checks if `path` is a direct property of `object` (not null, not undefined).
162
162
  *
@@ -181,4 +181,4 @@ export declare function _set<IN extends AnyObject, OUT = IN>(obj: IN, path: Prop
181
181
  * _.has(other, 'a');
182
182
  * // => false
183
183
  */
184
- export declare function _has<T extends AnyObject>(obj: T, path?: string): boolean;
184
+ export declare function _has<T extends AnyObject>(obj: T, path: string): boolean;
@@ -286,26 +286,25 @@ function _invertMap(m) {
286
286
  }
287
287
  exports._invertMap = _invertMap;
288
288
  /**
289
- * Gets the property value at path of object. If the resolved value is undefined the defaultValue is used
290
- * in its place.
289
+ * Gets the property value at path of object.
291
290
  *
292
- * @param obj The object to query.
293
- * @param path The path of the property to get.
294
- * @param def The value returned if the resolved value is undefined.
295
- * @return Returns the resolved value.
291
+ * @example
292
+ * const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
293
+ * _get(obj, 'a') // 'a'
294
+ * _get(obj, 'c.cc') // 'cc'
295
+ * _get(obj, 'c[cc]') // 'cc'
296
+ * _get(obj, 'unknown.path') // undefined
296
297
  */
297
- function _get(obj = {}, path = '', def) {
298
- const res = path
298
+ function _get(obj = {}, path = '') {
299
+ return path
299
300
  .replace(/\[([^\]]+)]/g, '.$1')
300
301
  .split('.')
301
- .reduce((o, p) => o[p], obj);
302
- return res === undefined ? def : res;
302
+ .reduce((o, p) => o?.[p], obj);
303
303
  }
304
304
  exports._get = _get;
305
305
  /**
306
306
  * Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
307
- * missing index properties while objects are created for all other missing properties. Use _.setWith to
308
- * customize path creation.
307
+ * missing index properties while objects are created for all other missing properties.
309
308
  *
310
309
  * @param obj The object to modify.
311
310
  * @param path The path of the property to set.
@@ -336,7 +335,7 @@ function _set(obj, path, value) {
336
335
  ? [] // Yes: assign a new array object
337
336
  : {}), // No: assign a new plain object
338
337
  obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
339
- return obj; // Return the top-level object to allow chaining
338
+ return obj; // allow chaining
340
339
  }
341
340
  exports._set = _set;
342
341
  /**
@@ -54,11 +54,11 @@ export function _stringEnumEntries(en) {
54
54
  export function _numberEnumInverse(en, v) {
55
55
  const r = en[v];
56
56
  if (!r)
57
- throw new Error(`enumInverse value not found for: ${v}`);
57
+ throw new Error(`_numberEnumInverse value not found for: ${v}`);
58
58
  return r;
59
59
  }
60
60
  /**
61
- * _enumInverse, but allows to get/return undefined output.
61
+ * _numberEnumInverse, but allows to get/return undefined output.
62
62
  */
63
63
  export function _numberEnumInverseNullable(en, v) {
64
64
  return en[v];
@@ -72,12 +72,30 @@ export function _numberEnumInverseNullable(en, v) {
72
72
  export function _numberEnumNormalize(en, v) {
73
73
  const r = _numberEnumNormalizeNullable(en, v);
74
74
  if (!r || !en[r])
75
- throw new Error(`enumNormalize value not found for: ${v}`);
75
+ throw new Error(`_numberEnumNormalize value not found for: ${v}`);
76
76
  return r;
77
77
  }
78
78
  /**
79
- * Same as _enumNormalize, but allows to return undefined values.
79
+ * Same as _numberEnumNormalize, but allows to return undefined values.
80
80
  */
81
81
  export function _numberEnumNormalizeNullable(en, v) {
82
82
  return typeof v === 'string' ? en[v] : v;
83
83
  }
84
+ /**
85
+ * Returns a String key for given NumberEnum value, or undefined if not found.
86
+ */
87
+ export function _numberEnumKeyNullable(en, v) {
88
+ const key = en[v];
89
+ // This prevents passing a Key (not a Value) of enum here, which returns unexpected result (number, not string)
90
+ return typeof key === 'string' ? key : undefined;
91
+ }
92
+ /**
93
+ * Returns a String key for given NumberEnum value, throws if not found.
94
+ */
95
+ export function _numberEnumKey(en, v) {
96
+ const key = en[v];
97
+ // This prevents passing a Key (not a Value) of enum here, which returns unexpected result (number, not string)
98
+ if (typeof key !== 'string')
99
+ throw new Error(`_enumKey value not found for: ${v}`);
100
+ return key;
101
+ }
@@ -263,25 +263,24 @@ export function _invertMap(m) {
263
263
  return inv;
264
264
  }
265
265
  /**
266
- * Gets the property value at path of object. If the resolved value is undefined the defaultValue is used
267
- * in its place.
266
+ * Gets the property value at path of object.
268
267
  *
269
- * @param obj The object to query.
270
- * @param path The path of the property to get.
271
- * @param def The value returned if the resolved value is undefined.
272
- * @return Returns the resolved value.
268
+ * @example
269
+ * const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
270
+ * _get(obj, 'a') // 'a'
271
+ * _get(obj, 'c.cc') // 'cc'
272
+ * _get(obj, 'c[cc]') // 'cc'
273
+ * _get(obj, 'unknown.path') // undefined
273
274
  */
274
- export function _get(obj = {}, path = '', def) {
275
- const res = path
275
+ export function _get(obj = {}, path = '') {
276
+ return path
276
277
  .replace(/\[([^\]]+)]/g, '.$1')
277
278
  .split('.')
278
- .reduce((o, p) => o[p], obj);
279
- return res === undefined ? def : res;
279
+ .reduce((o, p) => o === null || o === void 0 ? void 0 : o[p], obj);
280
280
  }
281
281
  /**
282
282
  * Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
283
- * missing index properties while objects are created for all other missing properties. Use _.setWith to
284
- * customize path creation.
283
+ * missing index properties while objects are created for all other missing properties.
285
284
  *
286
285
  * @param obj The object to modify.
287
286
  * @param path The path of the property to set.
@@ -312,7 +311,7 @@ export function _set(obj, path, value) {
312
311
  ? [] // Yes: assign a new array object
313
312
  : {}), // No: assign a new plain object
314
313
  obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
315
- return obj; // Return the top-level object to allow chaining
314
+ return obj; // allow chaining
316
315
  }
317
316
  /**
318
317
  * Checks if `path` is a direct property of `object` (not null, not undefined).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.110.0",
3
+ "version": "14.112.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -16,7 +16,7 @@
16
16
  "@naturalcycles/nodejs-lib": "^12.33.4",
17
17
  "@naturalcycles/time-lib": "^3.5.1",
18
18
  "@types/node": "^18.0.0",
19
- "expect-type": "^0.13.0",
19
+ "expect-type": "^0.14.2",
20
20
  "jest": "^29.0.0",
21
21
  "prettier": "^2.1.2",
22
22
  "rxjs": "^7.0.1",
package/src/enum.util.ts CHANGED
@@ -61,12 +61,12 @@ export function _stringEnumEntries<T extends StringEnum>(en: T): [k: string, v:
61
61
  */
62
62
  export function _numberEnumInverse<T extends NumberEnum>(en: T, v: string): T[keyof T] {
63
63
  const r = en[v as keyof T] as any
64
- if (!r) throw new Error(`enumInverse value not found for: ${v}`)
64
+ if (!r) throw new Error(`_numberEnumInverse value not found for: ${v}`)
65
65
  return r
66
66
  }
67
67
 
68
68
  /**
69
- * _enumInverse, but allows to get/return undefined output.
69
+ * _numberEnumInverse, but allows to get/return undefined output.
70
70
  */
71
71
  export function _numberEnumInverseNullable<T extends NumberEnum>(
72
72
  en: T,
@@ -83,12 +83,12 @@ export function _numberEnumInverseNullable<T extends NumberEnum>(
83
83
  */
84
84
  export function _numberEnumNormalize<T extends NumberEnum>(en: T, v: string | number): T[keyof T] {
85
85
  const r = _numberEnumNormalizeNullable(en, v)
86
- if (!r || !en[r as keyof T]) throw new Error(`enumNormalize value not found for: ${v}`)
86
+ if (!r || !en[r as keyof T]) throw new Error(`_numberEnumNormalize value not found for: ${v}`)
87
87
  return r
88
88
  }
89
89
 
90
90
  /**
91
- * Same as _enumNormalize, but allows to return undefined values.
91
+ * Same as _numberEnumNormalize, but allows to return undefined values.
92
92
  */
93
93
  export function _numberEnumNormalizeNullable<T extends NumberEnum>(
94
94
  en: T,
@@ -96,3 +96,28 @@ export function _numberEnumNormalizeNullable<T extends NumberEnum>(
96
96
  ): T[keyof T] | undefined {
97
97
  return typeof v === 'string' ? en[v as keyof T] : (v as any)
98
98
  }
99
+
100
+ /**
101
+ * Returns a String key for given NumberEnum value, or undefined if not found.
102
+ */
103
+ export function _numberEnumKeyNullable<T extends NumberEnum>(
104
+ en: T,
105
+ v: T[keyof T] | undefined | null,
106
+ ): keyof T | undefined {
107
+ const key = (en as any)[v]
108
+ // This prevents passing a Key (not a Value) of enum here, which returns unexpected result (number, not string)
109
+ return typeof key === 'string' ? key : undefined
110
+ }
111
+
112
+ /**
113
+ * Returns a String key for given NumberEnum value, throws if not found.
114
+ */
115
+ export function _numberEnumKey<T extends NumberEnum>(
116
+ en: T,
117
+ v: T[keyof T] | undefined | null,
118
+ ): keyof T {
119
+ const key = (en as any)[v]
120
+ // This prevents passing a Key (not a Value) of enum here, which returns unexpected result (number, not string)
121
+ if (typeof key !== 'string') throw new Error(`_enumKey value not found for: ${v}`)
122
+ return key
123
+ }
@@ -311,27 +311,25 @@ export function _invertMap<K, V>(m: ReadonlyMap<K, V>): Map<V, K> {
311
311
  }
312
312
 
313
313
  /**
314
- * Gets the property value at path of object. If the resolved value is undefined the defaultValue is used
315
- * in its place.
314
+ * Gets the property value at path of object.
316
315
  *
317
- * @param obj The object to query.
318
- * @param path The path of the property to get.
319
- * @param def The value returned if the resolved value is undefined.
320
- * @return Returns the resolved value.
316
+ * @example
317
+ * const obj = {a: 'a', b: 'b', c: { cc: 'cc' }}
318
+ * _get(obj, 'a') // 'a'
319
+ * _get(obj, 'c.cc') // 'cc'
320
+ * _get(obj, 'c[cc]') // 'cc'
321
+ * _get(obj, 'unknown.path') // undefined
321
322
  */
322
- export function _get<T extends AnyObject>(obj = {} as T, path = '', def?: any): any {
323
- const res = path
323
+ export function _get<T extends AnyObject>(obj = {} as T, path = ''): unknown {
324
+ return path
324
325
  .replace(/\[([^\]]+)]/g, '.$1')
325
326
  .split('.')
326
- .reduce((o, p) => o[p], obj)
327
-
328
- return res === undefined ? def : res
327
+ .reduce((o, p) => o?.[p], obj)
329
328
  }
330
329
 
331
330
  /**
332
331
  * Sets the value at path of object. If a portion of path doesn’t exist it’s created. Arrays are created for
333
- * missing index properties while objects are created for all other missing properties. Use _.setWith to
334
- * customize path creation.
332
+ * missing index properties while objects are created for all other missing properties.
335
333
  *
336
334
  * @param obj The object to modify.
337
335
  * @param path The path of the property to set.
@@ -340,11 +338,7 @@ export function _get<T extends AnyObject>(obj = {} as T, path = '', def?: any):
340
338
  *
341
339
  * Based on: https://stackoverflow.com/a/54733755/4919972
342
340
  */
343
- export function _set<IN extends AnyObject, OUT = IN>(
344
- obj: IN,
345
- path: PropertyPath,
346
- value?: any,
347
- ): OUT {
341
+ export function _set<T extends AnyObject>(obj: T, path: PropertyPath, value: any): T {
348
342
  if (!obj || Object(obj) !== obj || !path) return obj as any // When obj is not an object
349
343
 
350
344
  // If not yet an array, get the keys from the string-path
@@ -373,7 +367,7 @@ export function _set<IN extends AnyObject, OUT = IN>(
373
367
  obj,
374
368
  )[path[path.length - 1]!] = value // Finally assign the value to the last key
375
369
 
376
- return obj as any // Return the top-level object to allow chaining
370
+ return obj // allow chaining
377
371
  }
378
372
 
379
373
  /**
@@ -400,7 +394,7 @@ export function _set<IN extends AnyObject, OUT = IN>(
400
394
  * _.has(other, 'a');
401
395
  * // => false
402
396
  */
403
- export function _has<T extends AnyObject>(obj: T, path?: string): boolean {
397
+ export function _has<T extends AnyObject>(obj: T, path: string): boolean {
404
398
  const v = _get(obj, path)
405
399
  return v !== undefined && v !== null
406
400
  }