@webdeveric/utils 0.13.0 → 0.15.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 (82) hide show
  1. package/dist/cjs/asArray.js.map +1 -1
  2. package/dist/cjs/convert.js.map +1 -1
  3. package/dist/cjs/delay.js.map +1 -1
  4. package/dist/cjs/delayAnimationFrame.js.map +1 -1
  5. package/dist/cjs/getMaxValue.js.map +1 -1
  6. package/dist/cjs/getMinValue.js.map +1 -1
  7. package/dist/cjs/getOwnProperties.js +1 -4
  8. package/dist/cjs/getOwnProperties.js.map +1 -1
  9. package/dist/cjs/isEmpty.js +5 -1
  10. package/dist/cjs/isEmpty.js.map +1 -1
  11. package/dist/cjs/node/fcrdns.js.map +1 -1
  12. package/dist/cjs/normalize.js +0 -15
  13. package/dist/cjs/normalize.js.map +1 -1
  14. package/dist/cjs/secToString.js.map +1 -1
  15. package/dist/cjs/toPascalCase.js +1 -4
  16. package/dist/cjs/toPascalCase.js.map +1 -1
  17. package/dist/cjs/type-assertion.js.map +1 -1
  18. package/dist/cjs/type-predicate.js +5 -1
  19. package/dist/cjs/type-predicate.js.map +1 -1
  20. package/dist/cjs/uniqueItems.js.map +1 -1
  21. package/dist/cjs/until.js +9 -8
  22. package/dist/cjs/until.js.map +1 -1
  23. package/dist/mjs/asArray.js.map +1 -1
  24. package/dist/mjs/convert.js.map +1 -1
  25. package/dist/mjs/delay.js.map +1 -1
  26. package/dist/mjs/delayAnimationFrame.js.map +1 -1
  27. package/dist/mjs/getMaxValue.js.map +1 -1
  28. package/dist/mjs/getMinValue.js.map +1 -1
  29. package/dist/mjs/getOwnProperties.js +1 -4
  30. package/dist/mjs/getOwnProperties.js.map +1 -1
  31. package/dist/mjs/isEmpty.js +5 -1
  32. package/dist/mjs/isEmpty.js.map +1 -1
  33. package/dist/mjs/node/fcrdns.js.map +1 -1
  34. package/dist/mjs/normalize.js +0 -15
  35. package/dist/mjs/normalize.js.map +1 -1
  36. package/dist/mjs/secToString.js.map +1 -1
  37. package/dist/mjs/toPascalCase.js +1 -4
  38. package/dist/mjs/toPascalCase.js.map +1 -1
  39. package/dist/mjs/type-assertion.js.map +1 -1
  40. package/dist/mjs/type-predicate.js +3 -0
  41. package/dist/mjs/type-predicate.js.map +1 -1
  42. package/dist/mjs/uniqueItems.js.map +1 -1
  43. package/dist/mjs/until.js +5 -5
  44. package/dist/mjs/until.js.map +1 -1
  45. package/dist/types/asArray.d.ts.map +1 -1
  46. package/dist/types/delay.d.ts.map +1 -1
  47. package/dist/types/delayAnimationFrame.d.ts.map +1 -1
  48. package/dist/types/getMaxValue.d.ts.map +1 -1
  49. package/dist/types/getMinValue.d.ts.map +1 -1
  50. package/dist/types/getOwnProperties.d.ts.map +1 -1
  51. package/dist/types/isEmpty.d.ts.map +1 -1
  52. package/dist/types/node/fcrdns.d.ts.map +1 -1
  53. package/dist/types/normalize.d.ts +4 -2
  54. package/dist/types/normalize.d.ts.map +1 -1
  55. package/dist/types/secToString.d.ts.map +1 -1
  56. package/dist/types/toPascalCase.d.ts.map +1 -1
  57. package/dist/types/type-predicate.d.ts +3 -0
  58. package/dist/types/type-predicate.d.ts.map +1 -1
  59. package/dist/types/types.d.ts +3 -0
  60. package/dist/types/types.d.ts.map +1 -1
  61. package/dist/types/uniqueItems.d.ts.map +1 -1
  62. package/dist/types/until.d.ts +2 -1
  63. package/dist/types/until.d.ts.map +1 -1
  64. package/package.json +32 -13
  65. package/readme.md +1 -1
  66. package/src/asArray.ts +2 -3
  67. package/src/convert.ts +1 -1
  68. package/src/delay.ts +6 -7
  69. package/src/delayAnimationFrame.ts +6 -8
  70. package/src/getMaxValue.ts +4 -6
  71. package/src/getMinValue.ts +4 -6
  72. package/src/getOwnProperties.ts +1 -4
  73. package/src/isEmpty.ts +8 -2
  74. package/src/node/fcrdns.ts +10 -16
  75. package/src/normalize.ts +26 -9
  76. package/src/secToString.ts +19 -22
  77. package/src/toPascalCase.ts +9 -13
  78. package/src/type-assertion.ts +3 -3
  79. package/src/type-predicate.ts +5 -1
  80. package/src/types.ts +7 -0
  81. package/src/uniqueItems.ts +2 -3
  82. package/src/until.ts +35 -38
package/src/until.ts CHANGED
@@ -3,7 +3,7 @@ import { getType } from './getType.js';
3
3
 
4
4
  export interface UntilContext {
5
5
  callCount: number;
6
- lastCall?: number;
6
+ lastCall: number | undefined;
7
7
  data: AnyRecord;
8
8
  options: UntilOptions;
9
9
  }
@@ -30,38 +30,38 @@ const defaultOptions: Readonly<UntilOptions> = {
30
30
  delay: 10,
31
31
  };
