From 8fe19f20238658015a9a1e1def12c0e4af172e5f Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 15 Oct 2010 17:21:41 -0400 Subject: [PATCH] Underscore 1.1.2 --- docs/underscore.html | 10 ++++++---- index.html | 12 ++++++++++-- package.json | 2 +- test/objects.js | 2 +- underscore-min.js | 34 +++++++++++++++++----------------- underscore.js | 3 ++- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/docs/underscore.html b/docs/underscore.html index 85174c8bf..ce0b9e38f 100644 --- a/docs/underscore.html +++ b/docs/underscore.html @@ -20,7 +20,7 @@ are declared here.

nativeIndexOf = ArrayProto.indexOf, nativeLastIndexOf = ArrayProto.lastIndexOf, nativeIsArray = Array.isArray, - nativeKeys = Object.keys;

Create a safe reference to the Underscore object for use below.

  var _ = function(obj) { return new wrapper(obj); };

Export the Underscore object for CommonJS.

  if (typeof exports !== 'undefined') exports._ = _;

Export Underscore to the global scope.

  root._ = _;

Current version.

  _.VERSION = '1.1.1';

Collection Functions

The cornerstone, an each implementation, aka forEach. + nativeKeys = Object.keys;

Create a safe reference to the Underscore object for use below.

  var _ = function(obj) { return new wrapper(obj); };

Export the Underscore object for CommonJS.

  if (typeof exports !== 'undefined') exports._ = _;

Export Underscore to the global scope.

  root._ = _;

Current version.

  _.VERSION = '1.1.2';

Collection Functions

The cornerstone, an each implementation, aka forEach. Handles objects implementing forEach, arrays, and raw objects. Delegates to ECMAScript 5's native forEach if available.

  var each = _.each = _.forEach = function(obj, iterator, context) {
     try {
@@ -107,7 +107,8 @@ Aliased as any.

if (result = iterator.call(context, value, index, list)) _.breakLoop(); }); return result; - };

Determine if a given value is included in the array or object using ===.

  _.include = function(obj, target) {
+  };

