From 589ee945d14f57c3558bf9cc07b7caa12d444c66 Mon Sep 17 00:00:00 2001 From: Eirik Horvath Date: Fri, 2 Jun 2017 12:16:17 +0200 Subject: [PATCH 1/4] Created scripts. TODO: Finish tests. --- .babelrc | 3 + .gitignore | 2 + README.md | 4 +- dist/nested-object.assign.js | 123 +++++++++++++++++++++++++++++++++ package.json | 30 ++++++++ src/components/isObject.js | 3 + src/components/nestedAssign.js | 24 +++++++ src/index.js | 5 ++ test/test.js | 51 ++++++++++++++ webpack.config.js | 23 ++++++ 10 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 .babelrc create mode 100644 .gitignore create mode 100644 dist/nested-object.assign.js create mode 100644 package.json create mode 100644 src/components/isObject.js create mode 100644 src/components/nestedAssign.js create mode 100644 src/index.js create mode 100644 test/test.js create mode 100644 webpack.config.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..af0f0c3 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c5690e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 2fbe759..b91dedc 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# NestedObjectAssign \ No newline at end of file +# NestedObjectAssign + +Package that is created to give functionality which merges nested properties, which currently Object.Assign does not support. \ No newline at end of file diff --git a/dist/nested-object.assign.js b/dist/nested-object.assign.js new file mode 100644 index 0000000..ce3caa5 --- /dev/null +++ b/dist/nested-object.assign.js @@ -0,0 +1,123 @@ +/******/ (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] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = 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; +/******/ +/******/ // identity function for calling harmony imports with the correct context +/******/ __webpack_require__.i = function(value) { return value; }; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { +/******/ configurable: false, +/******/ enumerable: true, +/******/ get: getter +/******/ }); +/******/ } +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = 2); +/******/ }) +/************************************************************************/ +/******/ ([ +/* 0 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +/* harmony export (immutable) */ __webpack_exports__["a"] = nestedAssign; +/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__isObject__ = __webpack_require__(1); + + +function nestedAssign(target, ...sources) { + if (!sources.length) return target; + + const source = sources.shift(); + + if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(target) && __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(source)) { + for (const key in source) { + if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }); + + nestedAssign(target[key], source[key]); + } else { + Object.assign(target, { [key]: source[key] }); + } + } + } + + return nestedAssign(target, ...sources); +} + +/***/ }), +/* 1 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +/* harmony export (immutable) */ __webpack_exports__["a"] = isObject; +function isObject(item) { + return item && typeof item === 'object' && !Array.isArray(item); +} + +/***/ }), +/* 2 */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); +/* harmony export (immutable) */ __webpack_exports__["default"] = NestedAssign; +/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__components_nestedAssign__ = __webpack_require__(0); + + +function NestedAssign(target, ...sources) { + return __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__components_nestedAssign__["a" /* default */])(target, ...sources); +} + +/***/ }) +/******/ ]); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..be607e8 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "nested-object.assign", + "version": "1.0.0", + "description": "method for allowing nested objects to be merged, which object.assign doesn't currently support", + "main": "dist/nested-object.assign.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "mocha" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Geta/NestedObjectAssign.git" + }, + "author": "Eirik Horvath", + "license": "ISC", + "bugs": { + "url": "https://github.com/Geta/NestedObjectAssign/issues" + }, + "homepage": "https://github.com/Geta/NestedObjectAssign#readme", + "devDependencies": { + "babel": "^6.23.0", + "babel-core": "^6.24.1", + "babel-loader": "^7.0.0", + "babel-preset-es2015": "^6.24.1", + "mocha": "^3.4.2", + "webpack": "^2.6.1" + } +} diff --git a/src/components/isObject.js b/src/components/isObject.js new file mode 100644 index 0000000..3051f7a --- /dev/null +++ b/src/components/isObject.js @@ -0,0 +1,3 @@ +export default function isObject(item) { + return (item && typeof item === 'object' && !Array.isArray(item)); +} \ No newline at end of file diff --git a/src/components/nestedAssign.js b/src/components/nestedAssign.js new file mode 100644 index 0000000..94fe48f --- /dev/null +++ b/src/components/nestedAssign.js @@ -0,0 +1,24 @@ +import isObject from './isObject'; + +export default function nestedAssign(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]: {}}); + + nestedAssign(target[key], source[key]); + } + else { + Object.assign(target, {[key]: source[key]}); + } + } + } + + return nestedAssign(target, ...sources); +} \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..f867729 --- /dev/null +++ b/src/index.js @@ -0,0 +1,5 @@ +import nestedAssign from "./components/nestedAssign"; + +export default function nestedObjectAssign(target, ...sources){ + return nestedAssign(target, ...sources); +} \ No newline at end of file diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..06931bb --- /dev/null +++ b/test/test.js @@ -0,0 +1,51 @@ +var assert = require('assert'); +import nestedObjectAssign from '../src/index'; + +var mockData = { + default: { + heading: 'title', + body: { + paragraph: 'p', + heading: 'h1' + } + }, + first: { + body: { + span: 'span', + header: 'header' + } + }, + second: { + body: { + heading2: 'h2' + } + } + +} + +var expectedData = { + heading: 'title', + body: { + paragraph: 'p', + heading: 'h1', + span: 'span', + header: 'header', + heading2: 'h2' + } +} + +var test = function(){ + var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second); + + return mergedData === expectedData; +} + +describe('Object', function() { + describe('nestedObjectAssign', function() { + it('should return true when values are equal', function() { + var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second); + + assert.equal(mergedData, expectedData); + }) + }) +}); diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..8146310 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,23 @@ +var path = require('path'); +var webpack = require('webpack'); + +module.exports = { + context: path.resolve('src'), + entry: './index', + output: { + path: path.resolve('dist'), + filename: 'nested-object.assign.js' + }, + module:{ + loaders: [ + { + test: /\.js$/, + exclude: [/node_modules/, /dist/], + loader: 'babel-loader' + } + ] + }, + resolve: { + extensions: ['.js'] + } +}; \ No newline at end of file From a5c3e2083cea80128a28bfbdc35bec42665bc4ae Mon Sep 17 00:00:00 2001 From: Eirik Horvath Date: Fri, 2 Jun 2017 15:12:04 +0200 Subject: [PATCH 2/4] Completed tests. --- README.md | 4 +++- package.json | 2 +- test/test.js => tests/nestedObjectAssignTest.js | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) rename test/test.js => tests/nestedObjectAssignTest.js (86%) diff --git a/README.md b/README.md index b91dedc..c5ad6ec 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # NestedObjectAssign +This package extends the functionality given by Object.assign() to also include the values of nested objects. -Package that is created to give functionality which merges nested properties, which currently Object.Assign does not support. \ No newline at end of file +## Tests +Tests are done using mocha. to run tests, simply type `npm run tests`. \ No newline at end of file diff --git a/package.json b/package.json index be607e8..e169617 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "test" }, "scripts": { - "test": "mocha" + "tests": "mocha tests --compilers js:babel-core/register" }, "repository": { "type": "git", diff --git a/test/test.js b/tests/nestedObjectAssignTest.js similarity index 86% rename from test/test.js rename to tests/nestedObjectAssignTest.js index 06931bb..23275bf 100644 --- a/test/test.js +++ b/tests/nestedObjectAssignTest.js @@ -42,10 +42,10 @@ var test = function(){ describe('Object', function() { describe('nestedObjectAssign', function() { - it('should return true when values are equal', function() { + it('Return true when objects & values are equal', function() { var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second); - assert.equal(mergedData, expectedData); + assert.equal(JSON.stringify(mergedData), JSON.stringify(expectedData)); }) }) }); From b57a4c0c9218e11411e1699fd00dac084cabc699 Mon Sep 17 00:00:00 2001 From: Eirik Horvath Date: Fri, 2 Jun 2017 15:48:19 +0200 Subject: [PATCH 3/4] Updated naming --- .gitignore | 3 +- README.md | 3 + ...object.assign.js => nestedObjectAssign.js} | 73 +++++++++++++------ package.json | 2 +- ...{nestedAssign.js => nestedObjectAssign.js} | 6 +- src/index.js | 6 +- tests/nestedObjectAssignTest.js | 12 +-- webpack.config.js | 7 +- 8 files changed, 69 insertions(+), 43 deletions(-) rename dist/{nested-object.assign.js => nestedObjectAssign.js} (56%) rename src/components/{nestedAssign.js => nestedObjectAssign.js} (72%) diff --git a/.gitignore b/.gitignore index 2c5690e..2d50d8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ -/.idea/ \ No newline at end of file +/.idea/ +npm-debug.log diff --git a/README.md b/README.md index c5ad6ec..59bb019 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # NestedObjectAssign This package extends the functionality given by Object.assign() to also include the values of nested objects. +## Usage +To use this + ## Tests Tests are done using mocha. to run tests, simply type `npm run tests`. \ No newline at end of file diff --git a/dist/nested-object.assign.js b/dist/nestedObjectAssign.js similarity index 56% rename from dist/nested-object.assign.js rename to dist/nestedObjectAssign.js index ce3caa5..5e2d84d 100644 --- a/dist/nested-object.assign.js +++ b/dist/nestedObjectAssign.js @@ -68,56 +68,85 @@ /************************************************************************/ /******/ ([ /* 0 */ -/***/ (function(module, __webpack_exports__, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { "use strict"; -/* harmony export (immutable) */ __webpack_exports__["a"] = nestedAssign; -/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__isObject__ = __webpack_require__(1); -function nestedAssign(target, ...sources) { +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = nestedObjectAssign; + +var _isObject = __webpack_require__(1); + +var _isObject2 = _interopRequireDefault(_isObject); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _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; } + +function nestedObjectAssign(target) { + for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + sources[_key - 1] = arguments[_key]; + } + if (!sources.length) return target; - const source = sources.shift(); + var source = sources.shift(); - if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(target) && __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(source)) { - for (const key in source) { - if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(source[key])) { - if (!target[key]) Object.assign(target, { [key]: {} }); + if ((0, _isObject2.default)(target) && (0, _isObject2.default)(source)) { + for (var key in source) { + if ((0, _isObject2.default)(source[key])) { + if (!target[key]) Object.assign(target, _defineProperty({}, key, {})); - nestedAssign(target[key], source[key]); + nestedObjectAssign(target[key], source[key]); } else { - Object.assign(target, { [key]: source[key] }); + Object.assign(target, _defineProperty({}, key, source[key])); } } } - return nestedAssign(target, ...sources); + return nestedObjectAssign.apply(undefined, [target].concat(sources)); } /***/ }), /* 1 */ -/***/ (function(module, __webpack_exports__, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { "use strict"; -/* harmony export (immutable) */ __webpack_exports__["a"] = isObject; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _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; }; + +exports.default = isObject; function isObject(item) { - return item && typeof item === 'object' && !Array.isArray(item); + return item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && !Array.isArray(item); } /***/ }), /* 2 */ -/***/ (function(module, __webpack_exports__, __webpack_require__) { +/***/ (function(module, exports, __webpack_require__) { "use strict"; -Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); -/* harmony export (immutable) */ __webpack_exports__["default"] = NestedAssign; -/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__components_nestedAssign__ = __webpack_require__(0); -function NestedAssign(target, ...sources) { - return __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__components_nestedAssign__["a" /* default */])(target, ...sources); -} +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _nestedObjectAssign = __webpack_require__(0); + +Object.defineProperty(exports, 'nestedObjectAssign', { + enumerable: true, + get: function get() { + return _nestedObjectAssign.nestedObjectAssign; + } +}); /***/ }) /******/ ]); \ No newline at end of file diff --git a/package.json b/package.json index e169617..6d6e02c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "nested-object.assign", "version": "1.0.0", "description": "method for allowing nested objects to be merged, which object.assign doesn't currently support", - "main": "dist/nested-object.assign.js", + "main": "dist/nestedObjectAssign.js", "directories": { "test": "test" }, diff --git a/src/components/nestedAssign.js b/src/components/nestedObjectAssign.js similarity index 72% rename from src/components/nestedAssign.js rename to src/components/nestedObjectAssign.js index 94fe48f..04c2631 100644 --- a/src/components/nestedAssign.js +++ b/src/components/nestedObjectAssign.js @@ -1,6 +1,6 @@ import isObject from './isObject'; -export default function nestedAssign(target, ...sources){ +export default function nestedObjectAssign(target, ...sources){ if (!sources.length) return target; @@ -12,7 +12,7 @@ export default function nestedAssign(target, ...sources){ if (!target[key]) Object.assign(target, {[key]: {}}); - nestedAssign(target[key], source[key]); + nestedObjectAssign(target[key], source[key]); } else { Object.assign(target, {[key]: source[key]}); @@ -20,5 +20,5 @@ export default function nestedAssign(target, ...sources){ } } - return nestedAssign(target, ...sources); + return nestedObjectAssign(target, ...sources); } \ No newline at end of file diff --git a/src/index.js b/src/index.js index f867729..7cb3744 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1 @@ -import nestedAssign from "./components/nestedAssign"; - -export default function nestedObjectAssign(target, ...sources){ - return nestedAssign(target, ...sources); -} \ No newline at end of file +export {nestedObjectAssign} from './components/nestedObjectAssign'; \ No newline at end of file diff --git a/tests/nestedObjectAssignTest.js b/tests/nestedObjectAssignTest.js index 23275bf..1df9039 100644 --- a/tests/nestedObjectAssignTest.js +++ b/tests/nestedObjectAssignTest.js @@ -1,5 +1,5 @@ var assert = require('assert'); -import nestedObjectAssign from '../src/index'; +import nestedObjectAssign from '../dist/nestedObjectAssign'; var mockData = { default: { @@ -21,7 +21,7 @@ var mockData = { } } -} +}; var expectedData = { heading: 'title', @@ -32,13 +32,7 @@ var expectedData = { header: 'header', heading2: 'h2' } -} - -var test = function(){ - var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second); - - return mergedData === expectedData; -} +}; describe('Object', function() { describe('nestedObjectAssign', function() { diff --git a/webpack.config.js b/webpack.config.js index 8146310..b142542 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -6,14 +6,17 @@ module.exports = { entry: './index', output: { path: path.resolve('dist'), - filename: 'nested-object.assign.js' + filename: 'nestedObjectAssign.js' }, module:{ loaders: [ { test: /\.js$/, exclude: [/node_modules/, /dist/], - loader: 'babel-loader' + loader: 'babel-loader', + query: { + presets: ['es2015'] + } } ] }, From 7261a34b4c6efb85c486b1e41bc1eb9e026905c6 Mon Sep 17 00:00:00 2001 From: Eirik Horvath Date: Wed, 7 Jun 2017 15:24:35 +0200 Subject: [PATCH 4/4] Updated project to use ES6 starter library --- .babelrc | 3 +- .eslintrc | 56 +++++++ .gitignore | 5 +- .npmignore | 3 + .travis.yml | 12 ++ README.md | 4 +- dist/nestedObjectAssign.js | 152 ------------------ dist/nestedObjectAssign.web.js | 1 + gulpfile.babel.js | 118 ++++++++++++++ index.js | 3 + lib/nestedObjectAssign.js | 57 +++++++ package.json | 85 +++++++--- src/index.js | 1 - src/{components => }/isObject.js | 2 +- src/{components => }/nestedObjectAssign.js | 2 +- .../nestedObjectAssign.spec.js | 20 ++- webpack.config.js | 127 ++++++++++++--- 17 files changed, 443 insertions(+), 208 deletions(-) create mode 100644 .eslintrc create mode 100644 .npmignore create mode 100644 .travis.yml delete mode 100644 dist/nestedObjectAssign.js create mode 100644 dist/nestedObjectAssign.web.js create mode 100644 gulpfile.babel.js create mode 100644 index.js create mode 100644 lib/nestedObjectAssign.js delete mode 100644 src/index.js rename src/{components => }/isObject.js (63%) rename src/{components => }/nestedObjectAssign.js (94%) rename tests/nestedObjectAssignTest.js => test/nestedObjectAssign.spec.js (52%) diff --git a/.babelrc b/.babelrc index af0f0c3..d3d29d3 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,4 @@ { - "presets": ["es2015"] + "plugins": ["babel-plugin-add-module-exports"], + "presets": ["es2015"] } \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..650fa8e --- /dev/null +++ b/.eslintrc @@ -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 + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2d50d8d..57d55d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -/node_modules/ -/.idea/ +node_modules npm-debug.log + +\.idea/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..6e92ac0 --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +src +test +webpack.config.js \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..757480b --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index 59bb019..0798975 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ This package extends the functionality given by Object.assign() to also include the values of nested objects. ## Usage -To use this +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`. \ No newline at end of file diff --git a/dist/nestedObjectAssign.js b/dist/nestedObjectAssign.js deleted file mode 100644 index 5e2d84d..0000000 --- a/dist/nestedObjectAssign.js +++ /dev/null @@ -1,152 +0,0 @@ -/******/ (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] = { -/******/ i: moduleId, -/******/ l: false, -/******/ exports: {} -/******/ }; -/******/ -/******/ // Execute the module function -/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); -/******/ -/******/ // Flag the module as loaded -/******/ module.l = 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; -/******/ -/******/ // identity function for calling harmony imports with the correct context -/******/ __webpack_require__.i = function(value) { return value; }; -/******/ -/******/ // define getter function for harmony exports -/******/ __webpack_require__.d = function(exports, name, getter) { -/******/ if(!__webpack_require__.o(exports, name)) { -/******/ Object.defineProperty(exports, name, { -/******/ configurable: false, -/******/ enumerable: true, -/******/ get: getter -/******/ }); -/******/ } -/******/ }; -/******/ -/******/ // getDefaultExport function for compatibility with non-harmony modules -/******/ __webpack_require__.n = function(module) { -/******/ var getter = module && module.__esModule ? -/******/ function getDefault() { return module['default']; } : -/******/ function getModuleExports() { return module; }; -/******/ __webpack_require__.d(getter, 'a', getter); -/******/ return getter; -/******/ }; -/******/ -/******/ // Object.prototype.hasOwnProperty.call -/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; -/******/ -/******/ // __webpack_public_path__ -/******/ __webpack_require__.p = ""; -/******/ -/******/ // Load entry module and return exports -/******/ return __webpack_require__(__webpack_require__.s = 2); -/******/ }) -/************************************************************************/ -/******/ ([ -/* 0 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.default = nestedObjectAssign; - -var _isObject = __webpack_require__(1); - -var _isObject2 = _interopRequireDefault(_isObject); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -function _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; } - -function nestedObjectAssign(target) { - for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { - sources[_key - 1] = arguments[_key]; - } - - if (!sources.length) return target; - - var source = sources.shift(); - - if ((0, _isObject2.default)(target) && (0, _isObject2.default)(source)) { - for (var key in source) { - if ((0, _isObject2.default)(source[key])) { - if (!target[key]) Object.assign(target, _defineProperty({}, key, {})); - - nestedObjectAssign(target[key], source[key]); - } else { - Object.assign(target, _defineProperty({}, key, source[key])); - } - } - } - - return nestedObjectAssign.apply(undefined, [target].concat(sources)); -} - -/***/ }), -/* 1 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _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; }; - -exports.default = isObject; -function isObject(item) { - return item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && !Array.isArray(item); -} - -/***/ }), -/* 2 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _nestedObjectAssign = __webpack_require__(0); - -Object.defineProperty(exports, 'nestedObjectAssign', { - enumerable: true, - get: function get() { - return _nestedObjectAssign.nestedObjectAssign; - } -}); - -/***/ }) -/******/ ]); \ No newline at end of file diff --git a/dist/nestedObjectAssign.web.js b/dist/nestedObjectAssign.web.js new file mode 100644 index 0000000..743f5dc --- /dev/null +++ b/dist/nestedObjectAssign.web.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("nestedObjectAssign",[],t):"object"==typeof exports?exports.nestedObjectAssign=t():e.nestedObjectAssign=t()}(this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return e[o].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(e){for(var t=arguments.length,n=Array(t>1?t-1:0),f=1;f { + 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 + } + }); +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..6572211 --- /dev/null +++ b/index.js @@ -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); \ No newline at end of file diff --git a/lib/nestedObjectAssign.js b/lib/nestedObjectAssign.js new file mode 100644 index 0000000..30f9cff --- /dev/null +++ b/lib/nestedObjectAssign.js @@ -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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9zcmMvbmVzdGVkT2JqZWN0QXNzaWduLmpzP2ZmYWMiXSwibmFtZXMiOlsibmVzdGVkT2JqZWN0QXNzaWduIiwidGFyZ2V0Iiwic291cmNlcyIsImxlbmd0aCIsInNvdXJjZSIsInNoaWZ0Iiwia2V5IiwiT2JqZWN0IiwiYXNzaWduIl0sIm1hcHBpbmdzIjoiOzs7OztrQkFFd0JBLGtCOztBQUZ4Qjs7OztBQUVlLFNBQVNBLGtCQUFULENBQTRCQyxNQUE1QixFQUErQztBQUFBLHNDQUFSQyxPQUFRO0FBQVJBLGVBQVE7QUFBQTs7QUFDMUQsUUFBSSxDQUFDQSxRQUFRQyxNQUFiLEVBQ0ksT0FBT0YsTUFBUDs7QUFFSixRQUFNRyxTQUFTRixRQUFRRyxLQUFSLEVBQWY7O0FBRUEsUUFBSSx3QkFBU0osTUFBVCxLQUFvQix3QkFBU0csTUFBVCxDQUF4QixFQUF5QztBQUNyQyxhQUFLLElBQU1FLEdBQVgsSUFBa0JGLE1BQWxCLEVBQXlCO0FBQ3JCLGdCQUFJLHdCQUFTQSxPQUFPRSxHQUFQLENBQVQsQ0FBSixFQUEwQjtBQUN0QixvQkFBSSxDQUFDTCxPQUFPSyxHQUFQLENBQUwsRUFDSUMsT0FBT0MsTUFBUCxDQUFjUCxNQUFkLHNCQUF3QkssR0FBeEIsRUFBOEIsRUFBOUI7O0FBRUpOLG1DQUFtQkMsT0FBT0ssR0FBUCxDQUFuQixFQUFnQ0YsT0FBT0UsR0FBUCxDQUFoQztBQUNILGFBTEQsTUFNSztBQUNEQyx1QkFBT0MsTUFBUCxDQUFjUCxNQUFkLHNCQUF3QkssR0FBeEIsRUFBOEJGLE9BQU9FLEdBQVAsQ0FBOUI7QUFDSDtBQUNKO0FBQ0o7O0FBRUQsV0FBT04scUNBQW1CQyxNQUFuQixTQUE4QkMsT0FBOUIsRUFBUDtBQUNIIiwiZmlsZSI6IjAuanMiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQge2lzT2JqZWN0fSBmcm9tICcuL2lzT2JqZWN0JztcclxuXHJcbmV4cG9ydCBkZWZhdWx0IGZ1bmN0aW9uIG5lc3RlZE9iamVjdEFzc2lnbih0YXJnZXQsIC4uLnNvdXJjZXMpe1xyXG4gICAgaWYgKCFzb3VyY2VzLmxlbmd0aClcclxuICAgICAgICByZXR1cm4gdGFyZ2V0O1xyXG5cclxuICAgIGNvbnN0IHNvdXJjZSA9IHNvdXJjZXMuc2hpZnQoKTtcclxuXHJcbiAgICBpZiAoaXNPYmplY3QodGFyZ2V0KSAmJiBpc09iamVjdChzb3VyY2UpKXtcclxuICAgICAgICBmb3IgKGNvbnN0IGtleSBpbiBzb3VyY2Upe1xyXG4gICAgICAgICAgICBpZiAoaXNPYmplY3Qoc291cmNlW2tleV0pKXtcclxuICAgICAgICAgICAgICAgIGlmICghdGFyZ2V0W2tleV0pXHJcbiAgICAgICAgICAgICAgICAgICAgT2JqZWN0LmFzc2lnbih0YXJnZXQsIHtba2V5XToge319KTtcclxuXHJcbiAgICAgICAgICAgICAgICBuZXN0ZWRPYmplY3RBc3NpZ24odGFyZ2V0W2tleV0sIHNvdXJjZVtrZXldKTtcclxuICAgICAgICAgICAgfVxyXG4gICAgICAgICAgICBlbHNlIHtcclxuICAgICAgICAgICAgICAgIE9iamVjdC5hc3NpZ24odGFyZ2V0LCB7W2tleV06IHNvdXJjZVtrZXldfSk7XHJcbiAgICAgICAgICAgIH1cclxuICAgICAgICB9XHJcbiAgICB9XHJcblxyXG4gICAgcmV0dXJuIG5lc3RlZE9iamVjdEFzc2lnbih0YXJnZXQsIC4uLnNvdXJjZXMpO1xyXG59XG5cblxuLy8gV0VCUEFDSyBGT09URVIgLy9cbi8vIC4vc3JjL25lc3RlZE9iamVjdEFzc2lnbi5qcyJdLCJzb3VyY2VSb290IjoiIn0="); + +/***/ }), +/* 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=="); + +/***/ }) +/******/ ]); \ No newline at end of file diff --git a/package.json b/package.json index 6d6e02c..907afc7 100644 --- a/package.json +++ b/package.json @@ -1,30 +1,79 @@ { - "name": "nested-object.assign", - "version": "1.0.0", - "description": "method for allowing nested objects to be merged, which object.assign doesn't currently support", - "main": "dist/nestedObjectAssign.js", - "directories": { - "test": "test" - }, + "name": "nestedObjectAssign", + "version": "1.0.3", + "description": "Package to support nested merging of objects & properties, using Object.Assign", + "main": "./index.js", "scripts": { - "tests": "mocha tests --compilers js:babel-core/register" + "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": "git+https://github.com/Geta/NestedObjectAssign.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" }, - "author": "Eirik Horvath", - "license": "ISC", + "license": "MIT", "bugs": { "url": "https://github.com/Geta/NestedObjectAssign/issues" }, - "homepage": "https://github.com/Geta/NestedObjectAssign#readme", + "files": [ + "README.md", + "index.js", + "lib", + "dist" + ], + "homepage": "https://github.com/Geta/NestedObjectAssignl", "devDependencies": { - "babel": "^6.23.0", - "babel-core": "^6.24.1", - "babel-loader": "^7.0.0", - "babel-preset-es2015": "^6.24.1", - "mocha": "^3.4.2", - "webpack": "^2.6.1" + "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 } } diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 7cb3744..0000000 --- a/src/index.js +++ /dev/null @@ -1 +0,0 @@ -export {nestedObjectAssign} from './components/nestedObjectAssign'; \ No newline at end of file diff --git a/src/components/isObject.js b/src/isObject.js similarity index 63% rename from src/components/isObject.js rename to src/isObject.js index 3051f7a..caf0150 100644 --- a/src/components/isObject.js +++ b/src/isObject.js @@ -1,3 +1,3 @@ -export default function isObject(item) { +export function isObject(item){ return (item && typeof item === 'object' && !Array.isArray(item)); } \ No newline at end of file diff --git a/src/components/nestedObjectAssign.js b/src/nestedObjectAssign.js similarity index 94% rename from src/components/nestedObjectAssign.js rename to src/nestedObjectAssign.js index 04c2631..e3986d0 100644 --- a/src/components/nestedObjectAssign.js +++ b/src/nestedObjectAssign.js @@ -1,4 +1,4 @@ -import isObject from './isObject'; +import {isObject} from './isObject'; export default function nestedObjectAssign(target, ...sources){ if (!sources.length) diff --git a/tests/nestedObjectAssignTest.js b/test/nestedObjectAssign.spec.js similarity index 52% rename from tests/nestedObjectAssignTest.js rename to test/nestedObjectAssign.spec.js index 1df9039..7347b58 100644 --- a/tests/nestedObjectAssignTest.js +++ b/test/nestedObjectAssign.spec.js @@ -1,5 +1,5 @@ -var assert = require('assert'); -import nestedObjectAssign from '../dist/nestedObjectAssign'; +import { expect } from 'chai'; +import nestedObjectAssign from '../index.js'; var mockData = { default: { @@ -34,12 +34,10 @@ var expectedData = { } }; -describe('Object', function() { - describe('nestedObjectAssign', function() { - it('Return true when objects & values are equal', function() { - var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second); - - assert.equal(JSON.stringify(mergedData), JSON.stringify(expectedData)); - }) - }) -}); +describe('Given an instance of nestedObjectAssign', function() { + describe('when i merge the mockData', function() { + it('it should be equal to expectedData', () => { + expect(JSON.stringify(nestedObjectAssign({}, mockData.default, mockData.first, mockData.second))).to.be.equal(JSON.stringify(expectedData)); + }); + }); +}); \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index b142542..e174234 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,26 +1,113 @@ -var path = require('path'); var webpack = require('webpack'); +var path = require('path'); +var assign = require('object-assign'); +var nodeExternals = require('webpack-node-externals'); +var CleanWebpackPlugin = require('clean-webpack-plugin'); +var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin; + +module.exports = function getConfig(options){ + + var options = options || {}; + + var isProd = (options.BUILD_ENV || process.env.BUILD_ENV) === 'PROD'; + var isWeb = (options.TARGET_ENV || process.env.TARGET_ENV) === 'WEB'; + + // get library details from JSON config + var libraryDesc = require('./package.json').library; + var libraryName = libraryDesc.name; + + // determine output file name + var outputName = buildLibraryOutputName(libraryDesc, isWeb, isProd); + var outputFolder = isWeb ? 'dist' : 'lib'; + + // get base config + var config; + + // for the web + if(isWeb){ + config = assign(getBaseConfig(isProd), { + output: { + path: path.join(__dirname, outputFolder), + filename: outputName, + library: libraryName, + libraryTarget: 'umd', + umdNamedDefine: true + } + }); + } -module.exports = { - context: path.resolve('src'), - entry: './index', + // for the backend + else { + config = assign(getBaseConfig(isProd), { + output: { + path: path.join(__dirname, outputFolder), + filename: outputName, + library: libraryName, + libraryTarget: 'commonjs2' + }, + target: 'node', + node: { + __dirname: true, + __filename: true + }, + externals: [nodeExternals()] + }); + } + + config.plugins.push(new CleanWebpackPlugin([outputFolder])); + + return config; +} + +/** + * Build base config + * @param {Boolean} isProd [description] + * @return {[type]} [description] + */ +function getBaseConfig(isProd) { + + // get library details from JSON config + var libraryDesc = require('./package.json').library; + var libraryEntryPoint = path.join('src', libraryDesc.entry); + + // generate webpack base config + return { + entry: path.join(__dirname, libraryEntryPoint), output: { - path: path.resolve('dist'), - filename: 'nestedObjectAssign.js' + // ommitted - will be filled according to target env + }, + module: { + preLoaders: [ + {test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: "eslint-loader"} + ], + loaders: [ + {test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: "babel-loader"}, + ] }, - module:{ - loaders: [ - { - test: /\.js$/, - exclude: [/node_modules/, /dist/], - loader: 'babel-loader', - query: { - presets: ['es2015'] - } - } - ] + eslint: { + configFile: './.eslintrc' }, resolve: { - extensions: ['.js'] - } -}; \ No newline at end of file + root: path.resolve('./src'), + extensions: ['', '.js'] + }, + devtool: isProd ? null : '#eval-source-map', + debug: !isProd, + plugins: isProd ? [ + new webpack.DefinePlugin({'process.env': {'NODE_ENV': '"production"'}}), + new UglifyJsPlugin({ minimize: true }) + // Prod plugins here + ] : [ + new webpack.DefinePlugin({'process.env': {'NODE_ENV': '"development"'}}) + // Dev plugins here + ] + }; +} + +function buildLibraryOutputName(libraryDesc, isWeb, isProd){ + if(isWeb){ + return libraryDesc["dist-web"] || [libraryDesc.name, 'web', (isProd ? 'min.js' : 'js')].join('.'); + } else { + return libraryDesc["dist-node"] || [libraryDesc.name, (isProd ? 'min.js' : 'js')].join('.'); + } +} \ No newline at end of file