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

Abstract _.pick and _.omit via pick wrapper #1639

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
Abstract _.pick and _.omit via pick wrapper
Use a pickerCreator function to create omit and pick
  • Loading branch information
gyeates committed May 20, 2014
commit e72b42692c3c8ecc41baa04cc383fbaac63eb995
50 changes: 23 additions & 27 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,37 +888,33 @@
return obj;
};

// Return a copy of the object only containing the whitelisted properties.
_.pick = function(obj, iterator, context) {
var result = {}, key;
if (_.isFunction(iterator)) {
for (key in obj) {
var value = obj[key];
if (iterator.call(context, value, key, obj)) result[key] = value;
}
} else {
var keys = concat.apply([], slice.call(arguments, 1));
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key in obj) result[key] = obj[key];
// internal wrapper around a key picker
var createPicker = function(negateResult) {
return function(obj, iterator, context) {
var result = {},
state, key;
if (_.isFunction(iterator)) {
iterator = createCallback(iterator, context);
for (key in obj) {
state = iterator(obj[key], key, obj);
if (negateResult ? !state : state) result[key] = obj[key];
}
} else {
var keys = _.invert(concat.apply([], slice.call(arguments, 1)));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me know if the old way is preferred

Old way:

keys = _.map(concat.apply([], slice.call(arguments, 1)), String);
iterator = function(value, key) { return !_.contains(keys, key); };

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

http://jsperf.com/invert-vs-map which should be used for this case?

for (key in obj) {
state = key in keys;
if (negateResult ? !state : state) result[key] = obj[key];
}
}
}
return result;
return result;
};
};

// Return a copy of the object only containing the whitelisted properties.
_.pick = createPicker(false);

// Return a copy of the object without the blacklisted properties.
_.omit = function(obj, iterator, context) {
var keys;
if (_.isFunction(iterator)) {
iterator = _.negate(iterator);
} else {
keys = _.map(concat.apply([], slice.call(arguments, 1)), String);
iterator = function(value, key) {
return !_.contains(keys, key);
};
}
return _.pick(obj, iterator, context);
};
_.omit = createPicker(true);

// Fill in a given object with default properties.
_.defaults = function(obj) {
Expand Down