mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 20:37:48 +00:00
Simplify createIterator.
Former-commit-id: 3058b25bf7677a45261b640bd6ff6e1af1849c2c
This commit is contained in:
24
lodash.js
24
lodash.js
@@ -3,7 +3,7 @@
|
|||||||
* Copyright 2012 John-David Dalton <http://allyoucanleet.com/>
|
* Copyright 2012 John-David Dalton <http://allyoucanleet.com/>
|
||||||
* Based on Underscore.js 1.3.3, copyright 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
* Based on Underscore.js 1.3.3, copyright 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
||||||
* <http://documentcloud.github.com/underscore>
|
* <http://documentcloud.github.com/underscore>
|
||||||
* Available under MIT license <http://mths.be/mit>
|
* Available under MIT license <http://lodash.com/license>
|
||||||
*/
|
*/
|
||||||
;(function(window, undefined) {
|
;(function(window, undefined) {
|
||||||
'use strict';
|
'use strict';
|
||||||
@@ -326,18 +326,21 @@
|
|||||||
*/
|
*/
|
||||||
var isEmpty = createIterator({
|
var isEmpty = createIterator({
|
||||||
'args': 'value',
|
'args': 'value',
|
||||||
'iterate': 'objects',
|
|
||||||
'init': 'true',
|
'init': 'true',
|
||||||
'top':
|
'top':
|
||||||
'var className = toString.call(value);\n' +
|
'var className = toString.call(value);\n' +
|
||||||
'if (className == arrayClass || className == stringClass) return !value.length',
|
'if (className == arrayClass || className == stringClass) return !value.length',
|
||||||
'inLoop': 'return false'
|
'inLoop': {
|
||||||
|
'object': 'return false'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates compiled iteration functions.
|
* Creates compiled iteration functions. The iteration function will be created
|
||||||
|
* to iterate over only objects if the first argument of `options.args` is
|
||||||
|
* "object" or `options.inLoop.array` is falsey.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Object} [options1, options2, ..] The compile options objects.
|
* @param {Object} [options1, options2, ..] The compile options objects.
|
||||||
@@ -345,10 +348,6 @@
|
|||||||
* args - A string of comma separated arguments the iteration function will
|
* args - A string of comma separated arguments the iteration function will
|
||||||
* accept.
|
* accept.
|
||||||
*
|
*
|
||||||
* iterate - A string to specify whether the iteration function is only for
|
|
||||||
* "arrays" or "objects". It is automatically set if the first argument of
|
|
||||||
* `options.args` is "array" or "object".
|
|
||||||
*
|
|
||||||
* init - A string to specify the initial value of the `result` variable.
|
* init - A string to specify the initial value of the `result` variable.
|
||||||
*
|
*
|
||||||
* exit - A string of code to use in place of the default exit-early check
|
* exit - A string of code to use in place of the default exit-early check
|
||||||
@@ -406,10 +405,10 @@
|
|||||||
}
|
}
|
||||||
// set additional template data values
|
// set additional template data values
|
||||||
var args = data.args,
|
var args = data.args,
|
||||||
|
arrayBranch = data.arrayBranch,
|
||||||
objectBranch = data.objectBranch,
|
objectBranch = data.objectBranch,
|
||||||
firstArg = /^[^,]+/.exec(args)[0],
|
firstArg = /^[^,]+/.exec(args)[0],
|
||||||
loopExp = objectBranch.loopExp,
|
loopExp = objectBranch.loopExp,
|
||||||
iterate = data.iterate,
|
|
||||||
iteratedObject = /\S+$/.exec(loopExp || firstArg)[0];
|
iteratedObject = /\S+$/.exec(loopExp || firstArg)[0];
|
||||||
|
|
||||||
data.firstArg = firstArg;
|
data.firstArg = firstArg;
|
||||||
@@ -422,13 +421,10 @@
|
|||||||
if (!data.exit) {
|
if (!data.exit) {
|
||||||
data.exit = 'if (' + firstArg + ' == null) return result';
|
data.exit = 'if (' + firstArg + ' == null) return result';
|
||||||
}
|
}
|
||||||
if (firstArg == 'object' || iterate == 'objects') {
|
if (firstArg == 'object' || !arrayBranch.inLoop) {
|
||||||
data.arrayBranch = null;
|
data.arrayBranch = null;
|
||||||
}
|
}
|
||||||
if (firstArg == 'array' || iterate == 'arrays') {
|
if (!loopExp) {
|
||||||
data.objectBranch = null;
|
|
||||||
}
|
|
||||||
else if (!loopExp) {
|
|
||||||
objectBranch.loopExp = 'index in ' + iteratedObject;
|
objectBranch.loopExp = 'index in ' + iteratedObject;
|
||||||
}
|
}
|
||||||
// create the function factory
|
// create the function factory
|
||||||
|
|||||||
Reference in New Issue
Block a user