mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
adding an _.mixin utility function that allows you to add functions to Underscore (also used internally)
This commit is contained in:
@@ -16,7 +16,7 @@ $(document).ready(function() {
|
|||||||
"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",
|
||||||
"isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
|
"isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
|
||||||
"methods", "min", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select",
|
"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",
|
||||||
"uniqueId", "values", "without", "wrap", "zip"];
|
"uniqueId", "values", "without", "wrap", "zip"];
|
||||||
same(expected, _.methods(_), 'provides a sorted list of functions');
|
same(expected, _.methods(_), 'provides a sorted list of functions');
|
||||||
|
|||||||
@@ -40,6 +40,15 @@ $(document).ready(function() {
|
|||||||
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
|
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("utility: mixin", function() {
|
||||||
|
_.mixin({
|
||||||
|
myReverse: function(string) {
|
||||||
|
return string.split('').reverse().join('');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
equals(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');
|
||||||
|
equals(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper');
|
||||||
|
});
|
||||||
|
|
||||||
test("utility: template", function() {
|
test("utility: template", function() {
|
||||||
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
|
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
hasOwnProperty = ObjProto.hasOwnProperty,
|
hasOwnProperty = ObjProto.hasOwnProperty,
|
||||||
propertyIsEnumerable = ObjProto.propertyIsEnumerable;
|
propertyIsEnumerable = ObjProto.propertyIsEnumerable;
|
||||||
|
|
||||||
// All native implementations we hope to use are declared here.
|
// All ECMA5 native implementations we hope to use are declared here.
|
||||||
var
|
var
|
||||||
nativeForEach = ArrayProto.forEach,
|
nativeForEach = ArrayProto.forEach,
|
||||||
nativeMap = ArrayProto.map,
|
nativeMap = ArrayProto.map,
|
||||||
@@ -50,6 +50,15 @@
|
|||||||
// underscore functions. Wrapped objects may be chained.
|
// underscore functions. Wrapped objects may be chained.
|
||||||
var wrapper = function(obj) { this._wrapped = obj; };
|
var wrapper = function(obj) { this._wrapped = obj; };
|
||||||
|
|
||||||
|
// A method to easily add functions to the OOP wrapper.
|
||||||
|
var addToWrapper = function(name, func) {
|
||||||
|
wrapper.prototype[name] = function() {
|
||||||
|
var args = _.toArray(arguments);
|
||||||
|
unshift.call(args, this._wrapped);
|
||||||
|
return result(func.apply(_, args), this._chain);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// 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 new wrapper(obj); };
|
var _ = function(obj) { return new wrapper(obj); };
|
||||||
|
|
||||||
@@ -431,7 +440,7 @@
|
|||||||
return _.map(obj, _.identity);
|
return _.map(obj, _.identity);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a sorted list of the function names available in Underscore.
|
// Return a sorted list of the function names available on the object.
|
||||||
_.functions = function(obj) {
|
_.functions = function(obj) {
|
||||||
return _.select(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
return _.select(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
|
||||||
};
|
};
|
||||||
@@ -579,6 +588,14 @@
|
|||||||
throw breaker;
|
throw breaker;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add your own custom functions to the Underscore object, ensuring that
|
||||||
|
// they're correctly added to the OOP wrapper as well.
|
||||||
|
_.mixin = function(obj) {
|
||||||
|
_.each(_.functions(obj), function(name){
|
||||||
|
addToWrapper(name, _[name] = obj[name]);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Generate a unique integer id (unique within the entire client session).
|
// Generate a unique integer id (unique within the entire client session).
|
||||||
// Useful for temporary DOM ids.
|
// Useful for temporary DOM ids.
|
||||||
var idCounter = 0;
|
var idCounter = 0;
|
||||||
@@ -634,14 +651,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Add all of the Underscore functions to the wrapper object.
|
// Add all of the Underscore functions to the wrapper object.
|
||||||
each(_.functions(_), function(name) {
|
_.mixin(_);
|
||||||
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.
|
// Add all mutator Array functions to the wrapper.
|
||||||
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
|
||||||
|
|||||||
Reference in New Issue
Block a user