@webdeveric/utils 0.10.0 → 0.13.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 (106) 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 +16 -0
  6. package/dist/cjs/getMaxValue.js.map +1 -0
  7. package/dist/cjs/getMinValue.js +16 -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/toPascalCase.js +2 -2
  24. package/dist/cjs/toPascalCase.js.map +1 -1
  25. package/dist/cjs/type-assertion.js +23 -0
  26. package/dist/cjs/type-assertion.js.map +1 -0
  27. package/dist/cjs/type-predicate.js +28 -0
  28. package/dist/cjs/type-predicate.js.map +1 -0
  29. package/dist/cjs/types.js +3 -0
  30. package/dist/cjs/types.js.map +1 -0
  31. package/dist/cjs/until.js.map +1 -1
  32. package/dist/mjs/clamp.js +6 -0
  33. package/dist/mjs/clamp.js.map +1 -0
  34. package/dist/mjs/compose.js +1 -2
  35. package/dist/mjs/compose.js.map +1 -1
  36. package/dist/mjs/convert.js +15 -0
  37. package/dist/mjs/convert.js.map +1 -0
  38. package/dist/mjs/getMaxValue.js +12 -0
  39. package/dist/mjs/getMaxValue.js.map +1 -0
  40. package/dist/mjs/getMinValue.js +12 -0
  41. package/dist/mjs/getMinValue.js.map +1 -0
  42. package/dist/mjs/getOwnKeys.js +1 -4
  43. package/dist/mjs/getOwnKeys.js.map +1 -1
  44. package/dist/mjs/getOwnProperties.js +7 -0
  45. package/dist/mjs/getOwnProperties.js.map +1 -0
  46. package/dist/mjs/index.js +9 -0
  47. package/dist/mjs/index.js.map +1 -1
  48. package/dist/mjs/isEmpty.js +13 -0
  49. package/dist/mjs/isEmpty.js.map +1 -0
  50. package/dist/mjs/isObject.js +4 -0
  51. package/dist/mjs/isObject.js.map +1 -0
  52. package/dist/mjs/normalize.js +14 -15
  53. package/dist/mjs/normalize.js.map +1 -1
  54. package/dist/mjs/toPascalCase.js +2 -2
  55. package/dist/mjs/toPascalCase.js.map +1 -1
  56. package/dist/mjs/type-assertion.js +17 -0
  57. package/dist/mjs/type-assertion.js.map +1 -0
  58. package/dist/mjs/type-predicate.js +13 -0
  59. package/dist/mjs/type-predicate.js.map +1 -0
  60. package/dist/mjs/types.js +2 -0
  61. package/dist/mjs/types.js.map +1 -0
  62. package/dist/mjs/until.js.map +1 -1
  63. package/dist/types/clamp.d.ts +3 -0
  64. package/dist/types/clamp.d.ts.map +1 -0
  65. package/dist/types/convert.d.ts +8 -0
  66. package/dist/types/convert.d.ts.map +1 -0
  67. package/dist/types/getMaxValue.d.ts +3 -0
  68. package/dist/types/getMaxValue.d.ts.map +1 -0
  69. package/dist/types/getMinValue.d.ts +3 -0
  70. package/dist/types/getMinValue.d.ts.map +1 -0
  71. package/dist/types/getOwnKeys.d.ts +1 -1
  72. package/dist/types/getOwnKeys.d.ts.map +1 -1
  73. package/dist/types/getOwnProperties.d.ts +2 -0
  74. package/dist/types/getOwnProperties.d.ts.map +1 -0
  75. package/dist/types/index.d.ts +9 -0
  76. package/dist/types/index.d.ts.map +1 -1
  77. package/dist/types/isEmpty.d.ts +2 -0
  78. package/dist/types/isEmpty.d.ts.map +1 -0
  79. package/dist/types/isObject.d.ts +2 -0
  80. package/dist/types/isObject.d.ts.map +1 -0
  81. package/dist/types/normalize.d.ts +8 -6
  82. package/dist/types/normalize.d.ts.map +1 -1
  83. package/dist/types/type-assertion.d.ts +5 -0
  84. package/dist/types/type-assertion.d.ts.map +1 -0
  85. package/dist/types/type-predicate.d.ts +14 -0
  86. package/dist/types/type-predicate.d.ts.map +1 -0
  87. package/dist/types/types.d.ts +7 -0
  88. package/dist/types/types.d.ts.map +1 -0
  89. package/dist/types/until.d.ts +2 -1
  90. package/dist/types/until.d.ts.map +1 -1
  91. package/package.json +20 -9
  92. package/src/clamp.ts +7 -0
  93. package/src/convert.ts +30 -0
  94. package/src/getMaxValue.ts +18 -0
  95. package/src/getMinValue.ts +18 -0
  96. package/src/getOwnKeys.ts +2 -5
  97. package/src/getOwnProperties.ts +6 -0
  98. package/src/index.ts +9 -0
  99. package/src/isEmpty.ts +15 -0
  100. package/src/isObject.ts +3 -0
  101. package/src/normalize.ts +36 -29
  102. package/src/toPascalCase.ts +2 -2
  103. package/src/type-assertion.ts +20 -0
  104. package/src/type-predicate.ts +32 -0
  105. package/src/types.ts +12 -0
  106. package/src/until.ts +2 -2
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.js';
6
- import { getType } from './getType.js';
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?: AnyNormalizer<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
  }
