es-toolkit 1.33.0 → 1.34.0

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/_chunk/invariant-BfGFfr.js +21 -0
  3. package/dist/_chunk/{isWeakSet-TIM260.js → isWeakSet-CQZSI-.js} +10 -0
  4. package/dist/browser.global.js +1 -1
  5. package/dist/browser.global.js.map +1 -1
  6. package/dist/compat/_internal/assignValue.mjs +10 -0
  7. package/dist/compat/_internal/isKey.mjs +1 -1
  8. package/dist/compat/_internal/toKey.mjs +4 -1
  9. package/dist/compat/array/filter.mjs +14 -31
  10. package/dist/compat/array/intersectionWith.mjs +0 -1
  11. package/dist/compat/array/map.d.mts +1 -1
  12. package/dist/compat/array/map.d.ts +1 -1
  13. package/dist/compat/array/reject.d.mts +117 -0
  14. package/dist/compat/array/reject.d.ts +117 -0
  15. package/dist/compat/array/reject.mjs +9 -0
  16. package/dist/compat/array/sortedIndexOf.d.mts +34 -0
  17. package/dist/compat/array/sortedIndexOf.d.ts +34 -0
  18. package/dist/compat/array/sortedIndexOf.mjs +15 -0
  19. package/dist/compat/index.d.mts +9 -3
  20. package/dist/compat/index.d.ts +9 -3
  21. package/dist/compat/index.js +176 -93
  22. package/dist/compat/index.mjs +9 -3
  23. package/dist/compat/math/add.mjs +17 -0
  24. package/dist/compat/math/divide.mjs +0 -1
  25. package/dist/compat/math/mean.d.mts +16 -0
  26. package/dist/compat/math/mean.d.ts +16 -0
  27. package/dist/compat/math/mean.mjs +8 -0
  28. package/dist/compat/math/meanBy.d.mts +25 -0
  29. package/dist/compat/math/meanBy.d.ts +25 -0
  30. package/dist/compat/math/meanBy.mjs +11 -0
  31. package/dist/compat/math/minBy.d.mts +31 -0
  32. package/dist/compat/math/minBy.d.ts +31 -0
  33. package/dist/compat/math/minBy.mjs +11 -0
  34. package/dist/compat/math/subtract.mjs +17 -0
  35. package/dist/compat/object/functionsIn.d.mts +20 -0
  36. package/dist/compat/object/functionsIn.d.ts +20 -0
  37. package/dist/compat/object/functionsIn.mjs +16 -0
  38. package/dist/compat/object/set.mjs +21 -8
  39. package/dist/compat/util/invoke.mjs +1 -1
  40. package/dist/compat/util/stubObject.d.mts +1 -1
  41. package/dist/compat/util/stubObject.d.ts +1 -1
  42. package/dist/index.d.mts +4 -0
  43. package/dist/index.d.ts +4 -0
  44. package/dist/index.js +7 -2
  45. package/dist/index.mjs +4 -0
  46. package/dist/predicate/index.d.mts +2 -0
  47. package/dist/predicate/index.d.ts +2 -0
  48. package/dist/predicate/index.js +3 -1
  49. package/dist/predicate/index.mjs +2 -0
  50. package/dist/predicate/isBrowser.d.mts +17 -0
  51. package/dist/predicate/isBrowser.d.ts +17 -0
  52. package/dist/predicate/isBrowser.mjs +5 -0
  53. package/dist/predicate/isNode.d.mts +17 -0
  54. package/dist/predicate/isNode.d.ts +17 -0
  55. package/dist/predicate/isNode.mjs +5 -0
  56. package/dist/util/attempt.d.mts +42 -0
  57. package/dist/util/attempt.d.ts +42 -0
  58. package/dist/util/attempt.mjs +10 -0
  59. package/dist/util/attemptAsync.d.mts +35 -0
  60. package/dist/util/attemptAsync.d.ts +35 -0
  61. package/dist/util/attemptAsync.mjs +11 -0
  62. package/dist/util/index.d.mts +2 -0
  63. package/dist/util/index.d.ts +2 -0
  64. package/dist/util/index.js +11 -5
  65. package/dist/util/index.mjs +2 -0
  66. package/package.json +7 -5
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Finds the element in an array that has the minium value when applying
3
+ * the `iteratee` to each element.
4
+ *
5
+ * @template T - The type of elements in the array.
6
+ * @param {T[]} items The array of elements to search.
7
+ * @param {((element: T) => number) | keyof T | [keyof T, unknown] | Partial<T>} iteratee
8
+ * The criteria used to determine the minium value.
9
+ * - If a **function** is provided, it extracts a numeric value from each element.
10
+ * - If a **string** is provided, it is treated as a key to extract values from the objects.
11
+ * - If a **[key, value]** pair is provided, it matches elements with the specified key-value pair.
12
+ * - If an **object** is provided, it matches elements that contain the specified properties.
13
+ * @returns {T | undefined} The element with the minium value as determined by the `iteratee`.
14
+ * @example
15
+ * minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
16
+ * minBy([], x => x.a); // Returns: undefined
17
+ * minBy(
18
+ * [
19
+ * { name: 'john', age: 30 },
20
+ * { name: 'jane', age: 28 },
21
+ * { name: 'joe', age: 26 },
22
+ * ],
23
+ * x => x.age
24
+ * ); // Returns: { name: 'joe', age: 26 }
25
+ * minBy([{ a: 1 }, { a: 2 }], 'a'); // Returns: { a: 1 }
26
+ * minBy([{ a: 1 }, { a: 2 }], ['a', 1]); // Returns: { a: 2 }
27
+ * minBy([{ a: 1 }, { a: 2 }], { a: 1 }); // Returns: { a: 2 }
28
+ */
29
+ declare function minBy<T>(items: ArrayLike<T> | null | undefined, iteratee: ((element: T) => number) | keyof T | [keyof T, unknown] | Partial<T>): T | undefined;
30
+
31
+ export { minBy };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Finds the element in an array that has the minium value when applying
3
+ * the `iteratee` to each element.
4
+ *
5
+ * @template T - The type of elements in the array.
6
+ * @param {T[]} items The array of elements to search.
7
+ * @param {((element: T) => number) | keyof T | [keyof T, unknown] | Partial<T>} iteratee
8
+ * The criteria used to determine the minium value.
9
+ * - If a **function** is provided, it extracts a numeric value from each element.
10
+ * - If a **string** is provided, it is treated as a key to extract values from the objects.
11
+ * - If a **[key, value]** pair is provided, it matches elements with the specified key-value pair.
12
+ * - If an **object** is provided, it matches elements that contain the specified properties.
13
+ * @returns {T | undefined} The element with the minium value as determined by the `iteratee`.
14
+ * @example
15
+ * minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
16
+ * minBy([], x => x.a); // Returns: undefined
17
+ * minBy(
18
+ * [
19
+ * { name: 'john', age: 30 },
20
+ * { name: 'jane', age: 28 },
21
+ * { name: 'joe', age: 26 },
22
+ * ],
23
+ * x => x.age
24
+ * ); // Returns: { name: 'joe', age: 26 }
25
+ * minBy([{ a: 1 }, { a: 2 }], 'a'); // Returns: { a: 1 }
26
+ * minBy([{ a: 1 }, { a: 2 }], ['a', 1]); // Returns: { a: 2 }
27
+ * minBy([{ a: 1 }, { a: 2 }], { a: 1 }); // Returns: { a: 2 }
28
+ */
29
+ declare function minBy<T>(items: ArrayLike<T> | null | undefined, iteratee: ((element: T) => number) | keyof T | [keyof T, unknown] | Partial<T>): T | undefined;
30
+
31
+ export { minBy };
@@ -0,0 +1,11 @@
1
+ import { minBy as minBy$1 } from '../../array/minBy.mjs';
2
+ import { iteratee } from '../util/iteratee.mjs';
3
+
4
+ function minBy(items, iteratee$1) {
5
+ if (items == null) {
6
+ return undefined;
7
+ }
8
+ return minBy$1(Array.from(items), iteratee(iteratee$1));
9
+ }
10
+
11
+ export { minBy };
@@ -1,4 +1,21 @@
1
+ import { toNumber } from '../util/toNumber.mjs';
2
+ import { toString } from '../util/toString.mjs';
3
+
1
4
  function subtract(value, other) {
5
+ if (value === undefined && other === undefined) {
6
+ return 0;
7
+ }
8
+ if (value === undefined || other === undefined) {
9
+ return value ?? other;
10
+ }
11
+ if (typeof value === 'string' || typeof other === 'string') {
12
+ value = toString(value);
13
+ other = toString(other);
14
+ }
15
+ else {
16
+ value = toNumber(value);
17
+ other = toNumber(other);
18
+ }
2
19
  return value - other;
3
20
  }
