typeshi 2.0.2 → 2.1.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.
@@ -8,7 +8,6 @@ exports.isStringPadEnum = isStringPadEnum;
8
8
  exports.isStringReplaceParams = isStringReplaceParams;
9
9
  exports.isStringCondition = isStringCondition;
10
10
  exports.isStringStripCondition = isStringStripCondition;
11
- exports.DEP_isCleanStringOptions = DEP_isCleanStringOptions;
12
11
  /**
13
12
  * @file src/utils/regex/types/StringOptions.TypeGuards.ts
14
13
  */
@@ -34,7 +33,7 @@ function isStringPadOptions(value) {
34
33
  const candidate = value;
35
34
  return ((0, typeValidation_1.isObject)(candidate)
36
35
  && (0, typeValidation_1.isPositveInteger)(candidate.maxLength)
37
- && (!candidate.char || (typeof candidate.char === 'string' && candidate.char.length === 1))
36
+ && typeValidation_1.isUndefinedOr.string(candidate.char) // && candidate.char.length === 1
38
37
  && typeValidation_1.isUndefinedOr.type(candidate.side, isStringPadEnum));
39
38
  }
40
39
  function isStringCaseEnum(value) {
@@ -48,14 +47,15 @@ function isStringPadEnum(value) {
48
47
  function isStringReplaceParams(value) {
49
48
  const candidate = value;
50
49
  return ((0, typeValidation_1.isObject)(candidate)
51
- && (typeof candidate.searchValue === 'string' || candidate.searchValue instanceof RegExp)
50
+ && (typeof candidate.searchValue === 'string'
51
+ || candidate.searchValue instanceof RegExp)
52
52
  && (typeof candidate.replaceValue === 'string'));
53
53
  }
54
54
  function isStringCondition(value) {
55
55
  const candidate = value;
56
56
  return ((0, typeValidation_1.isObject)(candidate)
57
57
  && (0, typeValidation_1.isFunction)(candidate.condition)
58
- && (!candidate.args || Array.isArray(candidate.args)));
58
+ && typeValidation_1.isUndefinedOr.array(candidate.args));
59
59
  }
60
60
  function isStringStripCondition(value) {
61
61
  const candidate = value;
@@ -64,14 +64,3 @@ function isStringStripCondition(value) {
64
64
  && typeValidation_1.isUndefinedOr.array(candidate.args)
65
65
  && typeValidation_1.isUndefinedOr.positiveInteger(candidate.max));
66
66
  }
67
- /**
68
- * - {@link DEP_CleanStringOptions}
69
- * @param value `any`
70
- * @returns **`isCleanStringOptions`** `boolean`
71
- * - **`true`** if the `value` is an object with at least one key in `['strip', 'case', 'pad', 'replace']` and no other keys,
72
- * - **`false`** `otherwise`.
73
- */
74
- function DEP_isCleanStringOptions(value) {
75
- return (value && typeof value === 'object'
76
- && (0, typeValidation_1.hasKeys)(value, ['strip', 'case', 'pad', 'replace'], false, true));
77
- }
@@ -77,55 +77,3 @@ export declare enum RegExpFlagsEnum {
77
77
  UNICODE = "u",
78
78
  STICKY = "y"
79
79
  }
80
- /**
81
- * @typedefn **`CleanStringOptions`**
82
- */
83
- export type DEP_CleanStringOptions = {
84
- strip?: DEP_StringStripOptions;
85
- case?: DEP_StringCaseOptions;
86
- pad?: DEP_StringPadOptions;
87
- replace?: StringReplaceOptions;
88
- };
89
- /**
90
- * @typedefn **`StringCaseOptions`**
91
- * @property {boolean} [toUpper] - `true` if the string should be converted to upper case
92
- * @property {boolean} [toLower] - `true` if the string should be converted to lower case
93
- * @property {boolean} [toTitle] - `true` if the string should be converted to title case, see {@link toTitleCase}
94
- */
95
- export type DEP_StringCaseOptions = {
96
- toUpper?: boolean;
97
- toLower?: boolean;
98
- toTitle?: boolean;
99
- };
100
- /**
101
- * @typedefn **`StringPadOptions`**
102
- * @property {number} padLength - the length of the string after padding
103
- * @property {string} [padChar] - the character to use for padding, defaults to ' '
104
- * @property {boolean} [padLeft] - `true` if the padding should be added to the left side of the string
105
- * @property {boolean} [padRight] - `true` if the padding should be added to the right side of the string
106
- */
107
- export type DEP_StringPadOptions = {
108
- padLength: number;
109
- padChar?: string;
110
- padLeft?: boolean;
111
- padRight?: boolean;
112
- };
113
- /**
114
- * @typedefn **`StringStripOptions`**
115
- * @property {string} char - the character to strip from the string with {@link applyStripOptions}
116
- * @property {boolean} [escape] - `true` if the character should be escaped
117
- * @property {function} [stripLeftCondition] - a function that takes a string and returns `true` if the character should be stripped from the left side of the string
118
- * @property {any[]} [leftArgs] - arguments to pass to the `stripLeftCondition` function
119
- * - if `stripLeftCondition(s, leftArgs)` is `true` or `stripLeftCondition` is `undefined` (i.e. no conditions need to be met to strip left), the left side of `s` is stripped of `char`
120
- * @property {function} [stripRightCondition] - a function that takes a string and returns `true` if the character should be stripped from the right side of the string
121
- * @property {any[]} [rightArgs] - arguments to pass to the `stripRightCondition` function
122
- * - if `stripRightCondition(s, rightArgs)` is `true` or `stripRightCondition` is `undefined` (i.e. no conditions need to be met to strip right), the right side of `s` is stripped of `char`
123
- */
124
- export type DEP_StringStripOptions = {
125
- char: string;
126
- escape?: boolean;
127
- stripLeftCondition?: (s: string, ...args: any[]) => boolean;
128
- leftArgs?: any[];
129
- stripRightCondition?: (s: string, ...args: any[]) => boolean;
130
- rightArgs?: any[];
131
- };
@@ -1,39 +1,13 @@
1
1
  /**
2
2
  * @file src/utils/typeValidation.ts
3
3
  */
4
- /**
5
- * - alias for {@link isNullLike}
6
- * ... maybe should just change name of isNullLike but that might break things...
7
- * @param value `any` the value to check
8
- * @returns **`isNullLike`** `boolean` = `value is '' | (Array<any> & { length: 0 }) | null | undefined | Record<string, never>`
9
- * - **`true`** `if` the `value` is null, undefined, empty object (no keys), empty array, or empty string
10
- * - **`false`** `otherwise`
11
- */
12
- export declare const isEmpty: typeof isNullLike;
13
- /**
14
- * @param value `any` the value to check
15
- * @returns **`isNullLike`** `boolean` = `value is '' | (Array<any> & { length: 0 }) | null | undefined | Record<string, never>`
16
- * - **`true`** `if` the `value` is null, undefined, empty object (no keys), empty array, or empty string
17
- * - **`false`** `otherwise`
18
- */
19
- export declare function isNullLike(value: any): value is '' | null | undefined | (Array<any> & {
20
- length: 0;
21
- }) | Record<string, never>;
22
- /**
23
- * @deprecated no type predicate b/c "A type predicate cannot reference a rest parameter.ts(1229)"
24
- * @param values `any[]`
25
- * @returns `values.some(v => `{@link isNullLike}`(v))`
26
- * - **`true`** `if` any of the values are null, undefined, empty object (no keys), empty array, or empty string
27
- * - **`false`** `otherwise`.
28
- */
29
- export declare function anyNull(...values: any[]): boolean;
30
4
  /**
31
5
  * @param value
32
6
  * @returns **`isNonEmptyArray`** `boolean` = `value is Array<T> & { length: number }`
33
7
  * - **`true`** if `value` is an array and has at least one element,
34
8
  * - **`false`** otherwise.
35
9
  */
36
- export declare function isNonEmptyArray<T = any>(value: any): value is Array<T> & {
10
+ export declare function isNonEmptyArray<T>(value: any): value is Array<T> & {
37
11
  length: number;
38
12
  };
39
13
  /**
@@ -42,7 +16,7 @@ export declare function isNonEmptyArray<T = any>(value: any): value is Array<T>
42
16
  * - **`true`** if `value` is an array and has no elements,
43
17
  * - **`false`** `otherwise`
44
18
  */
45
- export declare function isEmptyArray<T = any>(value: any): value is Array<T> & {
19
+ export declare function isEmptyArray<T>(value: any): value is Array<T> & {
46
20
  length: 0;
47
21
  };
48
22
  /**
@@ -64,12 +38,12 @@ export declare function isIntegerArray(value: any, requireNonNegative?: boolean,
64
38
  * - `if` `false` then `value` can be empty array
65
39
  * @returns **`isStringArray`** `boolean` = `value is Array<string> & { length: number }`
66
40
  */
67
- export declare function isStringArray(value: any, requireNonEmpty?: boolean): value is Array<string> & {
41
+ export declare function isStringArray(value: any, requireNonEmpty?: boolean): value is string[] & {
68
42
  length: number;
69
43
  };
70
44
  /**
71
45
  * @note **passing in an array will return `false`.**
72
- * @note a value is considered trivial if {@link isNullLike}`(value)` returns `true` and vice versa
46
+ * @note a value is considered trivial if {@link isEmpty}`(value)` returns `true` and vice versa
73
47
  * @param obj `any` The object to check.
74
48
  * @param requireAll `boolean` - flag indicating whether all values must be nontrivial or not
75
49
  * @returns **`hasNonTrivialKeys`** `boolean`
@@ -235,6 +209,15 @@ export declare class isUndefinedOr {
235
209
  static boolean: (value: any) => value is boolean | undefined;
236
210
  static function: (value: any) => value is Function | undefined;
237
211
  }
212
+ /**
213
+ * @param value `any` the value to check
214
+ * @returns **`isEmpty`** `boolean` = `value is '' | (Array<any> & { length: 0 }) | null | undefined | Record<string, never>`
215
+ * - **`true`** `if` the `value` is null, undefined, empty object (no keys), empty array, or empty string
216
+ * - **`false`** `otherwise`
217
+ */
218
+ export declare function isEmpty(value: any): value is '' | null | undefined | (Array<any> & {
219
+ length: 0;
220
+ }) | Record<string, never>;
238
221
  /**
239
222
  * these may be unnecessary, but added for completeness
240
223
  */
@@ -248,6 +231,11 @@ export declare function isBoolean(value: any): value is boolean;
248
231
  * @returns **`isFunction`** `boolean`
249
232
  */
250
233
  export declare function isFunction(value: any): value is Function;
234
+ /**
235
+ * @param value
236
+ * @returns `value === null`
237
+ */
238
+ export declare function isNull(value: unknown): value is null;
251
239
  /**
252
240
  * - passing in `null` returns `false`
253
241
  * @param value `any`
@@ -3,9 +3,7 @@
3
3
  * @file src/utils/typeValidation.ts
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.isUndefinedOr = exports.isOptional = exports.isType = exports.isEmpty = void 0;
7
- exports.isNullLike = isNullLike;
8
- exports.anyNull = anyNull;
6
+ exports.isUndefinedOr = exports.isOptional = exports.isType = void 0;
9
7
  exports.isNonEmptyArray = isNonEmptyArray;
10
8
  exports.isEmptyArray = isEmptyArray;
11
9
  exports.isIntegerArray = isIntegerArray;
@@ -19,59 +17,13 @@ exports.isPrimitiveValue = isPrimitiveValue;
19
17
  exports.isInteger = isInteger;
20
18
  exports.isObject = isObject;
21
19
  exports.isPositveInteger = isPositveInteger;
20
+ exports.isEmpty = isEmpty;
22
21
  exports.isBoolean = isBoolean;
23
22
  exports.isFunction = isFunction;
23
+ exports.isNull = isNull;
24
24
  exports.isUndefined = isUndefined;
25
25
  exports.isUndefinedOrNull = isUndefinedOrNull;
26
26
  const index_1 = require("./regex/index");
27
- /**
28
- * - alias for {@link isNullLike}
29
- * ... maybe should just change name of isNullLike but that might break things...
30
- * @param value `any` the value to check
31
- * @returns **`isNullLike`** `boolean` = `value is '' | (Array<any> & { length: 0 }) | null | undefined | Record<string, never>`
32
- * - **`true`** `if` the `value` is null, undefined, empty object (no keys), empty array, or empty string
33
- * - **`false`** `otherwise`
34
- */
35
- exports.isEmpty = isNullLike;
36
- /**
37
- * @param value `any` the value to check
38
- * @returns **`isNullLike`** `boolean` = `value is '' | (Array<any> & { length: 0 }) | null | undefined | Record<string, never>`
39
- * - **`true`** `if` the `value` is null, undefined, empty object (no keys), empty array, or empty string
40
- * - **`false`** `otherwise`
41
- */
42
- function isNullLike(value) {
43
- if (value === null || value === undefined) {
44
- return true;
45
- }
46
- if (typeof value === 'boolean' || typeof value === 'number') {
47
- return false;
48
- }
49
- // Check for empty object or array
50
- if (typeof value === 'object' && isEmptyArray(Object.keys(value))) {
51
- return true;
52
- }
53
- const isNullLikeString = (typeof value === 'string'
54
- && (value.trim() === ''
55
- || value.toLowerCase() === 'undefined'
56
- || value.toLowerCase() === 'null'));
57
- if (isNullLikeString) {
58
- return true;
59
- }
60
- return false;
61
- }
62
- /**
63
- * @deprecated no type predicate b/c "A type predicate cannot reference a rest parameter.ts(1229)"
64
- * @param values `any[]`
65
- * @returns `values.some(v => `{@link isNullLike}`(v))`
66
- * - **`true`** `if` any of the values are null, undefined, empty object (no keys), empty array, or empty string
67
- * - **`false`** `otherwise`.
68
- */
69
- function anyNull(...values) {
70
- if (values === null || values === undefined) {
71
- return true;
72
- }
73
- return values.some(v => isNullLike(v));
74
- }
75
27
  /**
76
28
  * @param value
77
29
  * @returns **`isNonEmptyArray`** `boolean` = `value is Array<T> & { length: number }`
@@ -118,7 +70,7 @@ function isStringArray(value, requireNonEmpty = true) {
118
70
  }
119
71
  /**
120
72
  * @note **passing in an array will return `false`.**
121
- * @note a value is considered trivial if {@link isNullLike}`(value)` returns `true` and vice versa
73
+ * @note a value is considered trivial if {@link isEmpty}`(value)` returns `true` and vice versa
122
74
  * @param obj `any` The object to check.
123
75
  * @param requireAll `boolean` - flag indicating whether all values must be nontrivial or not
124
76
  * @returns **`hasNonTrivialKeys`** `boolean`
@@ -130,8 +82,8 @@ function hasNonTrivialKeys(obj, requireAll = false) {
130
82
  return false;
131
83
  }
132
84
  return (requireAll
133
- ? Object.values(obj).every(v => !isNullLike(v))
134
- : Object.values(obj).some(v => !isNullLike(v)));
85
+ ? Object.values(obj).every(v => !isEmpty(v))
86
+ : Object.values(obj).some(v => !isEmpty(v)));
135
87
  }
136
88
  /**
137
89
  * @TODO add overload on param `keys` where keys = `{ required: string[], optional: string[] }`
@@ -439,6 +391,32 @@ isUndefinedOr.boolean = (value) => {
439
391
  isUndefinedOr.function = (value) => {
440
392
  return isUndefined(value) || isFunction(value);
441
393
  };
394
+ /**
395
+ * @param value `any` the value to check
396
+ * @returns **`isEmpty`** `boolean` = `value is '' | (Array<any> & { length: 0 }) | null | undefined | Record<string, never>`
397
+ * - **`true`** `if` the `value` is null, undefined, empty object (no keys), empty array, or empty string
398
+ * - **`false`** `otherwise`
399
+ */
400
+ function isEmpty(value) {
401
+ if (value === null || value === undefined) {
402
+ return true;
403
+ }
404
+ if (typeof value === 'boolean' || typeof value === 'number') {
405
+ return false;
406
+ }
407
+ // Check for empty object or array
408
+ if (typeof value === 'object' && isEmptyArray(Object.keys(value))) {
409
+ return true;
410
+ }
411
+ const isEmptyString = (typeof value === 'string'
412
+ && (value.trim() === ''
413
+ || value.toLowerCase() === 'undefined'
414
+ || value.toLowerCase() === 'null'));
415
+ if (isEmptyString) {
416
+ return true;
417
+ }
418
+ return false;
419
+ }
442
420
  /**
443
421
  * these may be unnecessary, but added for completeness
444
422
  */
@@ -456,6 +434,13 @@ function isBoolean(value) {
456
434
  function isFunction(value) {
457
435
  return (typeof value === 'function');
458
436
  }
437
+ /**
438
+ * @param value
439
+ * @returns `value === null`
440
+ */
441
+ function isNull(value) {
442
+ return value === null;
443
+ }
459
444
  /**
460
445
  * - passing in `null` returns `false`
461
446
  * @param value `any`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",