es-toolkit 1.36.0-dev.1237 → 1.36.0-dev.1239

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.
@@ -15,9 +15,6 @@ function getPriority(a) {
15
15
  }
16
16
  const compareValues = (a, b, order) => {
17
17
  if (a !== b) {
18
- if (typeof a === 'string' && typeof b === 'string') {
19
- return order === 'desc' ? b.localeCompare(a) : a.localeCompare(b);
20
- }
21
18
  const aPriority = getPriority(a);
22
19
  const bPriority = getPriority(b);
23
20
  if (aPriority === bPriority && aPriority === 0) {
@@ -206,6 +206,7 @@ export { pick } from './object/pick.mjs';
206
206
  export { pickBy } from './object/pickBy.mjs';
207
207
  export { property } from './object/property.mjs';
208
208
  export { propertyOf } from './object/propertyOf.mjs';
209
+ export { result } from './object/result.mjs';
209
210
  export { set } from './object/set.mjs';
210
211
  export { toDefaulted } from './object/toDefaulted.mjs';
211
212
  export { toPairs } from './object/toPairs.mjs';
@@ -206,6 +206,7 @@ export { pick } from './object/pick.js';
206
206
  export { pickBy } from './object/pickBy.js';
207
207
  export { property } from './object/property.js';
208
208
  export { propertyOf } from './object/propertyOf.js';
209
+ export { result } from './object/result.js';
209
210
  export { set } from './object/set.js';
210
211
  export { toDefaulted } from './object/toDefaulted.js';
211
212
  export { toPairs } from './object/toPairs.js';
@@ -211,6 +211,7 @@ export { pick } from './object/pick.mjs';
211
211
  export { pickBy } from './object/pickBy.mjs';
212
212
  export { property } from './object/property.mjs';
213
213
  export { propertyOf } from './object/propertyOf.mjs';
214
+ export { result } from './object/result.mjs';
214
215
  export { set } from './object/set.mjs';
215
216
  export { toDefaulted } from './object/toDefaulted.mjs';
216
217
  export { toPairs } from './object/toPairs.mjs';
@@ -206,6 +206,7 @@ export { pick } from './object/pick.mjs';
206
206
  export { pickBy } from './object/pickBy.mjs';
207
207
  export { property } from './object/property.mjs';
208
208
  export { propertyOf } from './object/propertyOf.mjs';
209
+ export { result } from './object/result.mjs';
209
210
  export { set } from './object/set.mjs';
210
211
  export { toDefaulted } from './object/toDefaulted.mjs';
211
212
  export { toPairs } from './object/toPairs.mjs';
@@ -206,6 +206,7 @@ export { pick } from './object/pick.js';
206
206
  export { pickBy } from './object/pickBy.js';
207
207
  export { property } from './object/property.js';
208
208
  export { propertyOf } from './object/propertyOf.js';
209
+ export { result } from './object/result.js';
209
210
  export { set } from './object/set.js';
210
211
  export { toDefaulted } from './object/toDefaulted.js';
211
212
  export { toPairs } from './object/toPairs.js';
@@ -1107,9 +1107,6 @@ function getPriority(a) {
1107
1107
  }
1108
1108
  const compareValues = (a, b, order) => {
1109
1109
  if (a !== b) {
1110
- if (typeof a === 'string' && typeof b === 'string') {
1111
- return order === 'desc' ? b.localeCompare(a) : a.localeCompare(b);
1112
- }
1113
1110
  const aPriority = getPriority(a);
1114
1111
  const bPriority = getPriority(b);
1115
1112
  if (aPriority === bPriority && aPriority === 0) {
@@ -3309,6 +3306,22 @@ function propertyOf(object) {
3309
3306
  };
3310
3307
  }
3311
3308
 
3309
+ function result(object, path, defaultValue) {
3310
+ const resolvedPath = Array.isArray(path) ? path : isKey(path, object) ? [path] : toPath(String(path));
3311
+ const length = resolvedPath.length;
3312
+ for (let index = 0; index < (length || 1); index++) {
3313
+ const key = toKey(resolvedPath[index]);
3314
+ const value = object == null ? undefined : object[key];
3315
+ if (value === undefined) {
3316
+ object =
3317
+ typeof defaultValue === 'function' ? defaultValue.call(object) : defaultValue;
3318
+ break;
3319
+ }
3320
+ object = typeof value === 'function' ? value.call(object) : value;
3321
+ }
3322
+ return object;
3323
+ }
3324
+
3312
3325
  function toDefaulted(object, ...sources) {
3313
3326
  const cloned = cloneDeep(object);
3314
3327
  return defaults(cloned, ...sources);
@@ -4265,6 +4278,7 @@ const compat = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
4265
4278
  repeat,
4266
4279
  replace,
4267
4280
  rest,
4281
+ result,
4268
4282
  retry: unary.retry,
4269
4283
  reverse,
4270
4284
  reverseString: upperFirst$1.reverseString,
@@ -4609,6 +4623,7 @@ exports.remove = remove;
4609
4623
  exports.repeat = repeat;
4610
4624
  exports.replace = replace;
4611
4625
  exports.rest = rest;
4626
+ exports.result = result;
4612
4627
  exports.reverse = reverse;
4613
4628
  exports.round = round;
4614
4629
  exports.sample = sample;
@@ -211,6 +211,7 @@ export { pick } from './object/pick.mjs';
211
211
  export { pickBy } from './object/pickBy.mjs';
212
212
  export { property } from './object/property.mjs';
213
213
  export { propertyOf } from './object/propertyOf.mjs';
214
+ export { result } from './object/result.mjs';
214
215
  export { set } from './object/set.mjs';
215
216
  export { toDefaulted } from './object/toDefaulted.mjs';
216
217
  export { toPairs } from './object/toPairs.mjs';
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Retrieves the value at a given path of an object.
3
+ * If the resolved value is a function, it is invoked with the object as its `this` context.
4
+ * If the value is `undefined`, the `defaultValue` is returned.
5
+ *
6
+ * @template T - The expected return type.
7
+ * @param {unknown} object - The object to query.
8
+ * @param {string | number | symbol | ReadonlyArray<string | number | symbol>} path - The path of the property to get.
9
+ * @param {T | ((...args: unknown[]) => T)} [defaultValue] - The value returned if the resolved value is `undefined`.
10
+ * @returns {T} - Returns the resolved value.
11
+ *
12
+ * @example
13
+ * const obj = { a: { b: { c: 3 } } };
14
+ * result(obj, 'a.b.c');
15
+ * // => 3
16
+ *
17
+ * @example
18
+ * const obj = { a: () => 5 };
19
+ * result(obj, 'a');
20
+ * // => 5 (calls the function `a` and returns its result)
21
+ *
22
+ * @example
23
+ * const obj = { a: { b: null } };
24
+ * result(obj, 'a.b.c', 'default');
25
+ * // => 'default'
26
+ *
27
+ * @example
28
+ * const obj = { a: { b: { c: 3 } } };
29
+ * result(obj, 'a.b.d', () => 'default');
30
+ * // => 'default'
31
+ */
32
+ declare function result<T>(object: unknown, path: string | number | symbol | ReadonlyArray<string | number | symbol>, defaultValue?: T | ((...args: unknown[]) => T)): T;
33
+
34
+ export { result };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Retrieves the value at a given path of an object.
3
+ * If the resolved value is a function, it is invoked with the object as its `this` context.
4
+ * If the value is `undefined`, the `defaultValue` is returned.
5
+ *
6
+ * @template T - The expected return type.
7
+ * @param {unknown} object - The object to query.
8
+ * @param {string | number | symbol | ReadonlyArray<string | number | symbol>} path - The path of the property to get.
9
+ * @param {T | ((...args: unknown[]) => T)} [defaultValue] - The value returned if the resolved value is `undefined`.
10
+ * @returns {T} - Returns the resolved value.
11
+ *
12
+ * @example
13
+ * const obj = { a: { b: { c: 3 } } };
14
+ * result(obj, 'a.b.c');
15
+ * // => 3
16
+ *
17
+ * @example
18
+ * const obj = { a: () => 5 };
19
+ * result(obj, 'a');
20
+ * // => 5 (calls the function `a` and returns its result)
21
+ *
22
+ * @example
23
+ * const obj = { a: { b: null } };
24
+ * result(obj, 'a.b.c', 'default');
25
+ * // => 'default'
26
+ *
27
+ * @example
28
+ * const obj = { a: { b: { c: 3 } } };
29
+ * result(obj, 'a.b.d', () => 'default');
30
+ * // => 'default'
31
+ */
32
+ declare function result<T>(object: unknown, path: string | number | symbol | ReadonlyArray<string | number | symbol>, defaultValue?: T | ((...args: unknown[]) => T)): T;
33
+
34
+ export { result };
@@ -0,0 +1,21 @@
1
+ import { isKey } from '../_internal/isKey.mjs';
2
+ import { toKey } from '../_internal/toKey.mjs';
3
+ import { toPath } from '../util/toPath.mjs';
4
+
5
+ function result(object, path, defaultValue) {
6
+ const resolvedPath = Array.isArray(path) ? path : isKey(path, object) ? [path] : toPath(String(path));
7
+ const length = resolvedPath.length;
8
+ for (let index = 0; index < (length || 1); index++) {
9
+ const key = toKey(resolvedPath[index]);
10
+ const value = object == null ? undefined : object[key];
11
+ if (value === undefined) {
12
+ object =
13
+ typeof defaultValue === 'function' ? defaultValue.call(object) : defaultValue;
14
+ break;
15
+ }
16
+ object = typeof value === 'function' ? value.call(object) : value;
17
+ }
18
+ return object;
19
+ }
20
+
21
+ export { result };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.36.0-dev.1237+cf65b69c",
4
+ "version": "1.36.0-dev.1239+0a3da79d",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {