@webdeveric/utils 0.9.0 → 0.12.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 (96) hide show
  1. package/dist/cjs/clamp.js +10 -0
  2. package/dist/cjs/clamp.js.map +1 -0
  3. package/dist/cjs/convert.js +19 -0
  4. package/dist/cjs/convert.js.map +1 -0
  5. package/dist/cjs/getMaxValue.js +14 -0
  6. package/dist/cjs/getMaxValue.js.map +1 -0
  7. package/dist/cjs/getMinValue.js +14 -0
  8. package/dist/cjs/getMinValue.js.map +1 -0
  9. package/dist/cjs/getOwnKeys.js +1 -4
  10. package/dist/cjs/getOwnKeys.js.map +1 -1
  11. package/dist/cjs/getOwnProperties.js +11 -0
  12. package/dist/cjs/getOwnProperties.js.map +1 -0
  13. package/dist/cjs/index.js +14 -1
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/isEmpty.js +17 -0
  16. package/dist/cjs/isEmpty.js.map +1 -0
  17. package/dist/cjs/isObject.js +8 -0
  18. package/dist/cjs/isObject.js.map +1 -0
  19. package/dist/cjs/node/index.js +5 -1
  20. package/dist/cjs/node/index.js.map +1 -1
  21. package/dist/cjs/normalize.js +14 -15
  22. package/dist/cjs/normalize.js.map +1 -1
  23. package/dist/cjs/type-assertion.js +16 -0
  24. package/dist/cjs/type-assertion.js.map +1 -0
  25. package/dist/cjs/types.js +3 -0
  26. package/dist/cjs/types.js.map +1 -0
  27. package/dist/cjs/until.js +10 -13
  28. package/dist/cjs/until.js.map +1 -1
  29. package/dist/mjs/clamp.js +6 -0
  30. package/dist/mjs/clamp.js.map +1 -0
  31. package/dist/mjs/compose.js +1 -2
  32. package/dist/mjs/compose.js.map +1 -1
  33. package/dist/mjs/convert.js +15 -0
  34. package/dist/mjs/convert.js.map +1 -0
  35. package/dist/mjs/getMaxValue.js +10 -0
  36. package/dist/mjs/getMaxValue.js.map +1 -0
  37. package/dist/mjs/getMinValue.js +10 -0
  38. package/dist/mjs/getMinValue.js.map +1 -0
  39. package/dist/mjs/getOwnKeys.js +1 -4
  40. package/dist/mjs/getOwnKeys.js.map +1 -1
  41. package/dist/mjs/getOwnProperties.js +7 -0
  42. package/dist/mjs/getOwnProperties.js.map +1 -0
  43. package/dist/mjs/index.js +9 -0
  44. package/dist/mjs/index.js.map +1 -1
  45. package/dist/mjs/isEmpty.js +13 -0
  46. package/dist/mjs/isEmpty.js.map +1 -0
  47. package/dist/mjs/isObject.js +4 -0
  48. package/dist/mjs/isObject.js.map +1 -0
  49. package/dist/mjs/normalize.js +14 -15
  50. package/dist/mjs/normalize.js.map +1 -1
  51. package/dist/mjs/type-assertion.js +11 -0
  52. package/dist/mjs/type-assertion.js.map +1 -0
  53. package/dist/mjs/types.js +2 -0
  54. package/dist/mjs/types.js.map +1 -0
  55. package/dist/mjs/until.js +10 -13
  56. package/dist/mjs/until.js.map +1 -1
  57. package/dist/types/clamp.d.ts +3 -0
  58. package/dist/types/clamp.d.ts.map +1 -0
  59. package/dist/types/convert.d.ts +8 -0
  60. package/dist/types/convert.d.ts.map +1 -0
  61. package/dist/types/getMaxValue.d.ts +3 -0
  62. package/dist/types/getMaxValue.d.ts.map +1 -0
  63. package/dist/types/getMinValue.d.ts +3 -0
  64. package/dist/types/getMinValue.d.ts.map +1 -0
  65. package/dist/types/getOwnKeys.d.ts +1 -1
  66. package/dist/types/getOwnKeys.d.ts.map +1 -1
  67. package/dist/types/getOwnProperties.d.ts +2 -0
  68. package/dist/types/getOwnProperties.d.ts.map +1 -0
  69. package/dist/types/index.d.ts +9 -0
  70. package/dist/types/index.d.ts.map +1 -1
  71. package/dist/types/isEmpty.d.ts +2 -0
  72. package/dist/types/isEmpty.d.ts.map +1 -0
  73. package/dist/types/isObject.d.ts +2 -0
  74. package/dist/types/isObject.d.ts.map +1 -0
  75. package/dist/types/normalize.d.ts +8 -6
  76. package/dist/types/normalize.d.ts.map +1 -1
  77. package/dist/types/type-assertion.d.ts +3 -0
  78. package/dist/types/type-assertion.d.ts.map +1 -0
  79. package/dist/types/types.d.ts +7 -0
  80. package/dist/types/types.d.ts.map +1 -0
  81. package/dist/types/until.d.ts +8 -10
  82. package/dist/types/until.d.ts.map +1 -1
  83. package/package.json +27 -12
  84. package/src/clamp.ts +7 -0
  85. package/src/convert.ts +30 -0
  86. package/src/getMaxValue.ts +16 -0
  87. package/src/getMinValue.ts +16 -0
  88. package/src/getOwnKeys.ts +2 -5
  89. package/src/getOwnProperties.ts +6 -0
  90. package/src/index.ts +9 -0
  91. package/src/isEmpty.ts +15 -0
  92. package/src/isObject.ts +3 -0
  93. package/src/normalize.ts +36 -29
  94. package/src/type-assertion.ts +11 -0
  95. package/src/types.ts +12 -0
  96. package/src/until.ts +22 -29
