UNPKG

1.57 kBJavaScriptView Raw
1var assignValue = require('./_assignValue'),
2 copyObject = require('./_copyObject'),
3 createAssigner = require('./_createAssigner'),
4 isArrayLike = require('./isArrayLike'),
5 isPrototype = require('./_isPrototype'),
6 keys = require('./keys');
7
8/** Used for built-in method references. */
9var objectProto = Object.prototype;
10
11/** Used to check objects for own properties. */
12var hasOwnProperty = objectProto.hasOwnProperty;
13
14/**
15 * Assigns own enumerable string keyed properties of source objects to the
16 * destination object. Source objects are applied from left to right.
17 * Subsequent sources overwrite property assignments of previous sources.
18 *
19 * **Note:** This method mutates `object` and is loosely based on
20 * [`Object.assign`](https://mdn.io/Object/assign).
21 *
22 * @static
23 * @memberOf _
24 * @since 0.10.0
25 * @category Object
26 * @param {Object} object The destination object.
27 * @param {...Object} [sources] The source objects.
28 * @returns {Object} Returns `object`.
29 * @see _.assignIn
30 * @example
31 *
32 * function Foo() {
33 * this.a = 1;
34 * }
35 *
36 * function Bar() {
37 * this.c = 3;
38 * }
39 *
40 * Foo.prototype.b = 2;
41 * Bar.prototype.d = 4;
42 *
43 * _.assign({ 'a': 0 }, new Foo, new Bar);
44 * // => { 'a': 1, 'c': 3 }
45 */
46var assign = createAssigner(function(object, source) {
47 if (isPrototype(source) || isArrayLike(source)) {
48 copyObject(source, keys(source), object);
49 return;
50 }
51 for (var key in source) {
52 if (hasOwnProperty.call(source, key)) {
53 assignValue(object, key, source[key]);
54 }
55 }
56});
57
58module.exports = assign;