mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Merge branch 'master' of git://github.com/DmitryBaranovskiy/underscore
This commit is contained in:
@@ -26,12 +26,12 @@
|
|||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (obj.length) {
|
} else if (obj.length) {
|
||||||
for (var i=0; i<obj.length; i++) iterator.call(context, obj[i], i);
|
for (var i=0, ii = obj.length; i<ii; i++) iterator.call(context, obj[i], i);
|
||||||
} else if (obj.each) {
|
} else if (obj.each) {
|
||||||
obj.each(function(value) { iterator.call(context, value, index++); });
|
obj.each(function(value) { iterator.call(context, value, index++); });
|
||||||
} else {
|
} else {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
for (var key in obj) {
|
for (var key in obj) if (obj.hasOwnProperty(key)) {
|
||||||
var value = obj[key], pair = [key, value];
|
var value = obj[key], pair = [key, value];
|
||||||
pair.key = key;
|
pair.key = key;
|
||||||
pair.value = value;
|
pair.value = value;
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return the results of applying the iterator to each element. Use Javascript
|
// Return the results of applying the iterator to each element. Use JavaScript
|
||||||
// 1.6's version of map, if possible.
|
// 1.6's version of map, if possible.
|
||||||
_.map = function(obj, iterator, context) {
|
_.map = function(obj, iterator, context) {
|
||||||
if (obj && obj.map) return obj.map(iterator, context);
|
if (obj && obj.map) return obj.map(iterator, context);
|
||||||
@@ -76,13 +76,13 @@
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return all the elements that pass a truth test. Use Javascript 1.6's
|
// Return all the elements that pass a truth test. Use JavaScript 1.6's
|
||||||
// filter(), if it exists.
|
// filter(), if it exists.
|
||||||
_.select = function(obj, iterator, context) {
|
_.select = function(obj, iterator, context) {
|
||||||
if (obj.filter) return obj.filter(iterator, context);
|
if (obj.filter) return obj.filter(iterator, context);
|
||||||
var results = [];
|
var results = [];
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
if (iterator.call(context, value, index)) results.push(value);
|
iterator.call(context, value, index) && results.push(value);
|
||||||
});
|
});
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
@@ -91,32 +91,30 @@
|
|||||||
_.reject = function(obj, iterator, context) {
|
_.reject = function(obj, iterator, context) {
|
||||||
var results = [];
|
var results = [];
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
if (!iterator.call(context, value, index)) results.push(value);
|
!iterator.call(context, value, index) && results.push(value);
|
||||||
});
|
});
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Determine whether all of the elements match a truth test. Delegate to
|
// Determine whether all of the elements match a truth test. Delegate to
|
||||||
// Javascript 1.6's every(), if it is present.
|
// JavaScript 1.6's every(), if it is present.
|
||||||
_.all = function(obj, iterator, context) {
|
_.all = function(obj, iterator, context) {
|
||||||
iterator = iterator || function(v){ return v; };
|
|
||||||
if (obj.every) return obj.every(iterator, context);
|
if (obj.every) return obj.every(iterator, context);
|
||||||
var result = true;
|
var result = true;
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
result = result && !!iterator.call(context, value, index);
|
if (!(result = result && iterator ? iterator.call(context, value, index) : value)) throw '__break__';
|
||||||
if (!result) throw '__break__';
|
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Determine if at least one element in the object matches a truth test. Use
|
// Determine if at least one element in the object matches a truth test. Use
|
||||||
// Javascript 1.6's some(), if it exists.
|
// JavaScript 1.6's some(), if it exists.
|
||||||
_.any = function(obj, iterator, context) {
|
_.any = function(obj, iterator, context) {
|
||||||
iterator = iterator || function(v) { return v; };
|
iterator = iterator || function(v) { return v; };
|
||||||
if (obj.some) return obj.some(iterator, context);
|
if (obj.some) return obj.some(iterator, context);
|
||||||
var result = false;
|
var result = false;
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
if (result = !!iterator.call(context, value, index)) throw '__break__';
|
if (result = iterator ? iterator.call(context, value, index) : value) throw '__break__';
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
@@ -127,8 +125,7 @@
|
|||||||
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
||||||
var found = false;
|
var found = false;
|
||||||
_.each(obj, function(pair) {
|
_.each(obj, function(pair) {
|
||||||
if (pair.value === target) {
|
if (found = pair.value === target) {
|
||||||
found = true;
|
|
||||||
throw '__break__';
|
throw '__break__';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -153,10 +150,10 @@
|
|||||||
// Return the maximum item or (item-based computation).
|
// Return the maximum item or (item-based computation).
|
||||||
_.max = function(obj, iterator, context) {
|
_.max = function(obj, iterator, context) {
|
||||||
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj);
|
||||||
var result;
|
var result = {computed : -Infinity};
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
var computed = iterator ? iterator.call(context, value, index) : value;
|
var computed = iterator ? iterator.call(context, value, index) : value;
|
||||||
if (result == null || computed >= result.computed) result = {value : value, computed : computed};
|
computed >= result.computed && (result = {value : value, computed : computed});
|
||||||
});
|
});
|
||||||
return result.value;
|
return result.value;
|
||||||
};
|
};
|
||||||
@@ -164,10 +161,10 @@
|
|||||||
// Return the minimum element (or element-based computation).
|
// Return the minimum element (or element-based computation).
|
||||||
_.min = function(obj, iterator, context) {
|
_.min = function(obj, iterator, context) {
|
||||||
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj);
|
||||||
var result;
|
var result = {computed : Infinity};
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index) {
|
||||||
var computed = iterator ? iterator.call(context, value, index) : value;
|
var computed = iterator ? iterator.call(context, value, index) : value;
|
||||||
if (result == null || computed < result.computed) result = {value : value, computed : computed};
|
computed < result.computed && (result = {value : value, computed : computed});
|
||||||
});
|
});
|
||||||
return result.value;
|
return result.value;
|
||||||
};
|
};
|
||||||
@@ -276,15 +273,16 @@
|
|||||||
// item in an array, or -1 if the item is not included in the array.
|
// item in an array, or -1 if the item is not included in the array.
|
||||||
_.indexOf = function(array, item) {
|
_.indexOf = function(array, item) {
|
||||||
if (array.indexOf) return array.indexOf(item);
|
if (array.indexOf) return array.indexOf(item);
|
||||||
for (i=0; i<array.length; i++) if (array[i] === item) return i;
|
for (i=0, ii=array.length; i<ii; i++) if (array[i] === item) return i;
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Provide Javascript 1.6's lastIndexOf, delegating to the native function,
|
// Provide JavaScript 1.6's lastIndexOf, delegating to the native function,
|
||||||
// if possible.
|
// if possible.
|
||||||
_.lastIndexOf = function(array, item) {
|
_.lastIndexOf = function(array, item) {
|
||||||
if (array.lastIndexOf) return array.lastIndexOf(item);
|
if (array.lastIndexOf) return array.lastIndexOf(item);
|
||||||
for (i=array.length - 1; i>=0; i--) if (array[i] === item) return i;
|
var i = array.length;
|
||||||
|
while (i--) if (array[i] === item) return i;
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -403,7 +401,7 @@
|
|||||||
|
|
||||||
// Is a given value a Function?
|
// Is a given value a Function?
|
||||||
_.isFunction = function(obj) {
|
_.isFunction = function(obj) {
|
||||||
return typeof obj == 'function';
|
return Object.prototype.toString.call(obj) == '[object Function]';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Is a given variable undefined?
|
// Is a given variable undefined?
|
||||||
@@ -427,8 +425,8 @@
|
|||||||
return prefix ? prefix + id : id;
|
return prefix ? prefix + id : id;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Javascript templating a-la ERB, pilfered from John Resig's
|
// JavaScript templating a-la ERB, pilfered from John Resig's
|
||||||
// "Secrets of the Javascript Ninja", page 83.
|
// "Secrets of the JavaScript Ninja", page 83.
|
||||||
_.template = function(str, data) {
|
_.template = function(str, data) {
|
||||||
var fn = new Function('obj',
|
var fn = new Function('obj',
|
||||||
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
||||||
|
|||||||
Reference in New Issue
Block a user