Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegExp pick #55

Merged
merged 8 commits into from
Feb 15, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ var passAny = require('101/pass-any');
## pick

Returns a new object with the specified keys (with key values from obj).
Supports partial functionality (great with array functions, like map).
Supports regular expressions and partial functionality (great with array functions, like map).

```js
var pick = require('101/pick');
Expand All @@ -402,6 +402,7 @@ var obj = {
};

pick(obj, 'foo'); // { foo: 1 }
pick(obj, RegExp('oo$')); // { foo: 1 }
pick(obj, ['foo']); // { foo: 1 }
pick(obj, ['foo', 'bar']); // { foo: 1, bar: 2 }

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "common javascript utils that can be required selectively that assume es5+",
"main": "index.js",
"scripts": {
"test": "lab -c test",
"test": "lab -c -t 100 test",
"test-watch": "nodemon --exec lab -c test"
},
"repository": {
Expand Down
41 changes: 36 additions & 5 deletions pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var isObject = require('./is-object');
* When only keys are specified pick returns a partial-function which accepts obj.
* @function module:101/pick
* @param {object} [obj] - object whose keys are picked
* @param {string|array} keys... - keys which will be taken from obj (can be specifieds as args (strings and/or arrays)
* @param {string|regexp|array} keys... - keys which will be taken from obj, can be specifieds as args (strings, regular epxressions, and/or arrays)
* @return {object|function} Object with only the keys specified from the original obj or Partial-function pick (which accepts obj) and returns an object
*/
module.exports = function () {
Expand All @@ -27,18 +27,49 @@ module.exports = function () {

function pick (obj, args) {
var keys = [];
args.forEach(function (key) {
keys = keys.concat(key);
var regexps = [];
function concatElement(el) {
if (el instanceof RegExp) {
regexps = regexps.concat(el);
}
else {
keys = keys.concat(el);
}
}
args.forEach(function (element) {
if (Array.isArray(element)) {
element.forEach(function(subelement){
concatElement(subelement);
});
}
else {
concatElement(element);
}
});
var out = {};
keys.forEach(copy(obj, out));
if (keys.length > 0) {
keys.forEach(copyWithString(obj, out));
}
if (regexps.length > 0) {
regexps.forEach(function(regexp){
Object.keys(obj).forEach(copyWithRegExp(obj, out, regexp));
});
}
return out;
}

function copy (from, to) {
function copyWithString (from, to) {
return function (key) {
if (key in from) {
to[key] = from[key];
}
};
}

function copyWithRegExp (from, to, regexp) {
return function (key) {
if (regexp.test(key) && !(key in to)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pick should always override to (it should not work like 101/default)

to[key] = from[key];
}
};
}
90 changes: 90 additions & 0 deletions test/test-pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ describe('pick', function () {
});
expect(pick(obj)).to.eql({});
expect(pick(obj, [])).to.eql({});
expect(pick(obj, RegExp('a'))).to.eql({
bar: 1
});
expect(pick(obj, RegExp('a'), 'foo')).to.eql({
foo: 1,
bar: 1
});
expect(pick(obj, [RegExp('q|b')], 'bar')).to.eql({
bar: 1,
qux: 1
});
expect(pick(obj, [RegExp('q'), RegExp('f')], ['bar'])).to.eql({
foo: 1,
bar: 1,
qux: 1
});
expect(pick(obj, [RegExp('q'), 'foo'], 'bar')).to.eql({
foo: 1,
bar: 1,
qux: 1
});
expect(pick(obj, [RegExp('x$'), 'foo'], [RegExp('b')])).to.eql({
foo: 1,
bar: 1,
qux: 1
});
done();
});
it('should pick keys from objects in an array when used with map', function(done) {
Expand Down Expand Up @@ -135,6 +161,70 @@ describe('pick', function () {
bar: 3
}
]);
expect(objs.map(pick(RegExp('q|g')))).to.eql([
{},
{
qux: 2
},
{
goo: 3
}
]);
expect(objs.map(pick(RegExp('BAR', 'i'), 'foo'))).to.eql([
{
bar: 1
},
{
foo: 2,
bar: 2
},
{
foo: 3,
bar: 3
}
]);
expect(objs.map(pick([RegExp('b')], 'foo'))).to.eql([
{
bar: 1
},
{
foo: 2,
bar: 2
},
{
foo: 3,
bar: 3
}
]);
expect(objs.map(pick([RegExp('b'), 'qux'], ['foo']))).to.eql([
{
bar: 1
},
{
bar: 2,
foo: 2,
qux: 2
},
{
foo: 3,
bar: 3
}
]);
expect(objs.map(pick([RegExp('b'), RegExp('^f')], [RegExp('oo$')]))).to.eql([
{
bar: 1
},
{
bar: 2,
foo: 2
},
{
bar: 3,
foo: 3,
koo: 3,
goo: 3
}
]);
expect(objs.map(pick())).to.eql([
{}, {}, {}
]);
Expand Down