mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 17:37:50 +00:00
remove the advanced_optimizations concessions and the advanced_optimizations rake task. Simple will do.
This commit is contained in:
17
Rakefile
17
Rakefile
@@ -8,20 +8,3 @@ task :build do
|
|||||||
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
|
|
||||||
|
|
||||||
|
|||||||
109
underscore.js
109
underscore.js
@@ -45,8 +45,13 @@
|
|||||||
nativeIsArray = Array.isArray,
|
nativeIsArray = Array.isArray,
|
||||||
nativeKeys = Object.keys;
|
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.
|
// 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.
|
// Export the Underscore object for CommonJS.
|
||||||
if (typeof exports !== 'undefined') exports._ = _;
|
if (typeof exports !== 'undefined') exports._ = _;
|
||||||
@@ -643,68 +648,48 @@
|
|||||||
_.methods = _.functions;
|
_.methods = _.functions;
|
||||||
|
|
||||||
// ------------------------ Setup the OOP Wrapper: --------------------------
|
// ------------------------ Setup the OOP Wrapper: --------------------------
|
||||||
_.buildWrapper = function() { throw "Call _.initWrapper() to enable OO wrapping"; };
|
|
||||||
|
|
||||||
_.initWrapper = function() {
|
// Helper function to continue chaining intermediate results.
|
||||||
if (_._wrapper) return; // Already initialized
|
var result = function(obj, chain) {
|
||||||
|
return chain ? _(obj).chain() : obj;
|
||||||
// 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.
|
// Add all of the Underscore functions to the wrapper object.
|
||||||
var wrapper = function(obj) { this._wrapped = obj; };
|
each(_.functions(_), function(name) {
|
||||||
|
var method = _[name];
|
||||||
// Make wrapper object public so user code can extend it.
|
wrapper.prototype[name] = function() {
|
||||||
// Otherwise, there is no way to modify its prototype
|
var args = _.toArray(arguments);
|
||||||
_._wrapper = wrapper;
|
unshift.call(args, this._wrapped);
|
||||||
|
return result(method.apply(_, args), this._chain);
|
||||||
// Overwrite method called from _()
|
};
|
||||||
_.buildWrapper = function (obj) { return new wrapper(obj); };
|
});
|
||||||
|
|
||||||
// Helper function to continue chaining intermediate results.
|
// Add all mutator Array functions to the wrapper.
|
||||||
var result = function(obj, chain) {
|
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
||||||
return chain ? _(obj).chain() : obj;
|
var method = ArrayProto[name];
|
||||||
};
|
wrapper.prototype[name] = function() {
|
||||||
|
method.apply(this._wrapped, arguments);
|
||||||
// Add all of the Underscore functions to the wrapper object.
|
return result(this._wrapped, this._chain);
|
||||||
each(_.functions(_), function(name) {
|
};
|
||||||
var method = _[name];
|
});
|
||||||
wrapper.prototype[name] = function() {
|
|
||||||
var args = _.toArray(arguments);
|
// Add all accessor Array functions to the wrapper.
|
||||||
unshift.call(args, this._wrapped);
|
each(['concat', 'join', 'slice'], function(name) {
|
||||||
return result(method.apply(_, args), this._chain);
|
var method = ArrayProto[name];
|
||||||
};
|
wrapper.prototype[name] = function() {
|
||||||
});
|
return result(method.apply(this._wrapped, arguments), this._chain);
|
||||||
|
};
|
||||||
// Add all mutator Array functions to the wrapper.
|
});
|
||||||
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
|
||||||
var method = ArrayProto[name];
|
// Start chaining a wrapped Underscore object.
|
||||||
wrapper.prototype[name] = function() {
|
wrapper.prototype.chain = function() {
|
||||||
method.apply(this._wrapped, arguments);
|
this._chain = true;
|
||||||
return result(this._wrapped, this._chain);
|
return this;
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
// Extracts the result from a wrapped and chained object.
|
||||||
// Add all accessor Array functions to the wrapper.
|
wrapper.prototype.value = function() {
|
||||||
each(['concat', 'join', 'slice'], function(name) {
|
return this._wrapped;
|
||||||
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();
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user