├── .editorconfig
├── .eslintrc.json
├── .gitattributes
├── .gitignore
├── .travis.yml
├── .verb.md
├── LICENSE
├── README.md
├── benchmark
├── code
│ ├── 1-1-4.js
│ ├── assign.js
│ ├── extend-shallow.js
│ ├── for-arguments-in.js
│ ├── for-arguments.js
│ ├── for-each.js
│ ├── for-negative.js
│ ├── for-own-lib.js
│ ├── for-own-slice-call.js
│ ├── for-own.js
│ ├── for-prop-in.js
│ ├── for-slice-call-in.js
│ ├── for.js
│ ├── object-assign.js
│ ├── reduce.js
│ ├── while-arguments-in.js
│ ├── while-for-own.js
│ ├── while-for.js
│ ├── while-while.js
│ ├── while-while2.js
│ └── while.js
├── fixtures
│ ├── basic.js
│ ├── nested.js
│ └── sparse.js
└── index.js
├── bower.json
├── index.js
├── package.json
└── test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org/
2 | root = true
3 |
4 | [*]
5 | charset = utf-8
6 | end_of_line = lf
7 | indent_size = 2
8 | indent_style = space
9 | insert_final_newline = true
10 | trim_trailing_whitespace = true
11 |
12 | [{**/{actual,fixtures,expected,templates}/**,*.md}]
13 | trim_trailing_whitespace = false
14 | insert_final_newline = false
15 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "browser": false,
4 | "es6": true,
5 | "node": true,
6 | "mocha": true
7 | },
8 |
9 | "globals": {
10 | "document": false,
11 | "navigator": false,
12 | "window": false
13 | },
14 |
15 | "rules": {
16 | "accessor-pairs": 2,
17 | "arrow-spacing": [2, { "before": true, "after": true }],
18 | "block-spacing": [2, "always"],
19 | "brace-style": [2, "1tbs", { "allowSingleLine": true }],
20 | "comma-dangle": [2, "never"],
21 | "comma-spacing": [2, { "before": false, "after": true }],
22 | "comma-style": [2, "last"],
23 | "constructor-super": 2,
24 | "curly": [2, "multi-line"],
25 | "dot-location": [2, "property"],
26 | "eol-last": 2,
27 | "eqeqeq": [2, "allow-null"],
28 | "generator-star-spacing": [2, { "before": true, "after": true }],
29 | "handle-callback-err": [2, "^(err|error)$" ],
30 | "indent": [2, 2, { "SwitchCase": 1 }],
31 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
32 | "keyword-spacing": [2, { "before": true, "after": true }],
33 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
34 | "new-parens": 2,
35 | "no-array-constructor": 2,
36 | "no-caller": 2,
37 | "no-class-assign": 2,
38 | "no-cond-assign": 2,
39 | "no-const-assign": 2,
40 | "no-control-regex": 2,
41 | "no-debugger": 2,
42 | "no-delete-var": 2,
43 | "no-dupe-args": 2,
44 | "no-dupe-class-members": 2,
45 | "no-dupe-keys": 2,
46 | "no-duplicate-case": 2,
47 | "no-empty-character-class": 2,
48 | "no-eval": 2,
49 | "no-ex-assign": 2,
50 | "no-extend-native": 2,
51 | "no-extra-bind": 2,
52 | "no-extra-boolean-cast": 2,
53 | "no-extra-parens": [2, "functions"],
54 | "no-fallthrough": 2,
55 | "no-floating-decimal": 2,
56 | "no-func-assign": 2,
57 | "no-implied-eval": 2,
58 | "no-inner-declarations": [2, "functions"],
59 | "no-invalid-regexp": 2,
60 | "no-irregular-whitespace": 2,
61 | "no-iterator": 2,
62 | "no-label-var": 2,
63 | "no-labels": 2,
64 | "no-lone-blocks": 2,
65 | "no-mixed-spaces-and-tabs": 2,
66 | "no-multi-spaces": 2,
67 | "no-multi-str": 2,
68 | "no-multiple-empty-lines": [2, { "max": 1 }],
69 | "no-native-reassign": 0,
70 | "no-negated-in-lhs": 2,
71 | "no-new": 2,
72 | "no-new-func": 2,
73 | "no-new-object": 2,
74 | "no-new-require": 2,
75 | "no-new-wrappers": 2,
76 | "no-obj-calls": 2,
77 | "no-octal": 2,
78 | "no-octal-escape": 2,
79 | "no-proto": 0,
80 | "no-redeclare": 2,
81 | "no-regex-spaces": 2,
82 | "no-return-assign": 2,
83 | "no-self-compare": 2,
84 | "no-sequences": 2,
85 | "no-shadow-restricted-names": 2,
86 | "no-spaced-func": 2,
87 | "no-sparse-arrays": 2,
88 | "no-this-before-super": 2,
89 | "no-throw-literal": 2,
90 | "no-trailing-spaces": 0,
91 | "no-undef": 2,
92 | "no-undef-init": 2,
93 | "no-unexpected-multiline": 2,
94 | "no-unneeded-ternary": [2, { "defaultAssignment": false }],
95 | "no-unreachable": 2,
96 | "no-unused-vars": [2, { "vars": "all", "args": "none" }],
97 | "no-useless-call": 0,
98 | "no-with": 2,
99 | "one-var": [0, { "initialized": "never" }],
100 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
101 | "padded-blocks": [0, "never"],
102 | "quotes": [2, "single", "avoid-escape"],
103 | "radix": 2,
104 | "semi": [2, "always"],
105 | "semi-spacing": [2, { "before": false, "after": true }],
106 | "space-before-blocks": [2, "always"],
107 | "space-before-function-paren": [2, "never"],
108 | "space-in-parens": [2, "never"],
109 | "space-infix-ops": 2,
110 | "space-unary-ops": [2, { "words": true, "nonwords": false }],
111 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
112 | "use-isnan": 2,
113 | "valid-typeof": 2,
114 | "wrap-iife": [2, "any"],
115 | "yoda": [2, "never"]
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Enforce Unix newlines
2 | * text eol=lf
3 |
4 | # binaries
5 | *.ai binary
6 | *.psd binary
7 | *.jpg binary
8 | *.gif binary
9 | *.png binary
10 | *.jpeg binary
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # always ignore files
2 | *.DS_Store
3 | .idea
4 | *.sublime-*
5 |
6 | # test related, or directories generated by tests
7 | test/actual
8 | actual
9 | coverage
10 | .nyc*
11 |
12 | # npm
13 | node_modules
14 | npm-debug.log
15 |
16 | # yarn
17 | yarn.lock
18 | yarn-error.log
19 |
20 | # misc
21 | _gh_pages
22 | _draft
23 | _drafts
24 | bower_components
25 | vendor
26 | temp
27 | tmp
28 | TODO.md
29 | package-lock.json
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | os:
3 | - linux
4 | - osx
5 | language: node_js
6 | node_js:
7 | - node
8 | - '9'
9 | - '8'
10 | - '7'
11 | - '6'
12 | - '5'
13 | - '4'
14 | - '0.12'
15 | - '0.10'
16 |
--------------------------------------------------------------------------------
/.verb.md:
--------------------------------------------------------------------------------
1 | ## Usage
2 |
3 | ```js
4 | var extend = require('{%= name %}');
5 |
6 | extend({a: 'b'}, {c: 'd'})
7 | //=> {a: 'b', c: 'd'}
8 | ```
9 |
10 | Pass an empty object to shallow clone:
11 |
12 | ```js
13 | var obj = {};
14 | extend(obj, {a: 'b'}, {c: 'd'})
15 | //=> {a: 'b', c: 'd'}
16 | ```
17 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014-2015, 2017, Jon Schlinkert.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # extend-shallow [](https://www.npmjs.com/package/extend-shallow) [](https://npmjs.org/package/extend-shallow) [](https://npmjs.org/package/extend-shallow) [](https://travis-ci.org/jonschlinkert/extend-shallow)
2 |
3 | > Extend an object with the properties of additional objects. node.js/javascript util.
4 |
5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6 |
7 | ## Install
8 |
9 | Install with [npm](https://www.npmjs.com/):
10 |
11 | ```sh
12 | $ npm install --save extend-shallow
13 | ```
14 |
15 | ## Usage
16 |
17 | ```js
18 | var extend = require('extend-shallow');
19 |
20 | extend({a: 'b'}, {c: 'd'})
21 | //=> {a: 'b', c: 'd'}
22 | ```
23 |
24 | Pass an empty object to shallow clone:
25 |
26 | ```js
27 | var obj = {};
28 | extend(obj, {a: 'b'}, {c: 'd'})
29 | //=> {a: 'b', c: 'd'}
30 | ```
31 |
32 | ## About
33 |
34 |
35 | Contributing
36 |
37 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
38 |
39 |
40 |
41 |
42 | Running Tests
43 |
44 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
45 |
46 | ```sh
47 | $ npm install && npm test
48 | ```
49 |
50 |
51 |
52 |
53 | Building docs
54 |
55 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
56 |
57 | To generate the readme, run the following command:
58 |
59 | ```sh
60 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
61 | ```
62 |
63 |
64 |
65 | ### Related projects
66 |
67 | You might also be interested in these projects:
68 |
69 | * [extend-shallow](https://www.npmjs.com/package/extend-shallow): Extend an object with the properties of additional objects. node.js/javascript util. | [homepage](https://github.com/jonschlinkert/extend-shallow "Extend an object with the properties of additional objects. node.js/javascript util.")
70 | * [for-in](https://www.npmjs.com/package/for-in): Iterate over the own and inherited enumerable properties of an object, and return an object… [more](https://github.com/jonschlinkert/for-in) | [homepage](https://github.com/jonschlinkert/for-in "Iterate over the own and inherited enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning `false`. JavaScript/Node.js")
71 | * [for-own](https://www.npmjs.com/package/for-own): Iterate over the own enumerable properties of an object, and return an object with properties… [more](https://github.com/jonschlinkert/for-own) | [homepage](https://github.com/jonschlinkert/for-own "Iterate over the own enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning `false`. JavaScript/Node.js.")
72 | * [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object "Returns true if an object was created by the `Object` constructor.")
73 | * [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject "Returns true if the value is an object and not an array or null.")
74 | * [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.")
75 |
76 | ### Contributors
77 |
78 | | **Commits** | **Contributor** |
79 | | --- | --- |
80 | | 33 | [jonschlinkert](https://github.com/jonschlinkert) |
81 | | 1 | [pdehaan](https://github.com/pdehaan) |
82 |
83 | ### Author
84 |
85 | **Jon Schlinkert**
86 |
87 | * [github/jonschlinkert](https://github.com/jonschlinkert)
88 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
89 |
90 | ### License
91 |
92 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
93 | Released under the [MIT License](LICENSE).
94 |
95 | ***
96 |
97 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 19, 2017._
--------------------------------------------------------------------------------
/benchmark/code/1-1-4.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var typeOf = require('kind-of');
4 |
5 | /**
6 | * Expose `extend`
7 | */
8 |
9 | module.exports = extend;
10 |
11 | /**
12 | * Extend `o` with properties of other `objects`.
13 | *
14 | * @param {Object} `o` The target object. Pass an empty object to shallow clone.
15 | * @param {Object} `objects`
16 | * @return {Object}
17 | */
18 |
19 | function extend(o) {
20 | if (typeOf(o) !== 'object') { return {}; }
21 | var args = arguments;
22 | var len = args.length - 1;
23 |
24 | for (var i = 0; i < len; i++) {
25 | var obj = args[i + 1];
26 |
27 | if (typeOf(obj) === 'object' && typeOf(obj) !== 'regexp') {
28 | for (var key in obj) {
29 | if (obj.hasOwnProperty(key)) {
30 | o[key] = obj[key];
31 | }
32 | }
33 | }
34 | }
35 | return o;
36 | };
37 |
--------------------------------------------------------------------------------
/benchmark/code/assign.js:
--------------------------------------------------------------------------------
1 |
2 | var isObject = require('is-extendable');
3 |
4 | module.exports = function extend(a, b) {
5 | if (!a) return {};
6 | if (!b) return a;
7 | var len = arguments.length - 1, i = 1;
8 | while (len--) {
9 | var obj = arguments[i++];
10 | if (isObject(obj)) {
11 | assign(a, obj);
12 | }
13 | }
14 | return a;
15 | };
16 |
17 | function assign(o, obj) {
18 | for (var key in obj) {
19 | if (obj.hasOwnProperty(key)) {
20 | o[key] = obj[key];
21 | }
22 | }
23 | return o;
24 | }
25 |
--------------------------------------------------------------------------------
/benchmark/code/extend-shallow.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = require('../..');
4 |
--------------------------------------------------------------------------------
/benchmark/code/for-arguments-in.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) { o = {}; }
7 |
8 | var len = arguments.length;
9 | for (var i = 1; i < len; i++) {
10 | var obj = arguments[i];
11 |
12 | if (isObject(obj)) {
13 | assign(o, obj);
14 | }
15 | }
16 | return o;
17 | };
18 |
19 |
20 | function assign(a, b) {
21 | for (var key in b) {
22 | a[key] = b[key];
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/benchmark/code/for-arguments.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-plain-object');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!o || !objects) { return o || {}; }
7 |
8 | var len = arguments.length - 1;
9 | for (var i = 0; i < len; i++) {
10 | var obj = arguments[i + 1];
11 |
12 | if (isObject(obj)) {
13 | for (var key in obj) {
14 | if (obj.hasOwnProperty(key)) {
15 | o[key] = obj[key];
16 | }
17 | }
18 | }
19 | }
20 | return o;
21 | };
22 |
--------------------------------------------------------------------------------
/benchmark/code/for-each.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 |
8 | var args = [].slice.call(arguments);
9 |
10 | args.forEach(function (obj) {
11 | if (isObject(obj)) {
12 | for (var key in obj) {
13 | if (obj.hasOwnProperty(key)) {
14 | o[key] = obj[key];
15 | }
16 | }
17 | }
18 | });
19 |
20 | return o;
21 | };
22 |
23 |
--------------------------------------------------------------------------------
/benchmark/code/for-negative.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var slice = require('array-slice');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (o == null) { return {}; }
7 | if (objects == null) { return o; }
8 |
9 | var args = slice(arguments, 1);
10 | var len = args.length - 1;
11 |
12 | for (var i = len; i >= 0; i--) {
13 | var obj = args[i];
14 |
15 | for (var key in obj) {
16 | if (obj.hasOwnProperty(key)) {
17 | o[key] = obj[key];
18 | }
19 | }
20 | }
21 |
22 | return o;
23 | };
--------------------------------------------------------------------------------
/benchmark/code/for-own-lib.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var slice = require('array-slice');
4 | var forOwn = require('for-own');
5 |
6 | module.exports = function extend(o, objects) {
7 | if (o == null) { return {}; }
8 | if (objects == null) { return o; }
9 |
10 | var args = slice(arguments, 1);
11 | var len = args.length;
12 |
13 | for (var i = 0; i < len; i++) {
14 | var obj = args[i];
15 | forOwn(obj, function (value, key) {
16 | this[key] = value;
17 | }, o);
18 | }
19 |
20 | return o;
21 | };
22 |
--------------------------------------------------------------------------------
/benchmark/code/for-own-slice-call.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = function extend(o, objects) {
4 | if (o == null) { return {}; }
5 | if (objects == null) { return o; }
6 |
7 | var args = [].slice.call([], arguments, 1);
8 | var len = args.length;
9 |
10 | for (var i = 0; i < len; i++) {
11 | var obj = args[i];
12 |
13 | for (var key in obj) {
14 | if (obj.hasOwnProperty(key)) {
15 | o[key] = obj[key];
16 | }
17 | }
18 | }
19 | return o;
20 | };
21 |
--------------------------------------------------------------------------------
/benchmark/code/for-own.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var slice = require('array-slice');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (o == null) { return {}; }
7 | if (objects == null) { return o; }
8 |
9 | var args = slice(arguments, 1);
10 | var len = args.length;
11 |
12 | for (var i = 0; i < len; i++) {
13 | var obj = args[i];
14 |
15 | for (var key in obj) {
16 | if (obj.hasOwnProperty(key)) {
17 | o[key] = obj[key];
18 | }
19 | }
20 | }
21 |
22 | return o;
23 | };
24 |
--------------------------------------------------------------------------------
/benchmark/code/for-prop-in.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var slice = require('array-slice');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (o == null) { return {}; }
7 | if (objects == null) { return o; }
8 |
9 | var args = slice(arguments, 1);
10 | var len = args.length;
11 |
12 | for (var i = 0; i < len; i++) {
13 | var obj = args[i];
14 |
15 | for (var key in obj) {
16 | o[key] = obj[key];
17 | }
18 | }
19 |
20 | return o;
21 | };
22 |
--------------------------------------------------------------------------------
/benchmark/code/for-slice-call-in.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = function extend(o, objects) {
4 | if (o == null) { return {}; }
5 | if (objects == null) { return o; }
6 |
7 | var args = [].slice.call([], arguments, 1);
8 | var len = args.length;
9 |
10 | for (var i = 0; i < len; i++) {
11 | var obj = args[i];
12 |
13 | for (var key in obj) {
14 | o[key] = obj[key];
15 | }
16 | }
17 | return o;
18 | };
19 |
--------------------------------------------------------------------------------
/benchmark/code/for.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 | var len = arguments.length;
8 |
9 | for (var i = 1; i < len; i++) {
10 | var obj = arguments[i];
11 | if (isObject(obj)) {
12 | for (var key in obj) {
13 | if (obj.hasOwnProperty(key)) {
14 | o[key] = obj[key];
15 | }
16 | }
17 | }
18 | }
19 | return o;
20 | };
21 |
--------------------------------------------------------------------------------
/benchmark/code/object-assign.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = require('object-assign');
4 |
--------------------------------------------------------------------------------
/benchmark/code/reduce.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 | var args = [].slice.call(arguments, 1);
8 |
9 | return args.reduce(function (acc, val, i) {
10 | if (isObject(val)) {
11 | for (var key in val) {
12 | if (val.hasOwnProperty(key)) {
13 | acc[key] = val[key];
14 | }
15 | }
16 | }
17 | return acc;
18 | }, o);
19 | };
20 |
--------------------------------------------------------------------------------
/benchmark/code/while-arguments-in.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) { o = {}; }
7 |
8 | var len = arguments.length, i = 1;
9 | while (len--) {
10 | var obj = arguments[i++];
11 |
12 | if (isObject(obj)) {
13 |
14 | for (var key in obj) {
15 | o[key] = obj[key];
16 | }
17 | }
18 | }
19 | return o;
20 | };
21 |
--------------------------------------------------------------------------------
/benchmark/code/while-for-own.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 |
8 | var len = arguments.length;
9 | var i = 1;
10 |
11 | while (len--) {
12 | var obj = arguments[i++];
13 | if (isObject(obj)) {
14 | var keys = Object.keys(obj);
15 | var klen = keys.length;
16 |
17 | for (var j = 0; j < klen; j++) {
18 | var key = keys[j];
19 | if (obj.hasOwnProperty(key)) {
20 | o[key] = obj[key];
21 | }
22 | }
23 | }
24 | }
25 | return o;
26 | };
27 |
--------------------------------------------------------------------------------
/benchmark/code/while-for.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 |
8 | var len = arguments.length;
9 | var i = 1;
10 |
11 | while (len--) {
12 | var obj = arguments[i++];
13 | if (isObject(obj)) {
14 | var keys = Object.keys(obj);
15 | var klen = keys.length;
16 |
17 | for (var j = 0; j < klen; j++) {
18 | var key = keys[j];
19 | o[key] = obj[key];
20 | }
21 | }
22 | }
23 | return o;
24 | };
25 |
--------------------------------------------------------------------------------
/benchmark/code/while-while.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 |
8 | var len = arguments.length;
9 | var i = 1;
10 |
11 | while (len--) {
12 | var obj = arguments[i++];
13 | if (isObject(obj)) {
14 | var keys = Object.keys(obj);
15 | var klen = keys.length, j = 0;
16 |
17 | while (klen--) {
18 | var key = keys[j++];
19 | o[key] = obj[key];
20 | }
21 | }
22 | }
23 | return o;
24 | };
25 |
--------------------------------------------------------------------------------
/benchmark/code/while-while2.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = function extend(o, objects) {
6 | if (!isObject(o)) {o = {};}
7 |
8 | var len = arguments.length;
9 | var i = 1;
10 |
11 | while (len--) {
12 | var obj = arguments[i++];
13 | if (isObject(obj)) {
14 | var keys = Object.keys(obj);
15 | var klen = keys.length, j = 0;
16 | var key;
17 |
18 | while (key = keys[j++]) {
19 | o[key] = obj[key];
20 | }
21 | }
22 | }
23 | return o;
24 | };
25 |
--------------------------------------------------------------------------------
/benchmark/code/while.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isObject = require('is-extendable');
4 |
5 | module.exports = extend;
6 |
7 | function extend(o, objects) {
8 | if (!isObject(o)) {
9 | o = {};
10 | }
11 | var len = arguments.length, i = 1;
12 | while (len--) {
13 | var obj = arguments[i++];
14 |
15 | if (isObject(obj)) {
16 | for (var key in obj) {
17 | if (obj.hasOwnProperty(key)) {
18 | o[key] = obj[key];
19 | }
20 | }
21 | }
22 | }
23 | return o;
24 | }
25 |
--------------------------------------------------------------------------------
/benchmark/fixtures/basic.js:
--------------------------------------------------------------------------------
1 | module.exports = [{a: 'b'}, {c: 'd'}, {e: 'f'}];
2 |
--------------------------------------------------------------------------------
/benchmark/fixtures/nested.js:
--------------------------------------------------------------------------------
1 | module.exports = [
2 | {a: 'a', m: {x: 'x'}},
3 | {b: 'b', m: {x: 'q'}},
4 | {c: 'c', m: {y: 'y'}},
5 | {d: 'd', m: {z: 'z'}},
6 | {d: 'e'}
7 | ];
--------------------------------------------------------------------------------
/benchmark/fixtures/sparse.js:
--------------------------------------------------------------------------------
1 | module.exports = [
2 | {a: 'a', m: {x: 'x'}},
3 | {b: 'b', m: {x: 'q'}},
4 | '',
5 | {d: 'd', m: {z: 'z'}},
6 | null,
7 | {d: 'e'}
8 | ];
--------------------------------------------------------------------------------
/benchmark/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const path = require('path');
4 | const argv = require('minimist')(process.argv.slice(2));
5 | const suite = require('benchmarked');
6 | const glob = argv._[0] || '*';
7 |
8 | suite.run({code: `code/${glob}.js`, fixtures: `fixtures/*.js`})
9 | .then(function(stats) {
10 | stats.forEach(function(target) {
11 | target.fastest = target.fastest || ['n/a'];
12 | });
13 | console.log(suite.render(stats));
14 | })
15 | .catch(console.error);
16 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "extend-shallow",
3 | "description": "Extend an object with the properties of additional objects. node.js/javascript util.",
4 | "repository": "jonschlinkert/extend-shallow",
5 | "license": "MIT",
6 | "homepage": "https://github.com/jonschlinkert/extend-shallow",
7 | "authors": [
8 | "Jon Schlinkert (https://github.com/jonschlinkert)"
9 | ],
10 | "main": [
11 | "index.js"
12 | ],
13 | "dependencies": {
14 | "is-extendable": "^1.0.1"
15 | },
16 | "devDependencies": {
17 | "array-slice": "^1.0.0",
18 | "benchmarked": "^2.0.0",
19 | "for-own": "^1.0.0",
20 | "gulp-format-md": "^1.0.0",
21 | "is-plain-object": "^2.0.4",
22 | "kind-of": "^6.0.1",
23 | "minimist": "^1.2.0",
24 | "mocha": "^3.5.3",
25 | "object-assign": "^4.1.1"
26 | },
27 | "keywords": [
28 | "assign",
29 | "clone",
30 | "extend",
31 | "merge",
32 | "obj",
33 | "object",
34 | "object-assign",
35 | "object.assign",
36 | "prop",
37 | "properties",
38 | "property",
39 | "props",
40 | "shallow",
41 | "util",
42 | "utility",
43 | "utils",
44 | "value"
45 | ],
46 | "version": "2.0.1",
47 | "bugs": {
48 | "url": "https://github.com/jonschlinkert/extend-shallow/issues"
49 | },
50 | "files": [
51 | "index.js"
52 | ],
53 | "ignore": [
54 | "actual",
55 | "bower_components",
56 | "fixtures",
57 | "node_modules",
58 | "temp",
59 | "test",
60 | "test.js",
61 | "tmp"
62 | ],
63 | "contributors": [
64 | "Jon Schlinkert (http://twitter.com/jonschlinkert)",
65 | "Peter deHaan (http://about.me/peterdehaan)"
66 | ]
67 | }
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var isExtendable = require('is-extendable');
4 | var assignSymbols = require('assign-symbols');
5 |
6 | module.exports = Object.assign || function(obj/*, objects*/) {
7 | if (obj === null || typeof obj === 'undefined') {
8 | throw new TypeError('Cannot convert undefined or null to object');
9 | }
10 | if (!isObject(obj)) {
11 | obj = {};
12 | }
13 | for (var i = 1; i < arguments.length; i++) {
14 | var val = arguments[i];
15 | if (isString(val)) {
16 | val = toObject(val);
17 | }
18 | if (isObject(val)) {
19 | assign(obj, val);
20 | assignSymbols(obj, val);
21 | }
22 | }
23 | return obj;
24 | };
25 |
26 | function assign(a, b) {
27 | for (var key in b) {
28 | if (hasOwn(b, key)) {
29 | a[key] = b[key];
30 | }
31 | }
32 | }
33 |
34 | function isString(val) {
35 | return (val && typeof val === 'string');
36 | }
37 |
38 | function toObject(str) {
39 | var obj = {};
40 | for (var i in str) {
41 | obj[i] = str[i];
42 | }
43 | return obj;
44 | }
45 |
46 | function isObject(val) {
47 | return (val && typeof val === 'object') || isExtendable(val);
48 | }
49 |
50 | /**
51 | * Returns true if the given `key` is an own property of `obj`.
52 | */
53 |
54 | function hasOwn(obj, key) {
55 | return Object.prototype.hasOwnProperty.call(obj, key);
56 | }
57 |
58 | function isEnum(obj, key) {
59 | return Object.prototype.propertyIsEnumerable.call(obj, key);
60 | }
61 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "extend-shallow",
3 | "description": "Extend an object with the properties of additional objects. node.js/javascript util.",
4 | "version": "3.0.2",
5 | "homepage": "https://github.com/jonschlinkert/extend-shallow",
6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
7 | "contributors": [
8 | "Jon Schlinkert (http://twitter.com/jonschlinkert)",
9 | "Peter deHaan (http://about.me/peterdehaan)"
10 | ],
11 | "repository": "jonschlinkert/extend-shallow",
12 | "bugs": {
13 | "url": "https://github.com/jonschlinkert/extend-shallow/issues"
14 | },
15 | "license": "MIT",
16 | "files": [
17 | "index.js"
18 | ],
19 | "main": "index.js",
20 | "engines": {
21 | "node": ">=0.10.0"
22 | },
23 | "scripts": {
24 | "test": "mocha"
25 | },
26 | "dependencies": {
27 | "assign-symbols": "^1.0.0",
28 | "is-extendable": "^1.0.1"
29 | },
30 | "devDependencies": {
31 | "array-slice": "^1.0.0",
32 | "benchmarked": "^2.0.0",
33 | "for-own": "^1.0.0",
34 | "gulp-format-md": "^1.0.0",
35 | "is-plain-object": "^2.0.4",
36 | "kind-of": "^6.0.1",
37 | "minimist": "^1.2.0",
38 | "mocha": "^3.5.3",
39 | "object-assign": "^4.1.1"
40 | },
41 | "keywords": [
42 | "assign",
43 | "clone",
44 | "extend",
45 | "merge",
46 | "obj",
47 | "object",
48 | "object-assign",
49 | "object.assign",
50 | "prop",
51 | "properties",
52 | "property",
53 | "props",
54 | "shallow",
55 | "util",
56 | "utility",
57 | "utils",
58 | "value"
59 | ],
60 | "verb": {
61 | "toc": false,
62 | "layout": "default",
63 | "tasks": [
64 | "readme"
65 | ],
66 | "related": {
67 | "list": [
68 | "extend-shallow",
69 | "for-in",
70 | "for-own",
71 | "is-plain-object",
72 | "isobject",
73 | "kind-of"
74 | ]
75 | },
76 | "plugins": [
77 | "gulp-format-md"
78 | ],
79 | "lint": {
80 | "reflinks": true
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * extend-shallow
3 | *
4 | * Copyright (c) 2014-2015, Jon Schlinkert.
5 | * Licensed under the MIT License.
6 | */
7 |
8 | 'use strict';
9 |
10 | require('mocha');
11 | var hasSymbol = typeof global.Symbol === 'function';
12 | var path = require('path');
13 | var argv = require('minimist')(process.argv.slice(2));
14 | var assert = require('assert');
15 | var extend = require('./');
16 |
17 | if (argv && argv.lib) {
18 | extend = require(path.resolve('benchmark/code', argv.lib));
19 | }
20 |
21 | describe('extend', function() {
22 | it('should extend the first object with the properties of the other objects', function() {
23 | assert.deepEqual(extend({a: 'b'}, {c: 'd'}), {a: 'b', c: 'd'});
24 | assert.deepEqual(extend({a: 'b', c: 'd'}, {c: 'e'}), {a: 'b', c: 'e'});
25 | });
26 |
27 | it('should handle non-plain objects', function() {
28 | assert.deepEqual(extend({a: 'b'}, 'foo', {c: 'd'}), {0: 'f', 1: 'o', 2: 'o', a: 'b', c: 'd'});
29 | assert.deepEqual(extend({a: 'b'}, null, {c: 'd'}), {a: 'b', c: 'd'});
30 | assert.deepEqual(extend({a: 'b'}, new Date(), {c: 'd'}), {a: 'b', c: 'd'});
31 | });
32 |
33 | it('should extend a regex', function() {
34 | var fixture = /foo/;
35 | extend(fixture, {a: 'b'}, new Date(), {c: 'd'});
36 | assert.equal(fixture.a, 'b');
37 | assert.equal(fixture.c, 'd');
38 | });
39 |
40 | it('should extend a function', function() {
41 | var fixture = function() {};
42 | extend(fixture, {a: 'b'}, new Date(), {c: 'd'});
43 | assert.equal(fixture.a, 'b');
44 | assert.equal(fixture.c, 'd');
45 | });
46 |
47 | it('should extend an array', function() {
48 | var arr = [];
49 | extend(arr, {a: 'b'}, new Date(), {c: 'd'});
50 | assert.equal(arr.a, 'b');
51 | assert.equal(arr.c, 'd');
52 | });
53 |
54 | it('should throw when the first arg is invalid', function() {
55 | assert.throws(function() {
56 | extend(null);
57 | });
58 | assert.throws(function() {
59 | extend(undefined);
60 | });
61 | assert.throws(function() {
62 | extend();
63 | });
64 | });
65 |
66 | it('should return an empty object when non-object is passed', function() {
67 | assert.deepEqual(extend(123), {});
68 | });
69 |
70 | it('should not extend non-enumerable symbols', function() {
71 | if (!hasSymbol) return this.skip();
72 | var fixture = {};
73 | var obj = {};
74 | var symbol = Symbol('foo');
75 | Object.defineProperty(obj, symbol, {enumerable: false, value: 'bar'});
76 | extend(fixture, obj);
77 | var other = extend({}, obj);
78 | assert.equal(typeof fixture[symbol], 'undefined');
79 | assert.equal(typeof other[symbol], 'undefined');
80 | });
81 |
82 | it('should extend symbol properties', function() {
83 | if (!hasSymbol) return this.skip();
84 | var fixture = {};
85 | var obj = {};
86 | var symbol = Symbol('foo');
87 | obj[symbol] = 'bar';
88 | extend(fixture, obj);
89 | var other = extend({}, obj);
90 | assert.equal(fixture[symbol], 'bar');
91 | assert.equal(other[symbol], 'bar');
92 | });
93 | });
94 |
--------------------------------------------------------------------------------