4
21
 
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Returns an array of property names whose values are functions, including inherited properties.
3
+ *
4
+ * @param {*} object The object to inspect.
5
+ * @returns {Array} Returns the function names.
6
+ * @example
7
+ *
8
+ * function Foo() {
9
+ * this.a = function() { return 'a'; };
10
+ * this.b = function() { return 'b'; };
11
+ * }
12
+ *
13
+ * Foo.prototype.c = function() { return 'c'; };
14
+ *
15
+ * functionsIn(new Foo);
16
+ * // => ['a', 'b', 'c']
17
+ */
18
+ declare function functionsIn(object: any): string[];
19
+
20
+ export { functionsIn };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Returns an array of property names whose values are functions, including inherited properties.
3
+ *
4
+ * @param {*} object The object to inspect.
5
+ * @returns {Array} Returns the function names.
6
+ * @example
7
+ *
8
+ * function Foo() {
9
+ * this.a = function() { return 'a'; };
10
+ * this.b = function() { return 'b'; };
11
+ * }
12
+ *
13
+ * Foo.prototype.c = function() { return 'c'; };
14
+ *
15
+ * functionsIn(new Foo);
16
+ * // => ['a', 'b', 'c']
17
+ */
18
+ declare function functionsIn(object: any): string[];
19
+
20
+ export { functionsIn };
@@ -0,0 +1,16 @@
1
+ import { isFunction } from '../../predicate/isFunction.mjs';
2
+
3
+ function functionsIn(object) {
4
+ if (object == null) {
5
+ return [];
6
+ }
7
+ const result = [];
8
+ for (const key in object) {
9
+ if (isFunction(object[key])) {
10
+ result.push(key);
11
+ }
12
+ }
13
+ return result;
14
+ }
15
+
16
+ export { functionsIn };
@@ -1,19 +1,32 @@
1
+ import { assignValue } from '../_internal/assignValue.mjs';
1
2
  import { isIndex } from '../_internal/isIndex.mjs';
