1.3.7

Back | Minified | Changelog | Cautionlog
2012-11-29

select/reject

Analogous to Underscore's Object.pick and Object.omit, Sugar now has Object.select and Object.reject. These methods do pretty much what you would expect from the names -- returning a new object that contains properties that match/do not match the keys passed -- but have some special hidden features. Multiple keys may be specified through enumerated arguments, or also by passing an array of keys. Additionally, regexes will match against the key. And finally, passing another object will result in a match if that object also has the given key. This in effect will produce something like an "intersect" operation in the case of select, and a "subtract" operation in the case of reject. Some examples:

// Returns an object with the single member "user" Object.select(obj, 'user');
// Returns an object with all members except "user" Object.reject(obj, 'user');
// Returns an object with the members "user" or "count" Object.select(obj, 'user','count');
// Returns an object with all members except "user" and "count" Object.reject(obj, 'user','count');
// Returns an object with the members "user" or "count" Object.select(obj, ['user','count']);
// Returns an object with any members that match /^user/ Object.select(obj, /^user/);
// Returns an object with any members that do not match /^user/ Object.reject(obj, /^user/);
// Returns an object with members whose keys match those in obj2 Object.select(obj, obj2);
// Returns an object with all members except those in obj2 Object.reject(obj, obj2);

As "hash" methods, select and reject are most powerful when used in extended objects (Sugar's answer to hashes):

var obj = Object.extended(obj); obj.select('user'); obj.select('user', 'count'); obj.select(['user', 'count']);

Other changes

String#startsWith and String#endsWith were modified to allow a second argument that is the position from which startsWith or to which endsWith the operation will be performed on. This is to bring in line these methods with those of the Harmony proposals of the same name. The "case sensitivity" flag that was previously the second argument is now the third, and as always Sugar will detect when/if to defer to the native method if available.

Fixes