Skip to content

Initial release #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["babel-plugin-add-module-exports"],
"presets": ["es2015"]
}
56 changes: 56 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"parser": "babel-eslint",
"extends": "airbnb",
"env": {
"browser": true,
"es6": true,
"node": true
},
"ecmaFeatures": {
"jsx": true,
"classes": true,
"modules": true
},
"parserOptions": {
"sourceType": "module"
},
"extends": "eslint:recommended",
"rules": {
"indent": [
1,
4
],
"linebreak-style": [
2,
"windows"
],
"quotes": [
2,
"single"
],
"semi": [
2,
"always"
],
"strict": [
1,
"function"
],
"no-console": 0,
"no-unused-vars": [
1
]
},
"globals" : {
"document": false,
"escape": false,
"navigator": false,
"unescape": false,
"window": false,
"describe": true,
"before": true,
"it": true,
"expect": true,
"sinon": true
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log

\.idea/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
test
webpack.config.js
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- 7
- 6
- "0.10"
- "0.12"
- "iojs"
env:
- TEST_SUITE=unit
script:
- npm run build
- npm run $TEST_SUITE
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# NestedObjectAssign
# NestedObjectAssign
This package extends the functionality given by Object.assign() to also include the values of nested objects.

## Usage
Add the empty object first, then any objects you want merged into it after. unlimited amount of params.

Example: `nestedObjectAssign({}, defaults, object1, object2, object3)`

## Tests
Tests are done using mocha. to run tests, simply type `npm run tests`.
1 change: 1 addition & 0 deletions dist/nestedObjectAssign.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

// This gulpfile makes use of new JavaScript features.
// Babel handles this without us having to do anything. It just works.
// You can read more about the new JavaScript features here:
// https://babeljs.io/docs/learn-es2015/

import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import webpack from 'webpack';
import del from 'del';
import pkg from './package.json';
import webpackConfig from "./webpack.config.js";

const $ = gulpLoadPlugins();
const libFolder = 'lib';
const sources = './src/**/*.js';

gulp.task('default', ['build', 'build-web']);

// Build for node
gulp.task('build', pkg.library['bundle-node'] ? ['webpack:build-node'] : ['build-babel']);

// Build for node + watch
gulp.task('build-dev', ['webpack:build-node-dev'], () => {
gulp.watch([sources], ['webpack:build-node-dev'])
});

// Build for web
gulp.task('build-web', ['webpack:build-web']);

// Build for web + watch
gulp.task('build-web-dev', ['webpack:build-web-dev'], () => {
gulp.watch([sources], ['webpack:build-web-dev'])
});

// Run Babel only
gulp.task('build-babel', ['clean', 'lint'], () =>
gulp.src([sources])
.pipe($.babel())
// Output files
.pipe(gulp.dest(libFolder))
);

// Lint javascript
gulp.task('lint', () =>
gulp.src(sources)
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError())
);

// Clean folder
gulp.task('clean', () =>
del([`${libFolder}/**/*`])
);

// Webpack helper
gulp.task('webpack:build-web', done => {
var env = {'BUILD_ENV':'PROD', 'TARGET_ENV': 'WEB'};
var taskName = 'webpack:build-web';
// run webpack
webpack(webpackConfig(env), onBuild(done, taskName));
});

// Webpack watch helper
// create a single instance of the compiler to allow caching
var webDevCompiler = null;
gulp.task('webpack:build-web-dev', done => {
var env = {'BUILD_ENV':'DEV', 'TARGET_ENV': 'WEB'};
var taskName = 'webpack:build-web-dev';
// build dev compiler
if(!webDevCompiler){
webDevCompiler = webpack(webpackConfig(env));
}
// run webpack
webDevCompiler.run(onBuild(done, taskName));
});

// Webpack helper
gulp.task('webpack:build-node', done => {
var env = {'BUILD_ENV':'PROD', 'TARGET_ENV': 'NODE'};
var taskName = 'webpack:build-node';
// run webpack
webpack(webpackConfig(env), onBuild(done, taskName));
});

// Webpack watch helper
// create a single instance of the compiler to allow caching
var nodeDevCompiler = null;
gulp.task('webpack:build-node-dev', done => {
var env = {'BUILD_ENV':'DEV', 'TARGET_ENV': 'NODE'};
var taskName = 'webpack:build-node-dev';
// build dev compiler
if(!nodeDevCompiler){
nodeDevCompiler = webpack(webpackConfig(env));
}
// run webpack
nodeDevCompiler.run(onBuild(done, taskName));
});

function onBuild(done, taskName){
return (err, stats) => {
if(err)
throw new gutil.PluginError(taskName, err);
$.util.log(`${taskName}`, stats.toString({colors: true}));
done && done();
}
}

