@aiszlab/relax 2.0.4 → 2.0.5

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.
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
3
  var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
5
4
  var isString = require('../is/is-string.cjs');
6
5
 
@@ -11,13 +10,12 @@ function normalize() {
11
10
  }
12
11
  for (var _i = 0, _classNames2 = classNames; _i < _classNames2.length; _i++) {
13
12
  var _classNames = _classNames2[_i];
14
- if (!isString.isString(_classNames)) return;
13
+ if (!isString.isString(_classNames)) continue;
15
14
  var _iterator = _createForOfIteratorHelper(_classNames.matchAll(/\S+/g)),
16
15
  _step;
17
16
  try {
18
17
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
19
- var _step$value = _slicedToArray(_step.value, 1),
20
- className = _step$value[0];
18
+ var className = _step.value[0];
21
19
  values.add(className);
22
20
  }
23
21
  } catch (err) {
@@ -1,2 +1,3 @@
1
- declare function normalize(...classNames: string[]): string[] | undefined;
1
+ import type { Partialable } from "../types";
2
+ declare function normalize(...classNames: Partialable<string>[]): string[];
2
3
  export { normalize };
@@ -1,4 +1,3 @@
1
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
2
1
  import _createForOfIteratorHelper from '@babel/runtime/helpers/createForOfIteratorHelper';
3
2
  import { isString } from '../is/is-string.mjs';
4
3
 
@@ -9,13 +8,12 @@ function normalize() {
9
8
  }
10
9
  for (var _i = 0, _classNames2 = classNames; _i < _classNames2.length; _i++) {
11
10
  var _classNames = _classNames2[_i];
12
- if (!isString(_classNames)) return;
11
+ if (!isString(_classNames)) continue;
13
12
  var _iterator = _createForOfIteratorHelper(_classNames.matchAll(/\S+/g)),
14
13
  _step;
15
14
  try {
16
15
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
17
- var _step$value = _slicedToArray(_step.value, 1),
18
- className = _step$value[0];
16
+ var className = _step.value[0];
19
17
  values.add(className);
20
18
  }
21
19
  } catch (err) {
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ /**
6
+ * in react, ref always need a initial value
7
+ * but can not be a function
8
+ * so we create a lazy ref to get a value getter
9
+ */
10
+ function useLazyRef(getter) {
11
+ var ref = react.useRef(null);
12
+ return function () {
13
+ var _ref$current;
14
+ return (_ref$current = ref.current) !== null && _ref$current !== void 0 ? _ref$current : ref.current = getter();
15
+ };
16
+ }
17
+
18
+ exports.useLazyRef = useLazyRef;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * in react, ref always need a initial value
3
+ * but can not be a function
4
+ * so we create a lazy ref to get a value getter
5
+ */
6
+ declare function useLazyRef<T>(getter: () => T): () => T;
7
+ export { useLazyRef };
@@ -0,0 +1,16 @@
1
+ import { useRef } from 'react';
2
+
3
+ /**
4
+ * in react, ref always need a initial value
5
+ * but can not be a function
6
+ * so we create a lazy ref to get a value getter
7
+ */
8
+ function useLazyRef(getter) {
9
+ var ref = useRef(null);
10
+ return function () {
11
+ var _ref$current;
12
+ return (_ref$current = ref.current) !== null && _ref$current !== void 0 ? _ref$current : ref.current = getter();
13
+ };
14
+ }
15
+
16
+ export { useLazyRef };
package/dist/index.cjs CHANGED
@@ -40,6 +40,7 @@ var useEventSource = require('./hooks/use-event-source.cjs');
40
40
  var useResize = require('./hooks/use-resize.cjs');
41
41
  var useDraggable = require('./hooks/use-draggable.cjs');
42
42
  var useAsyncEffect = require('./hooks/use-async-effect.cjs');
43
+ var useLazyRef = require('./hooks/use-lazy-ref.cjs');
43
44
  var isRefable = require('./is/is-refable.cjs');
44
45
  var isUndefined = require('./is/is-undefined.cjs');
45
46
  var isNull = require('./is/is-null.cjs');
@@ -78,6 +79,9 @@ var max = require('./utils/max.cjs');
78
79
  var min = require('./utils/min.cjs');
79
80
  var load = require('./utils/load.cjs');
80
81
  var at = require('./utils/at.cjs');
82
+ var pick = require('./utils/pick.cjs');
83
+ var get = require('./utils/get.cjs');
84
+ var set = require('./utils/set.cjs');
81
85
  var replace = require('./utils/replace/replace.cjs');
82
86
  var replaceAt = require('./utils/replace/replace-at.cjs');
83
87
 
@@ -123,6 +127,7 @@ exports.useEventSource = useEventSource.useEventSource;
123
127
  exports.useResize = useResize.useResize;
124
128
  exports.useDraggable = useDraggable.useDraggable;
125
129
  exports.useAsyncEffect = useAsyncEffect.useAsyncEffect;
130
+ exports.useLazyRef = useLazyRef.useLazyRef;
126
131
  exports.isRefable = isRefable.isRefable;
127
132
  exports.isUndefined = isUndefined.isUndefined;
128
133
  exports.isNull = isNull.isNull;
@@ -161,5 +166,8 @@ exports.max = max.max;
161
166
  exports.min = min.min;
162
167
  exports.load = load.load;
163
168
  exports.at = at.at;
169
+ exports.pick = pick.pick;
170
+ exports.get = get.get;
171
+ exports.set = set.set;
164
172
  exports.replace = replace.replace;
165
173
  exports.replaceAt = replaceAt.replaceAt;
package/dist/index.d.ts CHANGED
@@ -42,6 +42,7 @@ export { useEventSource } from "./hooks/use-event-source";
42
42
  export { useResize } from "./hooks/use-resize";
43
43
  export { useDraggable } from "./hooks/use-draggable";
44
44
  export { useAsyncEffect } from "./hooks/use-async-effect";
45
+ export { useLazyRef } from "./hooks/use-lazy-ref";
45
46
  /**
46
47
  * @description
47
48
  * is
@@ -89,3 +90,6 @@ export { max } from "./utils/max";
89
90
  export { min } from "./utils/min";
90
91
  export { load } from "./utils/load";
91
92
  export { at } from "./utils/at";
93
+ export { pick } from "./utils/pick";
94
+ export { get } from "./utils/get";
95
+ export { set } from "./utils/set";
package/dist/index.mjs CHANGED
@@ -38,6 +38,7 @@ export { useEventSource } from './hooks/use-event-source.mjs';
38
38
  export { useResize } from './hooks/use-resize.mjs';
39
39
  export { useDraggable } from './hooks/use-draggable.mjs';
40
40
  export { useAsyncEffect } from './hooks/use-async-effect.mjs';
41
+ export { useLazyRef } from './hooks/use-lazy-ref.mjs';
41
42
  export { isRefable } from './is/is-refable.mjs';
42
43
  export { isUndefined } from './is/is-undefined.mjs';
43
44
  export { isNull } from './is/is-null.mjs';
@@ -76,5 +77,8 @@ export { max } from './utils/max.mjs';
76
77
  export { min } from './utils/min.mjs';
77
78
  export { load } from './utils/load.mjs';
78
79
  export { at } from './utils/at.mjs';
80
+ export { pick } from './utils/pick.mjs';
81
+ export { get } from './utils/get.mjs';
82
+ export { set } from './utils/set.mjs';
79
83
  export { replace } from './utils/replace/replace.mjs';
80
84
  export { replaceAt } from './utils/replace/replace-at.mjs';
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ var _typeof = require('@babel/runtime/helpers/typeof');
4
+
5
+ /**
6
+ * Checks if a given key is a deep key.
7
+ *
8
+ * A deep key is a string that contains a dot (.) or square brackets with a property accessor.
9
+ *
10
+ * @param {PropertyKey} key - The key to check.
11
+ * @returns {boolean} - Returns true if the key is a deep key, otherwise false.
12
+ *
13
+ * Examples:
14
+ *
15
+ * isDeepKey('a.b') // true
16
+ * isDeepKey('a[b]') // true
17
+ * isDeepKey('a') // false
18
+ * isDeepKey(123) // false
19
+ * isDeepKey('a.b.c') // true
20
+ * isDeepKey('a[b][c]') // true
21
+ */
22
+ function isDeepKey(key) {
23
+ switch (_typeof(key)) {
24
+ case "number":
25
+ case "symbol":
26
+ {
27
+ return false;
28
+ }
29
+ case "string":
30
+ {
31
+ var _deepKeys = new Set([".", "[", "]"]);
32
+ for (var index = 0; index < key.length; index++) {
33
+ if (_deepKeys.has(key[index])) {
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ }
39
+ default:
40
+ {
41
+ return false;
42
+ }
43
+ }
44
+ }
45
+
46
+ exports.isDeepKey = isDeepKey;
@@ -0,0 +1,44 @@
1
+ import _typeof from '@babel/runtime/helpers/typeof';
2
+
3
+ /**
4
+ * Checks if a given key is a deep key.
5
+ *
6
+ * A deep key is a string that contains a dot (.) or square brackets with a property accessor.
7
+ *
8
+ * @param {PropertyKey} key - The key to check.
9
+ * @returns {boolean} - Returns true if the key is a deep key, otherwise false.
10
+ *
11
+ * Examples:
12
+ *
13
+ * isDeepKey('a.b') // true
14
+ * isDeepKey('a[b]') // true
15
+ * isDeepKey('a') // false
16
+ * isDeepKey(123) // false
17
+ * isDeepKey('a.b.c') // true
18
+ * isDeepKey('a[b][c]') // true
19
+ */
20
+ function isDeepKey(key) {
21
+ switch (_typeof(key)) {
22
+ case "number":
23
+ case "symbol":
24
+ {
25
+ return false;
26
+ }
27
+ case "string":
28
+ {
29
+ var _deepKeys = new Set([".", "[", "]"]);
30
+ for (var index = 0; index < key.length; index++) {
31
+ if (_deepKeys.has(key[index])) {
32
+ return true;
33
+ }
34
+ }
35
+ return false;
36
+ }
37
+ default:
38
+ {
39
+ return false;
40
+ }
41
+ }
42
+ }
43
+
44
+ export { isDeepKey };
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ var isFunction = require('./is-function.cjs');
4
+
5
+ /**
6
+ * is iterable
7
+ */
8
+ function isIterable(value) {
9
+ return isFunction.isFunction(value === null || value === void 0 ? void 0 : value[Symbol.iterator]);
10
+ }
11
+
12
+ exports.isIterable = isIterable;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * is iterable
3
+ */
4
+ declare function isIterable(value: unknown): value is Iterable<unknown>;
5
+ export { isIterable };
@@ -0,0 +1,10 @@
1
+ import { isFunction } from './is-function.mjs';
2
+
3
+ /**
4
+ * is iterable
5
+ */
6
+ function isIterable(value) {
7
+ return isFunction(value === null || value === void 0 ? void 0 : value[Symbol.iterator]);
8
+ }
9
+
10
+ export { isIterable };
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ var isArray = require('./is-array.cjs');
4
+ var isBoolean = require('./is-boolean.cjs');
5
+ var isNull = require('./is-null.cjs');
6
+ var isNumber = require('./is-number.cjs');
7
+ var isObject = require('./is-object.cjs');
8
+ var isString = require('./is-string.cjs');
9
+ var isSymbol = require('./is-symbol.cjs');
10
+
11
+ /** Matches any deep property path. (e.g. `a.b[0].c`)*/
12
+ var _IS_DEEP_KEY = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/;
13
+ /** Matches any word character (alphanumeric & underscore).*/
14
+ var _IS_PLAIN_KEY = /^\w*$/;
15
+ /**
16
+ * Checks if `value` is a property name and not a property path. (It's ok that the `value` is not in the keys of the `object`)
17
+ * @param {unknown} value The value to check.
18
+ * @param {unknown} object The object to query.
19
+ * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
20
+ *
21
+ * @example
22
+ * isKey('a', { a: 1 });
23
+ * // => true
24
+ *
25
+ * isKey('a.b', { a: { b: 2 } });
26
+ * // => false
27
+ */
28
+ function isKey(value, object) {
29
+ if (isArray.isArray(value) || isObject.isObject(value)) {
30
+ return false;
31
+ }
32
+ if (isNumber.isNumber(value) || isBoolean.isBoolean(value) || isNull.isNull(value) || isSymbol.isSymbol(value)) {
33
+ return true;
34
+ }
35
+ return isString.isString(value) && (_IS_PLAIN_KEY.test(value) || !_IS_DEEP_KEY.test(value)) || object != null && Object.hasOwn(object, value);
36
+ }
37
+
38
+ exports.isKey = isKey;
@@ -0,0 +1,36 @@
1
+ import { isArray } from './is-array.mjs';
2
+ import { isBoolean } from './is-boolean.mjs';
3
+ import { isNull } from './is-null.mjs';
4
+ import { isNumber } from './is-number.mjs';
5
+ import { isObject } from './is-object.mjs';
6
+ import { isString } from './is-string.mjs';
7
+ import { isSymbol } from './is-symbol.mjs';
8
+
9
+ /** Matches any deep property path. (e.g. `a.b[0].c`)*/
10
+ var _IS_DEEP_KEY = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/;
11
+ /** Matches any word character (alphanumeric & underscore).*/
12
+ var _IS_PLAIN_KEY = /^\w*$/;
13
+ /**
14
+ * Checks if `value` is a property name and not a property path. (It's ok that the `value` is not in the keys of the `object`)
15
+ * @param {unknown} value The value to check.
16
+ * @param {unknown} object The object to query.
17
+ * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
18
+ *
19
+ * @example
20
+ * isKey('a', { a: 1 });
21
+ * // => true
22
+ *
23
+ * isKey('a.b', { a: { b: 2 } });
24
+ * // => false
25
+ */
26
+ function isKey(value, object) {
27
+ if (isArray(value) || isObject(value)) {
28
+ return false;
29
+ }
30
+ if (isNumber(value) || isBoolean(value) || isNull(value) || isSymbol(value)) {
31
+ return true;
32
+ }
33
+ return isString(value) && (_IS_PLAIN_KEY.test(value) || !_IS_DEEP_KEY.test(value)) || object != null && Object.hasOwn(object, value);
34
+ }
35
+
36
+ export { isKey };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ var _typeof = require('@babel/runtime/helpers/typeof');
4
+
5
+ /**
6
+ * Check whether a value is a symbol.
7
+ *
8
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `symbol`.
9
+ *
10
+ * @param {unknown} value The value to check.
11
+ * @returns {value is symbol} Returns `true` if `value` is a symbol, else `false`.
12
+ * @example
13
+ * isSymbol(Symbol.iterator);
14
+ * // => true
15
+ *
16
+ * isSymbol('abc');
17
+ * // => false
18
+ */
19
+ function isSymbol(value) {
20
+ return _typeof(value) === "symbol" || value instanceof Symbol;
21
+ }
22
+
23
+ exports.isSymbol = isSymbol;
@@ -0,0 +1,21 @@
1
+ import _typeof from '@babel/runtime/helpers/typeof';
2
+
3
+ /**
4
+ * Check whether a value is a symbol.
5
+ *
6
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `symbol`.
7
+ *
8
+ * @param {unknown} value The value to check.
9
+ * @returns {value is symbol} Returns `true` if `value` is a symbol, else `false`.
10
+ * @example
11
+ * isSymbol(Symbol.iterator);
12
+ * // => true
13
+ *
14
+ * isSymbol('abc');
15
+ * // => false
16
+ */
17
+ function isSymbol(value) {
18
+ return _typeof(value) === "symbol" || value instanceof Symbol;
19
+ }
20
+
21
+ export { isSymbol };
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ var _typeof = require('@babel/runtime/helpers/typeof');
4
+ var isDeepKey = require('../is/is-deep-key.cjs');
5
+ var toKey = require('./to-key.cjs');
6
+ var toPaths = require('./to-paths.cjs');
7
+
8
+ /**
9
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
10
+ *
11
+ * @param {unknown} object - The object to query.
12
+ * @param {PropertyKey | readonly PropertyKey[]} path - The path of the property to get.
13
+ * @param {unknown} [defaultValue] - The value returned if the resolved value is undefined.
14
+ * @returns {any} - Returns the resolved value.
15
+ */
16
+ function get(object, path, defaultValue) {
17
+ if (object == null) {
18
+ return defaultValue;
19
+ }
20
+ switch (_typeof(path)) {
21
+ case "string":
22
+ {
23
+ var result = object[path];
24
+ if (result === undefined) {
25
+ if (isDeepKey.isDeepKey(path)) {
26
+ return get(object, toPaths.toPaths(path), defaultValue);
27
+ } else {
28
+ return defaultValue;
29
+ }
30
+ }
31
+ return result;
32
+ }
33
+ case "number":
34
+ case "symbol":
35
+ {
36
+ if (typeof path === "number") {
37
+ path = toKey.toKey(path);
38
+ }
39
+ var _result = object[path];
40
+ if (_result === undefined) {
41
+ return defaultValue;
42
+ }
43
+ return _result;
44
+ }
45
+ default:
46
+ {
47
+ var _path;
48
+ if (Array.isArray(path)) {
49
+ return getWithPath(object, path, defaultValue);
50
+ }
51
+ if (Object.is((_path = path) === null || _path === void 0 ? void 0 : _path.valueOf(), -0)) {
52
+ path = "-0";
53
+ } else {
54
+ path = String(path);
55
+ }
56
+ var _result2 = object[path];
57
+ if (_result2 === undefined) {
58
+ return defaultValue;
59
+ }
60
+ return _result2;
61
+ }
62
+ }
63
+ }
64
+ function getWithPath(object, path, defaultValue) {
65
+ if (path.length === 0) {
66
+ return defaultValue;
67
+ }
68
+ var current = object;
69
+ for (var index = 0; index < path.length; index++) {
70
+ if (current == null) {
71
+ return defaultValue;
72
+ }
73
+ current = current[path[index]];
74
+ }
75
+ if (current === undefined) {
76
+ return defaultValue;
77
+ }
78
+ return current;
79
+ }
80
+
81
+ exports.get = get;
@@ -0,0 +1,79 @@
1
+ import _typeof from '@babel/runtime/helpers/typeof';
2
+ import { isDeepKey } from '../is/is-deep-key.mjs';
3
+ import { toKey } from './to-key.mjs';
4
+ import { toPaths } from './to-paths.mjs';
5
+
6
+ /**
7
+ * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead.
8
+ *
9
+ * @param {unknown} object - The object to query.
10
+ * @param {PropertyKey | readonly PropertyKey[]} path - The path of the property to get.
11
+ * @param {unknown} [defaultValue] - The value returned if the resolved value is undefined.
12
+ * @returns {any} - Returns the resolved value.
13
+ */
14
+ function get(object, path, defaultValue) {
15
+ if (object == null) {
16
+ return defaultValue;
17
+ }
18
+ switch (_typeof(path)) {
19
+ case "string":
20
+ {
21
+ var result = object[path];
22
+ if (result === undefined) {
23
+ if (isDeepKey(path)) {
24
+ return get(object, toPaths(path), defaultValue);
25
+ } else {
26
+ return defaultValue;
27
+ }
28
+ }
29
+ return result;
30
+ }
31
+ case "number":
32
+ case "symbol":
33
+ {
34
+ if (typeof path === "number") {
35
+ path = toKey(path);
36
+ }
37
+ var _result = object[path];
38
+ if (_result === undefined) {
39
+ return defaultValue;
40
+ }
41
+ return _result;
42
+ }
43
+ default:
44
+ {
45
+ var _path;
46
+ if (Array.isArray(path)) {
47
+ return getWithPath(object, path, defaultValue);
48
+ }
49
+ if (Object.is((_path = path) === null || _path === void 0 ? void 0 : _path.valueOf(), -0)) {
50
+ path = "-0";
51
+ } else {
52
+ path = String(path);
53
+ }
54
+ var _result2 = object[path];
55
+ if (_result2 === undefined) {
56
+ return defaultValue;
57
+ }
58
+ return _result2;
59
+ }
60
+ }
61
+ }
62
+ function getWithPath(object, path, defaultValue) {
63
+ if (path.length === 0) {
64
+ return defaultValue;
65
+ }
66
+ var current = object;
67
+ for (var index = 0; index < path.length; index++) {
68
+ if (current == null) {
69
+ return defaultValue;
70
+ }
71
+ current = current[path[index]];
72
+ }
73
+ if (current === undefined) {
74
+ return defaultValue;
75
+ }
76
+ return current;
77
+ }
78
+
79
+ export { get };
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ var _typeof = require('@babel/runtime/helpers/typeof');
4
+
5
+ var IS_UNSIGNED_INTEGER = /^(?:0|[1-9]\d*)$/;
6
+ function isIndex(value) {
7
+ var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Number.MAX_SAFE_INTEGER;
8
+ switch (_typeof(value)) {
9
+ case "number":
10
+ {
11
+ return Number.isInteger(value) && value >= 0 && value < length;
12
+ }
13
+ case "symbol":
14
+ {
15
+ return false;
16
+ }
17
+ case "string":
18
+ {
19
+ return IS_UNSIGNED_INTEGER.test(value);
20
+ }
21
+ default:
22
+ {
23
+ return false;
24
+ }
25
+ }
26
+ }
27
+
28
+ exports.isIndex = isIndex;
@@ -0,0 +1,26 @@
1
+ import _typeof from '@babel/runtime/helpers/typeof';
2
+
3
+ var IS_UNSIGNED_INTEGER = /^(?:0|[1-9]\d*)$/;
4
+ function isIndex(value) {
5
+ var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Number.MAX_SAFE_INTEGER;
6
+ switch (_typeof(value)) {
7
+ case "number":
8
+ {
9
+ return Number.isInteger(value) && value >= 0 && value < length;
10
+ }
11
+ case "symbol":
12
+ {
13
+ return false;
14
+ }
15
+ case "string":
16
+ {
17
+ return IS_UNSIGNED_INTEGER.test(value);
18
+ }
19
+ default:
20
+ {
21
+ return false;
22
+ }
23
+ }
24
+ }
25
+
26
+ export { isIndex };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var get = require('./get.cjs');
4
+ var set = require('./set.cjs');
5
+
6
+ /**
7
+ * pick value
8
+ */
9
+ function pick(value, keys) {
10
+ return keys.reduce(function (picked, key) {
11
+ set.set(picked, key, get.get(value, key));
12
+ return picked;
13
+ }, {});
14
+ }
15
+
16
+ exports.pick = pick;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * pick value
3
+ */
4
+ declare function pick<T extends object>(value: T, keys: PropertyKey[]): Partial<T>;
5
+ export { pick };
@@ -0,0 +1,14 @@
1
+ import { get } from './get.mjs';
2
+ import { set } from './set.mjs';
3
+
4
+ /**
5
+ * pick value
6
+ */
7
+ function pick(value, keys) {
8
+ return keys.reduce(function (picked, key) {
9
+ set(picked, key, get(value, key));
10
+ return picked;
11
+ }, {});
12
+ }
13
+
14
+ export { pick };
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ var update = require('./update.cjs');
4
+
5
+ /**
6
+ * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
7
+ *
8
+ * @template T - The type of the object.
9
+ * @param {T} obj - The object to modify.
10
+ * @param {PropertyKey | PropertyKey[]} path - The path of the property to set.
11
+ * @param {unknown} value - The value to set.
12
+ * @returns {T} - The modified object.
13
+ *
14
+ * @example
15
+ * // Set a value in a nested object
16
+ * const obj = { a: { b: { c: 3 } } };
17
+ * set(obj, 'a.b.c', 4);
18
+ * console.log(obj.a.b.c); // 4
19
+ *
20
+ * @example
21
+ * // Set a value in an array
22
+ * const arr = [1, 2, 3];
23
+ * set(arr, 1, 4);
24
+ * console.log(arr[1]); // 4
25
+ *
26
+ * @example
27
+ * // Create non-existent path and set value
28
+ * const obj = {};
29
+ * set(obj, 'a.b.c', 4);
30
+ * console.log(obj); // { a: { b: { c: 4 } } }
31
+ */
32
+ function set(obj, path, value) {
33
+ return update.update(obj, path, function () {
34
+ return value;
35
+ });
36
+ }
37
+
38
+ exports.set = set;
@@ -0,0 +1,36 @@
1
+ import { update } from './update.mjs';
2
+
3
+ /**
4
+ * Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.
5
+ *
6
+ * @template T - The type of the object.
7
+ * @param {T} obj - The object to modify.
8
+ * @param {PropertyKey | PropertyKey[]} path - The path of the property to set.
9
+ * @param {unknown} value - The value to set.
10
+ * @returns {T} - The modified object.
11
+ *
12
+ * @example
13
+ * // Set a value in a nested object
14
+ * const obj = { a: { b: { c: 3 } } };
15
+ * set(obj, 'a.b.c', 4);
16
+ * console.log(obj.a.b.c); // 4
17
+ *
18
+ * @example
19
+ * // Set a value in an array
20
+ * const arr = [1, 2, 3];
21
+ * set(arr, 1, 4);
22
+ * console.log(arr[1]); // 4
23
+ *
24
+ * @example
25
+ * // Create non-existent path and set value
26
+ * const obj = {};
27
+ * set(obj, 'a.b.c', 4);
28
+ * console.log(obj); // { a: { b: { c: 4 } } }
29
+ */
30
+ function set(obj, path, value) {
31
+ return update(obj, path, function () {
32
+ return value;
33
+ });
34
+ }
35
+
36
+ export { set };
@@ -1,6 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var isArray = require('../is/is-array.cjs');
4
+ var isIterable = require('../is/is-iterable.cjs');
5
+ var isString = require('../is/is-string.cjs');
4
6
  var isVoid = require('../is/is-void.cjs');
5
7
 
6
8
  /**
@@ -16,6 +18,11 @@ function toArray(value) {
16
18
  if (isArray.isArray(value)) {
17
19
  return value;
18
20
  }
21
+ // has iterator
22
+ // `string` to `[string]`
23
+ if (isIterable.isIterable(value) && !isString.isString(value)) {
24
+ return Array.from(value);
25
+ }
19
26
  return [value];
20
27
  }
21
28
 
@@ -1,4 +1,4 @@
1
- type ToArrayReturn<T> = T extends null ? unknown[] : T extends undefined ? unknown : T extends Array<infer E> ? E[] : T[];
1
+ type ToArrayReturn<T> = T extends null ? never[] : T extends undefined ? never[] : T extends string ? T[] : T extends Array<infer R> ? R[] : T extends Iterable<infer I> ? I[] : T[];
2
2
  /**
3
3
  * @description
4
4
  * convert any type data to a array
@@ -1,4 +1,6 @@
1
1
  import { isArray } from '../is/is-array.mjs';
2
+ import { isIterable } from '../is/is-iterable.mjs';
3
+ import { isString } from '../is/is-string.mjs';
2
4
  import { isVoid } from '../is/is-void.mjs';
3
5
 
4
6
  /**
@@ -14,6 +16,11 @@ function toArray(value) {
14
16
  if (isArray(value)) {
15
17
  return value;
16
18
  }
19
+ // has iterator
20
+ // `string` to `[string]`
21
+ if (isIterable(value) && !isString(value)) {
22
+ return Array.from(value);
23
+ }
17
24
  return [value];
18
25
  }
19
26
 
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ var _typeof = require('@babel/runtime/helpers/typeof');
4
+
5
+ /**
6
+ * Converts `value` to a string key if it's not a string or symbol.
7
+ *
8
+ * @private
9
+ * @param {unknown} value The value to inspect.
10
+ * @returns {string|symbol} Returns the key.
11
+ */
12
+ function toKey(value) {
13
+ var _value$valueOf;
14
+ if (typeof value === "string" || _typeof(value) === "symbol") {
15
+ return value;
16
+ }
17
+ if (Object.is(value === null || value === void 0 || (_value$valueOf = value.valueOf) === null || _value$valueOf === void 0 ? void 0 : _value$valueOf.call(value), -0)) {
18
+ return "-0";
19
+ }
20
+ return String(value);
21
+ }
22
+
23
+ exports.toKey = toKey;
@@ -0,0 +1,21 @@
1
+ import _typeof from '@babel/runtime/helpers/typeof';
2
+
3
+ /**
4
+ * Converts `value` to a string key if it's not a string or symbol.
5
+ *
6
+ * @private
7
+ * @param {unknown} value The value to inspect.
8
+ * @returns {string|symbol} Returns the key.
9
+ */
10
+ function toKey(value) {
11
+ var _value$valueOf;
12
+ if (typeof value === "string" || _typeof(value) === "symbol") {
13
+ return value;
14
+ }
15
+ if (Object.is(value === null || value === void 0 || (_value$valueOf = value.valueOf) === null || _value$valueOf === void 0 ? void 0 : _value$valueOf.call(value), -0)) {
16
+ return "-0";
17
+ }
18
+ return String(value);
19
+ }
20
+
21
+ export { toKey };
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Converts a deep key string into an array of path segments.
5
+ *
6
+ * 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.
7
+ *
8
+ * @param {string} deepKey - The deep key string to convert.
9
+ * @returns {string[]} An array of strings, each representing a segment of the path.
10
+ *
11
+ * Examples:
12
+ *
13
+ * toPath('a.b.c') // Returns ['a', 'b', 'c']
14
+ * toPath('a[b][c]') // Returns ['a', 'b', 'c']
15
+ * toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']
16
+ * toPath('a["b.c"].d') // Returns ['a', 'b.c', 'd']
17
+ * toPath('') // Returns []
18
+ * toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
19
+ */
20
+ function toPaths(deepKey) {
21
+ var result = [];
22
+ var length = deepKey.length;
23
+ if (length === 0) {
24
+ return result;
25
+ }
26
+ var index = 0;
27
+ var key = "";
28
+ var quoteChar = "";
29
+ var bracket = false;
30
+ // Leading dot
31
+ if (deepKey.charCodeAt(0) === 46) {
32
+ result.push("");
33
+ index++;
34
+ }
35
+ while (index < length) {
36
+ var _char = deepKey[index];
37
+ if (quoteChar) {
38
+ if (_char === "\\" && index + 1 < length) {
39
+ // Escape character
40
+ index++;
41
+ key += deepKey[index];
42
+ } else if (_char === quoteChar) {
43
+ // End of quote
44
+ quoteChar = "";
45
+ } else {
46
+ key += _char;
47
+ }
48
+ } else if (bracket) {
49
+ if (_char === '"' || _char === "'") {
50
+ // Start of quoted string inside brackets
51
+ quoteChar = _char;
52
+ } else if (_char === "]") {
53
+ // End of bracketed segment
54
+ bracket = false;
55
+ result.push(key);
56
+ key = "";
57
+ } else {
58
+ key += _char;
59
+ }
60
+ } else {
61
+ if (_char === "[") {
62
+ // Start of bracketed segment
63
+ bracket = true;
64
+ if (key) {
65
+ result.push(key);
66
+ key = "";
67
+ }
68
+ } else if (_char === ".") {
69
+ if (key) {
70
+ result.push(key);
71
+ key = "";
72
+ }
73
+ } else {
74
+ key += _char;
75
+ }
76
+ }
77
+ index++;
78
+ }
79
+ if (key) {
80
+ result.push(key);
81
+ }
82
+ return result;
83
+ }
84
+
85
+ exports.toPaths = toPaths;
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Converts a deep key string into an array of path segments.
3
+ *
4
+ * 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.
5
+ *
6
+ * @param {string} deepKey - The deep key string to convert.
7
+ * @returns {string[]} An array of strings, each representing a segment of the path.
8
+ *
9
+ * Examples:
10
+ *
11
+ * toPath('a.b.c') // Returns ['a', 'b', 'c']
12
+ * toPath('a[b][c]') // Returns ['a', 'b', 'c']
13
+ * toPath('.a.b.c') // Returns ['', 'a', 'b', 'c']
14
+ * toPath('a["b.c"].d') // Returns ['a', 'b.c', 'd']
15
+ * toPath('') // Returns []
16
+ * toPath('.a[b].c.d[e]["f.g"].h') // Returns ['', 'a', 'b', 'c', 'd', 'e', 'f.g', 'h']
17
+ */
18
+ function toPaths(deepKey) {
19
+ var result = [];
20
+ var length = deepKey.length;
21
+ if (length === 0) {
22
+ return result;
23
+ }
24
+ var index = 0;
25
+ var key = "";
26
+ var quoteChar = "";
27
+ var bracket = false;
28
+ // Leading dot
29
+ if (deepKey.charCodeAt(0) === 46) {
30
+ result.push("");
31
+ index++;
32
+ }
33
+ while (index < length) {
34
+ var _char = deepKey[index];
35
+ if (quoteChar) {
36
+ if (_char === "\\" && index + 1 < length) {
37
+ // Escape character
38
+ index++;
39
+ key += deepKey[index];
40
+ } else if (_char === quoteChar) {
41
+ // End of quote
42
+ quoteChar = "";
43
+ } else {
44
+ key += _char;
45
+ }
46
+ } else if (bracket) {
47
+ if (_char === '"' || _char === "'") {
48
+ // Start of quoted string inside brackets
49
+ quoteChar = _char;
50
+ } else if (_char === "]") {
51
+ // End of bracketed segment
52
+ bracket = false;
53
+ result.push(key);
54
+ key = "";
55
+ } else {
56
+ key += _char;
57
+ }
58
+ } else {
59
+ if (_char === "[") {
60
+ // Start of bracketed segment
61
+ bracket = true;
62
+ if (key) {
63
+ result.push(key);
64
+ key = "";
65
+ }
66
+ } else if (_char === ".") {
67
+ if (key) {
68
+ result.push(key);
69
+ key = "";
70
+ }
71
+ } else {
72
+ key += _char;
73
+ }
74
+ }
75
+ index++;
76
+ }
77
+ if (key) {
78
+ result.push(key);
79
+ }
80
+ return result;
81
+ }
82
+
83
+ export { toPaths };
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
4
+ var isKey = require('../is/is-key.cjs');
5
+ var isObject = require('../is/is-object.cjs');
6
+ var isUndefined = require('../is/is-undefined.cjs');
7
+ var isVoid = require('../is/is-void.cjs');
8
+ var isIndex = require('./is-index.cjs');
9
+ var toKey = require('./to-key.cjs');
10
+ var toPaths = require('./to-paths.cjs');
11
+
12
+ /**
13
+ * Updates the value at the specified path of the given object using an updater function.
14
+ * If any part of the path does not exist, it will be created.
15
+ *
16
+ * @template T - The type of the object.
17
+ * @param {T} target - The object to modify.
18
+ * @param {PropertyKey | PropertyKey[]} path - The path of the property to update.
19
+ * @param {(value: unknown) => unknown} updater - The function to produce the updated value.
20
+ * @returns {T} - The modified object.
21
+ */
22
+ function update(target, path, updater) {
23
+ if (isVoid.isVoid(target)) {
24
+ return target;
25
+ }
26
+ var _paths = isKey.isKey(path, target) ? [path] : Array.isArray(path) ? path : typeof path === "string" ? toPaths.toPaths(path) : [path];
27
+ var current = target;
28
+ for (var i = 0; i < _paths.length && current != null; i++) {
29
+ var key = toKey.toKey(_paths[i]);
30
+ var _newValue = void 0;
31
+ if (i === _paths.length - 1) {
32
+ // @ts-expect-error get value of key
33
+ _newValue = updater(current[key]);
34
+ } else {
35
+ // @ts-expect-error get value of key
36
+ var _value = current[key];
37
+ _newValue = !isUndefined.isUndefined(_value) ? _value : isObject.isObject(_value) ? _value : isIndex.isIndex(_paths[i + 1]) ? [] : {};
38
+ }
39
+ Object.assign(current, _defineProperty({}, key, _newValue));
40
+ // @ts-expect-error get value of key
41
+ current = current[key];
42
+ }
43
+ return target;
44
+ }
45
+
46
+ exports.update = update;
@@ -0,0 +1,44 @@
1
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
+ import { isKey } from '../is/is-key.mjs';
3
+ import { isObject } from '../is/is-object.mjs';
4
+ import { isUndefined } from '../is/is-undefined.mjs';
5
+ import { isVoid } from '../is/is-void.mjs';
6
+ import { isIndex } from './is-index.mjs';
7
+ import { toKey } from './to-key.mjs';
8
+ import { toPaths } from './to-paths.mjs';
9
+
10
+ /**
11
+ * Updates the value at the specified path of the given object using an updater function.
12
+ * If any part of the path does not exist, it will be created.
13
+ *
14
+ * @template T - The type of the object.
15
+ * @param {T} target - The object to modify.
16
+ * @param {PropertyKey | PropertyKey[]} path - The path of the property to update.
17
+ * @param {(value: unknown) => unknown} updater - The function to produce the updated value.
18
+ * @returns {T} - The modified object.
19
+ */
20
+ function update(target, path, updater) {
21
+ if (isVoid(target)) {
22
+ return target;
23
+ }
24
+ var _paths = isKey(path, target) ? [path] : Array.isArray(path) ? path : typeof path === "string" ? toPaths(path) : [path];
25
+ var current = target;
26
+ for (var i = 0; i < _paths.length && current != null; i++) {
27
+ var key = toKey(_paths[i]);
28
+ var _newValue = void 0;
29
+ if (i === _paths.length - 1) {
30
+ // @ts-expect-error get value of key
31
+ _newValue = updater(current[key]);
32
+ } else {
33
+ // @ts-expect-error get value of key
34
+ var _value = current[key];
35
+ _newValue = !isUndefined(_value) ? _value : isObject(_value) ? _value : isIndex(_paths[i + 1]) ? [] : {};
36
+ }
37
+ Object.assign(current, _defineProperty({}, key, _newValue));
38
+ // @ts-expect-error get value of key
39
+ current = current[key];
40
+ }
41
+ return target;
42
+ }
43
+
44
+ export { update };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
4
  "description": "react utils collection",
5
5
  "exports": {
6
6
  ".": {
@@ -20,30 +20,30 @@
20
20
  "./types": "./dist/types/index.d.ts"
21
21
  },
22
22
  "dependencies": {
23
- "@babel/runtime": "^7.27.0",
23
+ "@babel/runtime": "^7.27.1",
24
24
  "react-is": "^19.1.0",
25
25
  "rxjs": "^7.8.2"
26
26
  },
27
27
  "devDependencies": {
28
- "@babel/core": "^7.26.10",
29
- "@babel/plugin-transform-runtime": "^7.26.10",
30
- "@babel/preset-env": "^7.26.9",
31
- "@babel/preset-react": "^7.26.3",
32
- "@babel/preset-typescript": "^7.27.0",
28
+ "@babel/core": "^7.27.1",
29
+ "@babel/plugin-transform-runtime": "^7.27.1",
30
+ "@babel/preset-env": "^7.27.2",
31
+ "@babel/preset-react": "^7.27.1",
32
+ "@babel/preset-typescript": "^7.27.1",
33
33
  "@jest/globals": "^29.7.0",
34
34
  "@rollup/plugin-babel": "^6.0.4",
35
35
  "@rollup/plugin-node-resolve": "^16.0.1",
36
36
  "@rollup/plugin-typescript": "^12.1.2",
37
37
  "@testing-library/react": "^16.3.0",
38
38
  "@types/babel__core": "^7.20.5",
39
- "@types/react": "^19.1.2",
40
- "@types/react-dom": "^19.1.2",
39
+ "@types/react": "^19.1.3",
40
+ "@types/react-dom": "^19.1.3",
41
41
  "@types/react-is": "^19.0.0",
42
42
  "jest": "^29.7.0",
43
43
  "jest-environment-jsdom": "^29.7.0",
44
44
  "react": "^19.1.0",
45
45
  "react-dom": "^19.1.0",
46
- "rollup": "^4.40.0",
46
+ "rollup": "^4.40.2",
47
47
  "typescript": "5.8.3"
48
48
  },
49
49
  "peerDependencies": {