Permalink
Browse files

Remove compiling from `_.reject` and consolidate iterator option obje…

…cts.
  • Loading branch information...
1 parent 32e6b51 commit 82a4496833f8a8e6ab95a59f470d5ecac2b4c779 @jdalton jdalton committed Oct 14, 2012
Showing with 77 additions and 82 deletions.
  1. +3 −3 build.js
  2. +18 −23 lodash.js
  3. +27 −27 lodash.min.js
  4. +29 −29 lodash.underscore.min.js
View
@@ -135,7 +135,7 @@
'range': [],
'reduce': ['forEach'],
'reduceRight': ['forEach', 'keys'],
- 'reject': ['identity'],
+ 'reject': ['filter'],
'rest': [],
'result': ['isFunction'],
'shuffle': ['forEach'],
@@ -1191,8 +1191,8 @@
// simplify DOM node check from `_.isEqual`
source = source.replace(/(if *\(className *!= *objectClass).+?noNodeClass[\s\S]+?{/, '$1) {');
- // remove "exit early" feature from `_.each`
- source = source.replace(/( +)var baseIteratorOptions *=[\s\S]+?\n\1.+?;/, function(match) {
+ // remove "exit early" feature from `_.forEach`, `_.forIn`, and `_.forOwn`
+ source = source.replace(/( +)var forEachIteratorOptions *=[\s\S]+?\n\1.+?;/, function(match) {
return match.replace(/if *\(callback[^']+/, 'callback(value, index, collection)');
});
View
@@ -422,7 +422,7 @@
* Reusable iterator options shared by `every`, `filter`, forEach`, `forIn`,
* `forOwn`, `map`, `reject`, and `some`.
*/
- var baseIteratorOptions = {
+ var forEachIteratorOptions = {
'args': 'collection, callback, thisArg',
'top': 'callback = createCallback(callback, thisArg)',
'inLoop': 'if (callback(value, index, collection) === false) return result'
@@ -446,21 +446,10 @@
'bottom': ' }\n}'
};
- /** Reusable iterator options for `filter` and `reject` */
- var filterIteratorOptions = {
- 'init': '[]',
- 'inLoop': 'callback(value, index, collection) && result.push(value)'
- };
-
- /** Reusable iterator options for `forEach`, `forIn`, and `forOwn` */
- var forEachIteratorOptions = {
- 'top': 'callback = createCallback(callback, thisArg)'
- };
-
/** Reusable iterator options for `forIn` and `forOwn` */
var forOwnIteratorOptions = {
'inLoop': {
- 'object': baseIteratorOptions.inLoop
+ 'object': forEachIteratorOptions.inLoop
}
};
@@ -766,7 +755,7 @@
* });
* // => alerts 'name' and 'bark' (order is not guaranteed)
*/
- var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
+ var forIn = createIterator(forEachIteratorOptions, forOwnIteratorOptions, {
'useHas': false
});
@@ -790,7 +779,7 @@
* });
* // => alerts '0', '1', and 'length' (order is not guaranteed)
*/
- var forOwn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions);
+ var forOwn = createIterator(forEachIteratorOptions, forOwnIteratorOptions);
/**
* Checks if `value` is an `arguments` object.
@@ -1937,7 +1926,7 @@
* _.every([true, 1, null, 'yes'], Boolean);
* // => false
*/
- var every = createIterator(baseIteratorOptions, everyIteratorOptions);
+ var every = createIterator(forEachIteratorOptions, everyIteratorOptions);
/**
* Examines each element in a `collection`, returning an array of all elements
@@ -1957,7 +1946,10 @@
* var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
* // => [2, 4, 6]
*/
- var filter = createIterator(baseIteratorOptions, filterIteratorOptions);
+ var filter = createIterator(forEachIteratorOptions, {
+ 'init': '[]',
+ 'inLoop': 'callback(value, index, collection) && result.push(value)'
+ });
/**
* Examines each element in a `collection`, returning the first one the `callback`
@@ -2010,7 +2002,7 @@
* _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, alert);
* // => alerts each number (order is not guaranteed)
*/
- var forEach = createIterator(baseIteratorOptions, forEachIteratorOptions);
+ var forEach = createIterator(forEachIteratorOptions);
/**
* Creates an object composed of keys returned from running each element of
@@ -2102,7 +2094,7 @@
* _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
* // => [3, 6, 9] (order is not guaranteed)
*/
- var map = createIterator(baseIteratorOptions, {
+ var map = createIterator(forEachIteratorOptions, {
'init': 'collection || []',
'beforeLoop': {
'array': 'result = Array(length)',
@@ -2321,9 +2313,12 @@
* var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
* // => [1, 3, 5]
*/
- var reject = createIterator(baseIteratorOptions, filterIteratorOptions, {
- 'inLoop': '!' + filterIteratorOptions.inLoop
- });
+ function reject(collection, callback, thisArg) {
+ callback = createCallback(callback, thisArg);
+ return filter(collection, function(value, index, collection) {
+ return !callback(value, index, collection);
+ });
+ }
/**
* Creates an array of shuffled `array` values, using a version of the
@@ -2396,7 +2391,7 @@
* _.some([null, 0, 'yes', false]);
* // => true
*/
- var some = createIterator(baseIteratorOptions, everyIteratorOptions, {
+ var some = createIterator(forEachIteratorOptions, everyIteratorOptions, {
'init': 'false',
'inLoop': everyIteratorOptions.inLoop.replace('!', '')
});
Oops, something went wrong.

0 comments on commit 82a4496

Please sign in to comment.