typeshi 2.1.8 → 2.2.1
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.
|
@@ -52,16 +52,8 @@ exports.enumArgument = enumArgument;
|
|
|
52
52
|
exports.existingPathArgument = existingPathArgument;
|
|
53
53
|
/**
|
|
54
54
|
* @file src/utils/argumentValidation.ts
|
|
55
|
-
* @
|
|
56
|
-
* @description moved the content of parameter type checks at the start of
|
|
57
|
-
* functions to here. use these when you want your function to throw a fit when
|
|
55
|
+
* @description functions useful for sanity checks. use if want function to throw a fit when
|
|
58
56
|
* it receives bad input.
|
|
59
|
-
* @example
|
|
60
|
-
* import * as validate from "@typeshi/argumentValidation";
|
|
61
|
-
* @TODO add boolean value configurable by a setter function that specifies if errors should be thrown or only logged
|
|
62
|
-
* - maybe add a configurable value that the validation functions should return if the validation test fails
|
|
63
|
-
* - change the validation functions such that they return the validated value, if possible?
|
|
64
|
-
* - or maybe have them return boolean type predicates ?
|
|
65
57
|
*/
|
|
66
58
|
const typeValidation_1 = require("./typeValidation");
|
|
67
59
|
const setupLog_1 = require("../config/setupLog");
|
package/dist/utils/object.d.ts
CHANGED
|
@@ -32,10 +32,6 @@ export type TransformOptions<T extends object, K extends keyof T, S extends obje
|
|
|
32
32
|
* @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
|
|
33
33
|
*/
|
|
34
34
|
export declare function sanitizeAndMap<T extends object, S extends object = any>(obj: S, schema: TransformationSchema<T, S>, passThroughKeys?: (keyof T & keyof S)[]): T;
|
|
35
|
-
/**
|
|
36
|
-
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
37
|
-
*/
|
|
38
|
-
export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T | keyof any): boolean;
|
|
39
35
|
/**
|
|
40
36
|
* @returns `boolean`
|
|
41
37
|
* - `true` if all keys in obj are also in validKeys
|
|
@@ -46,6 +42,23 @@ export declare function hasValidKeysOnly<T extends object>(obj: object, validKey
|
|
|
46
42
|
* @returns a new object containing only the specified keys.
|
|
47
43
|
*/
|
|
48
44
|
export declare function picked<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
45
|
+
/**
|
|
46
|
+
* @param arr `T[]`
|
|
47
|
+
* @param maxLength `number` an integer greater than or equal to `0`
|
|
48
|
+
* - the original array is returned if `maxLength < 0`
|
|
49
|
+
* - converts `float` to `int` with `Math.floor()`;
|
|
50
|
+
* @note **original array is returned if `arr.length <= Math.floor(maxLength)`**
|
|
51
|
+
* @param principle `'LIFO' | 'FIFO' (optional)` `default = 'LIFO'`
|
|
52
|
+
* `(if arr.length > maxLength)` indicate from which end of array to remove elements
|
|
53
|
+
* - `'LIFO'` - remove elements from end (`'Last-In-First-Out'`)
|
|
54
|
+
* - `'FIFO'` - remove elements from front (`'First-In-First-Out'`)
|
|
55
|
+
* @note **assumes newest elements were pushed to end** i.e. `stack.push()` or `queue.enque()`
|
|
56
|
+
* @param inPlace `boolean (optional)` `default = true`
|
|
57
|
+
* - `true` - modify and return original array using: `arr.length = maxLength (LIFO)` or `arr.splice() (FIFO)`
|
|
58
|
+
* - `false` - return new array with: `arr.slice()` `arr.slice(0, maxLength) (LIFO) or arr.slice(i, n) (FIFO)` where `n - i === maxLength`
|
|
59
|
+
* @returns **`arr`** `T[]` where `arr.length <= maxLength`
|
|
60
|
+
*/
|
|
61
|
+
export declare function enforceMaxLength<T = unknown>(arr: T[], maxLength: number, principle?: 'LIFO' | 'FIFO', inPlace?: boolean): T[];
|
|
49
62
|
export declare class Restrict {
|
|
50
63
|
/**
|
|
51
64
|
* `hasValidKeysOnly`
|
|
@@ -56,4 +69,34 @@ export declare class Restrict {
|
|
|
56
69
|
* @returns a new object containing only the specified keys.
|
|
57
70
|
*/
|
|
58
71
|
static toPicked: typeof picked;
|
|
72
|
+
/**
|
|
73
|
+
* @param arr `T[]`
|
|
74
|
+
* @param maxLength `number` an integer greater than or equal to `0`
|
|
75
|
+
* - the original array is returned if `maxLength < 0`
|
|
76
|
+
* - converts `float` to `int` with `Math.floor()`;
|
|
77
|
+
* @note **original array is returned if `arr.length <= Math.floor(maxLength)`**
|
|
78
|
+
* @param principle `'LIFO' | 'FIFO' (optional)` `default = 'LIFO'`
|
|
79
|
+
* `(if arr.length > maxLength)` indicate from which end of array to remove elements
|
|
80
|
+
* - `'LIFO'` - remove elements from end (`'Last-In-First-Out'`)
|
|
81
|
+
* - `'FIFO'` - remove elements from front (`'First-In-First-Out'`)
|
|
82
|
+
* @note **assumes newest elements were pushed to end** i.e. `stack.push()` or `queue.enque()`
|
|
83
|
+
* @param inPlace `boolean (optional)` `default = true`
|
|
84
|
+
* - `true` - modify and return original array with: `arr.length = maxLength (LIFO)` or `arr.splice() (FIFO)`
|
|
85
|
+
* - - if `'LIFO'`, can use JS trick by setting `arr.length = maxLength`
|
|
86
|
+
* - `false` - return new array with: `arr.slice()` `arr.slice(0, maxLength) (LIFO) or arr.slice(i, n) (FIFO)` where `n - i === maxLength`
|
|
87
|
+
* @returns **`arr`** `T[]` where `arr.length <= maxLength`
|
|
88
|
+
*/
|
|
89
|
+
static arrayLength: typeof enforceMaxLength;
|
|
59
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
93
|
+
*/
|
|
94
|
+
export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T | keyof any): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* @returns **`containsKey`** `boolean = obj is { [K in keyof T]: T[K] }`
|
|
97
|
+
* - `true` if **`all`** `k` in `keys` return true for `Object.prototype.hasOwnProperty.call(obj, k)`
|
|
98
|
+
* - `false` otherwise
|
|
99
|
+
*/
|
|
100
|
+
export declare function containsKey<T extends object, K extends (keyof T | (keyof any & {})) = any>(obj: T, ...keys: K[]): obj is {
|
|
101
|
+
[K in keyof T]: T[K];
|
|
102
|
+
};
|
package/dist/utils/object.js
CHANGED
|
@@ -6,9 +6,11 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.Restrict = void 0;
|
|
8
8
|
exports.sanitizeAndMap = sanitizeAndMap;
|
|
9
|
-
exports.hasDefinedEntry = hasDefinedEntry;
|
|
10
9
|
exports.hasValidKeysOnly = hasValidKeysOnly;
|
|
11
10
|
exports.picked = picked;
|
|
11
|
+
exports.enforceMaxLength = enforceMaxLength;
|
|
12
|
+
exports.hasDefinedEntry = hasDefinedEntry;
|
|
13
|
+
exports.containsKey = containsKey;
|
|
12
14
|
const typeValidation_1 = require("./typeValidation");
|
|
13
15
|
/**
|
|
14
16
|
* @param obj `S` - source object (e.g., Request Body)
|
|
@@ -55,12 +57,6 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
|
|
|
55
57
|
}
|
|
56
58
|
return data;
|
|
57
59
|
}
|
|
58
|
-
/**
|
|
59
|
-
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
60
|
-
*/
|
|
61
|
-
function hasDefinedEntry(obj, key) {
|
|
62
|
-
return Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;
|
|
63
|
-
}
|
|
64
60
|
/**
|
|
65
61
|
* @returns `boolean`
|
|
66
62
|
* - `true` if all keys in obj are also in validKeys
|
|
@@ -80,6 +76,59 @@ function picked(obj, keys) {
|
|
|
80
76
|
}
|
|
81
77
|
return result;
|
|
82
78
|
}
|
|
79
|
+
// @TODO could try to generalize enforceMaxLength to handle objects operating on Object.keys()/Object.entries()
|
|
80
|
+
// but I don't think hashed keys of dicts/objects retain consistent information on what order they were added to object...
|
|
81
|
+
/**
|
|
82
|
+
* @param arr `T[]`
|
|
83
|
+
* @param maxLength `number` an integer greater than or equal to `0`
|
|
84
|
+
* - the original array is returned if `maxLength < 0`
|
|
85
|
+
* - converts `float` to `int` with `Math.floor()`;
|
|
86
|
+
* @note **original array is returned if `arr.length <= Math.floor(maxLength)`**
|
|
87
|
+
* @param principle `'LIFO' | 'FIFO' (optional)` `default = 'LIFO'`
|
|
88
|
+
* `(if arr.length > maxLength)` indicate from which end of array to remove elements
|
|
89
|
+
* - `'LIFO'` - remove elements from end (`'Last-In-First-Out'`)
|
|
90
|
+
* - `'FIFO'` - remove elements from front (`'First-In-First-Out'`)
|
|
91
|
+
* @note **assumes newest elements were pushed to end** i.e. `stack.push()` or `queue.enque()`
|
|
92
|
+
* @param inPlace `boolean (optional)` `default = true`
|
|
93
|
+
* - `true` - modify and return original array using: `arr.length = maxLength (LIFO)` or `arr.splice() (FIFO)`
|
|
94
|
+
* - `false` - return new array with: `arr.slice()` `arr.slice(0, maxLength) (LIFO) or arr.slice(i, n) (FIFO)` where `n - i === maxLength`
|
|
95
|
+
* @returns **`arr`** `T[]` where `arr.length <= maxLength`
|
|
96
|
+
*/
|
|
97
|
+
function enforceMaxLength(arr, maxLength, principle = 'LIFO', inPlace = true) {
|
|
98
|
+
if (maxLength < 0)
|
|
99
|
+
return arr;
|
|
100
|
+
maxLength = Math.floor(maxLength);
|
|
101
|
+
if (maxLength === 0) {
|
|
102
|
+
if (inPlace) {
|
|
103
|
+
arr.length = 0;
|
|
104
|
+
return arr;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const n = arr.length;
|
|
111
|
+
if (n <= maxLength)
|
|
112
|
+
return arr;
|
|
113
|
+
const excess = n - maxLength;
|
|
114
|
+
if (inPlace) {
|
|
115
|
+
if (principle === 'LIFO') {
|
|
116
|
+
arr.length = maxLength;
|
|
117
|
+
}
|
|
118
|
+
else { // 'FIFO'
|
|
119
|
+
arr.splice(0, excess);
|
|
120
|
+
}
|
|
121
|
+
return arr;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (principle === 'LIFO') {
|
|
125
|
+
return arr.slice(0, maxLength);
|
|
126
|
+
}
|
|
127
|
+
else { // 'FIFO'
|
|
128
|
+
return arr.slice(excess);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
83
132
|
class Restrict {
|
|
84
133
|
}
|
|
85
134
|
exports.Restrict = Restrict;
|
|
@@ -92,3 +141,39 @@ Restrict.keys = hasValidKeysOnly;
|
|
|
92
141
|
* @returns a new object containing only the specified keys.
|
|
93
142
|
*/
|
|
94
143
|
Restrict.toPicked = picked;
|
|
144
|
+
/**
|
|
145
|
+
* @param arr `T[]`
|
|
146
|
+
* @param maxLength `number` an integer greater than or equal to `0`
|
|
147
|
+
* - the original array is returned if `maxLength < 0`
|
|
148
|
+
* - converts `float` to `int` with `Math.floor()`;
|
|
149
|
+
* @note **original array is returned if `arr.length <= Math.floor(maxLength)`**
|
|
150
|
+
* @param principle `'LIFO' | 'FIFO' (optional)` `default = 'LIFO'`
|
|
151
|
+
* `(if arr.length > maxLength)` indicate from which end of array to remove elements
|
|
152
|
+
* - `'LIFO'` - remove elements from end (`'Last-In-First-Out'`)
|
|
153
|
+
* - `'FIFO'` - remove elements from front (`'First-In-First-Out'`)
|
|
154
|
+
* @note **assumes newest elements were pushed to end** i.e. `stack.push()` or `queue.enque()`
|
|
155
|
+
* @param inPlace `boolean (optional)` `default = true`
|
|
156
|
+
* - `true` - modify and return original array with: `arr.length = maxLength (LIFO)` or `arr.splice() (FIFO)`
|
|
157
|
+
* - - if `'LIFO'`, can use JS trick by setting `arr.length = maxLength`
|
|
158
|
+
* - `false` - return new array with: `arr.slice()` `arr.slice(0, maxLength) (LIFO) or arr.slice(i, n) (FIFO)` where `n - i === maxLength`
|
|
159
|
+
* @returns **`arr`** `T[]` where `arr.length <= maxLength`
|
|
160
|
+
*/
|
|
161
|
+
Restrict.arrayLength = enforceMaxLength;
|
|
162
|
+
/**
|
|
163
|
+
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
164
|
+
*/
|
|
165
|
+
function hasDefinedEntry(obj, key) {
|
|
166
|
+
return Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* @returns **`containsKey`** `boolean = obj is { [K in keyof T]: T[K] }`
|
|
170
|
+
* - `true` if **`all`** `k` in `keys` return true for `Object.prototype.hasOwnProperty.call(obj, k)`
|
|
171
|
+
* - `false` otherwise
|
|
172
|
+
*/
|
|
173
|
+
function containsKey(obj, ...keys) {
|
|
174
|
+
for (let k of keys) {
|
|
175
|
+
if (!Object.prototype.hasOwnProperty.call(obj, k))
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
@@ -25,9 +25,9 @@ export declare function isEmptyArray<T>(value: any): value is Array<T> & {
|
|
|
25
25
|
* @param requireNonEmpty `boolean` `default = true`
|
|
26
26
|
* - `if` `true` then `value` must be array with at least 1 element
|
|
27
27
|
* - `if` `false` then `value` can be empty array
|
|
28
|
-
* @returns **`isIntegerArray`** `boolean` = `value is
|
|
28
|
+
* @returns **`isIntegerArray`** `boolean` = `value is number[] & { length: number }`
|
|
29
29
|
*/
|
|
30
|
-
export declare function isIntegerArray(value: any, requireNonNegative?: boolean, requireNonEmpty?: boolean): value is
|
|
30
|
+
export declare function isIntegerArray(value: any, requireNonNegative?: boolean, requireNonEmpty?: boolean): value is number[] & {
|
|
31
31
|
length: number;
|
|
32
32
|
};
|
|
33
33
|
/**
|
|
@@ -36,23 +36,23 @@ export declare function isIntegerArray(value: any, requireNonNegative?: boolean,
|
|
|
36
36
|
* @param requireNonEmpty `boolean` `default = true`
|
|
37
37
|
* - `if` `true` then `value` must be array with at least 1 element and every element `isNonEmptyString`
|
|
38
38
|
* - `if` `false` then `value` can be empty array
|
|
39
|
-
* @returns **`isStringArray`** `boolean` = `value is
|
|
39
|
+
* @returns **`isStringArray`** `boolean` = `value is string[] & { length: number }`
|
|
40
40
|
*/
|
|
41
41
|
export declare function isStringArray(value: any, requireNonEmpty?: boolean): value is string[] & {
|
|
42
42
|
length: number;
|
|
43
43
|
};
|
|
44
44
|
/**
|
|
45
|
+
* `fka hasNonTrivialKeys`
|
|
45
46
|
* @note **passing in an array will return `false`.**
|
|
46
47
|
* @note a value is considered trivial if {@link isEmpty}`(value)` returns `true` and vice versa
|
|
47
48
|
* @param obj `any` The object to check.
|
|
48
49
|
* @param requireAll `boolean` - flag indicating whether all values must be nontrivial or not
|
|
49
|
-
* @returns **`
|
|
50
|
+
* @returns **`hasNonTrivialEntries`** `boolean`
|
|
50
51
|
* - **`true`** `if` the `obj` has non-empty keys,
|
|
51
52
|
* - **`false`** `otherwise`
|
|
52
53
|
*/
|
|
53
|
-
export declare function
|
|
54
|
+
export declare function hasNonTrivialEntries<T extends object>(obj: T, requireAll?: boolean): obj is T;
|
|
54
55
|
/**
|
|
55
|
-
* @TODO add overload on param `keys` where keys = `{ required: string[], optional: string[] }`
|
|
56
56
|
* @note uses `key in obj` for each element of param `keys`
|
|
57
57
|
* @param obj `T extends Object` the object to check
|
|
58
58
|
* @param keys `Array<keyof T> | string[] | string` the list of keys that obj must have
|
|
@@ -81,12 +81,12 @@ export declare function areEquivalentObjects(objA: Record<string, any>, objB: Re
|
|
|
81
81
|
* @param requireNonNegative `boolean` `default = false`
|
|
82
82
|
* @returns **`isNumeric`** `value is string | number`
|
|
83
83
|
* - **`true`** `if` `value` is either a `number` or a `string` that can be casted to a `number`
|
|
84
|
-
* while also meeting the
|
|
84
|
+
* while also meeting the other params
|
|
85
85
|
* - **`false`** `otherwise`
|
|
86
86
|
*/
|
|
87
|
-
export declare function isNumeric(value:
|
|
87
|
+
export declare function isNumeric<T extends string | number = string>(value: unknown, requireInteger?: boolean, requireNonNegative?: boolean): value is T;
|
|
88
88
|
/**
|
|
89
|
-
* @param value `
|
|
89
|
+
* @param value `unknown`
|
|
90
90
|
* @param requireNonSpace `boolean (optional)` `default` = `false`
|
|
91
91
|
* - `if true` then `value.trim()` must not result in empty string
|
|
92
92
|
* - `if false` then allows for `value` to consist solely of whitespace characters
|
|
@@ -94,30 +94,30 @@ export declare function isNumeric(value: any, requireInteger?: boolean, requireN
|
|
|
94
94
|
* - `true` `if` `value` is a non-empty string
|
|
95
95
|
* - `false` `otherwise`.
|
|
96
96
|
*/
|
|
97
|
-
export declare function isNonEmptyString(value:
|
|
97
|
+
export declare function isNonEmptyString(value: unknown, requireNonSpace?: boolean): value is string & {
|
|
98
98
|
length: number;
|
|
99
99
|
};
|
|
100
|
-
export declare function isPrimitiveValue(value:
|
|
100
|
+
export declare function isPrimitiveValue(value: unknown): value is string | number | boolean | null | undefined;
|
|
101
101
|
/**
|
|
102
|
-
* @param value `
|
|
102
|
+
* @param value `unknown`
|
|
103
103
|
* @param requireNonNegative `boolean`
|
|
104
104
|
* - `if` `true` then require that `value` be an integer `>= 0`
|
|
105
105
|
* - `if` `false` then the sign of the number doesn't matter
|
|
106
106
|
* @returns **`isInteger`** `boolean`
|
|
107
107
|
*/
|
|
108
|
-
export declare function isInteger(value:
|
|
108
|
+
export declare function isInteger(value: unknown, requireNonNegative?: boolean): value is number;
|
|
109
109
|
/**
|
|
110
|
-
* @param value `
|
|
110
|
+
* @param value `unknown`
|
|
111
111
|
* @param requireNonEmpty `boolean` `default = true`
|
|
112
112
|
* - `if` `true` then `value` must have at least 1 key
|
|
113
113
|
* - `if` `false` then `value` is allowed to be an empty object
|
|
114
114
|
* @param requireNonArray `boolean` `default = true`
|
|
115
115
|
* - `if` `true` then `value` must not be an array
|
|
116
116
|
* - `if` `false` then `value` is allowed to be an array
|
|
117
|
-
* @returns **`isObject`** `boolean` `value is
|
|
117
|
+
* @returns **`isObject`** `boolean` `value is T`
|
|
118
118
|
*/
|
|
119
|
-
export declare function isObject(value:
|
|
120
|
-
export declare function isPositveInteger(value:
|
|
119
|
+
export declare function isObject<T extends object = Record<string, any>>(value: unknown, requireNonEmpty?: boolean, requireNonArray?: boolean): value is T;
|
|
120
|
+
export declare function isPositveInteger(value: unknown): value is number;
|
|
121
121
|
export declare const isType: <T>(value: any, guard: (v: any, ...args: any[]) => v is T, ...args: any[]) => value is T;
|
|
122
122
|
/**
|
|
123
123
|
* - calls {@link isUndefinedOrNull}`(value)` which allows for value to be `undefined` or `null`
|
|
@@ -261,7 +261,12 @@ export type PrimitiveKeys<T, Required extends boolean = false> = {
|
|
|
261
261
|
export type Primitive = string | number | boolean | null | undefined;
|
|
262
262
|
/** Get the union of all values of `T` (like `valueof T`) */
|
|
263
263
|
export type ValueOf<T> = T[keyof T];
|
|
264
|
-
/**
|
|
265
|
-
|
|
266
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Keys of `T` whose values extend a given type `U`
|
|
266
|
+
* @template T - The object type
|
|
267
|
+
* @template U - The type to check each `T[K]` against
|
|
268
|
+
* @template Required - Whether the key is required (allows for `undefined` values if `false`) `default = false`
|
|
269
|
+
*/
|
|
270
|
+
export type KeysOfType<T, U, Required extends boolean = false> = {
|
|
271
|
+
[K in keyof T]: Required extends true ? (T[K] extends U ? K : never) : (T[K] extends U | undefined ? K : never);
|
|
267
272
|
}[keyof T][];
|
|
@@ -8,7 +8,7 @@ exports.isNonEmptyArray = isNonEmptyArray;
|
|
|
8
8
|
exports.isEmptyArray = isEmptyArray;
|
|
9
9
|
exports.isIntegerArray = isIntegerArray;
|
|
10
10
|
exports.isStringArray = isStringArray;
|
|
11
|
-
exports.
|
|
11
|
+
exports.hasNonTrivialEntries = hasNonTrivialEntries;
|
|
12
12
|
exports.hasKeys = hasKeys;
|
|
13
13
|
exports.areEquivalentObjects = areEquivalentObjects;
|
|
14
14
|
exports.isNumeric = isNumeric;
|
|
@@ -48,7 +48,7 @@ function isEmptyArray(value) {
|
|
|
48
48
|
* @param requireNonEmpty `boolean` `default = true`
|
|
49
49
|
* - `if` `true` then `value` must be array with at least 1 element
|
|
50
50
|
* - `if` `false` then `value` can be empty array
|
|
51
|
-
* @returns **`isIntegerArray`** `boolean` = `value is
|
|
51
|
+
* @returns **`isIntegerArray`** `boolean` = `value is number[] & { length: number }`
|
|
52
52
|
*/
|
|
53
53
|
function isIntegerArray(value, requireNonNegative = false, requireNonEmpty = true) {
|
|
54
54
|
return (requireNonEmpty
|
|
@@ -61,23 +61,25 @@ function isIntegerArray(value, requireNonNegative = false, requireNonEmpty = tru
|
|
|
61
61
|
* @param requireNonEmpty `boolean` `default = true`
|
|
62
62
|
* - `if` `true` then `value` must be array with at least 1 element and every element `isNonEmptyString`
|
|
63
63
|
* - `if` `false` then `value` can be empty array
|
|
64
|
-
* @returns **`isStringArray`** `boolean` = `value is
|
|
64
|
+
* @returns **`isStringArray`** `boolean` = `value is string[] & { length: number }`
|
|
65
65
|
*/
|
|
66
66
|
function isStringArray(value, requireNonEmpty = true) {
|
|
67
67
|
return (requireNonEmpty
|
|
68
68
|
? isNonEmptyArray(value) && value.every(el => isNonEmptyString(el))
|
|
69
69
|
: isEmptyArray(value));
|
|
70
70
|
}
|
|
71
|
+
// maybe deprecate this
|
|
71
72
|
/**
|
|
73
|
+
* `fka hasNonTrivialKeys`
|
|
72
74
|
* @note **passing in an array will return `false`.**
|
|
73
75
|
* @note a value is considered trivial if {@link isEmpty}`(value)` returns `true` and vice versa
|
|
74
76
|
* @param obj `any` The object to check.
|
|
75
77
|
* @param requireAll `boolean` - flag indicating whether all values must be nontrivial or not
|
|
76
|
-
* @returns **`
|
|
78
|
+
* @returns **`hasNonTrivialEntries`** `boolean`
|
|
77
79
|
* - **`true`** `if` the `obj` has non-empty keys,
|
|
78
80
|
* - **`false`** `otherwise`
|
|
79
81
|
*/
|
|
80
|
-
function
|
|
82
|
+
function hasNonTrivialEntries(obj, requireAll = false) {
|
|
81
83
|
if (!isObject(obj)) {
|
|
82
84
|
return false;
|
|
83
85
|
}
|
|
@@ -85,8 +87,9 @@ function hasNonTrivialKeys(obj, requireAll = false) {
|
|
|
85
87
|
? Object.values(obj).every(v => !isEmpty(v))
|
|
86
88
|
: Object.values(obj).some(v => !isEmpty(v)));
|
|
87
89
|
}
|
|
90
|
+
// @TODO add overload on param `keys` where keys = `{ required: string[], optional: string[] }`
|
|
91
|
+
// maybe deprecate this
|
|
88
92
|
/**
|
|
89
|
-
* @TODO add overload on param `keys` where keys = `{ required: string[], optional: string[] }`
|
|
90
93
|
* @note uses `key in obj` for each element of param `keys`
|
|
91
94
|
* @param obj `T extends Object` the object to check
|
|
92
95
|
* @param keys `Array<keyof T> | string[] | string` the list of keys that obj must have
|
|
@@ -105,7 +108,7 @@ function hasKeys(obj, keys, requireAll = true, restrictKeys = false) {
|
|
|
105
108
|
return false;
|
|
106
109
|
}
|
|
107
110
|
if (keys === null || keys === undefined) {
|
|
108
|
-
|
|
111
|
+
return false;
|
|
109
112
|
}
|
|
110
113
|
if (!isNonEmptyArray(keys)) {
|
|
111
114
|
keys = [keys]; // Convert string (assumed to be single key) to array of keys
|
|
@@ -168,7 +171,7 @@ function areEquivalentObjects(objA, objB) {
|
|
|
168
171
|
* @param requireNonNegative `boolean` `default = false`
|
|
169
172
|
* @returns **`isNumeric`** `value is string | number`
|
|
170
173
|
* - **`true`** `if` `value` is either a `number` or a `string` that can be casted to a `number`
|
|
171
|
-
* while also meeting the
|
|
174
|
+
* while also meeting the other params
|
|
172
175
|
* - **`false`** `otherwise`
|
|
173
176
|
*/
|
|
174
177
|
function isNumeric(value, requireInteger = false, requireNonNegative = false) {
|
|
@@ -195,7 +198,7 @@ function isNumeric(value, requireInteger = false, requireNonNegative = false) {
|
|
|
195
198
|
return true;
|
|
196
199
|
}
|
|
197
200
|
/**
|
|
198
|
-
* @param value `
|
|
201
|
+
* @param value `unknown`
|
|
199
202
|
* @param requireNonSpace `boolean (optional)` `default` = `false`
|
|
200
203
|
* - `if true` then `value.trim()` must not result in empty string
|
|
201
204
|
* - `if false` then allows for `value` to consist solely of whitespace characters
|
|
@@ -219,7 +222,7 @@ function isPrimitiveValue(value) {
|
|
|
219
222
|
return false;
|
|
220
223
|
}
|
|
221
224
|
/**
|
|
222
|
-
* @param value `
|
|
225
|
+
* @param value `unknown`
|
|
223
226
|
* @param requireNonNegative `boolean`
|
|
224
227
|
* - `if` `true` then require that `value` be an integer `>= 0`
|
|
225
228
|
* - `if` `false` then the sign of the number doesn't matter
|
|
@@ -231,17 +234,17 @@ function isInteger(value, requireNonNegative = false) {
|
|
|
231
234
|
&& (requireNonNegative ? value >= 0 : true));
|
|
232
235
|
}
|
|
233
236
|
/**
|
|
234
|
-
* @param value `
|
|
237
|
+
* @param value `unknown`
|
|
235
238
|
* @param requireNonEmpty `boolean` `default = true`
|
|
236
239
|
* - `if` `true` then `value` must have at least 1 key
|
|
237
240
|
* - `if` `false` then `value` is allowed to be an empty object
|
|
238
241
|
* @param requireNonArray `boolean` `default = true`
|
|
239
242
|
* - `if` `true` then `value` must not be an array
|
|
240
243
|
* - `if` `false` then `value` is allowed to be an array
|
|
241
|
-
* @returns **`isObject`** `boolean` `value is
|
|
244
|
+
* @returns **`isObject`** `boolean` `value is T`
|
|
242
245
|
*/
|
|
243
246
|
function isObject(value, requireNonEmpty = true, requireNonArray = true) {
|
|
244
|
-
return (value && typeof value === 'object'
|
|
247
|
+
return Boolean(value && typeof value === 'object'
|
|
245
248
|
&& (requireNonArray ? !Array.isArray(value) : true)
|
|
246
249
|
&& (requireNonEmpty ? Object.keys(value).length > 0 : true));
|
|
247
250
|
}
|
|
@@ -255,6 +258,7 @@ const isType = (value, guard, ...args) => {
|
|
|
255
258
|
return guard(value, ...args);
|
|
256
259
|
};
|
|
257
260
|
exports.isType = isType;
|
|
261
|
+
// * - rename to isNullable ?
|
|
258
262
|
/**
|
|
259
263
|
* - calls {@link isUndefinedOrNull}`(value)` which allows for value to be `undefined` or `null`
|
|
260
264
|
* - use {@link isUndefinedOr} if you want `value is T | undefined`
|