Reduce createIterator.

Former-commit-id: 8c27ca8e4d1f71b2727dd988bc62194510a850dc
This commit is contained in:
John-David Dalton
2012-10-03 23:48:19 -07:00
parent 25ba18e570
commit 21217dfda3
4 changed files with 27 additions and 37 deletions

View File

@@ -684,7 +684,7 @@
// remove `isKeysFast` from `inLoop.object` of `mapIteratorOptions`, `invoke`, `pairs`, `pluck`, and `sortBy` // remove `isKeysFast` from `inLoop.object` of `mapIteratorOptions`, `invoke`, `pairs`, `pluck`, and `sortBy`
.replace(/'\s*\+\s*\(isKeysFast[^)]+?\)\s*\+\s*'/g, '.push') .replace(/'\s*\+\s*\(isKeysFast[^)]+?\)\s*\+\s*'/g, '.push')
// remove data object property assignment in `createIterator` // remove data object property assignment in `createIterator`
.replace(/\s*.+?\.isKeysFast *=.+/, ''); .replace(/ *'isKeysFast':.+\n/, '');
} }
/** /**
@@ -795,10 +795,8 @@
.replace(/(?: *\/\/.*\n)*(\s*)' *<% *if *\(useStrict\).+/, value ? "$1'\\'use strict\\';\\n' +" : '') .replace(/(?: *\/\/.*\n)*(\s*)' *<% *if *\(useStrict\).+/, value ? "$1'\\'use strict\\';\\n' +" : '')
// remove `useStrict` from iterator options // remove `useStrict` from iterator options
.replace(/ *'useStrict': *false,\n/g, '') .replace(/ *'useStrict': *false,\n/g, '')
// remove `useStrict` variable assignment in `createIterator`
.replace(/,\s*useStrict *=[^;]+/, '')
// remove `useStrict` data object property assignment in `createIterator` // remove `useStrict` data object property assignment in `createIterator`
.replace(/\s*.+?\.useStrict *=.+/, ''); .replace(/ *'useStrict':.+\n/, '');
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/

View File

@@ -15,7 +15,6 @@
'createCallback', 'createCallback',
'ctor', 'ctor',
'hasOwnProperty', 'hasOwnProperty',
'identity',
'index', 'index',
'iteratee', 'iteratee',
'length', 'length',

View File

@@ -482,7 +482,7 @@
/** Reusable iterator options for `invoke`, `map`, `pluck`, and `sortBy` */ /** Reusable iterator options for `invoke`, `map`, `pluck`, and `sortBy` */
var mapIteratorOptions = { var mapIteratorOptions = {
'init': '', 'init': false,
'beforeLoop': { 'beforeLoop': {
'array': 'result = Array(length)', 'array': 'result = Array(length)',
'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]') 'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]')
@@ -672,8 +672,7 @@
* useStrict - A boolean to specify whether or not to include the ES5 * useStrict - A boolean to specify whether or not to include the ES5
* "use strict" directive. * "use strict" directive.
* *
* args - A string of comma separated arguments the iteration function will * args - A string of comma separated arguments the iteration function will accept.
* accept.
* *
* init - A string to specify the initial value of the `result` variable. * init - A string to specify the initial value of the `result` variable.
* *
@@ -691,31 +690,35 @@
* @returns {Function} Returns the compiled function. * @returns {Function} Returns the compiled function.
*/ */
function createIterator() { function createIterator() {
var object, var index = -1,
prop,
value,
index = -1,
length = arguments.length; length = arguments.length;
// merge options into a template data object // merge options into a template data object
var data = { var data = {
'bottom': '', 'bottom': '',
'hasDontEnumBug': hasDontEnumBug,
'isKeysFast': isKeysFast,
'noArgsEnum': noArgsEnum,
'noCharByIndex': noCharByIndex,
'shadowed': shadowed,
'top': '', 'top': '',
'arrayBranch': { 'beforeLoop': '' }, 'useHas': true,
'objectBranch': { 'beforeLoop': '' } 'useStrict': isStrictFast,
'arrayBranch': {},
'objectBranch': {}
}; };
while (++index < length) { while (++index < length) {
object = arguments[index]; var object = arguments[index];
for (prop in object) { for (var prop in object) {
value = (value = object[prop]) == null ? '' : value; var value = object[prop];
// keep this regexp explicit for the build pre-process // keep this regexp explicit for the build pre-process
if (/beforeLoop|inLoop/.test(prop)) { if (/beforeLoop|inLoop/.test(prop)) {
if (typeof value == 'string') { if (typeof value == 'string') {
value = { 'array': value, 'object': value }; value = { 'array': value, 'object': value };
} }
data.arrayBranch[prop] = value.array || ''; data.arrayBranch[prop] = value.array;
data.objectBranch[prop] = value.object || ''; data.objectBranch[prop] = value.object;
} else { } else {
data[prop] = value; data[prop] = value;
} }
@@ -724,28 +727,18 @@
// set additional template `data` values // set additional template `data` values
var args = data.args, var args = data.args,
firstArg = /^[^,]+/.exec(args)[0], firstArg = /^[^,]+/.exec(args)[0],
init = data.init, init = data.init;
useStrict = data.useStrict;
data.firstArg = firstArg; data.firstArg = firstArg;
data.hasDontEnumBug = hasDontEnumBug;
data.init = init == null ? firstArg : init; data.init = init == null ? firstArg : init;
data.isKeysFast = isKeysFast;
data.noArgsEnum = noArgsEnum;
data.shadowed = shadowed;
data.useHas = data.useHas !== false;
data.useStrict = useStrict == null ? isStrictFast : useStrict;
if (data.noCharByIndex == null) {
data.noCharByIndex = noCharByIndex;
}
if (firstArg != 'collection' || !data.arrayBranch.inLoop) { if (firstArg != 'collection' || !data.arrayBranch.inLoop) {
data.arrayBranch = null; data.arrayBranch = null;
} }
// create the function factory // create the function factory
var factory = Function( var factory = Function(
'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, createCallback, ' + 'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, createCallback, ' +
'forIn, hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction, ' + 'forIn, hasOwnProperty, indexOf, isArguments, isArray, isFunction, ' +
'isPlainObject, objectClass, objectTypes, nativeKeys, propertyIsEnumerable, ' + 'isPlainObject, objectClass, objectTypes, nativeKeys, propertyIsEnumerable, ' +
'slice, stringClass, toString, undefined', 'slice, stringClass, toString, undefined',
'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' + 'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
@@ -754,7 +747,7 @@
// return the compiled function // return the compiled function
return factory( return factory(
arrayLikeClasses, ArrayProto, bind, compareAscending, concat, createCallback, arrayLikeClasses, ArrayProto, bind, compareAscending, concat, createCallback,
forIn, hasOwnProperty, identity, indexOf, isArguments, isArray, isFunction, forIn, hasOwnProperty, indexOf, isArguments, isArray, isFunction,
isPlainObject, objectClass, objectTypes, nativeKeys, propertyIsEnumerable, isPlainObject, objectClass, objectTypes, nativeKeys, propertyIsEnumerable,
slice, stringClass, toString slice, stringClass, toString
); );
@@ -846,7 +839,7 @@
// fallback for browsers that can't detect `arguments` objects by [[Class]] // fallback for browsers that can't detect `arguments` objects by [[Class]]
if (noArgsClass) { if (noArgsClass) {
isArguments = function(value) { isArguments = function(value) {
return !!(value && hasOwnProperty.call(value, 'callee')); return value ? hasOwnProperty.call(value, 'callee') : false;
}; };
} }
@@ -1996,7 +1989,7 @@
* // => 2 * // => 2
*/ */
var find = createIterator(baseIteratorOptions, forEachIteratorOptions, { var find = createIterator(baseIteratorOptions, forEachIteratorOptions, {
'init': '', 'init': false,
'inLoop': 'if (callback(value, index, collection)) return value' 'inLoop': 'if (callback(value, index, collection)) return value'
}); });
@@ -3194,8 +3187,8 @@
' return result\n' + ' return result\n' +
'}', '}',
'inLoop': 'inLoop':
'if (isFunction(result[index])) {\n' + 'if (isFunction(value)) {\n' +
' result[index] = bind(result[index], result)\n' + ' result[index] = bind(value, result)\n' +
'}' '}'
}); });

View File

@@ -395,7 +395,7 @@
} else { } else {
func(object, { 'a': 1 }); func(object, { 'a': 1 });
} }
} catch(e) {console.log(e); } catch(e) {
pass = false; pass = false;
} }
ok(pass); ok(pass);