merging ratbeard/closure

This commit is contained in:
Jeremy Ashkenas
2010-02-24 11:45:05 -05:00
3 changed files with 133 additions and 139 deletions

View File

@@ -7,3 +7,21 @@ task :build do
min = Closure::Compiler.new.compile(js) min = Closure::Compiler.new.compile(js)
File.open('underscore-min.js', 'w') {|f| f.write(min) } File.open('underscore-min.js', 'w') {|f| f.write(min) }
end end
task :build_advanced do
js = File.read('underscore.js')
# remove wrapping anonymous function as this messes with closure compiler
# see
# http://groups.google.com/group/closure-compiler-discuss/browse_thread/thread/b59b54c1a0073aa5
js.sub!('(function() {', '').chomp!("_.initWrapper();\n})();\n")
compiler = Closure::Compiler.new \
:compilation_level => 'ADVANCED_OPTIMIZATIONS',
:formatting => 'PRETTY_PRINT'
min = compiler.compile(js)
File.open('underscore-min2.js', 'w') {|f| f.write(min) }
#
original_size = js.length
minimized_size = min.length
puts original_size, minimized_size
end

View File

@@ -11,7 +11,7 @@ $(document).ready(function() {
}); });
test("objects: functions", function() { test("objects: functions", function() {
var expected = ["alias", "all", "any", "bind", "bindAll", "breakLoop", "buildLookup", "clone", "compact", var expected = ["all", "any", "bind", "bindAll", "breakLoop", "buildLookup", "clone", "compact",
"compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first", "compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include", "flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual", "indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual",
@@ -19,7 +19,7 @@ $(document).ready(function() {
"methods", "min", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select", "methods", "min", "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",
"uniqueId", "values", "without", "wrap", "zip"]; "uniqueId", "values", "without", "wrap", "zip"];
ok(_(expected).isEqual(_.methods(_)), 'provides a sorted list of functions'); same(expected, _.methods(_), 'provides a sorted list of functions');
var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce}; var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce};
ok(_.isEqual(['b', 'd'], _.functions(obj)), 'can grab the function names of any passed-in object'); ok(_.isEqual(['b', 'd'], _.functions(obj)), 'can grab the function names of any passed-in object');
}); });
@@ -185,19 +185,4 @@ $(document).ready(function() {
value(); value();
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain'); ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
}); });
test("objects: alias", function() {
_.alias('isEqual', 'isTheSame');
ok(_.isTheSame(9, 9), 'by default aliases methods on underscore');
delete _.isTheSame;
//
var o = { hi: function () {return 9;} };
_.alias(o, 'hi', 'ho');
equals(o.ho(), 9, 'can add an alias on another object');
_.alias(o, 'hi', 'there', 'sir');
ok(o.hi==o.sir, 'can add multiple aliases');
});
}); });

View File

