Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

circle-assign

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

circle-assign - npm Package Compare versions

Comparing version 1.0.10 to 2.0.0

.eslintrc

181

dist/circle-assign.js

@@ -1,100 +0,127 @@

"use strict";
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.circleAssign = factory());
}(this, (function () { 'use strict';
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* Deep object assign
*
* @param {Object} target The target object object
* @param {...Object} sources The source object(s)
*/
function circleAssign(target) {
if (_typeof(target) !== 'object' || target === undefined || target === null) {
target = {};
/**
* Check if a value is an object
*
* @param {*} o The value to check
*
* @returns {boolean} Whether or not it is an object
*/
function isObj(o) {
return o instanceof Object && o.constructor === Object;
}
/**
* Merge the specified source object into the target object
*
* @param {Object} target The base target object
* @param {Object} source The object to merge into the target
*
* @returns {Object} The merged object
*/
var assign = function assign(target, source) {
var // get an array of keys from both target and source
targetKeys = Object.keys(target),
sourceKeys = Object.keys(source),
// create an empty object for the result
result = {}; // go through all the target keys
for (var i = 0; i < targetKeys.length; i++) {
var // get the current key
key = targetKeys[i]; // if the source keys contains the current key from target
function mergeObject(target, source) {
// create a variable to hold the target object
// so it can be changed if its not an object
var targetObject = target;
var sourceObject = source;
if (!isObj(target)) {
targetObject = {};
}
if (!isObj(source)) {
sourceObject = {};
} // get the object keys for the target and source objects
var targetKeys = Object.keys(targetObject);
var sourceKeys = Object.keys(sourceObject); // create a empty object for the result
var result = {}; // go through all the target keys
targetKeys.forEach(function (key) {
// check if the source object contains the key
if (sourceKeys.indexOf(key) !== -1) {
// check if the target is null if it is
// set the result as the source, this
// should be fine since if the source is
// null it isn't overriding and if it
// isn't null it is overriding
if (target[key] === null) {
result[key] = source[key];
} else if (_typeof(target[key]) === 'object' && !(target[key] instanceof Array)) {
// check if the current target object value is an object
// check if the corresponding source value is also an object
if (_typeof(source[key]) === 'object' && !(source[key] instanceof Array)) {
// if both target and source values are objects rerun
// the assign function with the target and source being
// the current values
result[key] = assign(target[key], source[key]);
// check if the target value is null if it is
// set the result as the source value, this
// should be fine because if the source value
// is null it isn't overriding the target value
// and if it isn't null it is overriding
// as expected
if (targetObject[key] === null) {
result[key] = sourceObject[key];
} else if (isObj(targetObject[key])) {
// check if the source value is an object if
// it is then we need to merge both objects and
// set the result value to the merged object
if (isObj(sourceObject[key])) {
result[key] = mergeObject(targetObject[key], sourceObject[key]);
} else {
// if the source value isn't an object assign the result key
// as the source value directly (this will override target objects
// with the new value from source)
result[key] = source[key];
// if the source value isn't an object we can
// simply override the value
result[key] = sourceObject[key];
}
} else {
// if the current target value isn't an object assign the result
// key as the source key (this will set strings, numbers, booleans etc.)
result[key] = source[key];
// if the target value isn't an object we can
// simply override the value
result[key] = sourceObject[key];
}
} else {
// if the source doesn't have the target key set the result to the
// old value (this will happen for keys which aren't defined in the source)
result[key] = target[key];
// if the source doesn't contain the key set the result
// as the original from the target
result[key] = targetObject[key];
}
} // go through the source keys
}); // go through all the source keys
for (var _i = 0; _i < sourceKeys.length; _i++) {
var // get the current key
_key = sourceKeys[_i]; // if the target keys doesn't contain the key
if (targetKeys.indexOf(_key) === -1) {
// set the result value to the target value (this will
// add new values to the object which are defined in the target)
result[_key] = source[_key];
sourceKeys.forEach(function (key) {
// if the target doesn't contain the key
// then the value is new and should be added
// to the result object
if (targetKeys.indexOf(key) === -1) {
result[key] = sourceObject[key];
}
} // return the new object result
});
return result;
}
// internals
/**
* Merge specified objects into one object with the most right
* object having the most priority
*
* @param {Object} target The base object
* @param {...Object} sources The object(s) to merge
*
* @returns {Object} The merged object(s) result
*/
return result;
}; // go through the targets
function merge(target) {
var targetObject = target;
if (!isObj(target)) {
targetObject = {};
} // for all the sources provided merge them with
// the target object
for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
var // get the current target
source = i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1];
if (source !== undefined && source !== null && _typeof(source) === 'object') {
// run assign with the source and current target
// this will return the deep assigned version of
// source with target merged into it
// it will then set source to that and if any more
// targets are specified it will loop over and repeat
// leading to each target overriding the previous
// source resulting in the original source overridden
// by the array of targets specified
target = assign(target, source);
for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
sources[_key - 1] = arguments[_key];
}
} // return the source (the true result)
sources.forEach(function (s) {
// before merging check the source is an object
if (isObj(s)) {
targetObject = mergeObject(targetObject, s);
}
});
return targetObject;
}
return target;
} // export the module
return merge;
module.exports = circleAssign;
})));

@@ -1,1 +0,1 @@

"use strict";function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj}}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj}}return _typeof(obj)}function circleAssign(target){if(_typeof(target)!=="object"||target===undefined||target===null){target={}}var assign=function assign(target,source){var targetKeys=Object.keys(target),sourceKeys=Object.keys(source),result={};for(var i=0;i<targetKeys.length;i++){var key=targetKeys[i];if(sourceKeys.indexOf(key)!==-1){if(target[key]===null){result[key]=source[key]}else if(_typeof(target[key])==="object"&&!(target[key]instanceof Array)){if(_typeof(source[key])==="object"&&!(source[key]instanceof Array)){result[key]=assign(target[key],source[key])}else{result[key]=source[key]}}else{result[key]=source[key]}}else{result[key]=target[key]}}for(var _i=0;_i<sourceKeys.length;_i++){var _key=sourceKeys[_i];if(targetKeys.indexOf(_key)===-1){result[_key]=source[_key]}}return result};for(var i=0;i<(arguments.length<=1?0:arguments.length-1);i++){var source=i+1<1||arguments.length<=i+1?undefined:arguments[i+1];if(source!==undefined&&source!==null&&_typeof(source)==="object"){target=assign(target,source)}}return target}module.exports=circleAssign;
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):global.circleAssign=factory()})(this,function(){"use strict";function isObj(o){return o instanceof Object&&o.constructor===Object}function mergeObject(target,source){var targetObject=target;var sourceObject=source;if(!isObj(target)){targetObject={}}if(!isObj(source)){sourceObject={}}var targetKeys=Object.keys(targetObject);var sourceKeys=Object.keys(sourceObject);var result={};targetKeys.forEach(function(key){if(sourceKeys.indexOf(key)!==-1){if(targetObject[key]===null){result[key]=sourceObject[key]}else if(isObj(targetObject[key])){if(isObj(sourceObject[key])){result[key]=mergeObject(targetObject[key],sourceObject[key])}else{result[key]=sourceObject[key]}}else{result[key]=sourceObject[key]}}else{result[key]=targetObject[key]}});sourceKeys.forEach(function(key){if(targetKeys.indexOf(key)===-1){result[key]=sourceObject[key]}});return result}function merge(target){var targetObject=target;if(!isObj(target)){targetObject={}}for(var _len=arguments.length,sources=new Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){sources[_key-1]=arguments[_key]}sources.forEach(function(s){if(isObj(s)){targetObject=mergeObject(targetObject,s)}});return targetObject}return merge});
{
"name": "circle-assign",
"version": "1.0.10",
"version": "2.0.0",
"description": "Simple deep object assign function",
"main": "src/index.js",
"main": "dist/circle-assign.js",
"module": "dist/circle-assign.js",
"scripts": {
"test": "./node_modules/.bin/jest --coverage --verbose",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"build:clean": "rm -rf dist/",
"build:babel": "mkdir dist && npx babel src/index.js --out-file dist/circle-assign.js",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"clean": "rm -rf ./dist",
"build:rollup": "rollup -c",
"build:watch": "rollup -c -w",
"build:uglify": "npx uglifyjs dist/circle-assign.js -o dist/circle-assign.min.js",
"build": "npm-run-all build:clean build:babel build:uglify"
"build": "npm-run-all clean build:rollup build:uglify",
"test:jest": "jest --coverage --verbose",
"test:eslint": "eslint src",
"test": "npm-run-all test:eslint test:jest"
},

@@ -34,10 +37,19 @@ "repository": {

"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"@babel/cli": "^7.1.5",
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"coveralls": "^3.0.2",
"eslint": "^5.9.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^22.1.0",
"jest": "^23.6.0",
"npm-run-all": "^4.1.3",
"npm-run-all": "^4.1.5",
"regenerator-runtime": "^0.13.1",
"rollup": "^0.67.3",
"rollup-plugin-babel": "^4.0.3",
"uglify-js": "^3.4.9"
}
}