3
+ import { isKey } from '../_internal/isKey.mjs';
4
+ import { toKey } from '../_internal/toKey.mjs';
5
+ import { isObject } from '../predicate/isObject.mjs';
2
6
  import { toPath } from '../util/toPath.mjs';
3
7
 
4
8
  function set(obj, path, value) {
5
- const resolvedPath = Array.isArray(path) ? path : typeof path === 'string' ? toPath(path) : [path];
9
+ if (obj == null && !isObject(obj)) {
10
+ return obj;
11
+ }
12
+ const resolvedPath = isKey(path, obj)
13
+ ? [path]
14
+ : Array.isArray(path)
15
+ ? path
16
+ : typeof path === 'string'
17
+ ? toPath(path)
18
+ : [path];
6
19
  let current = obj;
7
- for (let i = 0; i < resolvedPath.length - 1; i++) {
8
- const key = resolvedPath[i];
9
- const nextKey = resolvedPath[i + 1];
10
- if (current[key] == null) {
11
- current[key] = isIndex(nextKey) ? [] : {};
20
+ for (let i = 0; i < resolvedPath.length && current != null; i++) {
21
+ const key = toKey(resolvedPath[i]);
22
+ let newValue = value;
23
+ if (i !== resolvedPath.length - 1) {
24
+ const objValue = current[key];
25
+ newValue = isObject(objValue) ? objValue : isIndex(resolvedPath[i + 1]) ? [] : {};
12
26
  }
27
+ assignValue(current, key, newValue);
13
28
  current = current[key];
14
29
  }
15
- const lastKey = resolvedPath[resolvedPath.length - 1];
16
- current[lastKey] = value;
17
30
  return obj;
18
31
  }
19
32
 
@@ -34,7 +34,7 @@ function invokeImpl(object, path, args) {
34
34
  return undefined;
35
35
  }
36
36
  let lastKey = last(path);
37
- let lastValue = lastKey?.valueOf();
37
+ const lastValue = lastKey?.valueOf();
38
38
  if (typeof lastValue === 'number') {
39
39
  lastKey = toKey(lastValue);
40
40
  }
@@ -5,6 +5,6 @@
5
5
  * @example
6
6
  * stubObject() // Returns {}
7
7
  */
8
- declare function stubObject(): {};
8
+ declare function stubObject(): object;
9
9
 
10
10
  export { stubObject };
@@ -5,6 +5,6 @@
5
5
  * @example
6
6
  * stubObject() // Returns {}
7
7
  */
8
- declare function stubObject(): {};
8
+ declare function stubObject(): object;
9
9
 
10
10
  export { stubObject };
package/dist/index.d.mts CHANGED
@@ -112,6 +112,7 @@ export { toMerged } from './object/toMerged.mjs';
112
112
  export { isArrayBuffer } from './predicate/isArrayBuffer.mjs';
113
113
  export { isBlob } from './predicate/isBlob.mjs';
114
114
  export { isBoolean } from './predicate/isBoolean.mjs';
115
+ export { isBrowser } from './predicate/isBrowser.mjs';
115
116
  export { isBuffer } from './predicate/isBuffer.mjs';
116
117
  export { isDate } from './predicate/isDate.mjs';
117
118
  export { isEqual } from './predicate/isEqual.mjs';
@@ -124,6 +125,7 @@ export { isJSONArray, isJSONObject, isJSONValue } from './predicate/isJSONValue.
124
125
  export { isLength } from './predicate/isLength.mjs';
125
126
  export { isMap } from './predicate/isMap.mjs';
126
127
  export { isNil } from './predicate/isNil.mjs';
128
+ export { isNode } from './predicate/isNode.mjs';
127
129
  export { isNotNil } from './predicate/isNotNil.mjs';
128
130
  export { isNull } from './predicate/isNull.mjs';
129
131
  export { isPlainObject } from './predicate/isPlainObject.mjs';
@@ -163,4 +165,6 @@ export { unescape } from './string/unescape.mjs';
163
165
  export { upperCase } from './string/upperCase.mjs';
164
166
  export { upperFirst } from './string/upperFirst.mjs';
165
167
  export { words } from './string/words.mjs';
168
+ export { attempt } from './util/attempt.mjs';
169
+ export { attemptAsync } from './util/attemptAsync.mjs';
166
170
  export { invariant } from './util/invariant.mjs';
package/dist/index.d.ts CHANGED
@@ -112,6 +112,7 @@ export { toMerged } from './object/toMerged.js';
112
112
  export { isArrayBuffer } from './predicate/isArrayBuffer.js';
113
113
  export { isBlob } from './predicate/isBlob.js';
114
114
  export { isBoolean } from './predicate/isBoolean.js';
115
+ export { isBrowser } from './predicate/isBrowser.js';
115
116
  export { isBuffer } from './predicate/isBuffer.js';
116
117
  export { isDate } from './predicate/isDate.js';
117
118
  export { isEqual } from './predicate/isEqual.js';
@@ -124,6 +125,7 @@ export { isJSONArray, isJSONObject, isJSONValue } from './predicate/isJSONValue.
124
125
  export { isLength } from './predicate/isLength.js';
125
126
  export { isMap } from './predicate/isMap.js';
126
127
  export { isNil } from './predicate/isNil.js';
128
+ export { isNode } from './predicate/isNode.js';
127
129
  export { isNotNil } from './predicate/isNotNil.js';
128
130
  export { isNull } from './predicate/isNull.js';
129
131
  export { isPlainObject } from './predicate/isPlainObject.js';
@@ -163,4 +165,6 @@ export { unescape } from './string/unescape.js';
163
165
  export { upperCase } from './string/upperCase.js';
164
166
  export { upperFirst } from './string/upperFirst.js';
165
167
  export { words } from './string/words.js';
168
+ export { attempt } from './util/attempt.js';
169
+ export { attemptAsync } from './util/attemptAsync.js';
166
170
  export { invariant } from './util/invariant.js';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ const randomInt = require('./_chunk/randomInt-CF7bZK.js');
14
14
  const math_index = require('./math/index.js');
15
15
  const toMerged = require('./_chunk/toMerged-CwnQF6.js');
16
16
  const object_index = require('./object/index.js');
17
- const isWeakSet = require('./_chunk/isWeakSet-TIM260.js');
17
+ const isWeakSet = require('./_chunk/isWeakSet-CQZSI-.js');
18
18
  const predicate_index = require('./predicate/index.js');
19
19
  const isPlainObject = require('./_chunk/isPlainObject-Xaozpc.js');
20
20
  const delay = require('./_chunk/delay-_VMfFa.js');
@@ -22,6 +22,7 @@ const promise_index = require('./promise/index.js');
22
22
  const upperFirst = require('./_chunk/upperFirst-nA5L7X.js');
23
23
  const string_index = require('./string/index.js');
24
24
  const util_index = require('./util/index.js');
25
+ const invariant = require('./_chunk/invariant-BfGFfr.js');
25
26
 
26
27
 
27
28
 
@@ -138,6 +139,7 @@ exports.pick = object_index.pick;
138
139
  exports.pickBy = object_index.pickBy;
139
140
  exports.isArrayBuffer = isWeakSet.isArrayBuffer;
140
141
  exports.isBlob = isWeakSet.isBlob;
142
+ exports.isBrowser = isWeakSet.isBrowser;
141
143
  exports.isBuffer = isWeakSet.isBuffer;
142
144
  exports.isDate = isWeakSet.isDate;
143
145
  exports.isEqual = isWeakSet.isEqual;
@@ -151,6 +153,7 @@ exports.isJSONValue = isWeakSet.isJSONValue;
151
153
  exports.isLength = isWeakSet.isLength;
152
154
  exports.isMap = isWeakSet.isMap;
153
155
  exports.isNil = isWeakSet.isNil;
156
+ exports.isNode = isWeakSet.isNode;
154
157
  exports.isNotNil = isWeakSet.isNotNil;
155
158
  exports.isNull = isWeakSet.isNull;
156
159
  exports.isPromise = isWeakSet.isPromise;
@@ -192,4 +195,6 @@ exports.upperCase = upperFirst.upperCase;
192
195
  exports.upperFirst = upperFirst.upperFirst;
193
196
  exports.words = upperFirst.words;
194
197
  exports.startCase = string_index.startCase;
195
- exports.invariant = util_index.invariant;
198
+ exports.attempt = util_index.attempt;
199
+ exports.attemptAsync = invariant.attemptAsync;
200
+ exports.invariant = invariant.invariant;
package/dist/index.mjs CHANGED
@@ -112,6 +112,7 @@ export { toMerged } from './object/toMerged.mjs';
112
112
  export { isArrayBuffer } from './predicate/isArrayBuffer.mjs';
113
113
  export { isBlob } from './predicate/isBlob.mjs';
114
114
  export { isBoolean } from './predicate/isBoolean.mjs';
115
+ export { isBrowser } from './predicate/isBrowser.mjs';
115
116
  export { isBuffer } from './predicate/isBuffer.mjs';
116
117
  export { isDate } from './predicate/isDate.mjs';
117
118
  export { isEqual } from './predicate/isEqual.mjs';
@@ -124,6 +125,7 @@ export { isJSONArray, isJSONObject, isJSONValue } from './predicate/isJSONValue.
124
125
  export { isLength } from './predicate/isLength.mjs';
125
126
  export { isMap } from './predicate/isMap.mjs';
126
127
  export { isNil } from './predicate/isNil.mjs';
128
+ export { isNode } from './predicate/isNode.mjs';
127
129
  export { isNotNil } from './predicate/isNotNil.mjs';
128
130
  export { isNull } from './predicate/isNull.mjs';
129
131
  export { isPlainObject } from './predicate/isPlainObject.mjs';
@@ -163,4 +165,6 @@ export { unescape } from './string/unescape.mjs';
163
165
  export { upperCase } from './string/upperCase.mjs';
164
166
  export { upperFirst } from './string/upperFirst.mjs';
165
167
  export { words } from './string/words.mjs';
168
+ export { attempt } from './util/attempt.mjs';
169
+ export { attemptAsync } from './util/attemptAsync.mjs';
166
170
  export { invariant } from './util/invariant.mjs';
@@ -1,6 +1,7 @@
1
1
  export { isArrayBuffer } from './isArrayBuffer.mjs';
2
2
  export { isBlob } from './isBlob.mjs';
3
3
  export { isBoolean } from './isBoolean.mjs';
4
+ export { isBrowser } from './isBrowser.mjs';
4
5
  export { isBuffer } from './isBuffer.mjs';
5
6
  export { isDate } from './isDate.mjs';
6
7
  export { isEqual } from './isEqual.mjs';
@@ -13,6 +14,7 @@ export { isJSONArray, isJSONObject, isJSONValue } from './isJSONValue.mjs';
13
14
  export { isLength } from './isLength.mjs';
14
15
  export { isMap } from './isMap.mjs';
15
16
  export { isNil } from './isNil.mjs';
17
+ export { isNode } from './isNode.mjs';
16
18
  export { isNotNil } from './isNotNil.mjs';
17
19
  export { isNull } from './isNull.mjs';
18
20
  export { isPlainObject } from './isPlainObject.mjs';
@@ -1,6 +1,7 @@
1
1
  export { isArrayBuffer } from './isArrayBuffer.js';
2
2
  export { isBlob } from './isBlob.js';
3
3
  export { isBoolean } from './isBoolean.js';
4
+ export { isBrowser } from './isBrowser.js';
4
5
  export { isBuffer } from './isBuffer.js';
5
6
  export { isDate } from './isDate.js';
6
7
  export { isEqual } from './isEqual.js';
@@ -13,6 +14,7 @@ export { isJSONArray, isJSONObject, isJSONValue } from './isJSONValue.js';
13
14
  export { isLength } from './isLength.js';
14
15
  export { isMap } from './isMap.js';
15
16
  export { isNil } from './isNil.js';
17
+ export { isNode } from './isNode.js';
16
18
  export { isNotNil } from './isNotNil.js';
17
19
  export { isNull } from './isNull.js';
18
20
  export { isPlainObject } from './isPlainObject.js';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const isWeakSet = require('../_chunk/isWeakSet-TIM260.js');
5
+ const isWeakSet = require('../_chunk/isWeakSet-CQZSI-.js');
6
6
  const isPlainObject = require('../_chunk/isPlainObject-Xaozpc.js');
7
7
 
8
8
  function isBoolean(x) {
@@ -19,6 +19,7 @@ function isString(value) {
19
19
 
20
20
  exports.isArrayBuffer = isWeakSet.isArrayBuffer;
21
21
  exports.isBlob = isWeakSet.isBlob;
22
+ exports.isBrowser = isWeakSet.isBrowser;
22
23
  exports.isBuffer = isWeakSet.isBuffer;
23
24
  exports.isDate = isWeakSet.isDate;
24
25
  exports.isEqual = isWeakSet.isEqual;
@@ -32,6 +33,7 @@ exports.isJSONValue = isWeakSet.isJSONValue;
32
33
  exports.isLength = isWeakSet.isLength;
33
34
  exports.isMap = isWeakSet.isMap;
34
35
  exports.isNil = isWeakSet.isNil;
36
+ exports.isNode = isWeakSet.isNode;
35
37
  exports.isNotNil = isWeakSet.isNotNil;
36
38
  exports.isNull = isWeakSet.isNull;
37
39
  exports.isPromise = isWeakSet.isPromise;
@@ -1,6 +1,7 @@
1
1
  export { isArrayBuffer } from './isArrayBuffer.mjs';
2
2
  export { isBlob } from './isBlob.mjs';
3
3
  export { isBoolean } from './isBoolean.mjs';
4
+ export { isBrowser } from './isBrowser.mjs';
4
5
  export { isBuffer } from './isBuffer.mjs';
5
6
  export { isDate } from './isDate.mjs';
6
7
  export { isEqual } from './isEqual.mjs';
@@ -13,6 +14,7 @@ export { isJSONArray, isJSONObject, isJSONValue } from './isJSONValue.mjs';
13
14
  export { isLength } from './isLength.mjs';
14
15
  export { isMap } from './isMap.mjs';
15
16
  export { isNil } from './isNil.mjs';
17
+ export { isNode } from './isNode.mjs';
16
18
  export { isNotNil } from './isNotNil.mjs';
17
19
  export { isNull } from './isNull.mjs';
18
20
  export { isPlainObject } from './isPlainObject.mjs';
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks if the current environment is a browser.
3
+ *
4
+ * This function checks for the existence of the `window.document` property,
5
+ * which only exists in browser environments.
6
+ *
7
+ * @returns {boolean} `true` if the current environment is a browser, otherwise `false`.
8
+ *
9
+ * @example
10
+ * if (isBrowser()) {
11
+ * console.log("This is running in a browser");
12
+ * document.getElementById('app').innerHTML = 'Hello World';
13
+ * }
14
+ */
15
+ declare function isBrowser(): boolean;
16
+
17
+ export { isBrowser };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks if the current environment is a browser.
3
+ *
4
+ * This function checks for the existence of the `window.document` property,
5
+ * which only exists in browser environments.
6
+ *
7
+ * @returns {boolean} `true` if the current environment is a browser, otherwise `false`.
8
+ *
9
+ * @example
10
+ * if (isBrowser()) {
11
+ * console.log("This is running in a browser");
12
+ * document.getElementById('app').innerHTML = 'Hello World';
13
+ * }
14
+ */
15
+ declare function isBrowser(): boolean;
16
+
17
+ export { isBrowser };
@@ -0,0 +1,5 @@
1
+ function isBrowser() {
2
+ return globalThis.window?.document != null;
3
+ }
4
+
5
+ export { isBrowser };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks if the current environment is Node.js.
3
+ *
4
+ * This function checks for the existence of the `process.versions.node` property,
5
+ * which only exists in Node.js environments.
6
+ *
7
+ * @returns {boolean} `true` if the current environment is Node.js, otherwise `false`.
8
+ *
9
+ * @example
10
+ *if (isNode()) {
11
+ * console.log('This is running in Node.js');
12
+ * const fs = import('node:fs');
13
+ * }
14
+ */
15
+ declare function isNode(): boolean;
16
+
17
+ export { isNode };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Checks if the current environment is Node.js.
3
+ *
4
+ * This function checks for the existence of the `process.versions.node` property,
5
+ * which only exists in Node.js environments.
6
+ *
7
+ * @returns {boolean} `true` if the current environment is Node.js, otherwise `false`.
8
+ *
9
+ * @example
10
+ *if (isNode()) {
11
+ * console.log('This is running in Node.js');
12
+ * const fs = import('node:fs');
13
+ * }
14
+ */
15
+ declare function isNode(): boolean;
16
+
17
+ export { isNode };
@@ -0,0 +1,5 @@
1
+ function isNode() {
2
+ return globalThis.process?.versions?.node != null;
3
+ }
4
+
5
+ export { isNode };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Attempt to execute a function and return the result or error.
3
+ * Returns a tuple where:
4
+ * - On success: [null, Result] - First element is null, second is the result
5
+ * - On error: [Error, null] - First element is the caught error, second is null
6
+ *
7
+ * @template {unknown} T - The type of the result of the function.
8
+ * @template {unknown} E - The type of the error that can be thrown by the function.
9
+ * @param {() => T} func - The function to execute.
10
+ * @returns {[null, T] | [E, null]} A tuple containing either [null, result] or [error, null].
11
+ *
12
+ * @example
13
+ * // Successful execution
14
+ * const [error, result] = attempt(() => 42);
15
+ * // [null, 42]
16
+ *
17
+ * // Failed execution
18
+ * const [error, result] = attempt(() => {
19
+ * throw new Error('Something went wrong');
20
+ * });
21
+ * // [Error, null]
22
+ *
23
+ * // With type parameter
24
+ * const [error, names] = attempt<string[]>(() => ['Alice', 'Bob']);
25
+ * // [null, ['Alice', 'Bob']]
26
+ *
27
+ * @note
28
+ * Important: This function is not suitable for async functions (functions that return a `Promise`).
29
+ * When passing an async function, it will return `[null, Promise<Result>]`, but won't catch any
30
+ * errors if the Promise is rejected later.
31
+ *
32
+ * For handling async functions, use the `attemptAsync` function instead:
33
+ * ```
34
+ * const [error, data] = await attemptAsync(async () => {
35
+ * const response = await fetch('https://api.example.com/data');
36
+ * return response.json();
37
+ * });
38
+ * ```
39
+ */
40
+ declare function attempt<T, E>(func: () => T): [null, T] | [E, null];
41
+
42
+ export { attempt };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Attempt to execute a function and return the result or error.
3
+ * Returns a tuple where:
4
+ * - On success: [null, Result] - First element is null, second is the result
5
+ * - On error: [Error, null] - First element is the caught error, second is null
6
+ *
7
+ * @template {unknown} T - The type of the result of the function.
8
+ * @template {unknown} E - The type of the error that can be thrown by the function.
9
+ * @param {() => T} func - The function to execute.
10
+ * @returns {[null, T] | [E, null]} A tuple containing either [null, result] or [error, null].
11
+ *
12
+ * @example
13
+ * // Successful execution
14
+ * const [error, result] = attempt(() => 42);
15
+ * // [null, 42]
16
+ *
17
+ * // Failed execution
18
+ * const [error, result] = attempt(() => {
19
+ * throw new Error('Something went wrong');
20
+ * });
21
+ * // [Error, null]
22
+ *
23
+ * // With type parameter
24
+ * const [error, names] = attempt<string[]>(() => ['Alice', 'Bob']);
25
+ * // [null, ['Alice', 'Bob']]
26
+ *
27
+ * @note
28
+ * Important: This function is not suitable for async functions (functions that return a `Promise`).
29
+ * When passing an async function, it will return `[null, Promise<Result>]`, but won't catch any
30
+ * errors if the Promise is rejected later.
31
+ *
32
+ * For handling async functions, use the `attemptAsync` function instead:
33
+ * ```
34
+ * const [error, data] = await attemptAsync(async () => {
35
+ * const response = await fetch('https://api.example.com/data');
36
+ * return response.json();
37
+ * });
38
+ * ```
39
+ */
40
+ declare function attempt<T, E>(func: () => T): [null, T] | [E, null];
41
+
42
+ export { attempt };
@@ -0,0 +1,10 @@
1
+ function attempt(func) {
2
+ try {
3
+ return [null, func()];
4
+ }
5
+ catch (error) {
6
+ return [error, null];
7
+ }
8
+ }
9
+
10
+ export { attempt };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Attempt to execute an async function and return the result or error.
3
+ * Returns a Promise that resolves to a tuple where:
4
+ * - On success: [null, Result] - First element is null, second is the result
5
+ * - On error: [Error, null] - First element is the caught error, second is null
6
+ *
7
+ * @template {unknown} T - The type of the result of the async function.
8
+ * @template {unknown} E - The type of the error that can be thrown by the async function.
9
+ * @param {() => Promise<T>} func - The async function to execute.
10
+ * @returns {Promise<[null, T] | [E, null]>} A Promise that resolves to a tuple containing either [null, result] or [error, null].
11
+ *
12
+ * @example
13
+ * // Successful execution
14
+ * const [error, data] = await attemptAsync(async () => {
15
+ * const response = await fetch('https://api.example.com/data');
16
+ * return response.json();
17
+ * });
18
+ * // If successful: [null, { ... data ... }]
19
+ *
20
+ * // Failed execution
21
+ * const [error, data] = await attemptAsync(async () => {
22
+ * throw new Error('Network error');
23
+ * });
24
+ * // [Error, null]
25
+ *
26
+ * // With type parameter
27
+ * const [error, users] = await attemptAsync<User[]>(async () => {
28
+ * const response = await fetch('https://api.example.com/users');
29
+ * return response.json();
30
+ * });
31
+ * // users is typed as User[]
32
+ */
33
+ declare function attemptAsync<T, E>(func: () => Promise<T>): Promise<[null, T] | [E, null]>;
34
+
35
+ export { attemptAsync };