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

smart-setter

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smart-setter - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

126

lib/smartSetter.js

@@ -7,76 +7,78 @@ 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var smartSetter = function smartSetter(config) {
return function (source) {
return function (target) {
if (!config.isObjectOrMap(source)) return source;
return config.getKeys(source).reduce(function (acc, key) {
var _ret = function () {
switch (key) {
case '_replace':
return {
v: config.get(source, key)
};
break;
var smartSetter = function smartSetter(source) {
return function (target) {
if ((typeof source === 'undefined' ? 'undefined' : _typeof(source)) !== 'object') return source;
return Object.keys(source).reduce(function (acc, key) {
var _ret = function () {
switch (key) {
case '_replace':
return {
v: source[key]
};
break;
case '_insert':
var newEle = config.get(source, key);
var oldArr = target;
return {
v: config.push(oldArr, newEle)
};
break;
case '_insert':
var newEle = source[key];
var oldArr = target;
return {
v: oldArr.concat([newEle])
};
break;
case '_remove':
var properties = config.get(source, key);
var newArray = target.filter(function (ele) {
return !config.getKeys(properties).reduce(function (acc, key2) {
return acc && config.get(properties, key2) === config.get(ele, key2);
}, true);
});
return {
v: newArray
};
break;
case '_remove':
var properties = source[key];
var newArray = target.filter(function (ele) {
return !Object.keys(properties).reduce(function (acc, key2) {
return acc && properties[key2] === ele[key2];
}, true);
});
return {
v: newArray
};
break;
default:
if (config.isArrayOrList(target)) {
var _ret2 = function () {
var _key$split = key.split("=");
default:
if (Array.isArray(target)) {
var _ret2 = function () {
var _key$split = key.split("=");
var _key$split2 = _slicedToArray(_key$split, 2);
var _key$split2 = _slicedToArray(_key$split, 2);
var propKey = _key$split2[0];
var propValue = _key$split2[1];
var propKey = _key$split2[0];
var propValue = _key$split2[1];
return {
v: {
v: target.map(function (ele) {
if (config.get(ele, propKey) == propValue) {
return smartSetter(config)(config.get(source, key))(ele);
} else {
return ele;
}
})
}
};
}();
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
} else {
var nextSource = config.get(source, key);
var nextTarget = config.get(target, key);
var acc2 = config.set(acc, key, smartSetter(config)(nextSource)(nextTarget));
return {
v: {
v: target.map(function (ele) {
if (ele[propKey] == propValue) {
return smartSetter(source[key])(ele);
} else {
return ele;
}
})
}
v: acc2
};
}();
}
break;
}
}();
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
} else {
var nextSource = source[key];
var nextTarget = target[key];
acc[key] = smartSetter(nextSource)(nextTarget);
return {
v: acc
};
}
break;
}
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}, Object.assign({}, target));
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}, config.clone(target));
};
};

@@ -83,0 +85,0 @@ };

{
"name": "smart-setter",
"version": "1.0.0",
"version": "1.0.1",
"description": "create new object based on old object and set of modifications that can be applied at selective depths",

@@ -9,3 +9,4 @@ "main": "index.js",

"buildTest": "rm -rf test/lib && babel test/src -d test/lib",
"test": "npm run build && npm run buildTest && mocha 'test/lib/**/*.@(js|jsx)'"
"test": "npm run build && npm run buildTest && mocha 'test/lib/**/*.@(js|jsx)'",
"deploy": "npm run build && git add --all && git commit -m 'automatic commit' && git push origin && npm version patch && npm publish"
},

@@ -39,4 +40,7 @@ "repository": {

"chai": "^3.5.0",
"mocha": "^3.0.2"
"immutable": "^3.8.1",
"immutable-config-for-smart-setter": "^1.0.1",
"mocha": "^3.0.2",
"object-config-for-smart-setter": ""
}
}

@@ -1,19 +0,20 @@

const smartSetter = source => target => {
if (typeof source !== 'object') return source
return Object.keys(source).reduce((acc, key) => {
const smartSetter = config => source => target => {
if (!config.isObjectOrMap(source)) return source
return config.getKeys(source).reduce((acc, key) => {
switch(key) {
case '_replace':
return source[key]
return config.get(source, key)
break;
case '_insert':
const newEle = source[key]
const newEle = config.get(source, key)
const oldArr = target
return oldArr.concat([newEle])
return config.push(oldArr, newEle)
break;
case '_remove':
const properties = source[key]
const properties = config.get(source, key)
const newArray = target.filter(ele => (
!Object.keys(properties).reduce((acc, key2) => acc && properties[key2] === ele[key2], true)
!config.getKeys(properties).reduce(
(acc, key2) => acc && config.get(properties, key2) === config.get(ele,key2), true)
))

@@ -24,7 +25,7 @@ return newArray;

default:
if (Array.isArray(target)) {
if (config.isArrayOrList(target)) {
const [propKey, propValue] = key.split("=")
return target.map(function (ele) {
if (ele[propKey] == propValue) {
return smartSetter(source[key])(ele)
return target.map(ele => {
if (config.get(ele,propKey) == propValue) {
return smartSetter(config)(config.get(source,key))(ele)
} else {

@@ -36,12 +37,12 @@ return ele

} else {
const nextSource = source[key]
const nextTarget = target[key]
acc[key] = smartSetter(nextSource)(nextTarget)
return acc;
const nextSource = config.get(source,key)
const nextTarget = config.get(target,key)
const acc2 = config.set(acc, key, smartSetter(config)(nextSource)(nextTarget))
return acc2;
}
break;
}
}, Object.assign({}, target))
}, config.clone(target))
}
export default smartSetter

@@ -9,104 +9,163 @@ 'use strict';

var _objectConfigForSmartSetter = require('object-config-for-smart-setter');
var _objectConfigForSmartSetter2 = _interopRequireDefault(_objectConfigForSmartSetter);
var _immutableConfigForSmartSetter = require('immutable-config-for-smart-setter');
var _immutableConfigForSmartSetter2 = _interopRequireDefault(_immutableConfigForSmartSetter);
var _immutable = require('immutable');
var _immutable2 = _interopRequireDefault(_immutable);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// var _immutable2 = Immutable
// const immutableConfigForSmartSetter = {
// get: function get($$obj, propKey) {
// return $$obj.get(propKey);
// },
// set: function set($$obj, propKey, value) {
// return $$obj.set(propKey, value);
// },
// getKeys: function getKeys($$obj) {
// return $$obj.keySeq().toArray();
// },
// isObjectOrMap: function isObjectOrMap($$item) {
// return _immutable2.Map.isMap($$item);
// },
// isArrayOrList: function isArrayOrList($$item) {
// return _immutable2.List.isList($$item);
// },
// push: function push($$arr, $$item) {
// return $$arr.push($$item);
// },
// clone: function clone($$obj) {
// return $$obj; // no need to clone since object is immutable
// }
// };
var I = function I(item) {
return _immutable2.default.fromJS({ key: item }).get('key');
};
describe('smartSetter', function () {
var subject = _index2.default;
var helper = function helper(subject) {
it('works in trivial case', function () {
var source = {};
var target = {};
var result = subject(source)(target);
(0, _chai.expect)(result).to.deep.equal({});
});
it('works in trivial case', function () {
var source = {};
var target = {};
var result = subject(source)(target);
(0, _chai.expect)(result).to.deep.equal({});
});
it('merges by default, but can selectively replace', function () {
var source = {
key1: {
key1key2: 'val12'
},
key2: {
_replace: {
key2key2: 'val22'
it('merges by default, but can selectively replace', function () {
var source = {
key1: {
key1key2: 'val12'
},
key2: {
_replace: {
key2key2: 'val22'
}
}
}
};
};
var target = {
key1: {
key1key1: 'val11'
},
key2: {
key2key1: 'val21'
},
key3: 'val3'
};
var target = {
key1: {
key1key1: 'val11'
},
key2: {
key2key1: 'val21'
},
key3: 'val3'
};
var result = subject(source)(target);
var result = subject(source)(target);
var expected = {
key1: {
key1key1: 'val11',
key1key2: 'val12'
},
key2: {
key2key2: 'val22'
},
key3: 'val3'
};
var expected = {
key1: {
key1key1: 'val11',
key1key2: 'val12'
},
key2: {
key2key2: 'val22'
},
key3: 'val3'
};
(0, _chai.expect)(result).to.deep.equal(expected);
});
(0, _chai.expect)(result).to.deep.equal(expected);
});
it('can insert elements', function () {
var source = {
key1: {
_insert: 'val12'
}
};
var target = {
key1: ['val11']
};
var result = subject(source)(target);
var expected = {
key1: ['val11', 'val12']
};
(0, _chai.expect)(result).to.deep.equal(expected);
});
it('can insert elements', function () {
var source = {
key1: {
_insert: 'val12'
}
};
var target = {
key1: ['val11']
};
var result = subject(source)(target);
var expected = {
key1: ['val11', 'val12']
};
(0, _chai.expect)(result).to.deep.equal(expected);
});
it('can remove elements', function () {
var source = {
key1: {
_remove: { id: 1 }
}
};
var target = {
key1: [{ id: 1, name: 'great' }, { id: 2, name: 'nice' }]
};
var result = subject(source)(target);
var expected = {
key1: [{ id: 2, name: 'nice' }]
};
(0, _chai.expect)(result).to.deep.equal(expected);
});
it('can remove elements', function () {
var source = {
key1: {
_remove: { id: 1 }
}
};
var target = {
key1: [{ id: 1, name: 'great' }, { id: 2, name: 'nice' }]
};
var result = subject(source)(target);
var expected = {
key1: [{ id: 2, name: 'nice' }]
};
(0, _chai.expect)(result).to.deep.equal(expected);
});
it('cuts through arrays', function () {
var source = {
key1: {
"id=1": {
name: 'name1Updated'
it('cuts through arrays', function () {
var source = {
key1: {
"id=1": {
name: 'name1Updated'
}
}
}
};
};
var target = {
key1: [{ id: 1, name: 'name1' }, { id: 2, name: 'name2' }]
};
var target = {
key1: [{ id: 1, name: 'name1' }, { id: 2, name: 'name2' }]
};
var result = subject(source)(target);
var expected = {
key1: [{ id: 1, name: 'name1Updated' }, { id: 2, name: 'name2' }]
var result = subject(source)(target);
var expected = {
key1: [{ id: 1, name: 'name1Updated' }, { id: 2, name: 'name2' }]
};
(0, _chai.expect)(result).to.deep.equal(expected);
});
};
describe('it works with object config', function () {
helper((0, _index2.default)(_objectConfigForSmartSetter2.default));
});
describe('it works with immutable config', function () {
var helper2 = function helper2(source) {
return function (target) {
var source2 = I(source);
var target2 = I(target);
var result = (0, _index2.default)(_immutableConfigForSmartSetter2.default)(source2)(target2);
return result.toJS();
};
};
(0, _chai.expect)(result).to.deep.equal(expected);
helper(helper2);
});
});
import { expect } from 'chai';
import smartSetter from '../../index'
import objectConfigForSmartSetter from 'object-config-for-smart-setter';
import immutableConfigForSmartSetter from 'immutable-config-for-smart-setter';
import Immutable from 'immutable'
// var _immutable2 = Immutable
// const immutableConfigForSmartSetter = {
// get: function get($$obj, propKey) {
// return $$obj.get(propKey);
// },
// set: function set($$obj, propKey, value) {
// return $$obj.set(propKey, value);
// },
// getKeys: function getKeys($$obj) {
// return $$obj.keySeq().toArray();
// },
// isObjectOrMap: function isObjectOrMap($$item) {
// return _immutable2.Map.isMap($$item);
// },
// isArrayOrList: function isArrayOrList($$item) {
// return _immutable2.List.isList($$item);
// },
// push: function push($$arr, $$item) {
// return $$arr.push($$item);
// },
// clone: function clone($$obj) {
// return $$obj; // no need to clone since object is immutable
// }
// };
const I = item => Immutable.fromJS({key: item}).get('key')
describe('smartSetter', () => {
const subject = smartSetter
const helper = subject => {
it('works in trivial case', () => {
const source = {}
const target = {}
const result = subject(source)(target)
expect(result).to.deep.equal({})
})
it('works in trivial case', () => {
const source = {}
const target = {}
const result = subject(source)(target)
expect(result).to.deep.equal({})
})
it('merges by default, but can selectively replace', () => {
const source = {
key1: {
key1key2: 'val12',
},
key2: {
_replace: {
key2key2: 'val22',
it('merges by default, but can selectively replace', () => {
const source = {
key1: {
key1key2: 'val12',
},
},
}
key2: {
_replace: {
key2key2: 'val22',
},
},
}
const target = {
key1: {
key1key1: 'val11',
},
key2: {
key2key1: 'val21',
},
key3: 'val3'
}
const target = {
key1: {
key1key1: 'val11',
},
key2: {
key2key1: 'val21',
},
key3: 'val3'
}
const result = subject(source)(target)
const result = subject(source)(target)
const expected = {
key1: {
key1key1: 'val11',
key1key2: 'val12',
},
key2: {
key2key2: 'val22',
},
key3: 'val3',
}
const expected = {
key1: {
key1key1: 'val11',
key1key2: 'val12',
},
key2: {
key2key2: 'val22',
},
key3: 'val3',
}
expect(result).to.deep.equal(expected)
expect(result).to.deep.equal(expected)
})
})
it('can insert elements', () => {
const source = {
key1: {
_insert: 'val12'
it('can insert elements', () => {
const source = {
key1: {
_insert: 'val12'
}
}
}
const target = {
key1: ['val11']
}
const result = subject(source)(target)
const expected = {
key1: ['val11', 'val12']
}
expect(result).to.deep.equal(expected)
})
const target = {
key1: ['val11']
}
const result = subject(source)(target)
const expected = {
key1: ['val11', 'val12']
}
expect(result).to.deep.equal(expected)
})
it('can remove elements', () => {
const source = {
key1: {
_remove: {id: 1}
it('can remove elements', () => {
const source = {
key1: {
_remove: {id: 1}
}
}
}
const target = {
key1: [{id: 1, name: 'great'}, {id: 2, name: 'nice'}]
}
const result = subject(source)(target)
const expected = {
key1: [{id: 2, name: 'nice'}]
}
expect(result).to.deep.equal(expected)
})
const target = {
key1: [{id: 1, name: 'great'}, {id: 2, name: 'nice'}]
}
const result = subject(source)(target)
const expected = {
key1: [{id: 2, name: 'nice'}]
}
expect(result).to.deep.equal(expected)
})
it('cuts through arrays', () => {
const source = {
key1: {
"id=1": {
name: 'name1Updated',
it('cuts through arrays', () => {
const source = {
key1: {
"id=1": {
name: 'name1Updated',
},
},
},
}
}
const target = {
key1: [
{id: 1, name: 'name1'},
{id: 2, name: 'name2'},
]
}
const target = {
key1: [
{id: 1, name: 'name1'},
{id: 2, name: 'name2'},
]
}
const result = subject(source)(target)
const expected = {
key1: [
{id: 1, name: 'name1Updated'},
{id: 2, name: 'name2'},
],
const result = subject(source)(target)
const expected = {
key1: [
{id: 1, name: 'name1Updated'},
{id: 2, name: 'name2'},
],
}
expect(result).to.deep.equal(expected)
})
}
describe('it works with object config', () => {
helper(smartSetter(objectConfigForSmartSetter))
})
describe('it works with immutable config', () => {
const helper2 = source => target => {
const source2 = I(source)
const target2 = I(target)
const result = smartSetter(immutableConfigForSmartSetter)(source2)(target2)
return result.toJS()
}
expect(result).to.deep.equal(expected)
helper(helper2)
})
})
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