Skip to content

Commit 589ee94

Browse files
committed
Created scripts. TODO: Finish tests.
1 parent 31da5f1 commit 589ee94

File tree

10 files changed

+267
-1
lines changed

10 files changed

+267
-1
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/.idea/

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# NestedObjectAssign
1+
# NestedObjectAssign
2+
3+
Package that is created to give functionality which merges nested properties, which currently Object.Assign does not support.

dist/nested-object.assign.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // identity function for calling harmony imports with the correct context
37+
/******/ __webpack_require__.i = function(value) { return value; };
38+
/******/
39+
/******/ // define getter function for harmony exports
40+
/******/ __webpack_require__.d = function(exports, name, getter) {
41+
/******/ if(!__webpack_require__.o(exports, name)) {
42+
/******/ Object.defineProperty(exports, name, {
43+
/******/ configurable: false,
44+
/******/ enumerable: true,
45+
/******/ get: getter
46+
/******/ });
47+
/******/ }
48+
/******/ };
49+
/******/
50+
/******/ // getDefaultExport function for compatibility with non-harmony modules
51+
/******/ __webpack_require__.n = function(module) {
52+
/******/ var getter = module && module.__esModule ?
53+
/******/ function getDefault() { return module['default']; } :
54+
/******/ function getModuleExports() { return module; };
55+
/******/ __webpack_require__.d(getter, 'a', getter);
56+
/******/ return getter;
57+
/******/ };
58+
/******/
59+
/******/ // Object.prototype.hasOwnProperty.call
60+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
61+
/******/
62+
/******/ // __webpack_public_path__
63+
/******/ __webpack_require__.p = "";
64+
/******/
65+
/******/ // Load entry module and return exports
66+
/******/ return __webpack_require__(__webpack_require__.s = 2);
67+
/******/ })
68+
/************************************************************************/
69+
/******/ ([
70+
/* 0 */
71+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
72+
73+
"use strict";
74+
/* harmony export (immutable) */ __webpack_exports__["a"] = nestedAssign;
75+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__isObject__ = __webpack_require__(1);
76+
77+
78+
function nestedAssign(target, ...sources) {
79+
if (!sources.length) return target;
80+
81+
const source = sources.shift();
82+
83+
if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(target) && __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(source)) {
84+
for (const key in source) {
85+
if (__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__isObject__["a" /* default */])(source[key])) {
86+
if (!target[key]) Object.assign(target, { [key]: {} });
87+
88+
nestedAssign(target[key], source[key]);
89+
} else {
90+
Object.assign(target, { [key]: source[key] });
91+
}
92+
}
93+
}
94+
95+
return nestedAssign(target, ...sources);
96+
}
97+
98+
/***/ }),
99+
/* 1 */
100+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
101+
102+
"use strict";
103+
/* harmony export (immutable) */ __webpack_exports__["a"] = isObject;
104+
function isObject(item) {
105+
return item && typeof item === 'object' && !Array.isArray(item);
106+
}
107+
108+
/***/ }),
109+
/* 2 */
110+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
111+
112+
"use strict";
113+
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
114+
/* harmony export (immutable) */ __webpack_exports__["default"] = NestedAssign;
115+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__components_nestedAssign__ = __webpack_require__(0);
116+
117+
118+
function NestedAssign(target, ...sources) {
119+
return __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__components_nestedAssign__["a" /* default */])(target, ...sources);
120+
}
121+
122+
/***/ })
123+
/******/ ]);

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "nested-object.assign",
3+
"version": "1.0.0",
4+
"description": "method for allowing nested objects to be merged, which object.assign doesn't currently support",
5+
"main": "dist/nested-object.assign.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/Geta/NestedObjectAssign.git"
15+
},
16+
"author": "Eirik Horvath",
17+
"license": "ISC",
18+
"bugs": {
19+
"url": "https://github.com/Geta/NestedObjectAssign/issues"
20+
},
21+
"homepage": "https://github.com/Geta/NestedObjectAssign#readme",
22+
"devDependencies": {
23+
"babel": "^6.23.0",
24+
"babel-core": "^6.24.1",
25+
"babel-loader": "^7.0.0",
26+
"babel-preset-es2015": "^6.24.1",
27+
"mocha": "^3.4.2",
28+
"webpack": "^2.6.1"
29+
}
30+
}

src/components/isObject.js

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

src/components/nestedAssign.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 nestedAssign(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+
nestedAssign(target[key], source[key]);
16+
}
17+
else {
18+
Object.assign(target, {[key]: source[key]});
19+
}
20+
}
21+
}
22+
23+
return nestedAssign(target, ...sources);
24+
}

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import nestedAssign from "./components/nestedAssign";
2+
3+
export default function nestedObjectAssign(target, ...sources){
4+
return nestedAssign(target, ...sources);
5+
}

test/test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var assert = require('assert');
2+
import nestedObjectAssign from '../src/index';
3+
4+
var mockData = {
5+
default: {
6+
heading: 'title',
7+
body: {
8+
paragraph: 'p',
9+
heading: 'h1'
10+
}
11+
},
12+
first: {
13+
body: {
14+
span: 'span',
15+
header: 'header'
16+
}
17+
},
18+
second: {
19+
body: {
20+
heading2: 'h2'
21+
}
22+
}
23+
24+
}
25+
26+
var expectedData = {
27+
heading: 'title',
28+
body: {
29+
paragraph: 'p',
30+
heading: 'h1',
31+
span: 'span',
32+
header: 'header',
33+
heading2: 'h2'
34+
}
35+
}
36+
37+
var test = function(){
38+
var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second);
39+
40+
return mergedData === expectedData;
41+
}
42+
43+
describe('Object', function() {
44+
describe('nestedObjectAssign', function() {
45+
it('should return true when values are equal', function() {
46+
var mergedData = nestedObjectAssign({}, mockData.default, mockData.first, mockData.second);
47+
48+
assert.equal(mergedData, expectedData);
49+
})
50+
})
51+
});

webpack.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var path = require('path');
2+
var webpack = require('webpack');
3+
4+
module.exports = {
5+
context: path.resolve('src'),
6+
entry: './index',
7+
output: {
8+
path: path.resolve('dist'),
9+
filename: 'nested-object.assign.js'
10+
},
11+
module:{
12+
loaders: [
13+
{
14+
test: /\.js$/,
15+
exclude: [/node_modules/, /dist/],
16+
loader: 'babel-loader'
17+
}
18+
]
19+
},
20+
resolve: {
21+
extensions: ['.js']
22+
}
23+
};

0 commit comments

Comments
 (0)