@omnific/utils 0.0.2 → 0.0.4
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/dist/@types/GetFieldType.d.ts +13 -0
- package/dist/@types/Many.d.ts +4 -0
- package/dist/@types/PropertyPath.d.ts +6 -0
- package/dist/_internal/isDeepKey.js +25 -0
- package/dist/_internal/isUnsafeProperty.js +16 -0
- package/dist/_internal/toKey.js +19 -0
- package/dist/createChainedFunction.d.ts +19 -0
- package/dist/createChainedFunction.js +23 -0
- package/dist/get.d.ts +328 -0
- package/dist/get.js +58 -0
- package/dist/hasValue.d.ts +20 -0
- package/dist/hasValue.js +23 -0
- package/dist/isArray.d.ts +24 -0
- package/dist/isArray.js +26 -0
- package/dist/isBoolean.d.ts +26 -0
- package/dist/isBoolean.js +28 -0
- package/dist/isFunction.d.ts +17 -0
- package/dist/isFunction.js +19 -0
- package/dist/isNil.d.ts +23 -0
- package/dist/isNil.js +27 -0
- package/dist/isNull.d.ts +24 -0
- package/dist/isNull.js +26 -0
- package/dist/isNumber.d.ts +26 -0
- package/dist/isNumber.js +28 -0
- package/dist/isObject.d.ts +27 -0
- package/dist/isObject.js +29 -0
- package/dist/isPromise.d.ts +21 -0
- package/dist/isPromise.js +25 -0
- package/dist/isString.d.ts +21 -0
- package/dist/isString.js +23 -0
- package/dist/isSymbol.d.ts +27 -0
- package/dist/isSymbol.js +29 -0
- package/dist/isUndefined.d.ts +24 -0
- package/dist/isUndefined.js +26 -0
- package/dist/main.d.ts +16 -155
- package/dist/main.js +16 -194
- package/dist/resolveValue.d.ts +17 -0
- package/dist/resolveValue.js +21 -0
- package/dist/utils/toPath.js +67 -0
- package/dist/utils/toString.js +28 -0
- package/package.json +8 -3
package/dist/main.js
CHANGED
|
@@ -1,194 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
* console.log(isArray(value1)); // true
|
|
19
|
-
* console.log(isArray(value2)); // false
|
|
20
|
-
* console.log(isArray(value3)); // false
|
|
21
|
-
*/
|
|
22
|
-
function isArray(value) {
|
|
23
|
-
return Array.isArray(value);
|
|
24
|
-
}
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region isBoolean.ts
|
|
27
|
-
/**
|
|
28
|
-
* Checks if the given value is boolean.
|
|
29
|
-
*
|
|
30
|
-
* This function tests whether the provided value is strictly `boolean`.
|
|
31
|
-
* It returns `true` if the value is `boolean`, and `false` otherwise.
|
|
32
|
-
*
|
|
33
|
-
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `boolean`.
|
|
34
|
-
*
|
|
35
|
-
* @param {unknown} x - The Value to test if it is boolean.
|
|
36
|
-
* @returns {x is boolean} True if the value is boolean, false otherwise.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
*
|
|
40
|
-
* const value1 = true;
|
|
41
|
-
* const value2 = 0;
|
|
42
|
-
* const value3 = 'abc';
|
|
43
|
-
*
|
|
44
|
-
* console.log(isBoolean(value1)); // true
|
|
45
|
-
* console.log(isBoolean(value2)); // false
|
|
46
|
-
* console.log(isBoolean(value3)); // false
|
|
47
|
-
*
|
|
48
|
-
*/
|
|
49
|
-
function isBoolean(x) {
|
|
50
|
-
return typeof x === "boolean";
|
|
51
|
-
}
|
|
52
|
-
//#endregion
|
|
53
|
-
//#region isFunction.ts
|
|
54
|
-
/**
|
|
55
|
-
* Checks if `value` is a function.
|
|
56
|
-
*
|
|
57
|
-
* @param {any} value The value to check.
|
|
58
|
-
* @returns {boolean} Returns `true` if `value` is a function, else `false`.
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* isFunction(Array.prototype.slice); // true
|
|
62
|
-
* isFunction(async function () {}); // true
|
|
63
|
-
* isFunction(function* () {}); // true
|
|
64
|
-
* isFunction(Proxy); // true
|
|
65
|
-
* isFunction(Int8Array); // true
|
|
66
|
-
*/
|
|
67
|
-
function isFunction(value) {
|
|
68
|
-
return typeof value === "function";
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region isUndefined.ts
|
|
72
|
-
/**
|
|
73
|
-
* Checks if the given value is undefined.
|
|
74
|
-
*
|
|
75
|
-
* This function tests whether the provided value is strictly equal to `undefined`.
|
|
76
|
-
* It returns `true` if the value is `undefined`, and `false` otherwise.
|
|
77
|
-
*
|
|
78
|
-
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `undefined`.
|
|
79
|
-
*
|
|
80
|
-
* @param {unknown} value - The value to test if it is undefined.
|
|
81
|
-
* @returns {value is undefined} True if the value is undefined, false otherwise.
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
* const value1 = undefined;
|
|
85
|
-
* const value2 = null;
|
|
86
|
-
* const value3 = 42;
|
|
87
|
-
*
|
|
88
|
-
* console.log(isUndefined(value1)); // true
|
|
89
|
-
* console.log(isUndefined(value2)); // false
|
|
90
|
-
* console.log(isUndefined(value3)); // false
|
|
91
|
-
*/
|
|
92
|
-
const isUndefined = (value) => {
|
|
93
|
-
return value === void 0;
|
|
94
|
-
};
|
|
95
|
-
//#endregion
|
|
96
|
-
//#region isNull.ts
|
|
97
|
-
/**
|
|
98
|
-
* Checks if the given value is null.
|
|
99
|
-
*
|
|
100
|
-
* This function tests whether the provided value is strictly equal to `null`.
|
|
101
|
-
* It returns `true` if the value is `null`, and `false` otherwise.
|
|
102
|
-
*
|
|
103
|
-
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null`.
|
|
104
|
-
*
|
|
105
|
-
* @param {unknown} x - The value to test if it is null.
|
|
106
|
-
* @returns {x is null} True if the value is null, false otherwise.
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* const value1 = null;
|
|
110
|
-
* const value2 = undefined;
|
|
111
|
-
* const value3 = 42;
|
|
112
|
-
*
|
|
113
|
-
* console.log(isNull(value1)); // true
|
|
114
|
-
* console.log(isNull(value2)); // false
|
|
115
|
-
* console.log(isNull(value3)); // false
|
|
116
|
-
*/
|
|
117
|
-
function isNull(x) {
|
|
118
|
-
return x === null;
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
|
-
//#region isNil.ts
|
|
122
|
-
/**
|
|
123
|
-
* Checks if a given value is null or undefined.
|
|
124
|
-
*
|
|
125
|
-
* This function tests whether the provided value is either `null` or `undefined`.
|
|
126
|
-
* It returns `true` if the value is `null` or `undefined`, and `false` otherwise.
|
|
127
|
-
*
|
|
128
|
-
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null` or `undefined`.
|
|
129
|
-
*
|
|
130
|
-
* @param {unknown} x - The value to test for null or undefined.
|
|
131
|
-
* @returns {boolean} `true` if the value is null or undefined, `false` otherwise.
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* const value1 = null;
|
|
135
|
-
* const value2 = undefined;
|
|
136
|
-
* const value3 = 42;
|
|
137
|
-
* const result1 = isNil(value1); // true
|
|
138
|
-
* const result2 = isNil(value2); // true
|
|
139
|
-
* const result3 = isNil(value3); // false
|
|
140
|
-
*/
|
|
141
|
-
function isNil(x) {
|
|
142
|
-
return isNull(x) || isUndefined(x);
|
|
143
|
-
}
|
|
144
|
-
//#endregion
|
|
145
|
-
//#region isNumber.ts
|
|
146
|
-
/**
|
|
147
|
-
* Checks if the given value is a number.
|
|
148
|
-
*
|
|
149
|
-
* This function tests whether the provided value is strictly a `number`.
|
|
150
|
-
* It returns `true` if the value is a `number`, and `false` otherwise.
|
|
151
|
-
*
|
|
152
|
-
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `number`.
|
|
153
|
-
*
|
|
154
|
-
* @param {unknown} x - The value to test if it is a number.
|
|
155
|
-
* @returns {x is number} True if the value is a number, false otherwise.
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
*
|
|
159
|
-
* const value1 = 123;
|
|
160
|
-
* const value2 = 'abc';
|
|
161
|
-
* const value3 = true;
|
|
162
|
-
*
|
|
163
|
-
* console.log(isNumber(value1)); // true
|
|
164
|
-
* console.log(isNumber(value2)); // false
|
|
165
|
-
* console.log(isNumber(value3)); // false
|
|
166
|
-
*
|
|
167
|
-
*/
|
|
168
|
-
function isNumber(value) {
|
|
169
|
-
return typeof value === "number" && !Number.isNaN(value);
|
|
170
|
-
}
|
|
171
|
-
//#endregion
|
|
172
|
-
//#region isString.ts
|
|
173
|
-
/**
|
|
174
|
-
* Checks if a given value is string.
|
|
175
|
-
*
|
|
176
|
-
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `string`.
|
|
177
|
-
*
|
|
178
|
-
* @param {unknown} value The value to check if it is string.
|
|
179
|
-
* @returns {value is string} Returns `true` if `value` is a string, else `false`.
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* const value1 = 'abc';
|
|
183
|
-
* const value2 = 123;
|
|
184
|
-
* const value3 = true;
|
|
185
|
-
*
|
|
186
|
-
* console.log(isString(value1)); // true
|
|
187
|
-
* console.log(isString(value2)); // false
|
|
188
|
-
* console.log(isString(value3)); // false
|
|
189
|
-
*/
|
|
190
|
-
function isString(value) {
|
|
191
|
-
return typeof value === "string";
|
|
192
|
-
}
|
|
193
|
-
//#endregion
|
|
194
|
-
export { isArray, isBoolean, isFunction, isNil, isNumber, isString, isUndefined };
|
|
1
|
+
import { isArray } from "./isArray.js";
|
|
2
|
+
import { isBoolean } from "./isBoolean.js";
|
|
3
|
+
import { isFunction } from "./isFunction.js";
|
|
4
|
+
import { createChainedFunction } from "./createChainedFunction.js";
|
|
5
|
+
import { isUndefined } from "./isUndefined.js";
|
|
6
|
+
import { isNull } from "./isNull.js";
|
|
7
|
+
import { isNil } from "./isNil.js";
|
|
8
|
+
import hasValue from "./hasValue.js";
|
|
9
|
+
import { isNumber } from "./isNumber.js";
|
|
10
|
+
import { isString } from "./isString.js";
|
|
11
|
+
import { isObject } from "./isObject.js";
|
|
12
|
+
import { isPromise } from "./isPromise.js";
|
|
13
|
+
import { isSymbol } from "./isSymbol.js";
|
|
14
|
+
import { get } from "./get.js";
|
|
15
|
+
import resolveValue from "./resolveValue.js";
|
|
16
|
+
export { createChainedFunction, get, hasValue, isArray, isBoolean, isFunction, isNil, isNull, isNumber, isObject, isPromise, isString, isSymbol, isUndefined, resolveValue };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region resolveValue.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Resolves either a plain value or a lazily evaluated value factory.
|
|
4
|
+
*
|
|
5
|
+
* @template TValue
|
|
6
|
+
* @template TArgs
|
|
7
|
+
* @param {TValue | ((...args: TArgs) => TValue)} value - The value or resolver.
|
|
8
|
+
* @param {...TArgs} args - Arguments passed to the resolver when needed.
|
|
9
|
+
* @returns {TValue} The resolved value.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* resolveValue('ready');
|
|
13
|
+
* resolveValue(status => status === 'error' ? 'retry' : 'ok', 'error');
|
|
14
|
+
*/
|
|
15
|
+
declare function resolveValue<TValue, TArgs extends any[]>(value: TValue | ((...args: TArgs) => TValue), ...args: TArgs): TValue;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { resolveValue as default, resolveValue };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isFunction } from "./isFunction.js";
|
|
2
|
+
//#region resolveValue.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolves either a plain value or a lazily evaluated value factory.
|
|
5
|
+
*
|
|
6
|
+
* @template TValue
|
|
7
|
+
* @template TArgs
|
|
8
|
+
* @param {TValue | ((...args: TArgs) => TValue)} value - The value or resolver.
|
|
9
|
+
* @param {...TArgs} args - Arguments passed to the resolver when needed.
|
|
10
|
+
* @returns {TValue} The resolved value.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* resolveValue('ready');
|
|
14
|
+
* resolveValue(status => status === 'error' ? 'retry' : 'ok', 'error');
|
|
15
|
+
*/
|
|
16
|
+
function resolveValue(value, ...args) {
|
|
17
|
+
if (isFunction(value)) return value(...args);
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { resolveValue as default, resolveValue };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { toKey } from "../_internal/toKey.js";
|
|
2
|
+
import { toString } from "./toString.js";
|
|
3
|
+
//#region utils/toPath.ts
|
|
4
|
+
/**
|
|
5
|
+
* Converts a deep key string into an array of path segments.
|
|
6
|
+
*
|
|
7
|
+
* This function takes a string representing a deep key (e.g., 'a.b.c' or 'a[b][c]') and breaks it down into an array of strings, each representing a segment of the path.
|
|
8
|
+
*
|
|
9
|
+
* @param {any} deepKey - The deep key string to convert.
|
|
10
|
+
* @returns {string[]} An array of strings, each representing a segment of the path.
|
|
11
|
+
*
|
|
12
|
+
* Examples:
|
|
13
|
+
*
|
|
14
|
+
* toPath('a.b.c') // Returns ['a', 'b', 'c']
|
|
15
|
+
* toPath('a[b][c]') // Returns ['a', 'b', 'c']
|
|
16
|
+
* toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']
|
|
17
|
+
* toPath('a["b.c"].d') // Returns ['a', 'b.c', 'd']
|
|
18
|
+
* toPath('') // Returns []
|
|
19
|
+
* toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
|
|
20
|
+
*/
|
|
21
|
+
function toPath(deepKey) {
|
|
22
|
+
if (Array.isArray(deepKey)) return deepKey.map((item) => toKey(item));
|
|
23
|
+
if (typeof deepKey === "symbol") return [deepKey];
|
|
24
|
+
deepKey = toString(deepKey);
|
|
25
|
+
const result = [];
|
|
26
|
+
const length = deepKey.length;
|
|
27
|
+
if (length === 0) return result;
|
|
28
|
+
let index = 0;
|
|
29
|
+
let key = "";
|
|
30
|
+
let quoteChar = "";
|
|
31
|
+
let bracket = false;
|
|
32
|
+
if (deepKey.codePointAt(0) === 46) {
|
|
33
|
+
result.push("");
|
|
34
|
+
index++;
|
|
35
|
+
}
|
|
36
|
+
while (index < length) {
|
|
37
|
+
const char = deepKey[index];
|
|
38
|
+
if (quoteChar) if (char === "\\" && index + 1 < length) {
|
|
39
|
+
index++;
|
|
40
|
+
key += deepKey[index];
|
|
41
|
+
} else if (char === quoteChar) quoteChar = "";
|
|
42
|
+
else key += char;
|
|
43
|
+
else if (bracket) if (char === "\"" || char === "'") quoteChar = char;
|
|
44
|
+
else if (char === "]") {
|
|
45
|
+
bracket = false;
|
|
46
|
+
result.push(key);
|
|
47
|
+
key = "";
|
|
48
|
+
} else key += char;
|
|
49
|
+
else if (char === "[") {
|
|
50
|
+
bracket = true;
|
|
51
|
+
if (key) {
|
|
52
|
+
result.push(key);
|
|
53
|
+
key = "";
|
|
54
|
+
}
|
|
55
|
+
} else if (char === ".") {
|
|
56
|
+
if (key) {
|
|
57
|
+
result.push(key);
|
|
58
|
+
key = "";
|
|
59
|
+
}
|
|
60
|
+
} else key += char;
|
|
61
|
+
index++;
|
|
62
|
+
}
|
|
63
|
+
if (key) result.push(key);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { toPath };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isNil } from "../isNil.js";
|
|
2
|
+
import { isString } from "../isString.js";
|
|
3
|
+
import { normalizeKey } from "../_internal/toKey.js";
|
|
4
|
+
//#region utils/toString.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts `value` to a string.
|
|
7
|
+
*
|
|
8
|
+
* An empty string is returned for `null` and `undefined` values.
|
|
9
|
+
* The sign of `-0` is preserved.
|
|
10
|
+
*
|
|
11
|
+
* @param {any} value - The value to convert.
|
|
12
|
+
* @returns {string} Returns the converted string.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* toString(null) // returns ''
|
|
16
|
+
* toString(undefined) // returns ''
|
|
17
|
+
* toString(-0) // returns '-0'
|
|
18
|
+
* toString([1, 2, -0]) // returns '1,2,-0'
|
|
19
|
+
* toString([Symbol('a'), Symbol('b')]) // returns 'Symbol(a),Symbol(b)'
|
|
20
|
+
*/
|
|
21
|
+
function toString(value) {
|
|
22
|
+
if (isNil(value)) return "";
|
|
23
|
+
if (isString(value)) return value;
|
|
24
|
+
if (Array.isArray(value)) return value.map((item) => toString(item)).join(",");
|
|
25
|
+
return normalizeKey(value);
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { toString };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omnific/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Shared utility predicates for Omnific packages",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -24,8 +24,13 @@
|
|
|
24
24
|
"exports": {
|
|
25
25
|
".": {
|
|
26
26
|
"types": "./dist/main.d.ts",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"import": "./dist/main.js",
|
|
28
|
+
"default": "./dist/main.js"
|
|
29
|
+
},
|
|
30
|
+
"./*": {
|
|
31
|
+
"types": "./dist/*.d.ts",
|
|
32
|
+
"import": "./dist/*.js",
|
|
33
|
+
"default": "./dist/*.js"
|
|
29
34
|
}
|
|
30
35
|
},
|
|
31
36
|
"sideEffects": false,
|