Determine if a given value is included in the array or object using ===. +Aliased as contains.

  _.include = _.contains = function(obj, target) {
     if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
     var found = false;
     each(obj, function(value) {
@@ -187,13 +188,14 @@ check allows it to work with _.map.

var values = slice.call(arguments, 1); return _.filter(array, function(value){ return !_.include(values, value); }); };

Produce a duplicate-free version of the array. If the array has already -been sorted, you have the option of using a faster algorithm.

  _.uniq = function(array, isSorted) {
+been sorted, you have the option of using a faster algorithm.
+Aliased as unique.

  _.uniq = _.unique = function(array, isSorted) {
     return _.reduce(array, function(memo, el, i) {
       if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
       return memo;
     }, []);
   };

Produce an array that contains every item shared between all the -passed-in arrays. Aliased as contains.

  _.intersect = _.contains = function(array) {
+passed-in arrays.

  _.intersect = function(array) {
     var rest = slice.call(arguments, 1);
     return _.filter(_.uniq(array), function(item) {
       return _.every(rest, function(other) {
diff --git a/index.html b/index.html
index 757ed0c6f..ab31da513 100644
--- a/index.html
+++ b/index.html
@@ -114,11 +114,11 @@
 
     
-        
+        
-        
+        
Development Version (1.1.1)Development Version (1.1.2) 25kb, Uncompressed with Comments
Production Version (1.1.1)Production Version (1.1.2) 3kb, Packed and Gzipped
@@ -546,6 +546,7 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);

uniq_.uniq(array, [isSorted]) + Alias: unique
Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, @@ -1165,6 +1166,13 @@ _([1, 2, 3]).value();

Change Log

+

+ 1.1.2
+ Fixed _.contains, which was mistakenly pointing at + _.intersect instead of _.include, like it should + have been. Added _.unique as an alias for _.uniq. +

+

1.1.1
Improved the speed of _.template, and its handling of multiline diff --git a/package.json b/package.json index 59f200734..c5277099c 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,5 @@ "dependencies" : [], "lib" : ".", "main" : "underscore.js", - "version" : "1.1.1" + "version" : "1.1.2" } diff --git a/test/objects.js b/test/objects.js index 4f7ee02e0..fa0b56ee6 100644 --- a/test/objects.js +++ b/test/objects.js @@ -17,7 +17,7 @@ $(document).ready(function() { "indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isBoolean", "isDate", "isElement", "isEmpty", "isEqual", "isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", "memoize", "methods", "min", "mixin", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select", - "size", "some", "sortBy", "sortedIndex", "tail", "tap", "template", "times", "toArray", "uniq", + "size", "some", "sortBy", "sortedIndex", "tail", "tap", "template", "times", "toArray", "uniq", "unique", "uniqueId", "values", "without", "wrap", "zip"]; same(expected, _.methods(_), 'provides a sorted list of functions'); var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce}; diff --git a/underscore-min.js b/underscore-min.js index 01355501b..677e6559f 100644 --- a/underscore-min.js +++ b/underscore-min.js @@ -1,17 +1,17 @@ -(function(){var o=this,A=o._,r=typeof StopIteration!=="undefined"?StopIteration:"__break__",k=Array.prototype,m=Object.prototype,i=k.slice,B=k.unshift,C=m.toString,p=m.hasOwnProperty,s=k.forEach,t=k.map,u=k.reduce,v=k.reduceRight,w=k.filter,x=k.every,y=k.some,n=k.indexOf,z=k.lastIndexOf;m=Array.isArray;var D=Object.keys,c=function(a){return new l(a)};if(typeof exports!=="undefined")exports._=c;o._=c;c.VERSION="1.1.1";var j=c.each=c.forEach=function(a,b,d){try{if(s&&a.forEach===s)a.forEach(b,d);else if(c.isNumber(a.length))for(var e= -0,f=a.length;e=e.computed&&(e={value:f,computed:g})});return e.value};c.min=function(a,b,d){if(!b&&c.isArray(a))return Math.min.apply(Math,a);var e={computed:Infinity};j(a,function(f,g,h){g=b?b.call(d,f,g,h):f;gh?1:0}),"value")};c.sortedIndex=function(a,b,d){d=d||c.identity;for(var e=0,f=a.length;e>1;d(a[g])=0})})};c.zip=function(){for(var a=i.call(arguments),b=c.max(c.pluck(a,"length")),d=new Array(b),e=0;e=0;d--)b=[a[d].apply(this,b)];return b[0]}};c.keys=D||function(a){if(c.isArray(a))return c.range(0,a.length);var b=[];for(var d in a)if(p.call(a,d))b[b.length]=d;return b};c.values=function(a){return c.map(a,c.identity)};c.functions=c.methods=function(a){return c.filter(c.keys(a),function(b){return c.isFunction(a[b])}).sort()};c.extend=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});return a};c.clone=function(a){return c.isArray(a)?a.slice(0): -c.extend({},a)};c.tap=function(a,b){b(a);return a};c.isEqual=function(a,b){if(a===b)return true;var d=typeof a;if(d!=typeof b)return false;if(a==b)return true;if(!a&&b||a&&!b)return false;if(a.isEqual)return a.isEqual(b);if(c.isDate(a)&&c.isDate(b))return a.getTime()===b.getTime();if(c.isNaN(a)&&c.isNaN(b))return false;if(c.isRegExp(a)&&c.isRegExp(b))return a.source===b.source&&a.global===b.global&&a.ignoreCase===b.ignoreCase&&a.multiline===b.multiline;if(d!=="object")return false;if(a.length&&a.length!== -b.length)return false;d=c.keys(a);var e=c.keys(b);if(d.length!=e.length)return false;for(var f in a)if(!(f in b)||!c.isEqual(a[f],b[f]))return false;return true};c.isEmpty=function(a){if(c.isArray(a)||c.isString(a))return a.length===0;for(var b in a)if(p.call(a,b))return false;return true};c.isElement=function(a){return!!(a&&a.nodeType==1)};c.isArray=m||function(a){return!!(a&&a.concat&&a.unshift&&!a.callee)};c.isArguments=function(a){return!!(a&&a.callee)};c.isFunction=function(a){return!!(a&&a.constructor&& -a.call&&a.apply)};c.isString=function(a){return!!(a===""||a&&a.charCodeAt&&a.substr)};c.isNumber=function(a){return a===+a||C.call(a)==="[object Number]"};c.isBoolean=function(a){return a===true||a===false};c.isDate=function(a){return!!(a&&a.getTimezoneOffset&&a.setUTCFullYear)};c.isRegExp=function(a){return!!(a&&a.test&&a.exec&&(a.ignoreCase||a.ignoreCase===false))};c.isNaN=function(a){return c.isNumber(a)&&isNaN(a)};c.isNull=function(a){return a===null};c.isUndefined=function(a){return typeof a== -"undefined"};c.noConflict=function(){o._=A;return this};c.identity=function(a){return a};c.times=function(a,b,d){for(var e=0;e/g,interpolate:/<%=([\s\S]+?)%>/g};c.template=function(a,b){var d=c.templateSettings;d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+ -a.replace(/'/g,"\\'").replace(d.interpolate,function(e,f){return"',"+f.replace(/\\'/g,"'")+",'"}).replace(d.evaluate||null,function(e,f){return"');"+f.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');";d=new Function("obj",d);return b?d(b):d};var l=function(a){this._wrapped=a};c.prototype=l.prototype;var q=function(a,b){return b?c(a).chain():a},E=function(a,b){l.prototype[a]=function(){var d=i.call(arguments); -B.call(d,this._wrapped);return q(b.apply(c,d),this._chain)}};c.mixin(c);j(["pop","push","reverse","shift","sort","splice","unshift"],function(a){var b=k[a];l.prototype[a]=function(){b.apply(this._wrapped,arguments);return q(this._wrapped,this._chain)}});j(["concat","join","slice"],function(a){var b=k[a];l.prototype[a]=function(){return q(b.apply(this._wrapped,arguments),this._chain)}});l.prototype.chain=function(){this._chain=true;return this};l.prototype.value=function(){return this._wrapped}})(); +(function(){var o=this,A=o._,r=typeof StopIteration!=="undefined"?StopIteration:"__break__",k=Array.prototype,m=Object.prototype,i=k.slice,B=k.unshift,C=m.toString,p=m.hasOwnProperty,s=k.forEach,t=k.map,u=k.reduce,v=k.reduceRight,w=k.filter,x=k.every,y=k.some,n=k.indexOf,z=k.lastIndexOf;m=Array.isArray;var D=Object.keys,b=function(a){return new l(a)};if(typeof exports!=="undefined")exports._=b;o._=b;b.VERSION="1.1.2";var j=b.each=b.forEach=function(a,c,d){try{if(s&&a.forEach===s)a.forEach(c,d);else if(b.isNumber(a.length))for(var e= +0,f=a.length;e=e.computed&&(e={value:f,computed:g})});return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a))return Math.min.apply(Math,a);var e={computed:Infinity};j(a,function(f,g,h){g=c?c.call(d,f,g,h):f;gh?1:0}),"value")};b.sortedIndex=function(a,c,d){d=d||b.identity;for(var e=0,f=a.length;e>1;d(a[g])=0})})};b.zip=function(){for(var a=i.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c),e=0;e=0;d--)c=[a[d].apply(this,c)];return c[0]}};b.keys=D||function(a){if(b.isArray(a))return b.range(0,a.length);var c=[],d;for(d in a)if(p.call(a,d))c[c.length]=d;return c};b.values=function(a){return b.map(a,b.identity)};b.functions=b.methods=function(a){return b.filter(b.keys(a),function(c){return b.isFunction(a[c])}).sort()};b.extend=function(a){j(i.call(arguments,1),function(c){for(var d in c)a[d]=c[d]});return a};b.clone=function(a){return b.isArray(a)?a.slice():b.extend({},a)};b.tap= +function(a,c){c(a);return a};b.isEqual=function(a,c){if(a===c)return true;var d=typeof a;if(d!=typeof c)return false;if(a==c)return true;if(!a&&c||a&&!c)return false;if(a.isEqual)return a.isEqual(c);if(b.isDate(a)&&b.isDate(c))return a.getTime()===c.getTime();if(b.isNaN(a)&&b.isNaN(c))return false;if(b.isRegExp(a)&&b.isRegExp(c))return a.source===c.source&&a.global===c.global&&a.ignoreCase===c.ignoreCase&&a.multiline===c.multiline;if(d!=="object")return false;if(a.length&&a.length!==c.length)return false; +d=b.keys(a);var e=b.keys(c);if(d.length!=e.length)return false;for(var f in a)if(!(f in c)||!b.isEqual(a[f],c[f]))return false;return true};b.isEmpty=function(a){if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(p.call(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=m||function(a){return!!(a&&a.concat&&a.unshift&&!a.callee)};b.isArguments=function(a){return!!(a&&a.callee)};b.isFunction=function(a){return!!(a&&a.constructor&&a.call&&a.apply)}; +b.isString=function(a){return!!(a===""||a&&a.charCodeAt&&a.substr)};b.isNumber=function(a){return a===+a||C.call(a)==="[object Number]"};b.isBoolean=function(a){return a===true||a===false};b.isDate=function(a){return!!(a&&a.getTimezoneOffset&&a.setUTCFullYear)};b.isRegExp=function(a){return!!(a&&a.test&&a.exec&&(a.ignoreCase||a.ignoreCase===false))};b.isNaN=function(a){return b.isNumber(a)&&isNaN(a)};b.isNull=function(a){return a===null};b.isUndefined=function(a){return typeof a=="undefined"};b.noConflict= +function(){o._=A;return this};b.identity=function(a){return a};b.times=function(a,c,d){for(var e=0;e/g,interpolate:/<%=([\s\S]+?)%>/g};b.template=function(a,c){var d=b.templateSettings;d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+a.replace(/'/g, +"\\'").replace(d.interpolate,function(e,f){return"',"+f.replace(/\\'/g,"'")+",'"}).replace(d.evaluate||null,function(e,f){return"');"+f.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');";d=new Function("obj",d);return c?d(c):d};var l=function(a){this._wrapped=a};b.prototype=l.prototype;var q=function(a,c){return c?b(a).chain():a},E=function(a,c){l.prototype[a]=function(){var d=i.call(arguments);B.call(d, +this._wrapped);return q(c.apply(b,d),this._chain)}};b.mixin(b);j(["pop","push","reverse","shift","sort","splice","unshift"],function(a){var c=k[a];l.prototype[a]=function(){c.apply(this._wrapped,arguments);return q(this._wrapped,this._chain)}});j(["concat","join","slice"],function(a){var c=k[a];l.prototype[a]=function(){return q(c.apply(this._wrapped,arguments),this._chain)}});l.prototype.chain=function(){this._chain=true;return this};l.prototype.value=function(){return this._wrapped}})(); diff --git a/underscore.js b/underscore.js index f02da3896..6bf375ed8 100644 --- a/underscore.js +++ b/underscore.js @@ -54,7 +54,7 @@ root._ = _; // Current version. - _.VERSION = '1.1.1'; + _.VERSION = '1.1.2'; // Collection Functions // -------------------- @@ -303,6 +303,7 @@ // Produce a duplicate-free version of the array. If the array has already // been sorted, you have the option of using a faster algorithm. + // Aliased as `unique`. _.uniq = _.unique = function(array, isSorted) { return _.reduce(array, function(memo, el, i) { if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;