Skip to content

Commit

Permalink
forOwn should not call the callback for keys that are deleted in a pr…
Browse files Browse the repository at this point in the history
…evious callback
  • Loading branch information
DarrenPaulWright committed Feb 14, 2019
1 parent 4223d96 commit 4dd0395
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/forOwn.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
*
* @returns {Boolean} true if the callback function returns a truthy value for any key; otherwise, false.
*/
export default (object, callback) => !(!object || !Object.entries(object).some((data) => callback(data[1], data[0])));
export default (object, callback) => !(!object || !Object.keys(object)
.some((key) => key in object ? callback(object[key], key) : false));
25 changes: 25 additions & 0 deletions tests/forOwn.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ describe('forOwn', () => {
assert.isTrue(isCanceled);
});

it('should NOT call the callback for keys that are deleted in a previous callback', () => {
let total = 0;
let testVar = 0;
const object = {
key1: 'something1',
key2: 'something2',
key3: 'something3'
};

const isCanceled = forOwn(object, (value, key) => {
total++;
if (key === 'key1' && value === 'something1') {
testVar++;
}
if (key === 'key2' && value === 'something2') {
testVar++;
delete object.key3;
}
});

assert.equal(total, 2);
assert.equal(testVar, 2);
assert.isFalse(isCanceled);
});

it('should not call the callback for inherited properties', () => {
let total = 0;
let testVar = 0;
Expand Down

0 comments on commit 4dd0395

Please sign in to comment.