From 3c2e15fa6c8b771c573a0772bcbaff4f401471d7 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Mon, 27 Nov 2023 03:49:47 +0100 Subject: [PATCH] Fix bracket notation and `pick()` method when used with a function to apply `bool()` to the result of the function --- CHANGELOG.md | 1 + src/lang/compile-buildin.js | 4 ++-- test/methods/pick.js | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 827f23c..3bb3d7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/lang/compile-buildin.js b/src/lang/compile-buildin.js index cba2918..8274b6e 100644 --- a/src/lang/compile-buildin.js +++ b/src/lang/compile-buildin.js @@ -135,7 +135,7 @@ 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]; } } @@ -143,7 +143,7 @@ function pick(current, ref = () => true) { for (const key in current) { if (hasOwn(current, key)) { - if (ref(current[key], key)) { + if (isTruthy(ref(current[key], key))) { return current[key]; } } diff --git a/test/methods/pick.js b/test/methods/pick.js index 667c538..31a5397 100644 --- a/test/methods/pick.js +++ b/test/methods/pick.js @@ -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', () => {