@@ -14,3 +14,3 @@ ![circle-assign logo](logo.png)

```
$ npm install --save circle-assign
$ npm install --save circle-assign
```

@@ -25,6 +25,22 @@

#### NodeJS
```javascript
const circleAssign = require('circle-assign');
```
let source = {
#### Browser (ES6)
```javascript
import circleAssign from 'circle-assign';
```
#### Browser (CDN)
```html
<script
type="application/javascript"
src="https://cdn.jsdelivr.net/npm/circle-assign@2.0.0/dist/circle-assign.min.js"
/>
```
```javascript
const a = {
language: 'javascript',

@@ -37,3 +53,3 @@ features: {

let target = {
const b = {
language: 'JavaScript',

@@ -47,3 +63,9 @@ opensource: true,

console.log(circleAssign(source, target));
const c = {
coolFunc: () => {
console.log('Much wow');
}
};
console.log(circleAssign(a, b, c));
```

@@ -60,4 +82,7 @@

canMergeFunctions: true
},
coolFunc: () => {
console.log('Much wow');
}
}
```

@@ -0,148 +1,12 @@

import merge from './merge';
/**
* Deep object assign
* Merge specified objects into one object with the most right
* object having the most priority
*
* @param {Object} target The target object object
* @param {...Object} sources The source object(s)
* @param {Object} target The base object
* @param {...Object} sources The object(s) to merge
*
* @returns {Object} The merged object(s) result
*/
function circleAssign(target, ...sources) {
if(typeof target !== 'object' ||
target === undefined ||
target === null) {
target = {};
}
let
assign = function (target, source) {
let
// get an array of keys from both target and source
targetKeys = Object.keys(target),
sourceKeys = Object.keys(source),
// create an empty object for the result
result = {}
;
// go through all the target keys
for (let i = 0; i < targetKeys.length; i++) {
let
// get the current key
key = targetKeys[i]
;
// if the source keys contains the current key from target
if (sourceKeys.indexOf(key) !== -1) {
// check if the target is null if it is
// set the result as the source, this
// should be fine since if the source is
// null it isn't overriding and if it
// isn't null it is overriding
if(target[key] === null) {
result[key] = source[key];
} else if (typeof target[key] === 'object' &&
!(target[key] instanceof Array)) { // check if the current target object value is an object
// check if the corresponding source value is also an object
if (typeof source[key] === 'object' &&
!(source[key] instanceof Array)) {
// if both target and source values are objects rerun
// the assign function with the target and source being
// the current values
result[key] = assign(target[key], source[key]);
} else {
// if the source value isn't an object assign the result key
// as the source value directly (this will override target objects
// with the new value from source)
result[key] = source[key];
}
} else {
// if the current target value isn't an object assign the result
// key as the source key (this will set strings, numbers, booleans etc.)
result[key] = source[key];
}
} else {
// if the source doesn't have the target key set the result to the
// old value (this will happen for keys which aren't defined in the source)
result[key] = target[key];
}
}
// go through the source keys
for (let i = 0; i < sourceKeys.length; i++) {
let
// get the current key
key = sourceKeys[i]
;
// if the target keys doesn't contain the key
if (targetKeys.indexOf(key) === -1) {
// set the result value to the target value (this will
// add new values to the object which are defined in the target)
result[key] = source[key];
}
}
// return the new object result
return result;
}
;
// go through the targets
for(let i = 0; i < sources.length; i++) {
let
// get the current target
source = sources[i]
;
if(source !== undefined &&
source !== null &&
typeof source === 'object') {
// run assign with the source and current target
// this will return the deep assigned version of
// source with target merged into it
// it will then set source to that and if any more
// targets are specified it will loop over and repeat
// leading to each target overriding the previous
// source resulting in the original source overridden
// by the array of targets specified
target = assign(target, source);
}
}
// return the source (the true result)
return target;
}
// export the module
module.exports = circleAssign;
export default merge;

@@ -1,193 +0,196 @@

const
circleAssign = require('./../src/index')
;
import { isObj, mergeObject } from '../src/internals';
import merge from '../src/merge';
describe('internals', () => {
describe('isObj', () => {
test('isObj should return false for `"string"`', () => {
expect(isObj('string')).toBe(false);
});
test('requiring the module should return be a function', () => {
test('isObj should return false for `1`', () => {
expect(isObj(1)).toBe(false);
});
expect(typeof circleAssign).toBe('function');
test('isObj should return false for `[1, 2, 3]`', () => {
expect(isObj([1, 2, 3])).toBe(false);
});
});
test('isObj should return false for `() => {}`', () => {
expect(isObj(() => {})).toBe(false);
});
test('values should get overridden if the same key is specified in the target', () => {
test('isObj should return false for `null`', () => {
expect(isObj(null)).toBe(false);
});
let
target = {
test : 'Text'
},
source = {
test : false
}
;
test('isObj should return false for `undefined`', () => {
expect(isObj(undefined)).toBe(false);
});
expect(circleAssign(target, source)).toEqual({ test: false });
test('isObj should return false for `NaN`', () => {
expect(isObj(NaN)).toBe(false);
});
});
test('isObj should return true for `{}`', () => {
expect(isObj({})).toBe(true);
});
test('values should get overridden recursively if the same key is specified in the target', () => {
test('isObj should return true for `Object.create({})`', () => {
expect(isObj(Object.create({}))).toBe(true);
});
});
let
target = {
test : {
change : true
}
},
source = {
test : {
change : false
}
}
;
describe('mergeObject', () => {
test('mergeObject should return an object type', () => {
expect(typeof mergeObject(undefined, undefined)).toBe('object');
});
expect(circleAssign(target, source)).toEqual({ test: { change: false } });
test('mergeObject should return an empty object when no params are provided', () => {
expect(typeof mergeObject()).toBe('object');
});
});
test('mergeObject should return an empty object when only a target is provided', () => {
expect(typeof mergeObject({})).toBe('object');
});
test('multiple targets should override with the last target taking highest priority', () => {
test('mergeObject should return an empty object when none object arguments are provided', () => {
expect(typeof mergeObject(1, 'test')).toBe('object');
});
let
target = {
test : {
change : true
}
},
source = {
test : {
change : false
}
},
source2 = {
test : {
change : 'CHANGE'
}
}
;
test('mergeObject should return the target object when no source is provided', () => {
expect(mergeObject({ test: 'test' }).test).toBe('test');
});
expect(circleAssign(target, source, source2)).toEqual({ test: { change: 'CHANGE' } });
test('mergeObject should override all values with the ones in the source', () => {
const a = {
test: 'test',
bool: false,
};
});
const b = {
test: 'testing',
bool: true,
};
test('specifying a new key should add it to the target', () => {
const merged = mergeObject(a, b);
let
target = {
test : 'Text'
},
source = {
new : true
}
;
expect(merged.test === 'testing' && merged.bool === true).toBe(true);
});
expect(circleAssign(target, source)).toEqual({ test: 'Text', new: true });
test('mergeObject should add new values from source to the result', () => {
const a = {
test: 'test',
};
});
const b = {
new: true,
};
test('specifying a none object type on an object key should override it with the new value', () => {
let
target = {
test : {
text : 'something'
}
},
source = {
test : 'something'
}
;
expect(circleAssign(target, source)).toEqual({ test: 'something' });
});
const merged = mergeObject(a, b);
test('a none object target should become an empty object', () => {
let
target = 'not an object',
source = {
test : 'something'
}
;
expect(circleAssign(target, source)).toEqual({ test: 'something' });
});
expect(merged.new).toBe(true);
});
test('a null target should become an empty object', () => {
let
target = null,
source = {
test : 'something'
}
;
expect(circleAssign(target, source)).toEqual({ test: 'something' });
});
test('mergeObject should override values in target with objects from source', () => {
const a = {
test: 'test',
};
test('a undefined target should become an empty object', () => {
let
target = null,
source = {
test : 'something'
}
;
expect(circleAssign(target, source)).toEqual({ test: 'something' });
});
const b = {
test: {
override: true,
},
};
test('a none object source should become an empty object', () => {
let
target = {
test: 'something'
},
source = 'not an object'
;
expect(circleAssign(target, source)).toEqual({ test: 'something' });
});
const merged = mergeObject(a, b);
test('a null source should become an empty object', () => {
let
target = {
test: 'something'
},
source = null
;
expect(circleAssign(target, source)).toEqual({ test: 'something' });
});
expect(merged.test.override).toBe(true);
});
test('a undefined source should become an empty object', () => {
test('mergeObject should override values within objects in the target', () => {
const a = {
test: {
override: false,
},
};
let
target = {
test: 'something'
},
source = undefined
;
const b = {
test: {
override: true,
},
};
expect(circleAssign(target, source)).toEqual({ test: 'something' });
const merged = mergeObject(a, b);
expect(merged.test.override).toBe(true);
});
test('mergeObject should override null values with the source value', () => {
const a = {
test: null,
};
const b = {
test: true,
};
const merged = mergeObject(a, b);
expect(merged.test).toBe(true);
});
test('mergeObject should objects with new values from source', () => {
const a = {
test: {
override: true,
},
};
const b = {
test: true,
};
const merged = mergeObject(a, b);
expect(merged.test).toBe(true);
});
});
});
test('target should be updated accordingly even when target is null', () => {
describe('merge', () => {
test('merge should return an object type', () => {
expect(typeof merge(undefined, undefined)).toBe('object');
});
let
target = {
test: null
},
source = {
test: 'something'
}
;
test('merge should return an empty object when no params are provided', () => {
expect(typeof merge()).toBe('object');
});
expect(circleAssign(target, source)).toEqual({ test: 'something' });
test('merge should return an empty object when only a target is provided', () => {
expect(typeof merge({})).toBe('object');
});
test('merge should return an empty object when none object arguments are provided', () => {
expect(typeof merge(1, 'test')).toBe('object');
});
test('merge should return the target object when no source is provided', () => {
expect(merge({ test: 'test' }).test).toBe('test');
});
test('merge should merge all sources provided', () => {
const a = {
test: 1,
};
const b = {
test: 2,
};
const c = {
test: 3,
};
expect(merge(a, b, c).test).toBe(3);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc