Skip to content

Commit 0b621e2

Browse files
authored
Merge pull request #1 from Geta/develop
Initial release
2 parents 31da5f1 + 7261a34 commit 0b621e2

15 files changed

+530
-1
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["babel-plugin-add-module-exports"],
3+
"presets": ["es2015"]
4+
}

.eslintrc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "airbnb",
4+
"env": {
5+
"browser": true,
6+
"es6": true,
7+
"node": true
8+
},
9+
"ecmaFeatures": {
10+
"jsx": true,
11+
"classes": true,
12+
"modules": true
13+
},
14+
"parserOptions": {
15+
"sourceType": "module"
16+
},
17+
"extends": "eslint:recommended",
18+
"rules": {
19+
"indent": [
20+
1,
21+
4
22+
],
23+
"linebreak-style": [
24+
2,
25+
"windows"
26+
],
27+
"quotes": [
28+
2,
29+
"single"
30+
],
31+
"semi": [
32+
2,
33+
"always"
34+
],
35+
"strict": [
36+
1,
37+
"function"
38+
],
39+
"no-console": 0,
40+
"no-unused-vars": [
41+
1
42+
]
43+
},
44+
"globals" : {
45+
"document": false,
46+
"escape": false,
47+
"navigator": false,
48+
"unescape": false,
49+
"window": false,
50+
"describe": true,
51+
"before": true,
52+
"it": true,
53+
"expect": true,
54+
"sinon": true
55+
}
56+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
npm-debug.log
3+
4+
\.idea/

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
test
3+
webpack.config.js

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: node_js
2+
node_js:
3+
- 7
4+
- 6
5+
- "0.10"
6+
- "0.12"
7+
- "iojs"
8+
env:
9+
- TEST_SUITE=unit
10+
script:
11+
- npm run build
12+
- npm run $TEST_SUITE

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# NestedObjectAssign
1+
# NestedObjectAssign
2+
This package extends the functionality given by Object.assign() to also include the values of nested objects.
3+
4+
## Usage
5+
Add the empty object first, then any objects you want merged into it after. unlimited amount of params.
6+
7+
Example: `nestedObjectAssign({}, defaults, object1, object2, object3)`
8+
9+
## Tests
10+
Tests are done using mocha. to run tests, simply type `npm run tests`.

dist/nestedObjectAssign.web.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.babel.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
'use strict';
2+
3+
// This gulpfile makes use of new JavaScript features.
4+
// Babel handles this without us having to do anything. It just works.
5+
// You can read more about the new JavaScript features here:
6+
// https://babeljs.io/docs/learn-es2015/
7+
8+
import gulp from 'gulp';
9+
import gulpLoadPlugins from 'gulp-load-plugins';
10+
import webpack from 'webpack';
11+
import del from 'del';
12+
import pkg from './package.json';
13+
import webpackConfig from "./webpack.config.js";
14+
15+
const $ = gulpLoadPlugins();
16+
const libFolder = 'lib';
17+
const sources = './src/**/*.js';
18+
19+
gulp.task('default', ['build', 'build-web']);
20+
21+
// Build for node
22+
gulp.task('build', pkg.library['bundle-node'] ? ['webpack:build-node'] : ['build-babel']);
23+
24+
// Build for node + watch
25+
gulp.task('build-dev', ['webpack:build-node-dev'], () => {
26+
gulp.watch([sources], ['webpack:build-node-dev'])
27+
});
28+
29+
// Build for web
30+
gulp.task('build-web', ['webpack:build-web']);
31+
32+
// Build for web + watch
33+
gulp.task('build-web-dev', ['webpack:build-web-dev'], () => {
34+
gulp.watch([sources], ['webpack:build-web-dev'])
35+
});
36+
37+
// Run Babel only
38+
gulp.task('build-babel', ['clean', 'lint'], () =>
39+
gulp.src([sources])
40+
.pipe($.babel())
41+
// Output files
42+
.pipe(gulp.dest(libFolder))
43+
);
44+
45+
// Lint javascript
46+
gulp.task('lint', () =>
47+
gulp.src(sources)
48+
.pipe($.eslint())
49+
.pipe($.eslint.format())
50+
.pipe($.eslint.failOnError())
51+
);
52+
53+
// Clean folder
54+
gulp.task('clean', () =>
55+
del([`${libFolder}/**/*`])
56+
);
57+
58+
// Webpack helper
59+
gulp.task('webpack:build-web', done => {
60+
var env = {'BUILD_ENV':'PROD', 'TARGET_ENV': 'WEB'};
61+
var taskName = 'webpack:build-web';
62+
// run webpack
63+
webpack(webpackConfig(env), onBuild(done, taskName));
64+
});
65+
66+
// Webpack watch helper
67+
// create a single instance of the compiler to allow caching
68+
var webDevCompiler = null;
69+
gulp.task('webpack:build-web-dev', done => {
70+
var env = {'BUILD_ENV':'DEV', 'TARGET_ENV': 'WEB'};
71+
var taskName = 'webpack:build-web-dev';
72+
// build dev compiler
73+
if(!webDevCompiler){
74+
webDevCompiler = webpack(webpackConfig(env));
75+
}
76+
// run webpack
77+
webDevCompiler.run(onBuild(done, taskName));
78+
});
79+
80+
// Webpack helper
81+
gulp.task('webpack:build-node', done => {
82+
var env = {'BUILD_ENV':'PROD', 'TARGET_ENV': 'NODE'};
83+
var taskName = 'webpack:build-node';
84+
// run webpack
85+
webpack(webpackConfig(env), onBuild(done, taskName));
86+
});
87+
88+
// Webpack watch helper
89+
// create a single instance of the compiler to allow caching
90+
var nodeDevCompiler = null;
91+
gulp.task('webpack:build-node-dev', done => {
92+
var env = {'BUILD_ENV':'DEV', 'TARGET_ENV': 'NODE'};
93+
var taskName = 'webpack:build-node-dev';
94+
// build dev compiler
95+
if(!nodeDevCompiler){
96+
nodeDevCompiler = webpack(webpackConfig(env));
97+
}
98+
// run webpack
99+
nodeDevCompiler.run(onBuild(done, taskName));
100+
});
101+
102+
function onBuild(done, taskName){
103+
return (err, stats) => {
104+
if(err)
105+
throw new gutil.PluginError(taskName, err);
106+
$.util.log(`${taskName}`, stats.toString({colors: true}));
107+
done && done();
108+
}
109+
}
110+
111+
// Sets environment variable
112+
function setEnv(buildEnv){
113+
$.env({
114+
vars: {
115+
BUILD_ENV: buildEnv
116+
}
117+
});
118+
}

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var pkg = require('./package.json');
2+
var libFile = pkg.library['bundle-node'] ? pkg.library['dist-node'] : pkg.library['entry'];
3+
module.exports = require('./lib/' + libFile);

lib/nestedObjectAssign.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module.exports =
2+
/******/ (function(modules) { // webpackBootstrap
3+
/******/ // The module cache
4+
/******/ var installedModules = {};
5+
6+
/******/ // The require function
7+
/******/ function __webpack_require__(moduleId) {
8+
9+
/******/ // Check if module is in cache
10+
/******/ if(installedModules[moduleId])
11+
/******/ return installedModules[moduleId].exports;
12+
13+
/******/ // Create a new module (and put it into the cache)
14+
/******/ var module = installedModules[moduleId] = {
15+
/******/ exports: {},
16+
/******/ id: moduleId,
17+
/******/ loaded: false
18+
/******/ };
19+
20+
/******/ // Execute the module function
21+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
22+
23+
/******/ // Flag the module as loaded
24+
/******/ module.loaded = true;
25+
26+
/******/ // Return the exports of the module
27+
/******/ return module.exports;
28+
/******/ }
29+
30+
31+
/******/ // expose the modules object (__webpack_modules__)
32+
/******/ __webpack_require__.m = modules;
33+
34+
/******/ // expose the module cache
35+
/******/ __webpack_require__.c = installedModules;
36+
37+
/******/ // __webpack_public_path__
38+
/******/ __webpack_require__.p = "";
39+
40+
/******/ // Load entry module and return exports
41+
/******/ return __webpack_require__(0);
42+
/******/ })
43+
/************************************************************************/
44+
/******/ ([
45+
/* 0 */
46+
/***/ (function(module, exports, __webpack_require__) {
47+
48+
eval("'use strict';\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = nestedObjectAssign;\n\nvar _isObject = __webpack_require__(1);\n\nfunction _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }\n\nfunction nestedObjectAssign(target) {\n for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n sources[_key - 1] = arguments[_key];\n }\n\n if (!sources.length) return target;\n\n var source = sources.shift();\n\n if ((0, _isObject.isObject)(target) && (0, _isObject.isObject)(source)) {\n for (var key in source) {\n if ((0, _isObject.isObject)(source[key])) {\n if (!target[key]) Object.assign(target, _defineProperty({}, key, {}));\n\n nestedObjectAssign(target[key], source[key]);\n } else {\n Object.assign(target, _defineProperty({}, key, source[key]));\n }\n }\n }\n\n return nestedObjectAssign.apply(undefined, [target].concat(sources));\n}\nmodule.exports = exports['default'];\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9zcmMvbmVzdGVkT2JqZWN0QXNzaWduLmpzP2ZmYWMiXSwibmFtZXMiOlsibmVzdGVkT2JqZWN0QXNzaWduIiwidGFyZ2V0Iiwic291cmNlcyIsImxlbmd0aCIsInNvdXJjZSIsInNoaWZ0Iiwia2V5IiwiT2JqZWN0IiwiYXNzaWduIl0sIm1hcHBpbmdzIjoiOzs7OztrQkFFd0JBLGtCOztBQUZ4Qjs7OztBQUVlLFNBQVNBLGtCQUFULENBQTRCQyxNQUE1QixFQUErQztBQUFBLHNDQUFSQyxPQUFRO0FBQVJBLGVBQVE7QUFBQTs7QUFDMUQsUUFBSSxDQUFDQSxRQUFRQyxNQUFiLEVBQ0ksT0FBT0YsTUFBUDs7QUFFSixRQUFNRyxTQUFTRixRQUFRRyxLQUFSLEVBQWY7O0FBRUEsUUFBSSx3QkFBU0osTUFBVCxLQUFvQix3QkFBU0csTUFBVCxDQUF4QixFQUF5QztBQUNyQyxhQU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zc2lnbih0YXJnZXQsIHtba2V5XToge319KTtcclxuXHJcbiAgICAgICAgICAgICAgICBuZXN0ZWRPYmplY3RBc3NpZ24odGFyZ2V0W2tleV0sIHNvdXJjZVtrZXldKTtcclxuICAgICAgICAgICAgfVxyXG4gICAgICAgICAgICBlbHNlIHtcclxuICAgICAgICAgICAgICAgIE9iamVjdC5hc3NpZ24odGFyZ2V0LCB7W2tleV06IHNvdXJjZVtrZXldfSk7XHJcbiAgICAgICAgICAgIH1cclxuICAgICAgICB9XHJcbiAgICB9XHJcblxyXG4gICAgcmV0dXJuIG5lc3RlZE9iamVjdEFzc2lnbih0YXJnZXQsIC4uLnNvdXJjZXMpO1xyXG59XG5cblxuLy8gV0VCUEFDSyBGT09URVIgLy9cbi8vIC4vc3JjL25lc3RlZE9iamVjdEFzc2lnbi5qcyJdLCJzb3VyY2VSb290IjoiIn0=");
49+
50+
/***/ }),
51+
/* 1 */
52+
/***/ (function(module, exports) {
53+
54+
eval("'use strict';\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; };\n\nexports.isObject = isObject;\nfunction isObject(item) {\n return item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && !Array.isArray(item);\n}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9zcmMvaXNPYmplY3QuanM/YzBhOCJdLCJuYW1lcyI6WyJpc09iamVjdCIsIml0ZW0iLCJBcnJheSIsImlzQXJyYXkiXSwibWFwcGluZ3MiOiI7Ozs7Ozs7O1FBQWdCQSxRLEdBQUFBLFE7QUFBVCxTQUFTQSxRQUFULENBQWtCQyxJQUFsQixFQUF1QjtBQUMxQixXQUFRQSxRQUFRLFFBQU9BLElBQVAseUNBQU9BLElBQVAsT0FBZ0IsUUFBeEIsSUFBb0MsQ0FBQ0MsTUFBTUMsT0FBTixDQUFjRixJQUFkLENBQTdDO0FBQ0giLCJmaWxlIjoiMS5qcyIsInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBmdW5jdGlvbiBpc09iamVjdChpdGVtKXtcclxuICAgIHJldHVybiAoaXRlbSAmJiB0eXBlb2YgaXRlbSA9PT0gJ29iamVjdCcgJiYgIUFycmF5LmlzQXJyYXkoaXRlbSkpO1xyXG59XG5cblxuLy8gV0VCUEFDSyBGT09URVIgLy9cbi8vIC4vc3JjL2lzT2JqZWN0LmpzIl0sInNvdXJjZVJvb3QiOiIifQ==");
55+
56+
/***/ })
57+
/******/ ]);