32
32
 
33
+ export const isDelay = (input: unknown): input is Delay => Number.isInteger(input) || typeof input === 'function';
34
+
33
35
  /**
34
36
  * Return a Promise that delegates resolving/rejecting to the passed in function.
35
37
  */
36
- export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultOptions ) : Promise<T>
37
- {
38
- if ( typeof fn !== 'function' ) {
38
+ export function until<T>(fn: UntilCallback<T>, options: UntilOptions = defaultOptions): Promise<T> {
39
+ if (typeof fn !== 'function') {
39
40
  return Promise.reject(new Error('fn must be a function'));
40
41
  }
41
42
 
42
- if ( ! options || typeof options !== 'object' ) {
43
- return Promise.reject(new Error(`options must be an object. ${getType(options)} provided.`));
43
+ if (!options || typeof options !== 'object') {
44
+ return Promise.reject(new TypeError(`options must be an object. ${getType(options)} provided.`));
44
45
  }
45
46
 
46
47
  const { delay, timeout, callLimit } = Object.assign({}, defaultOptions, options);
47
48
 
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;
49
+ const getDelay = (delay: Delay, context: UntilContext): number =>
50
+ typeof delay === 'function' ? delay(context) : delay;
51
51
 
52
- if ( ! isDelay( delay ) ) {
53
- return Promise.reject(new Error('invalid delay value'));
52
+ if (!isDelay(delay)) {
53
+ return Promise.reject(new TypeError('invalid delay value'));
54
54
  }
55
55
 
56
- if ( timeout !== undefined && ! isDelay( timeout ) ) {
57
- return Promise.reject(new Error('invalid timeout value'));
56
+ if (timeout !== undefined && !isDelay(timeout)) {
57
+ return Promise.reject(new TypeError('invalid timeout value'));
58
58
  }
59
59
 
60
- if ( callLimit !== undefined && ! Number.isInteger( callLimit ) ) {
61
- return Promise.reject(new Error('invalid callLimit value'));
60
+ if (callLimit !== undefined && !Number.isInteger(callLimit)) {
61
+ return Promise.reject(new TypeError('invalid callLimit value'));
62
62
  }
63
63
 
64
- return new Promise<T>( ( resolutionFn: ResolveFn<T>, rejectionFn: RejectFn ) => {
64
+ return new Promise<T>((resolutionFn: ResolveFn<T>, rejectionFn: RejectFn) => {
65
65
  let callCount = 0;
66
66
  let lastCall: number | undefined;
67
67
  let done = false;
@@ -70,19 +70,19 @@ export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultO
70
70
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
71
  let timeoutTimer: any;
72
72
 
73
- const finish = () : void => {
73
+ const finish = (): void => {
74
74
  done = true;
75
- clearTimeout( timer );
76
- clearTimeout( timeoutTimer );
75
+ clearTimeout(timer);
76
+ clearTimeout(timeoutTimer);
77
77
  };
78
78
 
79
- const resolve: ResolveFn<T> = (value: ResolverValue<T>) : void => {
80
- resolutionFn( value );
79
+ const resolve: ResolveFn<T> = (value: ResolverValue<T>): void => {
80
+ resolutionFn(value);
81
81
  finish();
82
82
  };
83
83
 
84
- const reject: RejectFn = (error?: Error) : void => {
85
- rejectionFn( error );
84
+ const reject: RejectFn = (error?: Error): void => {
85
+ rejectionFn(error);
86
86
  finish();
87
87
  };
88
88
 
@@ -99,35 +99,32 @@ export function until<T>( fn: UntilCallback<T>, options: UntilOptions = defaultO
99
99
  data: {},
100
100
  });
101
101
 
102
- const callUntilDone = ( fn: UntilCallback<T>, delay: Delay, resolve: ResolveFn<T>, reject: RejectFn ) : void => {
102
+ const callUntilDone = (fn: UntilCallback<T>, delay: Delay, resolve: ResolveFn<T>, reject: RejectFn): void => {
103
103
  try {
104
- if ( callLimit && callCount >= callLimit ) {
104
+ if (callLimit && callCount >= callLimit) {
105
105
  throw new Error(`until callLimit reached: ${callLimit}`);
106
106
  }
107
107
 
108
108
  ++callCount;
109
109
 
110
- fn( resolve, reject, context );
110
+ fn(resolve, reject, context);
111
111
 
112
112
  lastCall = Date.now();
113
113
 
114
- if ( ! done ) {
115
- timer = setTimeout( callUntilDone, getDelay( delay, context ), fn, delay, resolve, reject );
114
+ if (!done) {
115
+ timer = setTimeout(callUntilDone, getDelay(delay, context), fn, delay, resolve, reject);
116
116
  }
117
- } catch ( error ) {
118
- reject( error instanceof Error ? error : new Error(`${error}`) );
117
+ } catch (error) {
118
+ reject(error instanceof Error ? error : new Error(`${error}`));
119
119
  }
120
120
  };
121
121
 
122
- callUntilDone( fn, delay, resolve, reject );
122
+ callUntilDone(fn, delay, resolve, reject);
123
123
 
124
- if ( ! done && timeout !== undefined ) {
125
- timeoutTimer = setTimeout(
126
- () => {
127
- reject( new Error('until: timed out') );
128
- },
129
- getDelay( timeout, context ),
130
- );
124
+ if (!done && timeout !== undefined) {
125
+ timeoutTimer = setTimeout(() => {
126
+ reject(new Error('until: timed out'));
127
+ }, getDelay(timeout, context));
131
128
  }
132
129
  });
133
130
  }