Skip to content

Commit

Permalink
Fix bracket notation and pick() method when used with a function to…
Browse files Browse the repository at this point in the history
… apply `bool()` to the result of the function
  • Loading branch information
lahmatiy committed Nov 27, 2023
1 parent 6cf0e31 commit 3c2e15f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## next

- Fixed a query compilation error when `$` was used as an object entry, e.g. `{ $, prop: 1 }`
- Fixed bracket notation and `pick()` method when used with a function to apply `bool()` to the result of the function

## 1.0.0-beta.9 (October 29, 2023)

Expand Down
4 changes: 2 additions & 2 deletions src/lang/compile-buildin.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ function pick(current, ref = () => true) {
if (typeof ref === 'function') {
if (Array.isArray(current) || typeof current === 'string') {
for (let i = 0; i < current.length; i++) {
if (ref(current[i], i)) {
if (isTruthy(ref(current[i], i))) {
return current[i];
}
}
}

for (const key in current) {
if (hasOwn(current, key)) {
if (ref(current[key], key)) {
if (isTruthy(ref(current[key], key))) {
return current[key];
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/methods/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,27 @@ describe('pick()', () => {
undefined
);
});

it('should treat empty arrays as false', () => {
assert.deepStrictEqual(
query('pick(=> foo)')([{ foo: [] }, { foo: [1] }]),
{ foo: [1] }
);
assert.deepStrictEqual(
query('pick(=> foo)')({ a: { foo: [] }, b: { foo: [1] } }),
{ foo: [1] }
);
});
it('should treat empty objects as false', () => {
assert.deepStrictEqual(
query('pick(=> foo)')([{ foo: {} }, { foo: { ok: 1 } }]),
{ foo: { ok: 1 } }
);
assert.deepStrictEqual(
query('pick(=> foo)')({ a: { foo: {} }, b: { foo: { ok: 1 } } }),
{ foo: { ok: 1 } }
);
});
});

describe('special cases', () => {
Expand Down

0 comments on commit 3c2e15f

Please sign in to comment.