// Sets environment variable
function setEnv(buildEnv){
$.env({
vars: {
BUILD_ENV: buildEnv
}
});
}
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var pkg = require('./package.json');
var libFile = pkg.library['bundle-node'] ? pkg.library['dist-node'] : pkg.library['entry'];
module.exports = require('./lib/' + libFile);
57 changes: 57 additions & 0 deletions lib/nestedObjectAssign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9zcmMvbmVzdGVkT2JqZWN0QXNzaWduLmpzP2ZmYWMiXSwibmFtZXMiOlsibmVzdGVkT2JqZWN0QXNzaWduIiwidGFyZ2V0Iiwic291cmNlcyIsImxlbmd0aCIsInNvdXJjZSIsInNoaW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gICAgaWYgKCFzb3VyY2VzLmxlbmd0aClcclxuICAgICAgICByZXR1cm4gdGFyZ2V0O1xyXG5cclxuICAgIGNvbnN0IHNvdXJjZSA9IHNvdXJjZXMuc2hpZnQoKTtcclxuXHJcbiAgICBpZiAoaXNPYmplY3QodGFyZ2V0KSAmJiBpc09iamVjdChzb3VyY2UpKXtcclxuICAgICAgICBmb3IgKGNvbnN0IGtleSBpbiBzb3VyY2Upe1xyXG4gICAgICAgICAgICBpZiAoaXNPYmplY3Qoc291cmNlW2tleV0pKXtcclxuICAgICAgICAgICAgICAgIGlmICghdGFyZ2V0W2tleV0pXHJcbiAgICAgICAgICAgICAgICAgICAgT2JqZWN0LmFzc2lnbih0YXJnZXQsIHtba2V5XToge319KTtcclxuXHJcbiAgICAgICAgICAgICAgICBuZXN0ZWRPYmplY3RBc3NpZ24odGFyZ2V0W2tleV0sIHNvdXJjZVtrZXldKTtcclxuICAgICAgICAgICAgfVxyXG4gICAgICAgICAgICBlbHNlIHtcclxuICAgICAgICAgICAgICAgIE9iamVjdC5hc3NpZ24odGFyZ2V0LCB7W2tleV06IHNvdXJjZVtrZXldfSk7XHJcbiAgICAgICAgICAgIH1cclxuICAgICAgICB9XHJcbiAgICB9XHJcblxyXG4gICAgcmV0dXJuIG5lc3RlZE9iamVjdEFzc2lnbih0YXJnZXQsIC4uLnNvdXJjZXMpO1xyXG59XG5cblxuLy8gV0VCUEFDSyBGT09URVIgLy9cbi8vIC4vc3JjL25lc3RlZE9iamVjdEFzc2lnbi5qcyJdLCJzb3VyY2VSb290IjoiIn0=");

/***/ }),
/* 1 */
/***/ (function(module, exports) {

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==");

/***/ })
/******/ ]);
79 changes: 79 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"name": "nestedObjectAssign",
"version": "1.0.3",
"description": "Package to support nested merging of objects & properties, using Object.Assign",
"main": "./index.js",
"scripts": {
"prepublish": "npm run build-all",
"preversion": "npm run build-all && npm run unit",
"version": "git add .",
"postversion": "git push && git push --tags",
"build": "gulp build",
"build-dev": "gulp build-dev",
"build-web": "gulp build-web",
"build-web-dev": "gulp build-web-dev",
"build-all": "gulp",
"unit": "mocha --compilers js:babel-core/register --colors ./test/*.spec.js",
"unit-watch": "mocha --compilers js:babel-core/register --colors -w ./test/*.spec.js",
"test": "npm run unit-watch"
},
"repository": {
"type": "git",
"url": "https://github.com/Geta/NestedObjectAssign.git"
},
"keywords": [
"es6",
"npm",
"nested",
"object",
"assign"
],
"author": {
"name": "Geta AS / Eirik Horvath",
"url": "https://github.com/Geta"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/Geta/NestedObjectAssign/issues"
},
"files": [
"README.md",
"index.js",
"lib",
"dist"
],
"homepage": "https://github.com/Geta/NestedObjectAssignl",
"devDependencies": {
"babel": "^6.3.26",
"babel-core": "^6.4.0",
"babel-eslint": "^5.0.0-beta6",
"babel-loader": "^6.2.1",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-preset-es2015": "^6.3.13",
"chai": "^3.4.1",
"clean-webpack-plugin": "^0.1.8",
"del": "^2.2.0",
"eslint": "^1.10.3",
"eslint-config-airbnb": "^4.0.0",
"eslint-loader": "^1.2.0",
"eslint-plugin-react": "^3.16.1",
"eslint-plugin-standard": "^1.3.1",
"gulp": "^3.9.0",
"gulp-babel": "^6.1.1",
"gulp-env": "^0.2.0",
"gulp-eslint": "^1.1.1",
"gulp-load-plugins": "^1.2.0",
"gulp-util": "^3.0.6",
"mocha": "^2.3.4",
"object-assign": "^4.0.1",
"webpack": "^1.12.11",
"webpack-node-externals": "^0.4.1"
},
"library": {
"name": "nestedObjectAssign",
"entry": "nestedObjectAssign.js",
"dist-node": "nestedObjectAssign.js",
"dist-web": "nestedObjectAssign.web.js",
"bundle-node": true
}
}
3 changes: 3 additions & 0 deletions src/isObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isObject(item){
return (item && typeof item === 'object' && !Array.isArray(item));
}
24 changes: 24 additions & 0 deletions src/nestedObjectAssign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {isObject} from './isObject';

export default function nestedObjectAssign(target, ...sources){
if (!sources.length)
return target;

const source = sources.shift();

if (isObject(target) && isObject(source)){
for (const key in source){
if (isObject(source[key])){
if (!target[key])
Object.assign(target, {[key]: {}});

nestedObjectAssign(target[key], source[key]);
}
else {
Object.assign(target, {[key]: source[key]});
}
}
}

return nestedObjectAssign(target, ...sources);
}
Loading