Skip to content

Commit

Permalink
Merge pull request #1 from fp-dom/regexp-pick-refactor
Browse files Browse the repository at this point in the history
refactoring regexp-pick
  • Loading branch information
tristaaan committed Feb 10, 2015
2 parents f4c95ac + e5affd4 commit eec0d12
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,30 @@ module.exports = function () {

function pick (obj, args) {
var keys = [];
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);
}
args.forEach(function (key) {
keys = keys.concat(key);
});
var 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));
});
}
keys.forEach(copy(obj, out));
return out;
}

function copyWithString (from, to) {
function copy (from, to) {
return function (key) {
if (key in from) {
to[key] = from[key];
if (isRegExp(key)) {
Object.keys(from).forEach(function(keyFrom) {
if (key.test(keyFrom)) {
to[keyFrom] = from[keyFrom];
}
});
} else {
if (key in from) {
to[key] = from[key];
}
}
};
}

function copyWithRegExp (from, to, regexp) {
return function (key) {
if (regexp.test(key)) {
to[key] = from[key];
}
};
}
function isRegExp (obj) {

This comment has been minimized.

Copy link
@tjmehta

tjmehta Feb 11, 2015

make this a 101/is-regexp?

return Object.prototype.toString.call(obj) == '[object RegExp]';
}

0 comments on commit eec0d12

Please sign in to comment.