Skip to content

Commit

Permalink
remove the advanced_optimizations concessions and the advanced_optimi…
Browse files Browse the repository at this point in the history
…zations rake task. Simple will do.
  • Loading branch information
jashkenas committed Feb 24, 2010
1 parent 69bb349 commit 2c8fbe7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 75 deletions.
19 changes: 1 addition & 18 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
require 'rubygems'
require 'closure-compiler'

desc "Use the Closure Compiler to compress Underscore.js"
task :build do
js = File.open('underscore.js', 'r')
min = Closure::Compiler.new.compile(js)
File.open('underscore-min.js', 'w') {|f| f.write(min) }
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

99 changes: 42 additions & 57 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@
nativeIsArray = Array.isArray,
nativeKeys = Object.keys;

// 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; };

// Create a safe reference to the Underscore object for reference below.
var _ = function(obj) { return _.buildWrapper(obj); };
var _ = function(obj) { return new wrapper(obj); };

// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') exports._ = _;
Expand Down Expand Up @@ -643,68 +648,48 @@
_.methods = _.functions;

// ------------------------ Setup the OOP Wrapper: --------------------------
_.buildWrapper = function() { throw "Call _.initWrapper() to enable OO wrapping"; };

_.initWrapper = function() {
if (_._wrapper) return; // Already initialized

// 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; };

// Make wrapper object public so user code can extend it.
// 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;
};

// Helper function to continue chaining intermediate results.
var result = function(obj, chain) {
return chain ? _(obj).chain() : obj;
// Add all of the Underscore functions to the wrapper object.
each(_.functions(_), function(name) {
var method = _[name];
wrapper.prototype[name] = function() {
var args = _.toArray(arguments);
unshift.call(args, this._wrapped);
return result(method.apply(_, args), this._chain);
};

// Add all of the Underscore functions to the wrapper object.
each(_.functions(_), function(name) {
var method = _[name];
wrapper.prototype[name] = function() {
var args = _.toArray(arguments);
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 = ArrayProto[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 = ArrayProto[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 mutator Array functions to the wrapper.
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
var method = ArrayProto[name];
wrapper.prototype[name] = function() {
method.apply(this._wrapped, arguments);
return result(this._wrapped, this._chain);
};
});

// Extracts the result from a wrapped and chained object.
wrapper.prototype.value = function() {
return this._wrapped;
// Add all accessor Array functions to the wrapper.
each(['concat', 'join', 'slice'], function(name) {
var method = ArrayProto[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;
};

// 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();
})();

0 comments on commit 2c8fbe7

Please sign in to comment.