lodash: Sync with Underscore.js commits. [jddalton]

Former-commit-id: 0a92b5c9dcd0ff5c4c712e083fa60556ebb1d96a
This commit is contained in:
John-David Dalton
2012-04-22 18:00:02 -04:00
parent 56b2d76eec
commit 9af078eb3a
5 changed files with 46 additions and 15 deletions

View File

@@ -1753,7 +1753,7 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return Math.sin(num); });
<!-- div -->
### <a id="_.sortedIndex" href="https://github.com/bestiejs/lodash/blob/master/lodash.js#L844" title="View in source">`_.sortedIndex(array, value [, callback])`</a>
Uses a binary search to determine the index at which the `value` should be inserted into the `collection` in order to maintain the `collection`'s sorted order. If `callback` is passed, it will be executed for each value in the `collection` to compute their sort ranking. The `callback` is invoked with `1` arguments.
Uses a binary search to determine the smallest index at which the `value` should be inserted into the `collection` in order to maintain the sort order of the `collection`. If `callback` is passed, it will be executed for each value in the `collection` to compute their sort ranking. The `callback` is invoked with `1` argument.
[&#9650;][1]
#### Arguments

View File

@@ -822,11 +822,11 @@
});
/**
* Uses a binary search to determine the index at which the `value` should be
* inserted into the `collection` in order to maintain the `collection`'s sorted
* order. If `callback` is passed, it will be executed for each value in the
* `collection` to compute their sort ranking. The `callback` is invoked with
* 1 arguments.
* Uses a binary search to determine the smallest index at which the `value`
* should be inserted into the `collection` in order to maintain the sort order
* of the `collection`. If `callback` is passed, it will be executed for each
* value in the `collection` to compute their sort ranking. The `callback` is
* invoked with 1 argument.
*
* @static
* @memberOf _
@@ -2456,10 +2456,10 @@
return '\\' + escapes[match];
})
.replace(options.escape || reNoMatch, function(match, code) {
return "'+\n_.escape(" + unescape(code) + ")+\n'";
return "'+\n((__t=(" + unescape(code) + "))==null?'':_.escape(__t))+\n'";
})
.replace(options.interpolate || reNoMatch, function(match, code) {
return "'+\n(" + unescape(code) + ")+\n'";
return "'+\n((__t=(" + unescape(code) + "))==null?'':__t)+\n'";
})
.replace(options.evaluate || reNoMatch, function(match, code) {
return "';\n" + unescape(code) + ";\n__p+='";
@@ -2470,8 +2470,8 @@
source = 'with(object||{}){\n' + source + '\n}\n';
}
source = 'var __p="";' +
'function print(){__p+=Array.prototype.join.call(arguments,"")}\n' +
source = 'var __t,__j=Array.prototype.join,__p="";' +
'function print(){__p+=__j.call(arguments,"")}\n' +
source + 'return __p';
var render = Function(options.variable || 'object', '_', source);

10
lodash.min.js vendored
View File

@@ -19,8 +19,8 @@ Object(a)?a:y(a)?a.slice():Q({},a)},compact:D,compose:function(){var a=arguments
a[b]).apply(a,c)})},isArguments:E,isArray:y,isBoolean:function(a){return a===n||a===o||k.call(a)==ka},isDate:function(a){return k.call(a)==la},isElement:function(a){return!!(a&&1==a.nodeType)},isEmpty:da,isEqual:Y,isFinite:function(a){return za(a)&&k.call(a)==L},isFunction:q,isNaN:function(a){return a!=+a&&k.call(a)==L},isNull:function(a){return null===a},isNumber:function(a){return k.call(a)==L},isObject:function(a){return a===Object(a)},isRegExp:function(a){return k.call(a)==ma},isString:function(a){return k.call(a)==
H},isUndefined:function(a){return a===h},keys:T,last:ha,lastIndexOf:function(a,b){if(a==h)return-1;for(var c=a.length;c--;)if(a[c]===b)return c;return-1},map:B,max:ra,memoize:function(a,b){var c={};return function(){var d=b?b.apply(this,arguments):arguments[0];return r.call(c,d)?c[d]:c[d]=a.apply(this,arguments)}},min:x,mixin:na,noConflict:function(){t._=xa;return this},once:function(a){var b,c=o;return function(){if(c)return b;c=n;return b=a.apply(this,arguments)}},pick:function(a){for(var b,c=-1,
d=I(l.call(arguments,1)),f=d.length,e={};++c<f;)b=d[c],b in a&&(e[b]=a[b]);return e},pluck:P,range:function(a,b,c){c||(c=1);2>arguments.length&&(b=a||0,a=0);for(var d=-1,f=Math.max(Math.ceil((b-a)/c),0),e=Array(f);++d<f;)e[d]=a,a+=c;return e},reduce:A,reduceRight:ea,reject:C,rest:ia,result:function(a,b){if(a==h)return null;var c=a[b];return q(c)?a[b]():c},shuffle:function(a){var b,c=[];s(a,function(a,f){b=Math.floor(Math.random()*(f+1));c[f]=c[b];c[b]=a});return c},size:function(a){var b=k.call(a);
return b==F||b==H?a.length:T(a).length},some:w,sortBy:function(a,b,c){if(q(b))c&&(b=u(b,c));else var d=b,b=function(a){return a[d]};return P(B(a,function(c,e){return{a:b(c,e,a),b:c}}).sort(function(a,b){var c=a.a,d=b.a;return c===h?1:d===h?-1:c<d?-1:c>d?1:0}),"b")},sortedIndex:fa,tap:function(a,b){b(a);return a},template:function(a,b,c){function d(a){return f.call(this,a,i)}c=ta(c||{},i.templateSettings);a="__p+='"+a.replace(ya,function(a){return"\\"+z[a]}).replace(c.escape||$,function(a,b){return"'+_['escape']("+
S(b)+")+'"}).replace(c.interpolate||$,function(a,b){return"'+("+S(b)+")+'"}).replace(c.evaluate||$,function(a,b){return"';"+S(b)+";__p+='"})+"';";c.variable||(a="with(object||{}){"+a+"}");var a='var __p="";function print(){__p+=Array.prototype.join.call(arguments,"")}'+a+"return __p",f=Function(c.variable||"object","_",a);if(b)return f(b,i);d.source="function("+(c.variable||"object")+"){"+a+"}";return d},throttle:function(a,b){var c,d,f,e,g,i,k=ja(function(){d=g=o},b);return function(){c=arguments;
e=this;i||(i=K(function(){i=h;d&&a.apply(e,c);k()},b));g?d=n:f=a.apply(e,c);k();g=n;return f}},times:function(a,b,c){c&&(b=u(b,c));for(c=0;c<a;c++)b(c)},toArray:function(a){return!a?[]:y(a)||E(a)?l.call(a):q(a.toArray)?a.toArray():sa(a)},union:function(){return J(I(arguments,n))},uniq:J,uniqueId:function(a){var b=wa++;return a?a+b:b},values:sa,without:O,wrap:function(a,b){return function(){var c=[a];V.apply(c,arguments);return b.apply(this,c)}},zip:function(){for(var a=-1,b=ra(P(arguments,"length")),
c=Array(b);++a<b;)c[a]=P(arguments,a);return c},all:X,any:w,collect:B,detect:qa,each:s,foldl:A,foldr:ea,head:U,include:pa,inject:A,intersect:ga,methods:M,select:W,tail:ia,take:U,unique:J});p.prototype=i.prototype;na(i);s("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=v[a];i.prototype[a]=function(){var a=this.o;b.apply(a,arguments);a.length===0&&delete a[0];return this.p?(new p(a)).chain():a}});s(["concat","join","slice"],function(a){var b=v[a];p.prototype[a]=function(){var a=
b.apply(this.o,arguments);return this.p?(new p(a)).chain():a}});Q(p.prototype,{chain:function(){this.p=n;return this},value:function(){return this.o}});Z?"object"==typeof module&&module&&module.q==Z?(module.q=i)._=i:Z._=i:"function"==typeof define&&"object"==typeof define.amd&&define.amd&&define.amd.lodash?define("lodash",function(){return i}):t._=i})(this);
return b==F||b==H?a.length:T(a).length},some:w,sortBy:function(a,b,c){if(q(b))c&&(b=u(b,c));else var d=b,b=function(a){return a[d]};return P(B(a,function(c,e){return{a:b(c,e,a),b:c}}).sort(function(a,b){var c=a.a,d=b.a;return c===h?1:d===h?-1:c<d?-1:c>d?1:0}),"b")},sortedIndex:fa,tap:function(a,b){b(a);return a},template:function(a,b,c){function d(a){return f.call(this,a,i)}c=ta(c||{},i.templateSettings);a="__p+='"+a.replace(ya,function(a){return"\\"+z[a]}).replace(c.escape||$,function(a,b){return"'+((__t=("+
S(b)+"))==null?'':_['escape'](__t))+'"}).replace(c.interpolate||$,function(a,b){return"'+((__t=("+S(b)+"))==null?'':__t)+'"}).replace(c.evaluate||$,function(a,b){return"';"+S(b)+";__p+='"})+"';";c.variable||(a="with(object||{}){"+a+"}");var a='var __t,__j=Array.prototype.join,__p="";function print(){__p+=__j.call(arguments,"")}'+a+"return __p",f=Function(c.variable||"object","_",a);if(b)return f(b,i);d.source="function("+(c.variable||"object")+"){"+a+"}";return d},throttle:function(a,b){var c,d,f,
e,g,i,k=ja(function(){d=g=o},b);return function(){c=arguments;e=this;i||(i=K(function(){i=h;d&&a.apply(e,c);k()},b));g?d=n:f=a.apply(e,c);k();g=n;return f}},times:function(a,b,c){c&&(b=u(b,c));for(c=0;c<a;c++)b(c)},toArray:function(a){return!a?[]:y(a)||E(a)?l.call(a):q(a.toArray)?a.toArray():sa(a)},union:function(){return J(I(arguments,n))},uniq:J,uniqueId:function(a){var b=wa++;return a?a+b:b},values:sa,without:O,wrap:function(a,b){return function(){var c=[a];V.apply(c,arguments);return b.apply(this,
c)}},zip:function(){for(var a=-1,b=ra(P(arguments,"length")),c=Array(b);++a<b;)c[a]=P(arguments,a);return c},all:X,any:w,collect:B,detect:qa,each:s,foldl:A,foldr:ea,head:U,include:pa,inject:A,intersect:ga,methods:M,select:W,tail:ia,take:U,unique:J});p.prototype=i.prototype;na(i);s("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=v[a];i.prototype[a]=function(){var a=this.o;b.apply(a,arguments);a.length===0&&delete a[0];return this.p?(new p(a)).chain():a}});s(["concat","join",
"slice"],function(a){var b=v[a];p.prototype[a]=function(){var a=b.apply(this.o,arguments);return this.p?(new p(a)).chain():a}});Q(p.prototype,{chain:function(){this.p=n;return this},value:function(){return this.o}});Z?"object"==typeof module&&module&&module.q==Z?(module.q=i)._=i:Z._=i:"function"==typeof define&&"object"==typeof define.amd&&define.amd&&define.amd.lodash?define("lodash",function(){return i}):t._=i})(this);

View File

@@ -260,6 +260,9 @@ $(document).ready(function() {
var numbers = [10, 20, 30, 40, 50], num = 35;
var index = _.sortedIndex(numbers, num);
equal(index, 3, '35 should be inserted at index 3');
var index2 = _.sortedIndex(numbers, 30);
equal(index2, 2, '30 should be inserted at index 2');
});
test('collections: shuffle', function() {

View File

@@ -192,4 +192,32 @@ $(document).ready(function() {
ok(!_.templateSettings.variable);
});
test('#556 - undefined template variables.', function() {
var template = _.template('<%=x%>');
strictEqual(template({x: null}), '');
strictEqual(template({x: undefined}), '');
var templateEscaped = _.template('<%-x%>');
strictEqual(templateEscaped({x: null}), '');
strictEqual(templateEscaped({x: undefined}), '');
var templateWithProperty = _.template('<%=x.foo%>');
strictEqual(templateWithProperty({x: {} }), '');
strictEqual(templateWithProperty({x: {} }), '');
var templateWithPropertyEscaped = _.template('<%-x.foo%>');
strictEqual(templateWithPropertyEscaped({x: {} }), '');
strictEqual(templateWithPropertyEscaped({x: {} }), '');
});
test('interpolate evaluates code only once.', 2, function() {
var count = 0;
var template = _.template('<%= f() %>');
template({f: function(){ ok(!(count++)); }});
var countEscaped = 0;
var templateEscaped = _.template('<%- f() %>');
templateEscaped({f: function(){ ok(!(countEscaped++)); }});
});
});