Add _.create.

This commit is contained in:
John-David Dalton
2013-10-19 01:08:09 -07:00
parent cd9332cddf
commit de8e6d7174
7 changed files with 364 additions and 255 deletions

126
dist/lodash.compat.js vendored
View File

@@ -1263,7 +1263,7 @@
// non `Object` object instances with different constructors are not equal
if (ctorA != ctorB &&
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
(!nativeCreate || ('constructor' in a && 'constructor' in b))
('constructor' in a && 'constructor' in b)
) {
return false;
}
@@ -1603,7 +1603,7 @@
}
if (this instanceof bound) {
// ensure `new bound` is an instance of `func`
thisBinding = createObject(func.prototype);
thisBinding = create(func.prototype);
// mimic the constructor's `return` behavior
// http://es5.github.io/#x13.2.2
@@ -1665,28 +1665,6 @@
);
}
/**
* Creates a new object with the specified `prototype`.
*
* @private
* @param {Object} prototype The prototype object.
* @returns {Object} Returns the new object.
*/
function createObject(prototype) {
return isObject(prototype) ? nativeCreate(prototype) : {};
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
createObject = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
}
return result || {};
};
}
/**
* Used by `escape` to convert characters to HTML entities.
*
@@ -2076,6 +2054,50 @@
return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
}
/**
* Creates a new object with the specified `prototype`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} prototype The prototype object.
* @returns {Object} Returns the new object.
* @example
*
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* function Circle() {
* Shape.call(this);
* }
*
* Circle.prototype = _.create(Shape.prototype);
* Circle.prototype.constructor = Circle;
*
* var circle = new Circle;
* circle instanceof Circle
* // => true
*
* circle instanceof Shape
* // => true
*/
function create(prototype) {
return isObject(prototype) ? nativeCreate(prototype) : {};
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
create = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
}
return result || {};
};
}
/**
* Assigns own enumerable properties of source object(s) to the destination
* object for all destination properties that resolve to `undefined`. Once a
@@ -2220,18 +2242,20 @@
* @returns {Object} Returns `object`.
* @example
*
* function Dog(name) {
* this.name = name;
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* Dog.prototype.bark = function() {
* console.log('Woof, woof!');
* Shape.prototype.move = function(x, y) {
* this.x += x;
* this.y += y;
* };
*
* _.forIn(new Dog('Dagny'), function(value, key) {
* _.forIn(new Shape, function(value, key) {
* console.log(key);
* });
* // => logs 'bark' and 'name' (property order is not guaranteed across environments)
* // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments)
*/
var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, {
'useHas': false
@@ -2250,18 +2274,20 @@
* @returns {Object} Returns `object`.
* @example
*
* function Dog(name) {
* this.name = name;
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* Dog.prototype.bark = function() {
* console.log('Woof, woof!');
* Shape.prototype.move = function(x, y) {
* this.x += x;
* this.y += y;
* };
*
* _.forInRight(new Dog('Dagny'), function(value, key) {
* _.forInRight(new Shape, function(value, key) {
* console.log(key);
* });
* // => logs 'name' and 'bark' assuming `_.forIn ` logs 'bark' and 'name'
* // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move'
*/
function forInRight(object, callback, thisArg) {
var pairs = [];
@@ -2703,18 +2729,18 @@
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
* @example
*
* function Person(name, age) {
* this.name = name;
* this.age = age;
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* _.isPlainObject(new Person('fred', 40));
* _.isPlainObject(new Shape);
* // => false
*
* _.isPlainObject([1, 2, 3]);
* // => false
*
* _.isPlainObject({ 'name': 'fred', 'age': 40 });
* _.isPlainObject({ 'x': 0, 'y': 0 });
* // => true
*/
var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) {
@@ -2998,7 +3024,7 @@
* @static
* @memberOf _
* @category Objects
* @param {Array|Object} collection The collection to iterate over.
* @param {Array|Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [accumulator] The custom accumulator value.
* @param {*} [thisArg] The `this` binding of `callback`.
@@ -3020,8 +3046,6 @@
*/
function transform(object, callback, accumulator, thisArg) {
var isArr = isArray(object);
callback = baseCreateCallback(callback, thisArg, 4);
if (accumulator == null) {
if (isArr) {
accumulator = [];
@@ -3029,12 +3053,15 @@
var ctor = object && object.constructor,
proto = ctor && ctor.prototype;
accumulator = createObject(proto);
accumulator = create(proto);
}
}
(isArr ? baseEach : forOwn)(object, function(value, index, object) {
return callback(accumulator, value, index, object);
});
if (callback) {
callback = baseCreateCallback(callback, thisArg, 4);
(isArr ? baseEach : forOwn)(object, function(value, index, object) {
return callback(accumulator, value, index, object);
});
}
return accumulator;
}
@@ -6036,7 +6063,7 @@
* // => 8
*/
var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) {
// Firefox and Opera still follow the ES3 specified implementation of `parseInt`
// Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt`
return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0);
};
@@ -6528,6 +6555,7 @@
lodash.compact = compact;
lodash.compose = compose;
lodash.countBy = countBy;
lodash.create = create;
lodash.createCallback = createCallback;
lodash.curry = curry;
lodash.debounce = debounce;