8000 Initial release by eirhor · Pull Request #1 · Geta/NestedObjectAssign · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
Created scripts. TODO: Finish tests.
  • Loading branch information
eirhor committed Jun 2, 2017
commit 589ee945d14f57c3558bf9cc07b7caa12d444c66
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/.idea/
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# NestedObjectAssign
# NestedObjectAssign

Package that is created to give functionality which merges nested properties, which currently Object.Assign does not support.
123 changes: 123 additions & 0 deletions dist/nested-object.assign.js
Original file line number Diff line number Diff line change
@@ -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);
}

/***/ })
/******/ ]);
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 3 additions & 0 deletions src/components/isObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
24 changes: 24 additions & 0 deletions src/components/nestedAssign.js
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import nestedAssign from "./components/nestedAssign";

export default function nestedObjectAssign(target, ...sources){
return nestedAssign(target, ...sources);
}
51 changes: 51 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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);
})
})
});
23 changes: 23 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -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']
}
};
0