@omnific/utils 0.0.2 → 0.0.3

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/main.js CHANGED
@@ -1,194 +1,13 @@
1
- //#region isArray.ts
2
- /**
3
- * Checks if the given value is an array.
4
- *
5
- * This function tests whether the provided value is strictly an array.
6
- * It returns `true` if the value is an array, and `false` otherwise.
7
- *
8
- * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to an array.
9
- *
10
- * @param {unknown} value - The value to test if it is an array.
11
- * @returns {value is T[]} True if the value is an array, false otherwise.
12
- *
13
- * @example
14
- * const value1 = [];
15
- * const value2 = {};
16
- * const value3 = 'abc';
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 { isUndefined } from "./isUndefined.js";
5
+ import { isNull } from "./isNull.js";
6
+ import { isNil } from "./isNil.js";
7
+ import { isNumber } from "./isNumber.js";
8
+ import { isString } from "./isString.js";
9
+ import { isObject } from "./isObject.js";
10
+ import { isPromise } from "./isPromise.js";
11
+ import { isSymbol } from "./isSymbol.js";
12
+ import { get } from "./get.js";
13
+ export { get, isArray, isBoolean, isFunction, isNil, isNull, isNumber, isObject, isPromise, isString, isSymbol, isUndefined };
@@ -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.2",
3
+ "version": "0.0.3",
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
- "default": "./dist/main.js",
28
- "import": "./dist/main.js"
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,