diff --git a/doc/README.md b/doc/README.md index 37fc3e4a3..49d2ff4da 100644 --- a/doc/README.md +++ b/doc/README.md @@ -1753,7 +1753,7 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return Math.sin(num); }); ### `_.sortedIndex(array, value [, callback])` -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. [▲][1] #### Arguments diff --git a/lodash.js b/lodash.js index b40fa033b..878bf964c 100644 --- a/lodash.js +++ b/lodash.js @@ -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); diff --git a/lodash.min.js b/lodash.min.js index 374341422..57d2aba69 100644 --- a/lodash.min.js +++ b/lodash.min.js @@ -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={};++carguments.length&&(b=a||0,a=0);for(var d=-1,f=Math.max(Math.ceil((b-a)/c),0),e=Array(f);++dd?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;cd?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'); + 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++)); }}); + }); + });