package.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"name": "nestedObjectAssign",
3+
"version": "1.0.3",
4+
"description": "Package to support nested merging of objects & properties, using Object.Assign",
5+
"main": "./index.js",
6+
"scripts": {
7+
"prepublish": "npm run build-all",
8+
"preversion": "npm run build-all && npm run unit",
9+
"version": "git add .",
10+
"postversion": "git push && git push --tags",
11+
"build": "gulp build",
12+
"build-dev": "gulp build-dev",
13+
"build-web": "gulp build-web",
14+
"build-web-dev": "gulp build-web-dev",
15+
"build-all": "gulp",
16+
"unit": "mocha --compilers js:babel-core/register --colors ./test/*.spec.js",
17+
"unit-watch": "mocha --compilers js:babel-core/register --colors -w ./test/*.spec.js",
18+
"test": "npm run unit-watch"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/Geta/NestedObjectAssign.git"
23+
},
24+
"keywords": [
25+
"es6",
26+
"npm",
27+
"nested",
28+
"object",
29+
"assign"
30+
],
31+
"author": {
32+
"name": "Geta AS / Eirik Horvath",
33+
"url": "https://github.com/Geta"
34+
},
35+
"license": "MIT",
36+
"bugs": {
37+
"url": "https://github.com/Geta/NestedObjectAssign/issues"
38+
},
39+
"files": [
40+
"README.md",
41+
"index.js",
42+
"lib",
43+
"dist"
44+
],
45+
"homepage": "https://github.com/Geta/NestedObjectAssignl",
46+
"devDependencies": {
47+
"babel": "^6.3.26",
48+
"babel-core": "^6.4.0",
49+
"babel-eslint": "^5.0.0-beta6",
50+
"babel-loader": "^6.2.1",
51+
"babel-plugin-add-module-exports": "^0.1.2",
52+
"babel-preset-es2015": "^6.3.13",
53+
"chai": "^3.4.1",
54+
"clean-webpack-plugin": "^0.1.8",
55+
"del": "^2.2.0",
56+
"eslint": "^1.10.3",
57+
"eslint-config-airbnb": "^4.0.0",
58+
"eslint-loader": "^1.2.0",
59+
"eslint-plugin-react": "^3.16.1",
60+
"eslint-plugin-standard": "^1.3.1",
61+
"gulp": "^3.9.0",
62+
"gulp-babel": "^6.1.1",
63+
"gulp-env": "^0.2.0",
64+
"gulp-eslint": "^1.1.1",
65+
"gulp-load-plugins": "^1.2.0",
66+
"gulp-util": "^3.0.6",
67+
"mocha": "^2.3.4",
68+
"object-assign": "^4.0.1",
69+
"webpack": "^1.12.11",
70+
"webpack-node-externals": "^0.4.1"
71+
},
72+
"library": {
73+
"name": "nestedObjectAssign",
74+
"entry": "nestedObjectAssign.js",
75+
"dist-node": "nestedObjectAssign.js",
76+
"dist-web": "nestedObjectAssign.web.js",
77+
"bundle-node": true
78+
}
79+
}

src/isObject.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isObject(item){
2+
return (item && typeof item === 'object' && !Array.isArray(item));
3+
}

src/nestedObjectAssign.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {isObject} from './isObject';
2+
3+
export default function nestedObjectAssign(target, ...sources){
4+
if (!sources.length)
5+
return target;
6+
7+
const source = sources.shift();
8+
9+
if (isObject(target) && isObject(source)){
10+
for (const key in source){
11+
if (isObject(source[key])){
12+
if (!target[key])
13+
Object.assign(target, {[key]: {}});
14+
15+
nestedObjectAssign(target[key], source[key]);
16+
}
17+
else {
18+
Object.assign(target, {[key]: source[key]});
19+
}
20+
}
21+
}
22+
23+
return nestedObjectAssign(target, ...sources);
24+
}

0 commit comments

Comments
 (0)