Skip to content

Commit

Permalink
change mapById, groupById to match the writeup (omit undefined)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Feb 16, 2020
1 parent 3ade8d2 commit 67e135f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ array. The value is `undefined` if the property is not set.

Map the objects by a property value. Returns a hash mapping each value to the
first object whose `idName` is set to that value. Null and undefined objects are skipped.
Objects that do not have that property are skipped.
Id values should be strings or numbers. Returns the target object, which is `{}` by default.

var items = [{ id: 'a', v: 1 }, { id: 'b' }, { id: 'a', v: 2 }, { v: 3 }];
Expand All @@ -132,7 +133,7 @@ Id values should be strings or numbers. Returns the target object, which is `{}
### qibl.groupById( items, idName [,target] )

Similar to `mapById`, but group objects by property value into arrays. Returns a mapping
of ids to lists of objects.
of ids to lists of objects. Objects that do not have the `idName` property set are omitted.

var items = [{ id: 'a', v: 1 }, { id: 'b' }, { id: 'a', v: 2 }, { v: 3 }];
qibl.mapById(items, 'id')
Expand Down
1 change: 1 addition & 0 deletions qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ function _mapById( arrayOfObjects, idName, target, all ) {
var obj = arrayOfObjects[i];
if (obj == undefined) continue;
var key = obj[idName];
if (key === undefined) continue;
(!all) ? target[key] = obj : (target[key]) ? target[key].push(obj) : target[key] = new Array(obj);
}
return target;
Expand Down
14 changes: 7 additions & 7 deletions test-qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,15 +999,15 @@ module.exports = {
t.deepEqual(qibl.mapById([{a:1}, {a:2}], 'a'), {1: {a:1}, 2: {a:2}});
t.deepEqual(qibl.mapById([{a:1}, {a:2, b:2}], 'a'), {1: {a:1}, 2: {a:2, b:2}});

t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'a'), {1: {a:1}, 'undefined': {b:2}});
t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'b'), {2: {b:2}, 'undefined': {a:1}});
t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'c'), {'undefined': {b:2}});
t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'a'), {1: {a:1}});
t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'b'), {2: {b:2}});
t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'c'), {});

t.deepEqual(qibl.mapById([], 'a'), {});
t.deepEqual(qibl.mapById([,,,null,undefined,,,7,false,0,"string"], 'a'), {'undefined': "string"});
t.deepEqual(qibl.mapById([,,,null,undefined,,,7,false,0,"string"], 'a'), {});
t.deepEqual(qibl.mapById([,,{a:1}], 'a'), {1: {a:1}});

t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'a', {x:9}), {x:9, 1: {a:1}, 'undefined': {b:2}});
t.deepEqual(qibl.mapById([{a:1}, {b:2}], 'a', {x:9}), {x:9, 1: {a:1}});

t.done();
},
Expand All @@ -1016,8 +1016,8 @@ module.exports = {
'groupById': {
'should return arrays of objects': function(t) {
var a1 = {a:1}, a2 = {a:2}, b1 = {b:1};
t.deepEqual(qibl.groupById([a1, a2, a1, b1], 'a'), {1: [a1, a1], 2: [a2], 'undefined': [b1]});
t.deepEqual(qibl.groupById([a1, b1], 'c'), {'undefined': [a1, b1]});
t.deepEqual(qibl.groupById([a1, a2, a1, b1], 'a'), {1: [a1, a1], 2: [a2]});
t.deepEqual(qibl.groupById([a1, b1], 'c'), {});
t.done();
},
},
Expand Down

0 comments on commit 67e135f

Please sign in to comment.