Skip to content

Commit 5f10f58

Browse files
authored
Merge pull request #1795 from simlu/dev
fix: handling sparse arrays
2 parents 185ffd9 + c08541a commit 5f10f58

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

README.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![NPM](https://img.shields.io/npm/v/object-scan.svg)](https://www.npmjs.com/package/object-scan)
55
[![Downloads](https://img.shields.io/npm/dt/object-scan.svg)](https://www.npmjs.com/package/object-scan)
66
[![Size](https://shields.io/badge/min%20+%20gz-5.00%20KB-informational)](https://bundlephobia.com/package/object-scan)
7-
[![Test Ratio](https://shields.io/badge/test%20:%20code-9.9%20:%201-informational)](./test/readme/replace-variables/ratio-badge.js)
7+
[![Test Ratio](https://shields.io/badge/test%20:%20code-9.8%20:%201-informational)](./test/readme/replace-variables/ratio-badge.js)
88

99
Traverse object hierarchies using matching and callbacks.
1010

@@ -75,7 +75,8 @@ objectScan(['a.*.f'], { joined: true })(haystack);
7575
   <a href="#81-traversal-order"><img alt="Traversal Order" src="https://shields.io/badge/8.1.-Traversal%20Order-c96c01?style=flat-square"></a><br>
7676
   <a href="#82-empty-string"><img alt="Empty String" src="https://shields.io/badge/8.2.-Empty%20String-c96c01?style=flat-square"></a><br>
7777
   <a href="#83-array-string-keys"><img alt="Array String Keys" src="https://shields.io/badge/8.3.-Array%20String%20Keys-c96c01?style=flat-square"></a><br>
78-
   <a href="#84-internals"><img alt="Internals" src="https://shields.io/badge/8.4.-Internals-c96c01?style=flat-square"></a></details>
78+
   <a href="#84-sparse-arrays"><img alt="Sparse Arrays" src="https://shields.io/badge/8.4.-Sparse%20Arrays-c96c01?style=flat-square"></a><br>
79+
   <a href="#85-internals"><img alt="Internals" src="https://shields.io/badge/8.5.-Internals-c96c01?style=flat-square"></a></details>
7980

8081
## 3. Features
8182

@@ -1594,7 +1595,24 @@ objectScan(['**'], {
15941595
```
15951596
</details>
15961597

1597-
### 8.4. Internals
1598+
### 8.4. Sparse Arrays
1599+
1600+
Only set keys are traversed for spare Arrays.
1601+
1602+
<details><summary> <code>['**']</code> <em>(empty entries skipped)</em> </summary>
1603+
1604+
<!-- eslint-disable no-undef -->
1605+
```js
1606+
const haystack = (() => { const r = []; r[1] = 'a'; return r; })();
1607+
objectScan(['**'], {
1608+
joined: true,
1609+
rtn: 'entry'
1610+
})(haystack);
1611+
// => [ [ '[1]', 'a' ] ]
1612+
```
1613+
</details>
1614+
1615+
### 8.5. Internals
15981616

15991617
This library has been designed around performance as a core feature.
16001618

@@ -1611,7 +1629,7 @@ Conceptually this package works as follows:
16111629

16121630
1. During initialization the needles are parsed and built into a search tree.
16131631
Various information is pre-computed and stored for every node.
1614-
Finally the search function is returned.
1632+
Finally, the search function is returned.
16151633

16161634
2. When the search function is invoked, the input is traversed simultaneously with
16171635
the relevant nodes of the search tree. Processing multiple search tree branches

src/core/find.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ export default (haystack_, search_, ctx) => {
152152
&& haystack instanceof Object
153153
) {
154154
const isArray = Array.isArray(haystack);
155-
const keys = isArray ? haystack.map((_, i) => i) : Object.keys(haystack);
155+
const keys = isArray
156+
? haystack.map((_, i) => i).filter(() => true)
157+
: Object.keys(haystack);
156158
if (!isArray && ctx.compareFn) {
157159
keys.sort(ctx.compareFn(kwargs));
158160
}

test/index.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1052,4 +1052,15 @@ describe('Testing Find', () => {
10521052
})(data);
10531053
expect(r).to.deep.equal([[[1], 'b'], [[0], 'a']]);
10541054
});
1055+
1056+
it('Testing sparse array keys', () => {
1057+
const data = [];
1058+
data[1] = 'a';
1059+
data[2] = undefined;
1060+
const r = objectScan(['**'], {
1061+
joined: false,
1062+
rtn: 'entry'
1063+
})(data);
1064+
expect(r).to.deep.equal([[[2], undefined], [[1], 'a']]);
1065+
});
10551066
});

test/readme/README.template.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,17 @@ rtn: 'entry'
11181118
comment: str key skipped
11191119
</example></pre>
11201120

1121+
### Sparse Arrays
1122+
1123+
Only set keys are traversed for spare Arrays.
1124+
1125+
<pre><example>
1126+
haystack: (() => { const r = []; r[1] = 'a'; return r; })()
1127+
needles: ['**']
1128+
rtn: 'entry'
1129+
comment: empty entries skipped
1130+
</example></pre>
1131+
11211132
### Internals
11221133

11231134
This library has been designed around performance as a core feature.
@@ -1135,7 +1146,7 @@ Conceptually this package works as follows:
11351146

11361147
1. During initialization the needles are parsed and built into a search tree.
11371148
Various information is pre-computed and stored for every node.
1138-
Finally the search function is returned.
1149+
Finally, the search function is returned.
11391150

11401151
2. When the search function is invoked, the input is traversed simultaneously with
11411152
the relevant nodes of the search tree. Processing multiple search tree branches

0 commit comments

Comments
 (0)