@@ -7,7 +7,6 @@
// http://documentcloud.github.com/underscore // http://documentcloud.github.com/underscore
(function() { (function() {
// ------------------------- Baseline setup --------------------------------- // ------------------------- Baseline setup ---------------------------------
// Establish the root object, "window" in the browser, or "global" on the server. // Establish the root object, "window" in the browser, or "global" on the server.
@@ -16,49 +15,44 @@
// Save the previous value of the "_" variable. // Save the previous value of the "_" variable.
var previousUnderscore = root._; var previousUnderscore = root._;
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
var wrapper = function(obj) { this._wrapped = obj; };
// Establish the object that gets thrown to break out of a loop iteration. // Establish the object that gets thrown to break out of a loop iteration.
var breaker = typeof StopIteration !== 'undefined' ? StopIteration : '__break__'; var breaker = typeof StopIteration !== 'undefined' ? StopIteration : '__break__';
// Create a safe reference to the Underscore object for reference below.
var _ = root._ = function(obj) { return new wrapper(obj); };
// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') exports._ = _;
// Quick regexp-escaping function, because JS doesn't have RegExp.escape(). // Quick regexp-escaping function, because JS doesn't have RegExp.escape().
var escapeRegExp = function(s) { return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); }; var escapeRegExp = function(s) { return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'); };
// Save bytes in the minified (but not gzipped) version: // Save bytes in the minified (but not gzipped) version:
var ArrayPrototype = Array.prototype; var Array_Prototype = Array.prototype;
// Create quick reference variables for speed access to core prototypes. // Create quick reference variables for speed access to core prototypes.
var slice = ArrayPrototype.slice, var slice = Array_Prototype.slice,
unshift = ArrayPrototype.unshift, unshift = Array_Prototype.unshift,
toString = Object.prototype.toString, toString = Object.prototype.toString,
hasOwnProperty = Object.prototype.hasOwnProperty, hasOwnProperty = Object.prototype.hasOwnProperty,
propertyIsEnumerable = Object.prototype.propertyIsEnumerable; propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
// All native implementations we hope to use are declared here. // All native implementations we hope to use are declared here.
// 'native' is on the long list of reserved words. var
// Ok to use as a property name, though jsLint doesn't agree. native_forEach = Array_Prototype.forEach,
var Native = _['native'] = { native_map = Array_Prototype.map,
forEach: ArrayPrototype.forEach, native_reduce = Array_Prototype.reduce,
map: ArrayPrototype.map, native_reduceRight = Array_Prototype.reduceRight,
reduce: ArrayPrototype.reduce, native_filter = Array_Prototype.filter,
reduceRight: ArrayPrototype.reduceRight, native_every = Array_Prototype.every,
filter: ArrayPrototype.filter, native_some = Array_Prototype.some,
every: ArrayPrototype.every, native_indexOf = Array_Prototype.indexOf,
some: ArrayPrototype.some, native_lastIndexOf = Array_Prototype.lastIndexOf,
indexOf: ArrayPrototype.indexOf, native_isArray = Array['isArray'], // use [] notation since not in closure's externs
lastIndexOf: ArrayPrototype.lastIndexOf, native_keys = Object['keys'];
isArray: Array.isArray,
keys: Object.keys // Create a safe reference to the Underscore object for reference below.
}; var _ = function(obj) { return _.buildWrapper(obj) };
// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') exports._ = _;
// Export underscore to global scope.
root._ = _;
// Current version. // Current version.
_.VERSION = '0.5.8'; _.VERSION = '0.5.8';
@@ -72,13 +66,16 @@
_.forEach = function(obj, iterator, context) { _.forEach = function(obj, iterator, context) {
var index = 0; var index = 0;
try { try {
if (obj.forEach === Native.forEach) { if (obj.forEach === native_forEach) {
obj.forEach(iterator, context); obj.forEach(iterator, context);
} else if (_.isNumber(obj.length)) { } else if (_.isNumber(obj.length)) {
for (var i=0, l=obj.length; i<l; i++) iterator.call(context, obj[i], i, obj); for (var i=0, l=obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
} else { } else {
var keys = _.keys(obj), l = keys.length; for (var key in obj)
for (var i=0; i<l; i++) iterator.call(context, obj[keys[i]], keys[i], obj); if (hasOwnProperty.call(obj, key))
iterator.call(context, obj[key], key, obj);
// var keys = _.keys(obj), l = keys.length;
// for (var i=0; i<l; i++) iterator.call(context, obj[keys[i]], keys[i], obj);
} }
} catch(e) { } catch(e) {
if (e != breaker) throw e; if (e != breaker) throw e;
@@ -89,7 +86,7 @@
// Return the results of applying the iterator to each element. // Return the results of applying the iterator to each element.
// Delegates to JavaScript 1.6's native map if available. // Delegates to JavaScript 1.6's native map if available.
_.map = function(obj, iterator, context) { _.map = function(obj, iterator, context) {
if (obj.map === Native.map) return obj.map(iterator, context); if (obj.map === native_map) return obj.map(iterator, context);
var results = []; var results = [];
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
results.push(iterator.call(context, value, index, list)); results.push(iterator.call(context, value, index, list));
@@ -97,11 +94,10 @@
return results; return results;
}; };
// Reduce builds up a single result from a list of values. Also known as // Reduce builds up a single result from a list of values, aka inject, or foldl.
// inject, or foldl.
// Delegates to JavaScript 1.8's native reduce if available. // Delegates to JavaScript 1.8's native reduce if available.
_.reduce = function(obj, memo, iterator, context) { _.reduce = function(obj, memo, iterator, context) {
if (obj.reduce === Native.reduce) return obj.reduce(_.bind(iterator, context), memo); if (obj.reduce === native_reduce) return obj.reduce(_.bind(iterator, context), memo);
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
memo = iterator.call(context, memo, value, index, list); memo = iterator.call(context, memo, value, index, list);
}); });
@@ -111,7 +107,7 @@
// The right-associative version of reduce, also known as foldr. Uses // The right-associative version of reduce, also known as foldr. Uses
// Delegates to JavaScript 1.8's native reduceRight if available. // Delegates to JavaScript 1.8's native reduceRight if available.
_.reduceRight = function(obj, memo, iterator, context) { _.reduceRight = function(obj, memo, iterator, context) {
if (obj.reduceRight === Native.reduceRight) return obj.reduceRight(_.bind(iterator, context), memo); if (obj.reduceRight === native_reduceRight) return obj.reduceRight(_.bind(iterator, context), memo);
var reversed = _.clone(_.toArray(obj)).reverse(); var reversed = _.clone(_.toArray(obj)).reverse();
return reduce(reversed, memo, iterator, context); return reduce(reversed, memo, iterator, context);
}; };
@@ -131,7 +127,7 @@
// Return all the elements that pass a truth test. // Return all the elements that pass a truth test.
// Delegates to JavaScript 1.6's native filter if available. // Delegates to JavaScript 1.6's native filter if available.
_.filter = function(obj, iterator, context) { _.filter = function(obj, iterator, context) {
if (obj.filter === Native.filter) return obj.filter(iterator, context); if (obj.filter === native_filter) return obj.filter(iterator, context);
var results = []; var results = [];
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
iterator.call(context, value, index, list) && results.push(value); iterator.call(context, value, index, list) && results.push(value);
@@ -152,7 +148,7 @@
// Delegates to JavaScript 1.6's native every if available. // Delegates to JavaScript 1.6's native every if available.
_.every = function(obj, iterator, context) { _.every = function(obj, iterator, context) {
iterator = iterator || _.identity; iterator = iterator || _.identity;
if (obj.every === Native.every) return obj.every(iterator, context); if (obj.every === native_every) return obj.every(iterator, context);
var result = true; var result = true;
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop(); if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop();
@@ -164,7 +160,7 @@
// Delegates to JavaScript 1.6's native some if available. // Delegates to JavaScript 1.6's native some if available.
_.some = function(obj, iterator, context) { _.some = function(obj, iterator, context) {
iterator = iterator || _.identity; iterator = iterator || _.identity;
if (obj.some === Native.some) return obj.some(iterator, context); if (obj.some === native_some) return obj.some(iterator, context);
var result = false; var result = false;
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
if (result = iterator.call(context, value, index, list)) _.breakLoop(); if (result = iterator.call(context, value, index, list)) _.breakLoop();
@@ -349,7 +345,7 @@
// 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.
// Delegates to JavaScript 1.8's native indexOf if available. // Delegates to JavaScript 1.8's native indexOf if available.
_.indexOf = function(array, item) { _.indexOf = function(array, item) {
if (array.indexOf === Native.indexOf) return array.indexOf(item); if (array.indexOf === native_indexOf) return array.indexOf(item);
for (var i=0, l=array.length; i<l; i++) if (array[i] === item) return i; for (var i=0, l=array.length; i<l; i++) if (array[i] === item) return i;
return -1; return -1;
}; };
@@ -357,7 +353,7 @@
// Delegates to JavaScript 1.6's native lastIndexOf if available. // Delegates to JavaScript 1.6's native lastIndexOf if available.
_.lastIndexOf = function(array, item) { _.lastIndexOf = function(array, item) {
if (array.lastIndexOf === Native.lastIndexOf) return array.lastIndexOf(item); if (array.lastIndexOf === native_lastIndexOf) return array.lastIndexOf(item);
var i = array.length; var i = array.length;
while (i--) if (array[i] === item) return i; while (i--) if (array[i] === item) return i;
return -1; return -1;
@@ -439,7 +435,7 @@
// Retrieve the names of an object's properties. // Retrieve the names of an object's properties.
// Delegates to ECMA5's native Object.keys // Delegates to ECMA5's native Object.keys
_.keys = Native.keys || function(obj) { _.keys = native_keys || function(obj) {
if (_.isArray(obj)) return _.range(0, obj.length); if (_.isArray(obj)) return _.range(0, obj.length);
var keys = []; var keys = [];
for (var key in obj) if (hasOwnProperty.call(obj, key)) keys.push(key); for (var key in obj) if (hasOwnProperty.call(obj, key)) keys.push(key);
@@ -475,31 +471,6 @@
return obj; return obj;
}; };
// Alias a method on an object to another name(s).
// If the first argument is NOT an object, then the object is assumed to
// be underscore.
// The first string argument is the existing method name, any following
// strings will be aliased to that name.
// Returns the object for chainability.
//
// Examples:
//
// Alias isEquals to isSame and eql on underscore:
// _.alias('isEqual', 'isSame', 'eql');
//
// Alias `toString` to `to_s` on a given object:
// _.alias({}, 'toString', 'to_s')
//
// Implementation: explicitly cast arguments to array as calling mutating
// array methods on arguments object has strange behavior (at least in FF 3.6)
_.alias = function () {
var args = _.toArray(arguments),
obj = (typeof args[0] === 'string')? _ : args.shift(),
fn = obj[args.shift()];
each(args, function (alias) { obj[alias] = fn; });
return obj;
};
// Perform a deep comparison to check if two objects are equal. // Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) { _.isEqual = function(a, b) {
// Check object identity. // Check object identity.
@@ -550,7 +521,7 @@
// Is a given value an array? // Is a given value an array?
// Delegates to ECMA5's native Array.isArray // Delegates to ECMA5's native Array.isArray
_.isArray = Native.isArray || function(obj) { _.isArray = native_isArray || function(obj) {
return !!(obj && obj.concat && obj.unshift); return !!(obj && obj.concat && obj.unshift);
}; };
@@ -663,59 +634,79 @@
// ------------------------------- Aliases ---------------------------------- // ------------------------------- Aliases ----------------------------------
_.alias('forEach', 'each'). _.each = _.forEach;
alias('reduce', 'foldl', 'inject'). _.foldl = _.inject = _.reduce;
alias('reduceRight', 'foldr'). _.foldr = _.reduceRight;
alias('filter', 'select'). _.select = _.filter;
alias('every', 'all'). _.all = _.every;
alias('some', 'any'). _.any = _.some;
alias('first', 'head'). _.head = _.first;
alias('rest', 'tail'). _.tail = _.rest;
alias('functions', 'methods'); _.methods = _.functions;
// ------------------------ Setup the OOP Wrapper: -------------------------- // ------------------------ Setup the OOP Wrapper: --------------------------
_.buildWrapper = function () { throw "Call _.initWrapper() to enable OO wrapping" }
// Helper function to continue chaining intermediate results. _.initWrapper = function () {
var result = function(obj, chain) { if (_._wrapper) return; // Already initialized
return chain ? _(obj).chain() : obj;
};
// Add all of the Underscore functions to the wrapper object. // If Underscore is called as a function, it returns a wrapped object that
each(_.functions(_), function(name) { // can be used OO-style. This wrapper holds altered versions of all the
var method = _[name]; // underscore functions. Wrapped objects may be chained.
wrapper.prototype[name] = function() { var wrapper = function(obj) { this._wrapped = obj; };
var args = _.toArray(arguments);
unshift.call(args, this._wrapped); // Make wrapper object public so user code can extend it.
return result(method.apply(_, args), this._chain); // Otherwise, there is no way to modify its prototype
_._wrapper = wrapper;
// Overwrite method called from _()
_.buildWrapper = function (obj) { return new wrapper(obj) };
// Helper function to continue chaining intermediate results.
var result = function(obj, chain) {
return chain ? _(obj).chain() : obj;
}; };
});
// Add all mutator Array functions to the wrapper. // Add all of the Underscore functions to the wrapper object.
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) { each(_.functions(_), function(name) {
var method = ArrayPrototype[name]; var method = _[name];
wrapper.prototype[name] = function() { wrapper.prototype[name] = function() {
method.apply(this._wrapped, arguments); var args = _.toArray(arguments);
return result(this._wrapped, this._chain); unshift.call(args, this._wrapped);
return result(method.apply(_, args), this._chain);
};
});
// Add all mutator Array functions to the wrapper.
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
var method = Array_Prototype[name];
wrapper.prototype[name] = function() {
method.apply(this._wrapped, arguments);
return result(this._wrapped, this._chain);
};
});
// Add all accessor Array functions to the wrapper.
each(['concat', 'join', 'slice'], function(name) {
var method = Array_Prototype[name];
wrapper.prototype[name] = function() {
return result(method.apply(this._wrapped, arguments), this._chain);
};
});
// Start chaining a wrapped Underscore object.
wrapper.prototype.chain = function() {
this._chain = true;
return this;
}; };
});
// Add all accessor Array functions to the wrapper. // Extracts the result from a wrapped and chained object.
each(['concat', 'join', 'slice'], function(name) { wrapper.prototype.value = function() {
var method = ArrayPrototype[name]; return this._wrapped;
wrapper.prototype[name] = function() {
return result(method.apply(this._wrapped, arguments), this._chain);
}; };
}); }
// Start chaining a wrapped Underscore object.
wrapper.prototype.chain = function() {
this._chain = true;
return this;
};
// Extracts the result from a wrapped and chained object.
wrapper.prototype.value = function() {
return this._wrapped;
};
// For backwards compatability, init the OO wrapper
// the advanced minifying rake task will strip this out
_.initWrapper();
})(); })();