package/src/normalize.ts CHANGED
@@ -2,26 +2,32 @@ import cloneDeep from 'lodash.clonedeep';
2
2
 
3
3
  import type { Builtin } from 'ts-essentials';
4
4
 
5
- import { getOwnKeys } from './getOwnKeys';
6
- import { getType } from './getType';
5
+ import { getOwnProperties } from './getOwnProperties.js';
6
+ import { isObject } from './isObject.js';
7
7
 
8
- export type NormalizeContext<OwnerRecordType> = {
8
+ import type { AnyRecord } from './types';
9
+
10
+ export type NormalizeContext<OwnerRecordType, ContextData extends AnyRecord> = {
9
11
  original: Readonly<OwnerRecordType>;
10
12
  current: OwnerRecordType;
11
- data: Record<PropertyKey, unknown>;
13
+ data: ContextData;
12
14
  };
13
15
 
14
- export type NormalizerFn<OutputType, OwnerRecordType> = (
15
- input: unknown,
16
- context: NormalizeContext<OwnerRecordType>,
17
- ) => OutputType;
16
+ export type NormalizerFn<PropertyType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = (
17
+ input: PropertyType,
18
+ context: NormalizeContext<OwnerRecordType, ContextData>,
19
+ ) => PropertyType;
18
20
 
19
- export type NormalizersRecord<RecordType, OwnerRecordType = RecordType> = {
20
- [Property in keyof RecordType]?: RecordType[Property] extends Builtin
21
- ? NormalizerFn<RecordType[Property], OwnerRecordType>
22
- : NormalizerFn<RecordType[Property], OwnerRecordType> | NormalizersRecord<RecordType[Property], OwnerRecordType>;
21
+ export type NormalizersRecord<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> = {
22
+ readonly [Property in keyof RecordType]?: RecordType[Property] extends Builtin
23
+ ? NormalizerFn<RecordType[Property], OwnerRecordType, ContextData>
24
+ : AnyNormalizer<RecordType[Property], OwnerRecordType, ContextData>
23
25
  };
24
26
 
27
+ export type AnyNormalizer<RecordType, OwnerRecordType, ContextData extends AnyRecord = AnyRecord> =
28
+ | NormalizerFn<RecordType, OwnerRecordType, ContextData>
29
+ | NormalizersRecord<RecordType, OwnerRecordType, ContextData>;
30
+
25
31
  /**
26
32
  * @example
27
33
  *
@@ -37,44 +43,45 @@ export type NormalizersRecord<RecordType, OwnerRecordType = RecordType> = {
37
43
  * normalize(true, () => false );
38
44
  * ```
39
45
  */
40
- export function normalize<Data>(
41
- input: Data,
42
- normalizers?: NormalizerFn<Data, Data> | NormalizersRecord<Data, Data>,
46
+ export function normalize<Data, ContextData extends AnyRecord = AnyRecord>(
47
+ input: Readonly<Data>,
48
+ normalizers?: AnyNormalizer<Data, Data, ContextData>,
49
+ initContextData: (data: typeof input, dataNormalizers: typeof normalizers) => ContextData = (): AnyRecord => ({}),
43
50
  ): Data {
44
- const normalized = cloneDeep(input);
51
+ const current = cloneDeep(input);
45
52
 
46
- const context: NormalizeContext<Data> = Object.seal({
53
+ const context: NormalizeContext<Data, ContextData> = Object.seal({
47
54
  original: input,
48
- current: normalized,
49
- data: {},
55
+ current,
56
+ data: initContextData(input, normalizers),
50
57
  });
51
58
 
52
59
  const walk = <CurrentRecord>(
53
60
  currentRecord: CurrentRecord,
54
- currentNormalizers?: NormalizerFn<CurrentRecord, Data> | NormalizersRecord<CurrentRecord, Data>,
61
+ currentNormalizers?: NormalizerFn<CurrentRecord, Data, ContextData> | NormalizersRecord<CurrentRecord, Data, ContextData>,
55
62
  ): CurrentRecord => {
56
- if (typeof currentNormalizers === 'undefined') {
57
- return currentRecord;
58
- }
59
-
60
63
  if (typeof currentNormalizers === 'function') {
61
64
  return currentNormalizers(currentRecord, context);
62
65
  }
63
66
 
64
67
  if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
65
68
  if (typeof currentRecord === 'object' && currentRecord !== null) {
66
- return getOwnKeys(currentNormalizers).reduce((data, key) => {
67
- data[ key ] = walk(data[ key ], currentNormalizers[ key ]);
69
+ return getOwnProperties(currentNormalizers).reduce((data, key) => {
70
+ const normalizers = currentNormalizers[ key ];
71
+ // If the value is undefined but there is a normalizer record, set value to empty object.
72
+ const value = typeof data[ key ] === 'undefined' && isObject(normalizers)
73
+ ? {} as CurrentRecord[keyof CurrentRecord]
74
+ : data[ key ];
75
+
76
+ data[ key ] = walk(value, normalizers);
68
77
 
69
78
  return data;
70
79
  }, currentRecord);
71
80
  }
72
- } else {
73
- throw new TypeError(`normalizers must be function or object. ${getType(currentNormalizers)} was received.`);
74
81
  }
75
82
 
76
83
  return currentRecord;
77
84
  };
78
85
 
79
- return walk(normalized, normalizers);
86
+ return walk(current, normalizers);
80
87
  }
@@ -0,0 +1,11 @@
1
+ export function assertsIsInteger(input: unknown): asserts input is number {
2
+ if (! Number.isInteger(input)) {
3
+ throw new TypeError('input is not an integer');
4
+ }
5
+ }
6
+
7
+ export function assertsIsFiniteNumber(input: unknown): asserts input is number {
8
+ if (! Number.isFinite(input)) {
9
+ throw new TypeError('input is not a finite number');
10
+ }
11
+ }
package/src/types.ts ADDED
@@ -0,0 +1,12 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export type AnyRecord = Record<PropertyKey, any>;
3
+
4
+ export type UnknownRecord = Record<PropertyKey, unknown>;
5
+
6
+ export type NumericString<T extends string | number = number> = `${T}` extends `${number}` ? `${T}` : never;
7
+
8
+ export type NumericValue = number | bigint | NumericString;
9
+
10
+ export type MaybePlural<T extends string> = T | `${T}s`;
11
+
12
+ export type StringKeys<T> = keyof T & string;
package/src/until.ts CHANGED
@@ -1,13 +1,16 @@
1
+ import type { AnyRecord } from './types';
2
+ import { getType } from './getType.js';
3
+
1
4
  export interface UntilContext {
2
5
  callCount: number;
3
6
  lastCall?: number;
4
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
- data: Record<string, any>;
7
+ data: AnyRecord;
8
+ options: UntilOptions;
6
9
  }
7
10
 
8
- type DelayFn = (context: Readonly<UntilContext>) => number;
11
+ export type DelayFn = (context: Readonly<UntilContext>) => number;
9
12
 
10
- type Delay = DelayFn | number;
13
+ export type Delay = DelayFn | number;
11
14
 
12
15
  export type UntilOptions = {
13
16
  delay: Delay;
@@ -15,29 +18,15 @@ export type UntilOptions = {
15
18
  callLimit?: number;
16
19
  };
17
20
 
18
- export interface UntilContext {
19
- options: UntilOptions;
20
- }
21
-
22
- type ResolverValue<T> = T | PromiseLike<T>;
21
+ export type ResolverValue<T> = T | PromiseLike<T>;
23
22
 
24
- type ResolveFn<T> = (value: ResolverValue<T>) => void;
23
+ export type ResolveFn<T> = (value: ResolverValue<T>) => void;
25
24
 
26
- type RejectFn = (error?: Error) => void;
25
+ export type RejectFn = (error?: Error) => void;
27
26
 
28
27
  export type UntilCallback<T> = (resolve: ResolveFn<T>, reject: RejectFn, context: Readonly<UntilContext>) => void;
29
28
 
30
- function looksLikeDelay( delay: unknown ) : delay is Delay
31
- {
32
- return Number.isInteger( delay ) || typeof delay === 'function';
33
- }
34
-
35
- function getDelay( delay: Delay, context: UntilContext ) : number
36
- {
37
- return typeof delay === 'function' ? delay( context ) : delay;
38
- }
39
-
40
- const defaultOptions: UntilOptions = {
29
+ const defaultOptions: Readonly<UntilOptions> = {
41
30
  delay: 10,
42
31
  };
43
32
 
@@ -47,25 +36,29 @@ const defaultOptions: UntilOptions = {
47
36
  export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultOptions ) : Promise<T>
48
37
  {
49
38
  if ( typeof fn !== 'function' ) {
50
- throw new Error('fn must be a function');
39
+ return Promise.reject(new Error('fn must be a function'));
51
40
  }
52
41
 
53
42
  if ( ! options || typeof options !== 'object' ) {
54
- throw new Error(`invalid options: ${JSON.stringify(options)}`);
43
+ return Promise.reject(new Error(`options must be an object. ${getType(options)} provided.`));
55
44
  }
56
45
 
57
46
  const { delay, timeout, callLimit } = Object.assign({}, defaultOptions, options);
58
47
 
59
- if ( ! looksLikeDelay( delay ) ) {
60
- throw new Error('invalid delay value');
48
+ const isDelay = ( input: unknown ) : input is Delay => Number.isInteger( input ) || typeof input === 'function';
49
+
50
+ const getDelay = ( delay: Delay, context: UntilContext ) : number => typeof delay === 'function' ? delay( context ) : delay;
51
+
52
+ if ( ! isDelay( delay ) ) {
53
+ return Promise.reject(new Error('invalid delay value'));
61
54
  }
62
55
 
63
- if ( timeout !== undefined && ! looksLikeDelay( timeout ) ) {
64
- throw new Error('invalid timeout value');
56
+ if ( timeout !== undefined && ! isDelay( timeout ) ) {
57
+ return Promise.reject(new Error('invalid timeout value'));
65
58
  }
66
59
 
67
60
  if ( callLimit !== undefined && ! Number.isInteger( callLimit ) ) {
68
- throw new Error('invalid callLimit value');
61
+ return Promise.reject(new Error('invalid callLimit value'));
69
62
  }
70
63
 
71
64
  return new Promise<T>( ( resolutionFn: ResolveFn<T>, rejectionFn: RejectFn ) => {