Skip to content

Commit

Permalink
fix extractTo to not transfer properties that are set to undefined in…
Browse files Browse the repository at this point in the history
… the mask
  • Loading branch information
andrasq committed Nov 12, 2022
1 parent caf769f commit 1e51133
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,16 @@ returns `target`.

### qibl.extractTo( target, source, mask )

Assign to `target` properties of `source` that occur in `mask`. Assigns `undefined` if the
property is not set on `source`.
Assign to `target` properties of `source` that are set in `mask`.
A mask property set to `undefined` is treated as unset. Assigns `undefined` if the
property is not set on `source`. The assignment is recursive, nested properties are assigned
under the control of the same-named nested properties in the `mask`. Returns the `target`.

qibl.extractTo({ a: 1 }, { a: 11, b: 22, c: 33 }, { b: undefined, d: 4 });
// => { a: 1, b: 22, d: undefined }
qibl.extractTo({ a:1 }, { a:111, b:222, c:333 }, { b: undefined, c: null, d: 4 });
// => { a: 1, c: 333, d: undefined }

qibl.extractTo({ a: { b:2, c:3 } }, { a: { c:333, d:444 } }, { a: { c: 'yes' } });
// => { a: { b:2, c:333 } }

### qibl.extractNotTo( target, source, mask )

Expand Down Expand Up @@ -1159,7 +1164,8 @@ elapsed times as the values.
Changelog
---------

- 1.22.0 - new `removeByIndex`, new `str_reverse`, new `remove2`, faster `concat2`, new `extractNotTo`
- 1.22.0 - new `removeByIndex`, new `str_reverse`, new `remove2`, faster `concat2`, new `extractNotTo`,
fix `extractTo` to not copy the property if mask is set to `undefined`
- 1.21.2 - new preliminary `str_count`, prune search tree for much faster `globdir`, allow duplicate calls
to makeIteratorPeekable, fix str_count to not infinite loop on zero-length patterns,
recognize `mergeTo` as meaning `merge`, fix mergeTo to ensure hash when nesting properties
Expand Down
4 changes: 3 additions & 1 deletion qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1704,9 +1704,11 @@ function flipTo( target, item ) {
}

// based on minisql utils:
// copy onto dst all properties of src that are present in mask
// copy onto dst all properties of src that are set in mask
// MAYBE: do not transfer properties unset in src?
function extractTo( dst, src, mask ) {
for (var k in mask) {
if (mask[k] === undefined) continue;
dst[k] = isHash(mask[k]) && isHash(src[k]) ? extractTo(isHash(dst[k]) ? dst[k] : {}, src[k], mask[k]) : src[k];
}
return dst;
Expand Down
5 changes: 5 additions & 0 deletions test-qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3720,6 +3720,11 @@ module.exports = {
t.deepEqual(qibl.extractTo({}, {a: 1, b: {c: 2}}, {b: {x: true}}), {b: {x: undefined}});
t.deepEqual(qibl.extractTo({}, {a: 1, b: {c: 2}}, {b: {x: {y: true}}}), {b: {x: undefined}});

// mask property is undefined
// TODO: pending fix to extractTo:
t.deepEqual(qibl.extractTo({}, {a:1, b:2}, {a:undefined, b:true}), {b:2});
t.deepEqual(qibl.extractTo({}, {x:{a:1, b:2}}, {x:{a:undefined, b:true}}), {x:{b:2}});

t.done();
},
},
Expand Down

0 comments on commit 1e51133

Please sign in to comment.