@@ -12,7 +12,7 @@ export function toPascalCase(text: string, customWords?: Record<string, string>)
12
12
  words = words.map( word => {
13
13
  const fixedWord = word.replace(/'/g, '');
14
14
 
15
- return fixedWord[ 0 ].toUpperCase() + fixedWord.substring(1);
15
+ return fixedWord.substring(0, 1).toUpperCase() + fixedWord.substring(1);
16
16
  });
17
17
 
18
18
  if ( customWords && getType(customWords) === 'Object' ) {
@@ -27,7 +27,7 @@ export function toPascalCase(text: string, customWords?: Record<string, string>)
27
27
  const lowerWord = word.toLowerCase();
28
28
 
29
29
  for (const [ key, value ] of replacements) {
30
- if (key === lowerWord) {
30
+ if (key === lowerWord && typeof value === 'string') {
31
31
  return value;
32
32
  }
33
33
  }
@@ -0,0 +1,20 @@
1
+ import { isNumericValueArray } from './type-predicate.js';
2
+ import type { NumericValue } from './types';
3
+
4
+ export function assertIsInteger(input: unknown): asserts input is number {
5
+ if (! Number.isInteger(input)) {
6
+ throw new TypeError('input is not an integer');
7
+ }
8
+ }
9
+
10
+ export function assertIsFiniteNumber(input: unknown): asserts input is number {
11
+ if (! Number.isFinite(input)) {
12
+ throw new TypeError('input is not a finite number');
13
+ }
14
+ }
15
+
16
+ export function assertIsNumericValueArray(input: unknown): asserts input is NumericValue[] {
17
+ if (! isNumericValueArray(input)) {
18
+ throw new TypeError('all items must be a NumericValue');
19
+ }
20
+ }
@@ -0,0 +1,32 @@
1
+ import type { NumericString, NumericValue } from './types';
2
+
3
+ export const isOptionalString = (input: unknown): input is string | undefined =>
4
+ typeof input === 'string' || typeof input === 'undefined';
5
+
6
+ export const isOptionalNumber = (input: unknown): input is number | undefined =>
7
+ (typeof input === 'number' && ! Number.isNaN(input)) || typeof input === 'undefined';
8
+
9
+ export const isStringMatching = (input: unknown, pattern: RegExp): input is string =>
10
+ typeof input === 'string' && pattern.test(input);
11
+
12
+ export const isPropertyKey = (input: unknown): input is PropertyKey =>
13
+ typeof input === 'string' || typeof input === 'number' || typeof input === 'symbol';
14
+
15
+ export const isNumericString = (input: unknown): input is NumericString =>
16
+ isStringMatching(input, /^[-+]?\d+(\.\d+)?$/);
17
+
18
+ export const isNumericValue = (input: unknown): input is NumericValue =>
19
+ typeof input === 'number' || typeof input === 'bigint' || isNumericString(input);
20
+
21
+ export const isNumericValueArray = (input: unknown): input is NumericValue[] =>
22
+ Array.isArray(input) && input.every(isNumericValue);
23
+
24
+ export const isIntString = (input: unknown): input is NumericString => isStringMatching(input, /^[-+]?\d+$/);
25
+
26
+ export const isFiniteNumber = (input: unknown): input is number => typeof input === 'number' && Number.isFinite(input);
27
+
28
+ export const isInteger = (input: unknown): input is number => Number.isInteger(input);
29
+
30
+ export const isPositiveInteger = (input: unknown): input is number => isInteger(input) && input >= 0;
31
+
32
+ export const isPositiveFiniteNumber = (input: unknown): input is number => isFiniteNumber(input) && input >= 0;
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,10 +1,10 @@
1
+ import type { AnyRecord } from './types';
1
2
  import { getType } from './getType.js';
2
3
 
3
4
  export interface UntilContext {
4
5
  callCount: number;
5
6
  lastCall?: number;
6
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
- data: Record<PropertyKey, any>;
7
+ data: AnyRecord;
8
8
  options: UntilOptions;
9
9
  }
10
10