@pcg/predicates 1.0.0-alpha.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.
- package/CHANGELOG.md +7 -0
- package/dist/index.d.ts +111 -0
- package/dist/index.js +123 -0
- package/dist/index.js.map +1 -0
- package/eslint.config.cjs +14 -0
- package/package.json +22 -0
- package/src/basic.ts +58 -0
- package/src/containers.ts +76 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +9 -0
- package/tsconfig.lib.json +9 -0
- package/tsdown.config.ts +11 -0
- package/vitest.config.ts +19 -0
package/CHANGELOG.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Entries } from "type-fest";
|
|
2
|
+
|
|
3
|
+
//#region src/basic.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Checks if the given parameter is callable (a function).
|
|
7
|
+
* @param {unknown} fn - The function to check.
|
|
8
|
+
* @returns {boolean} Returns true if `fn` is a function, false otherwise.
|
|
9
|
+
* @example
|
|
10
|
+
* isCallable(() => {}); // Returns true
|
|
11
|
+
* isCallable('Hello'); // Returns false
|
|
12
|
+
*/
|
|
13
|
+
declare const isCallable: (fn: unknown) => fn is (...args: any[]) => any;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if the given value is a string.
|
|
16
|
+
* @param {unknown} val - The value to check.
|
|
17
|
+
* @returns {boolean} Returns true if `val` is a string, false otherwise.
|
|
18
|
+
* @example
|
|
19
|
+
* isString('Hello'); // Returns true
|
|
20
|
+
* isString(123); // Returns false
|
|
21
|
+
*/
|
|
22
|
+
declare const isString: (val: unknown) => val is string;
|
|
23
|
+
/**
|
|
24
|
+
* Checks if the given value is a number.
|
|
25
|
+
* @param {unknown} val - The value to check.
|
|
26
|
+
* @returns {boolean} Returns true if `val` is a number, false otherwise.
|
|
27
|
+
* @example
|
|
28
|
+
* isNumber(123); // Returns true
|
|
29
|
+
* isNumber('Hello'); // Returns false
|
|
30
|
+
*/
|
|
31
|
+
declare const isNumber: (val: unknown) => val is number;
|
|
32
|
+
/**
|
|
33
|
+
* Checks if a given value can be used as an index (a non-negative integer).
|
|
34
|
+
* @param {unknown} value - The value to check.
|
|
35
|
+
* @returns {boolean} Returns true if `value` can be used as an index, false otherwise.
|
|
36
|
+
* @example
|
|
37
|
+
* isIndex(2); // Returns true
|
|
38
|
+
* isIndex('2'); // Returns true
|
|
39
|
+
* isIndex(-1); // Returns false
|
|
40
|
+
*/
|
|
41
|
+
declare const isIndex: (value: unknown) => value is number;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the given value is null or undefined.
|
|
44
|
+
* @param {unknown} value - The value to check.
|
|
45
|
+
* @returns {boolean} Returns true if `value` is null or undefined, false otherwise.
|
|
46
|
+
* @example
|
|
47
|
+
* isNullOrUndefined(null); // Returns true
|
|
48
|
+
* isNullOrUndefined(undefined); // Returns true
|
|
49
|
+
* isNullOrUndefined('Hello'); // Returns false
|
|
50
|
+
*/
|
|
51
|
+
declare const isNullOrUndefined: (value: unknown) => value is undefined | null;
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/containers.d.ts
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the given value is an empty array.
|
|
56
|
+
* @param {unknown} arr - The array to check.
|
|
57
|
+
* @returns {boolean} Returns true if `arr` is an empty array, false otherwise.
|
|
58
|
+
* @example
|
|
59
|
+
* isEmptyArray([]); // Returns true
|
|
60
|
+
* isEmptyArray([1, 2, 3]); // Returns false
|
|
61
|
+
*/
|
|
62
|
+
declare const isEmptyArray: (arr: unknown) => boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Gets the keys of the given object.
|
|
65
|
+
* @param {T} arr - The object to get the keys from.
|
|
66
|
+
* @returns {Array<keyof T>} Returns an array of keys of the given object.
|
|
67
|
+
* @example
|
|
68
|
+
* keysOf({ a: 1, b: 2 }); // Returns ['a', 'b']
|
|
69
|
+
*/
|
|
70
|
+
declare const keysOf: <T>(arr: T) => (keyof T)[];
|
|
71
|
+
/**
|
|
72
|
+
* Gets the entries of the given object.
|
|
73
|
+
* @param {T} arr - The object to get the entries from.
|
|
74
|
+
* @returns {Entries<T>} Returns an array of entries of the given object.
|
|
75
|
+
* @example
|
|
76
|
+
* entriesOf({ a: 1, b: 2 }); // Returns [['a', 1], ['b', 2]]
|
|
77
|
+
*/
|
|
78
|
+
declare const entriesOf: <T>(arr: T) => Entries<T>;
|
|
79
|
+
/**
|
|
80
|
+
* Checks if the given value is an object (not an array).
|
|
81
|
+
* @param {unknown} obj - The value to check.
|
|
82
|
+
* @returns {boolean} Returns true if `obj` is an object, false otherwise.
|
|
83
|
+
* @example
|
|
84
|
+
* isObject({ a: 1 }); // Returns true
|
|
85
|
+
* isObject([1, 2, 3]); // Returns false
|
|
86
|
+
*/
|
|
87
|
+
declare const isObject: (obj: unknown) => obj is Record<string, unknown>;
|
|
88
|
+
/**
|
|
89
|
+
* Checks if the given value is a container (an object or an array).
|
|
90
|
+
* @param {unknown} value - The value to check.
|
|
91
|
+
* @returns {boolean} Returns true if `value` is a container, false otherwise.
|
|
92
|
+
* @example
|
|
93
|
+
* isContainerValue({ a: 1 }); // Returns true
|
|
94
|
+
* isContainerValue([1, 2, 3]); // Returns true
|
|
95
|
+
* isContainerValue('Hello'); // Returns false
|
|
96
|
+
*/
|
|
97
|
+
declare const isContainerValue: (value: unknown) => value is Record<string, unknown>;
|
|
98
|
+
/**
|
|
99
|
+
* Checks if the given value is an empty container (an empty object or array).
|
|
100
|
+
* @param {unknown} value - The value to check.
|
|
101
|
+
* @returns {boolean} Returns true if `value` is an empty container, false otherwise.
|
|
102
|
+
* @example
|
|
103
|
+
* isEmptyContainer({}); // Returns true
|
|
104
|
+
* isEmptyContainer([]); // Returns true
|
|
105
|
+
* isEmptyContainer({ a: 1 }); // Returns false
|
|
106
|
+
* isEmptyContainer([1, 2, 3]); // Returns false
|
|
107
|
+
*/
|
|
108
|
+
declare const isEmptyContainer: (value: unknown) => boolean;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { entriesOf, isCallable, isContainerValue, isEmptyArray, isEmptyContainer, isIndex, isNullOrUndefined, isNumber, isObject, isString, keysOf };
|
|
111
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
//#region src/basic.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given parameter is callable (a function).
|
|
4
|
+
* @param {unknown} fn - The function to check.
|
|
5
|
+
* @returns {boolean} Returns true if `fn` is a function, false otherwise.
|
|
6
|
+
* @example
|
|
7
|
+
* isCallable(() => {}); // Returns true
|
|
8
|
+
* isCallable('Hello'); // Returns false
|
|
9
|
+
*/
|
|
10
|
+
const isCallable = (fn) => {
|
|
11
|
+
return typeof fn === "function";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Checks if the given value is a string.
|
|
15
|
+
* @param {unknown} val - The value to check.
|
|
16
|
+
* @returns {boolean} Returns true if `val` is a string, false otherwise.
|
|
17
|
+
* @example
|
|
18
|
+
* isString('Hello'); // Returns true
|
|
19
|
+
* isString(123); // Returns false
|
|
20
|
+
*/
|
|
21
|
+
const isString = (val) => typeof val === "string";
|
|
22
|
+
/**
|
|
23
|
+
* Checks if the given value is a number.
|
|
24
|
+
* @param {unknown} val - The value to check.
|
|
25
|
+
* @returns {boolean} Returns true if `val` is a number, false otherwise.
|
|
26
|
+
* @example
|
|
27
|
+
* isNumber(123); // Returns true
|
|
28
|
+
* isNumber('Hello'); // Returns false
|
|
29
|
+
*/
|
|
30
|
+
const isNumber = (val) => typeof val === "number";
|
|
31
|
+
/**
|
|
32
|
+
* Checks if a given value can be used as an index (a non-negative integer).
|
|
33
|
+
* @param {unknown} value - The value to check.
|
|
34
|
+
* @returns {boolean} Returns true if `value` can be used as an index, false otherwise.
|
|
35
|
+
* @example
|
|
36
|
+
* isIndex(2); // Returns true
|
|
37
|
+
* isIndex('2'); // Returns true
|
|
38
|
+
* isIndex(-1); // Returns false
|
|
39
|
+
*/
|
|
40
|
+
const isIndex = (value) => {
|
|
41
|
+
return (isNumber(value) || isString(value)) && Number(value) >= 0;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the given value is null or undefined.
|
|
45
|
+
* @param {unknown} value - The value to check.
|
|
46
|
+
* @returns {boolean} Returns true if `value` is null or undefined, false otherwise.
|
|
47
|
+
* @example
|
|
48
|
+
* isNullOrUndefined(null); // Returns true
|
|
49
|
+
* isNullOrUndefined(undefined); // Returns true
|
|
50
|
+
* isNullOrUndefined('Hello'); // Returns false
|
|
51
|
+
*/
|
|
52
|
+
const isNullOrUndefined = (value) => {
|
|
53
|
+
return value === null || value === void 0;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/containers.ts
|
|
58
|
+
/**
|
|
59
|
+
* Checks if the given value is an empty array.
|
|
60
|
+
* @param {unknown} arr - The array to check.
|
|
61
|
+
* @returns {boolean} Returns true if `arr` is an empty array, false otherwise.
|
|
62
|
+
* @example
|
|
63
|
+
* isEmptyArray([]); // Returns true
|
|
64
|
+
* isEmptyArray([1, 2, 3]); // Returns false
|
|
65
|
+
*/
|
|
66
|
+
const isEmptyArray = (arr) => {
|
|
67
|
+
return Array.isArray(arr) && arr.length === 0;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Gets the keys of the given object.
|
|
71
|
+
* @param {T} arr - The object to get the keys from.
|
|
72
|
+
* @returns {Array<keyof T>} Returns an array of keys of the given object.
|
|
73
|
+
* @example
|
|
74
|
+
* keysOf({ a: 1, b: 2 }); // Returns ['a', 'b']
|
|
75
|
+
*/
|
|
76
|
+
const keysOf = (arr) => Object.keys(arr);
|
|
77
|
+
/**
|
|
78
|
+
* Gets the entries of the given object.
|
|
79
|
+
* @param {T} arr - The object to get the entries from.
|
|
80
|
+
* @returns {Entries<T>} Returns an array of entries of the given object.
|
|
81
|
+
* @example
|
|
82
|
+
* entriesOf({ a: 1, b: 2 }); // Returns [['a', 1], ['b', 2]]
|
|
83
|
+
*/
|
|
84
|
+
const entriesOf = (arr) => Object.entries(arr);
|
|
85
|
+
/**
|
|
86
|
+
* Checks if the given value is an object (not an array).
|
|
87
|
+
* @param {unknown} obj - The value to check.
|
|
88
|
+
* @returns {boolean} Returns true if `obj` is an object, false otherwise.
|
|
89
|
+
* @example
|
|
90
|
+
* isObject({ a: 1 }); // Returns true
|
|
91
|
+
* isObject([1, 2, 3]); // Returns false
|
|
92
|
+
*/
|
|
93
|
+
const isObject = (obj) => obj !== null && !!obj && typeof obj === "object" && !Array.isArray(obj);
|
|
94
|
+
/**
|
|
95
|
+
* Checks if the given value is a container (an object or an array).
|
|
96
|
+
* @param {unknown} value - The value to check.
|
|
97
|
+
* @returns {boolean} Returns true if `value` is a container, false otherwise.
|
|
98
|
+
* @example
|
|
99
|
+
* isContainerValue({ a: 1 }); // Returns true
|
|
100
|
+
* isContainerValue([1, 2, 3]); // Returns true
|
|
101
|
+
* isContainerValue('Hello'); // Returns false
|
|
102
|
+
*/
|
|
103
|
+
const isContainerValue = (value) => {
|
|
104
|
+
return isObject(value) || Array.isArray(value);
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Checks if the given value is an empty container (an empty object or array).
|
|
108
|
+
* @param {unknown} value - The value to check.
|
|
109
|
+
* @returns {boolean} Returns true if `value` is an empty container, false otherwise.
|
|
110
|
+
* @example
|
|
111
|
+
* isEmptyContainer({}); // Returns true
|
|
112
|
+
* isEmptyContainer([]); // Returns true
|
|
113
|
+
* isEmptyContainer({ a: 1 }); // Returns false
|
|
114
|
+
* isEmptyContainer([1, 2, 3]); // Returns false
|
|
115
|
+
*/
|
|
116
|
+
const isEmptyContainer = (value) => {
|
|
117
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
118
|
+
return isObject(value) && Object.keys(value).length === 0;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
export { entriesOf, isCallable, isContainerValue, isEmptyArray, isEmptyContainer, isIndex, isNullOrUndefined, isNumber, isObject, isString, keysOf };
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/basic.ts","../src/containers.ts"],"sourcesContent":["/**\n * Checks if the given parameter is callable (a function).\n * @param {unknown} fn - The function to check.\n * @returns {boolean} Returns true if `fn` is a function, false otherwise.\n * @example\n * isCallable(() => {}); // Returns true\n * isCallable('Hello'); // Returns false\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const isCallable = (fn: unknown): fn is (...args: any[]) => any => {\n return typeof fn === 'function';\n};\n\n/**\n * Checks if the given value is a string.\n * @param {unknown} val - The value to check.\n * @returns {boolean} Returns true if `val` is a string, false otherwise.\n * @example\n * isString('Hello'); // Returns true\n * isString(123); // Returns false\n */\nexport const isString = (val: unknown): val is string => typeof val === 'string';\n\n/**\n * Checks if the given value is a number.\n * @param {unknown} val - The value to check.\n * @returns {boolean} Returns true if `val` is a number, false otherwise.\n * @example\n * isNumber(123); // Returns true\n * isNumber('Hello'); // Returns false\n */\nexport const isNumber = (val: unknown): val is number => typeof val === 'number';\n\n/**\n * Checks if a given value can be used as an index (a non-negative integer).\n * @param {unknown} value - The value to check.\n * @returns {boolean} Returns true if `value` can be used as an index, false otherwise.\n * @example\n * isIndex(2); // Returns true\n * isIndex('2'); // Returns true\n * isIndex(-1); // Returns false\n */\nexport const isIndex = (value: unknown): value is number => {\n return (isNumber(value) || isString(value)) && Number(value) >= 0;\n};\n\n/**\n * Checks if the given value is null or undefined.\n * @param {unknown} value - The value to check.\n * @returns {boolean} Returns true if `value` is null or undefined, false otherwise.\n * @example\n * isNullOrUndefined(null); // Returns true\n * isNullOrUndefined(undefined); // Returns true\n * isNullOrUndefined('Hello'); // Returns false\n */\nexport const isNullOrUndefined = (value: unknown): value is undefined | null => {\n return value === null || value === undefined;\n};\n","// https://github.com/logaretm/vee-validate/blob/main/packages/shared/utils.ts\n// https://github.com/logaretm/vee-validate/blob/main/packages/vee-validate/src/utils/assertions.ts\n\nimport type { Entries } from 'type-fest';\n\n/**\n * Checks if the given value is an empty array.\n * @param {unknown} arr - The array to check.\n * @returns {boolean} Returns true if `arr` is an empty array, false otherwise.\n * @example\n * isEmptyArray([]); // Returns true\n * isEmptyArray([1, 2, 3]); // Returns false\n */\nexport const isEmptyArray = (arr: unknown): boolean => {\n return Array.isArray(arr) && arr.length === 0;\n};\n\n/**\n * Gets the keys of the given object.\n * @param {T} arr - The object to get the keys from.\n * @returns {Array<keyof T>} Returns an array of keys of the given object.\n * @example\n * keysOf({ a: 1, b: 2 }); // Returns ['a', 'b']\n */\nexport const keysOf = <T>(arr: T) => Object.keys(arr as unknown[]) as (keyof T)[];\n\n/**\n * Gets the entries of the given object.\n * @param {T} arr - The object to get the entries from.\n * @returns {Entries<T>} Returns an array of entries of the given object.\n * @example\n * entriesOf({ a: 1, b: 2 }); // Returns [['a', 1], ['b', 2]]\n */\nexport const entriesOf = <T>(arr: T) => Object.entries(arr as unknown[]) as Entries<T>;\n\n/**\n * Checks if the given value is an object (not an array).\n * @param {unknown} obj - The value to check.\n * @returns {boolean} Returns true if `obj` is an object, false otherwise.\n * @example\n * isObject({ a: 1 }); // Returns true\n * isObject([1, 2, 3]); // Returns false\n */\nexport const isObject = (obj: unknown): obj is Record<string, unknown> =>\n obj !== null && !!obj && typeof obj === 'object' && !Array.isArray(obj);\n\n/**\n * Checks if the given value is a container (an object or an array).\n * @param {unknown} value - The value to check.\n * @returns {boolean} Returns true if `value` is a container, false otherwise.\n * @example\n * isContainerValue({ a: 1 }); // Returns true\n * isContainerValue([1, 2, 3]); // Returns true\n * isContainerValue('Hello'); // Returns false\n */\nexport const isContainerValue = (value: unknown): value is Record<string, unknown> => {\n return isObject(value) || Array.isArray(value);\n};\n\n/**\n * Checks if the given value is an empty container (an empty object or array).\n * @param {unknown} value - The value to check.\n * @returns {boolean} Returns true if `value` is an empty container, false otherwise.\n * @example\n * isEmptyContainer({}); // Returns true\n * isEmptyContainer([]); // Returns true\n * isEmptyContainer({ a: 1 }); // Returns false\n * isEmptyContainer([1, 2, 3]); // Returns false\n */\nexport const isEmptyContainer = (value: unknown): boolean => {\n if (Array.isArray(value)) {\n return value.length === 0;\n }\n\n return isObject(value) && Object.keys(value).length === 0;\n};\n"],"mappings":";;;;;;;;;AASA,MAAa,cAAc,OAA+C;AACxE,QAAO,OAAO,OAAO;;;;;;;;;;AAWvB,MAAa,YAAY,QAAgC,OAAO,QAAQ;;;;;;;;;AAUxE,MAAa,YAAY,QAAgC,OAAO,QAAQ;;;;;;;;;;AAWxE,MAAa,WAAW,UAAoC;AAC1D,SAAQ,SAAS,MAAM,IAAI,SAAS,MAAM,KAAK,OAAO,MAAM,IAAI;;;;;;;;;;;AAYlE,MAAa,qBAAqB,UAA8C;AAC9E,QAAO,UAAU,QAAQ,UAAU;;;;;;;;;;;;;AC3CrC,MAAa,gBAAgB,QAA0B;AACrD,QAAO,MAAM,QAAQ,IAAI,IAAI,IAAI,WAAW;;;;;;;;;AAU9C,MAAa,UAAa,QAAW,OAAO,KAAK,IAAiB;;;;;;;;AASlE,MAAa,aAAgB,QAAW,OAAO,QAAQ,IAAiB;;;;;;;;;AAUxE,MAAa,YAAY,QACvB,QAAQ,QAAQ,CAAC,CAAC,OAAO,OAAO,QAAQ,YAAY,CAAC,MAAM,QAAQ,IAAI;;;;;;;;;;AAWzE,MAAa,oBAAoB,UAAqD;AACpF,QAAO,SAAS,MAAM,IAAI,MAAM,QAAQ,MAAM;;;;;;;;;;;;AAahD,MAAa,oBAAoB,UAA4B;AAC3D,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,WAAW;AAG1B,QAAO,SAAS,MAAM,IAAI,OAAO,KAAK,MAAM,CAAC,WAAW"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//
|
|
2
|
+
// @pkg 📦 DeepVision ESLint Config [NestJS]
|
|
3
|
+
// @version 📍 1.0.0
|
|
4
|
+
// @author 🐳 DeepVision Team <code@deepvision.team>
|
|
5
|
+
//
|
|
6
|
+
// Documentation reference: https://eslint.org/docs/user-guide/configuring/
|
|
7
|
+
// ESLint versions: https://eslint.org/blog/
|
|
8
|
+
//
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports, node/no-unpublished-require
|
|
10
|
+
const deep = require('@deepvision/eslint-plugin');
|
|
11
|
+
|
|
12
|
+
module.exports = [
|
|
13
|
+
...deep.default.configs.node,
|
|
14
|
+
];
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pcg/predicates",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "Type predicate utilities and assertions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"type-fest": "^4.41.0"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@vitest/ui": "^3.2.4",
|
|
13
|
+
"vitest": "^3.2.4"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "tsdown --watch",
|
|
17
|
+
"build": "tsdown",
|
|
18
|
+
"test": "vitest run",
|
|
19
|
+
"test:watch": "vitest",
|
|
20
|
+
"lint": "eslint \"src/**/*.ts\""
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/basic.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given parameter is callable (a function).
|
|
3
|
+
* @param {unknown} fn - The function to check.
|
|
4
|
+
* @returns {boolean} Returns true if `fn` is a function, false otherwise.
|
|
5
|
+
* @example
|
|
6
|
+
* isCallable(() => {}); // Returns true
|
|
7
|
+
* isCallable('Hello'); // Returns false
|
|
8
|
+
*/
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
export const isCallable = (fn: unknown): fn is (...args: any[]) => any => {
|
|
11
|
+
return typeof fn === 'function';
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks if the given value is a string.
|
|
16
|
+
* @param {unknown} val - The value to check.
|
|
17
|
+
* @returns {boolean} Returns true if `val` is a string, false otherwise.
|
|
18
|
+
* @example
|
|
19
|
+
* isString('Hello'); // Returns true
|
|
20
|
+
* isString(123); // Returns false
|
|
21
|
+
*/
|
|
22
|
+
export const isString = (val: unknown): val is string => typeof val === 'string';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Checks if the given value is a number.
|
|
26
|
+
* @param {unknown} val - The value to check.
|
|
27
|
+
* @returns {boolean} Returns true if `val` is a number, false otherwise.
|
|
28
|
+
* @example
|
|
29
|
+
* isNumber(123); // Returns true
|
|
30
|
+
* isNumber('Hello'); // Returns false
|
|
31
|
+
*/
|
|
32
|
+
export const isNumber = (val: unknown): val is number => typeof val === 'number';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if a given value can be used as an index (a non-negative integer).
|
|
36
|
+
* @param {unknown} value - The value to check.
|
|
37
|
+
* @returns {boolean} Returns true if `value` can be used as an index, false otherwise.
|
|
38
|
+
* @example
|
|
39
|
+
* isIndex(2); // Returns true
|
|
40
|
+
* isIndex('2'); // Returns true
|
|
41
|
+
* isIndex(-1); // Returns false
|
|
42
|
+
*/
|
|
43
|
+
export const isIndex = (value: unknown): value is number => {
|
|
44
|
+
return (isNumber(value) || isString(value)) && Number(value) >= 0;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks if the given value is null or undefined.
|
|
49
|
+
* @param {unknown} value - The value to check.
|
|
50
|
+
* @returns {boolean} Returns true if `value` is null or undefined, false otherwise.
|
|
51
|
+
* @example
|
|
52
|
+
* isNullOrUndefined(null); // Returns true
|
|
53
|
+
* isNullOrUndefined(undefined); // Returns true
|
|
54
|
+
* isNullOrUndefined('Hello'); // Returns false
|
|
55
|
+
*/
|
|
56
|
+
export const isNullOrUndefined = (value: unknown): value is undefined | null => {
|
|
57
|
+
return value === null || value === undefined;
|
|
58
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// https://github.com/logaretm/vee-validate/blob/main/packages/shared/utils.ts
|
|
2
|
+
// https://github.com/logaretm/vee-validate/blob/main/packages/vee-validate/src/utils/assertions.ts
|
|
3
|
+
|
|
4
|
+
import type { Entries } from 'type-fest';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Checks if the given value is an empty array.
|
|
8
|
+
* @param {unknown} arr - The array to check.
|
|
9
|
+
* @returns {boolean} Returns true if `arr` is an empty array, false otherwise.
|
|
10
|
+
* @example
|
|
11
|
+
* isEmptyArray([]); // Returns true
|
|
12
|
+
* isEmptyArray([1, 2, 3]); // Returns false
|
|
13
|
+
*/
|
|
14
|
+
export const isEmptyArray = (arr: unknown): boolean => {
|
|
15
|
+
return Array.isArray(arr) && arr.length === 0;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Gets the keys of the given object.
|
|
20
|
+
* @param {T} arr - The object to get the keys from.
|
|
21
|
+
* @returns {Array<keyof T>} Returns an array of keys of the given object.
|
|
22
|
+
* @example
|
|
23
|
+
* keysOf({ a: 1, b: 2 }); // Returns ['a', 'b']
|
|
24
|
+
*/
|
|
25
|
+
export const keysOf = <T>(arr: T) => Object.keys(arr as unknown[]) as (keyof T)[];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets the entries of the given object.
|
|
29
|
+
* @param {T} arr - The object to get the entries from.
|
|
30
|
+
* @returns {Entries<T>} Returns an array of entries of the given object.
|
|
31
|
+
* @example
|
|
32
|
+
* entriesOf({ a: 1, b: 2 }); // Returns [['a', 1], ['b', 2]]
|
|
33
|
+
*/
|
|
34
|
+
export const entriesOf = <T>(arr: T) => Object.entries(arr as unknown[]) as Entries<T>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checks if the given value is an object (not an array).
|
|
38
|
+
* @param {unknown} obj - The value to check.
|
|
39
|
+
* @returns {boolean} Returns true if `obj` is an object, false otherwise.
|
|
40
|
+
* @example
|
|
41
|
+
* isObject({ a: 1 }); // Returns true
|
|
42
|
+
* isObject([1, 2, 3]); // Returns false
|
|
43
|
+
*/
|
|
44
|
+
export const isObject = (obj: unknown): obj is Record<string, unknown> =>
|
|
45
|
+
obj !== null && !!obj && typeof obj === 'object' && !Array.isArray(obj);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks if the given value is a container (an object or an array).
|
|
49
|
+
* @param {unknown} value - The value to check.
|
|
50
|
+
* @returns {boolean} Returns true if `value` is a container, false otherwise.
|
|
51
|
+
* @example
|
|
52
|
+
* isContainerValue({ a: 1 }); // Returns true
|
|
53
|
+
* isContainerValue([1, 2, 3]); // Returns true
|
|
54
|
+
* isContainerValue('Hello'); // Returns false
|
|
55
|
+
*/
|
|
56
|
+
export const isContainerValue = (value: unknown): value is Record<string, unknown> => {
|
|
57
|
+
return isObject(value) || Array.isArray(value);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Checks if the given value is an empty container (an empty object or array).
|
|
62
|
+
* @param {unknown} value - The value to check.
|
|
63
|
+
* @returns {boolean} Returns true if `value` is an empty container, false otherwise.
|
|
64
|
+
* @example
|
|
65
|
+
* isEmptyContainer({}); // Returns true
|
|
66
|
+
* isEmptyContainer([]); // Returns true
|
|
67
|
+
* isEmptyContainer({ a: 1 }); // Returns false
|
|
68
|
+
* isEmptyContainer([1, 2, 3]); // Returns false
|
|
69
|
+
*/
|
|
70
|
+
export const isEmptyContainer = (value: unknown): boolean => {
|
|
71
|
+
if (Array.isArray(value)) {
|
|
72
|
+
return value.length === 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return isObject(value) && Object.keys(value).length === 0;
|
|
76
|
+
};
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
package/tsdown.config.ts
ADDED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// / <reference types="vitest" />
|
|
2
|
+
// eslint-disable-next-line node/no-unpublished-import
|
|
3
|
+
import { defineConfig } from 'vitest/config';
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
test: {
|
|
7
|
+
environment: 'node',
|
|
8
|
+
include: ['tests/**/*.test.ts'],
|
|
9
|
+
globals: true,
|
|
10
|
+
typecheck: {
|
|
11
|
+
tsconfig: './tsconfig.json',
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
resolve: {
|
|
15
|
+
alias: {
|
|
16
|
+
'@': new URL('./src', import.meta.url).pathname,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|