es-toolkit 1.35.0-dev.1184 → 1.35.0-dev.1185

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.
@@ -190,6 +190,7 @@ export { functions } from './object/functions.mjs';
190
190
  export { functionsIn } from './object/functionsIn.mjs';
191
191
  export { get } from './object/get.mjs';
192
192
  export { has } from './object/has.mjs';
193
+ export { hasIn } from './object/hasIn.mjs';
193
194
  export { invertBy } from './object/invertBy.mjs';
194
195
  export { keys } from './object/keys.mjs';
195
196
  export { keysIn } from './object/keysIn.mjs';
@@ -190,6 +190,7 @@ export { functions } from './object/functions.js';
190
190
  export { functionsIn } from './object/functionsIn.js';
191
191
  export { get } from './object/get.js';
192
192
  export { has } from './object/has.js';
193
+ export { hasIn } from './object/hasIn.js';
193
194
  export { invertBy } from './object/invertBy.js';
194
195
  export { keys } from './object/keys.js';
195
196
  export { keysIn } from './object/keysIn.js';
@@ -2726,6 +2726,34 @@ function functionsIn(object) {
2726
2726
  return result;
2727
2727
  }
2728
2728
 
2729
+ function hasIn(object, path) {
2730
+ let resolvedPath;
2731
+ if (Array.isArray(path)) {
2732
+ resolvedPath = path;
2733
+ }
2734
+ else if (typeof path === 'string' && isDeepKey(path) && object?.[path] == null) {
2735
+ resolvedPath = toPath(path);
2736
+ }
2737
+ else {
2738
+ resolvedPath = [path];
2739
+ }
2740
+ if (resolvedPath.length === 0) {
2741
+ return false;
2742
+ }
2743
+ let current = object;
2744
+ for (let i = 0; i < resolvedPath.length; i++) {
2745
+ const key = resolvedPath[i];
2746
+ if (current == null || !(key in Object(current))) {
2747
+ const isSparseIndex = (Array.isArray(current) || isArguments(current)) && isIndex(key) && key < current.length;
2748
+ if (!isSparseIndex) {
2749
+ return false;
2750
+ }
2751
+ }
2752
+ current = current[key];
2753
+ }
2754
+ return true;
2755
+ }
2756
+
2729
2757
  function invertBy(object, iteratee) {
2730
2758
  const result = {};
2731
2759
  if (isPromise.isNil(object)) {
@@ -3803,6 +3831,7 @@ exports.get = get;
3803
3831
  exports.gt = gt;
3804
3832
  exports.gte = gte;
3805
3833
  exports.has = has;
3834
+ exports.hasIn = hasIn;
3806
3835
  exports.head = head;
3807
3836
  exports.inRange = inRange;
3808
3837
  exports.includes = includes;
@@ -192,6 +192,7 @@ export { functions } from './object/functions.mjs';
192
192
  export { functionsIn } from './object/functionsIn.mjs';
193
193
  export { get } from './object/get.mjs';
194
194
  export { has } from './object/has.mjs';
195
+ export { hasIn } from './object/hasIn.mjs';
195
196
  export { invertBy } from './object/invertBy.mjs';
196
197
  export { keys } from './object/keys.mjs';
197
198
  export { keysIn } from './object/keysIn.mjs';
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Checks if a given path exists in an object, **including inherited properties**.
3
+ *
4
+ * You can provide the path as a single property key, an array of property keys,
5
+ * or a string representing a deep path.
6
+ *
7
+ * Unlike `has`, which only checks for own properties, `hasIn` also checks for properties
8
+ * in the prototype chain.
9
+ *
10
+ * If the path is an index and the object is an array or an arguments object, the function will verify
11
+ * if the index is valid and within the bounds of the array or arguments object, even if the array or
12
+ * arguments object is sparse (i.e., not all indexes are defined).
13
+ *
14
+ * @param {object} object - The object to query.
15
+ * @param {PropertyKey | PropertyKey[]} path - The path to check. This can be a single property key,
16
+ * an array of property keys, or a string representing a deep path.
17
+ * @returns {boolean} Returns `true` if the path exists (own or inherited), `false` otherwise.
18
+ *
19
+ * @example
20
+ *
21
+ * const obj = { a: { b: { c: 3 } } };
22
+ *
23
+ * hasIn(obj, 'a'); // true
24
+ * hasIn(obj, ['a', 'b']); // true
25
+ * hasIn(obj, ['a', 'b', 'c']); // true
26
+ * hasIn(obj, 'a.b.c'); // true
27
+ * hasIn(obj, 'a.b.d'); // false
28
+ * hasIn(obj, ['a', 'b', 'c', 'd']); // false
29
+ *
30
+ * // Example with inherited properties:
31
+ * function Rectangle() {}
32
+ * Rectangle.prototype.area = function() {};
33
+ *
34
+ * const rect = new Rectangle();
35
+ * hasIn(rect, 'area'); // true
36
+ * has(rect, 'area'); // false - has only checks own properties
37
+ */
38
+ declare function hasIn(object: unknown, path: PropertyKey | readonly PropertyKey[]): boolean;
39
+
40
+ export { hasIn };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Checks if a given path exists in an object, **including inherited properties**.
3
+ *
4
+ * You can provide the path as a single property key, an array of property keys,
5
+ * or a string representing a deep path.
6
+ *
7
+ * Unlike `has`, which only checks for own properties, `hasIn` also checks for properties
8
+ * in the prototype chain.
9
+ *
10
+ * If the path is an index and the object is an array or an arguments object, the function will verify
11
+ * if the index is valid and within the bounds of the array or arguments object, even if the array or
12
+ * arguments object is sparse (i.e., not all indexes are defined).
13
+ *
14
+ * @param {object} object - The object to query.
15
+ * @param {PropertyKey | PropertyKey[]} path - The path to check. This can be a single property key,
16
+ * an array of property keys, or a string representing a deep path.
17
+ * @returns {boolean} Returns `true` if the path exists (own or inherited), `false` otherwise.
18
+ *
19
+ * @example
20
+ *
21
+ * const obj = { a: { b: { c: 3 } } };
22
+ *
23
+ * hasIn(obj, 'a'); // true
24
+ * hasIn(obj, ['a', 'b']); // true
25
+ * hasIn(obj, ['a', 'b', 'c']); // true
26
+ * hasIn(obj, 'a.b.c'); // true
27
+ * hasIn(obj, 'a.b.d'); // false
28
+ * hasIn(obj, ['a', 'b', 'c', 'd']); // false
29
+ *
30
+ * // Example with inherited properties:
31
+ * function Rectangle() {}
32
+ * Rectangle.prototype.area = function() {};
33
+ *
34
+ * const rect = new Rectangle();
35
+ * hasIn(rect, 'area'); // true
36
+ * has(rect, 'area'); // false - has only checks own properties
37
+ */
38
+ declare function hasIn(object: unknown, path: PropertyKey | readonly PropertyKey[]): boolean;
39
+
40
+ export { hasIn };
@@ -0,0 +1,34 @@
1
+ import { isDeepKey } from '../_internal/isDeepKey.mjs';
2
+ import { isIndex } from '../_internal/isIndex.mjs';
3
+ import { isArguments } from '../predicate/isArguments.mjs';
4
+ import { toPath } from '../util/toPath.mjs';
5
+
6
+ function hasIn(object, path) {
7
+ let resolvedPath;
8
+ if (Array.isArray(path)) {
9
+ resolvedPath = path;
10
+ }
11
+ else if (typeof path === 'string' && isDeepKey(path) && object?.[path] == null) {
12
+ resolvedPath = toPath(path);
13
+ }
14
+ else {
15
+ resolvedPath = [path];
16
+ }
17
+ if (resolvedPath.length === 0) {
18
+ return false;
19
+ }
20
+ let current = object;
21
+ for (let i = 0; i < resolvedPath.length; i++) {
22
+ const key = resolvedPath[i];
23
+ if (current == null || !(key in Object(current))) {
24
+ const isSparseIndex = (Array.isArray(current) || isArguments(current)) && isIndex(key) && key < current.length;
25
+ if (!isSparseIndex) {
26
+ return false;
27
+ }
28
+ }
29
+ current = current[key];
30
+ }
31
+ return true;
32
+ }
33
+
34
+ export { hasIn };
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.35.0-dev.1184+470acc29",
4
+ "version": "1.35.0-dev.1185+b4f5e529",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {