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

mitsuketa

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mitsuketa - npm Package Compare versions

Comparing version 1.3.4 to 1.4.1

154

index.js

@@ -6,2 +6,151 @@ /**

/**
* Performs deep search on object tree, and renames the all matching keys
* @param {Any} identity
* @param {string} keyName
* @param {string} newKeyName
* @param {Optional Number} maxDepth
*/
function renameKeys(identity,keyName,newKeyName,maxDepth=null){
if(getType(keyName)!=='string') return undefined;
if(getType(newKeyName)!=='string') return undefined;
function _renameKeys(identity,keyName,newKeyName,maxDepth,currentDepth=0){
let keys;
switch(getType(identity)){
case 'array':
var Arr = [];
keys = Object.keys(identity);
for(var i = 0, l = keys.length; i < l; i++){
let
key = keys[i],
subIdentity = identity[key];
Arr[key] = _renameKeys(subIdentity,keyName,newKeyName,maxDepth,currentDepth + 1);
}
return Arr;
case 'object':
var Obj = {};
keys = Object.keys(identity);
for(var i = 0, l = keys.length; i < l; i++){
let
key = keys[i],
subIdentity = identity[key];
if( maxDepth !== null ? currentDepth < maxDepth : true)
if(key===keyName) key = newKeyName;
Obj[key] = _renameKeys(subIdentity,keyName,newKeyName,maxDepth,currentDepth + 1);
}
return Obj;
case 'string': return '' + identity;
case 'number': return 0 + identity;
case 'boolean': if(identity) return true; return false;
case 'null': return null;
case 'undefined': return undefined;
}
}
return _renameKeys(identity,keyName,newKeyName,maxDepth,0);
}
/**
* Performs deep search on object tree, then renames the first matching key
* @param {Any} identity
* @param {string} keyName
* @param {string} newKeyName
* @param {Optional Number} maxDepth
*/
function renameKey(identity,keyName,newKeyName,maxDepth=null){
if(getType(keyName)!=='string') return undefined;
if(getType(newKeyName)!=='string') return undefined;
var applied=false;
function _renameKey(identity,keyName,newKeyName,maxDepth,currentDepth=0){
let keys;
switch(getType(identity)){
case 'array':
var Arr = [];
keys = Object.keys(identity);
for(var i = 0, l = keys.length; i < l; i++){
let
key = keys[i],
subIdentity = identity[key];
Arr[key] = _renameKey(subIdentity,keyName,newKeyName,maxDepth,currentDepth + 1);
}
return Arr;
case 'object':
var Obj = {};
keys = Object.keys(identity);
for(var i = 0, l = keys.length; i < l; i++){
let
key = keys[i],
subIdentity = identity[key];
if( maxDepth !== null ? currentDepth < maxDepth : true)
if(!applied)
if(key===keyName){ key = newKeyName; applied = true; }
Obj[key] = _renameKey(subIdentity,keyName,newKeyName,maxDepth,currentDepth + 1);
}
return Obj;
case 'string': return '' + identity;
case 'number': return 0 + identity;
case 'boolean': if(identity) return true; return false;
case 'null': return null;
case 'undefined': return undefined;
}
}
return _renameKey(identity,keyName,newKeyName,maxDepth,0);
}
/**
* Creates a non-reference clone that is an exact copy to the identity provided.
* @param {Any} identity
* @param {Optional Number} maxDepth
* @param {Optional Number} startDepth
* @return {Any} identity
*/
function deepClone(identity,maxDepth=null,startDepth=null){
var R = [];
function _deepClone(identity,maxDepth,startDepth,currentDepth=0){
let keys;
if( startDepth !== null ? currentDepth < startDepth : false){
if(isIterable(identity)){
keys = Object.keys(identity);
keys.forEach( key => { _deepClone(identity[key],maxDepth,startDepth,currentDepth + 1); });
}
return;
}
if( startDepth !== null ? currentDepth == startDepth : false){
if(startDepth==0){ R = _deepClone(identity,maxDepth,null,currentDepth); return; }
if(isIterable(identity)) R.push(_deepClone(identity,maxDepth,startDepth,currentDepth + 1));
return;
}
switch(getType(identity)){
case 'array':
var Arr = [];
keys = Object.keys(identity);
if( maxDepth !== null ? currentDepth < maxDepth : true)
for(var i = 0, l = keys.length; i < l; i++){
const
key = keys[i],
subIdentity = identity[key];
Arr[key] = _deepClone(subIdentity,maxDepth,startDepth,currentDepth + 1);
}
return Arr;
case 'object':
var Obj = {};
keys = Object.keys(identity);
if( maxDepth !== null ? currentDepth < maxDepth : true)
for(var i = 0, l = keys.length; i < l; i++){
const
key = keys[i],
subIdentity = identity[key];
Obj[key] = _deepClone(subIdentity,maxDepth,startDepth,currentDepth + 1);
}
return Obj;
case 'string': return '' + identity;
case 'number': return 0 + identity;
case 'boolean': if(identity) return true; return false;
case 'null': return null;
case 'undefined': return undefined;
}
}
if(startDepth === null) return _deepClone(identity,maxDepth,startDepth,0);
_deepClone(identity,maxDepth,startDepth,0); return R;
}
/**
* Performs deep search on collection to find all matches to the key name, and returns a list of identities containing the matched instances. If no matches found, it returns `undefined`.

@@ -668,5 +817,8 @@ * @param {Any} collection

locateAll_Key : function(collection,keyName,maxDepth) { return locateAll_Key(collection,keyName,maxDepth); },
deepFilter_Key : function(collection,keyName,maxDepth) { return deepFilter_Key(collection,keyName,maxDepth); }
deepFilter_Key : function(collection,keyName,maxDepth) { return deepFilter_Key(collection,keyName,maxDepth); },
deepClone : function(identity,maxDepth,startDepth) { return deepClone(identity,maxDepth,startDepth); },
renameKey : function(identity,keyName,newKeyName,maxDepth) { return renameKey(identity,keyName,newKeyName,maxDepth); },
renameKeys : function(identity,keyName,newKeyName,maxDepth) { return renameKeys(identity,keyName,newKeyName,maxDepth); }
}
module.exports = exports = mitsuketa;

2

package.json
{
"name": "mitsuketa",
"version": "1.3.4",
"version": "1.4.1",
"description": "A Javascript library that enables you to handle deeply nested objects easily.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -20,4 +20,4 @@ # Mitsuketa ![Build Status](https://travis-ci.org/AndrewRedican/mitsuketa.svg?branch=master)

1. `locate()`, `deepGet()`, `locateAll()`, `deepFilter()` functions now have a counterpart each that supports deep search by `key` or `property name`. Check them out! [**locate_Key**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.3.x#locate_key-collection--keyname--optionalmaxdepth-), [**deepGet_Key**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.3.x#deepget_key-collection--keyname--optionalmaxdepth-), [**locateAll_Key**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.3.x#locateall_key-collection--keyname--optionalmaxdepth-), [**deepFilter_Key**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.3.x#deepfilter_key-collection--keyname--optionalmaxdepth-)
2. Most of the features so far have focused on obtaining data from complex object trees. Now we are taking an exciting new focus on actual **deeply nested object manipulation**. Check out our [**project board**](https://github.com/AndrewRedican/mitsuketa/projects/1) for a sneak peek to the new features. Feel free suggest any feature that you'd like to see by answering the survey down below.
1. Three new features have been released. [**renameKey**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#renamekey-identity--keyname--newkeyname--optionalmaxdepth-), [**renameKeys**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#renamekeys-identity--keyname--newkeyname--optionalmaxdepth-) to rename the properties or keys of any simple or deeply nested object on the fly! [**deepClone**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#deepclone-identity--optionalmaxdepth--optionalstartdepth---) allows you to create identical `non-reference` copies. It also has the option to create `shallow clones` and even `branch clones`.
2. Up until recently, most of the features have focused on obtaining data from complex object trees. Now we are taking an exciting new focus on actual **deeply nested object manipulation**. Check out our [**project board**](https://github.com/AndrewRedican/mitsuketa/projects/1) for a sneak peek to the new features. Feel free suggest any feature that you'd like to see by answering the survey down below.
3. If you haven't checked it out already, you can learn more about how we [set up and execute tests](https://github.com/AndrewRedican/mitsuketa/wiki/How-to-Create-and-Run-Tests).

@@ -57,2 +57,5 @@

| [deepFilter_Key](https://github.com/AndrewRedican/mitsuketa/wiki/v1.3.x#deepfilter_key-collection--keyname--optionalmaxdepth-) | Uses `locateAll` and returns an array of all an identites matched inside `collection` | collection, key | array of identities |
| [deepClone](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#deepclone-identity--optionalmaxdepth--optionalstartdepth---) | Creates a non-reference clone that is an exact copy to the identity provided. Can be used to create `shallow clones` of specific depths. Can also be used to create `branch clones`. Read more in [**documentation**](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#deepclone-identity--optionalmaxdepth--optionalstartdepth---) | collection, key | any |
| [renameKey](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#renamekey-identity--keyname--newkeyname--optionalmaxdepth-) | Performs deep search on the `identity`'s object tree to rename the first matching key. | identity, key, key | identity |
| [renameKeys](https://github.com/AndrewRedican/mitsuketa/wiki/v1.4.x#renamekeys-identity--keyname--newkeyname--optionalmaxdepth-) | Performs deep search on the `identity`'s object tree to rename all matching keys. | identity, key, key | identity |

@@ -62,3 +65,3 @@

* Vanilla Javascript, ES5, ES6
* Vanilla [**Javascript**](https://betterexplained.com/articles/the-single-page-javascript-overview/), ES5, [**ES6**](http://es6-features.org/#Constants)

@@ -80,2 +83,2 @@ ## Authors

If you have a minute to spare, can you answer these two questions?
https://www.surveymonkey.com/r/XJ37XSP
https://www.surveymonkey.com/r/XJ37XSP

@@ -1026,3 +1026,3 @@ /**

*
* VERSION 2.0 FEATURES
* VERSION 1.3.4 FEATURES
*

@@ -1147,2 +1147,366 @@ */

});
});
});
/**
*
* VERSION 1.4 FEATURES
*
*/
describe('deepClone(identity)', function() {
var tests = [
{args: bicycles, expected: bicycles },
{args: assessments, expected: assessments },
{args: complexObject, expected: complexObject }
];
tests.forEach(function(test) {
it(opDescription(1,test.args,test.expected), function() {
var res = mitsuketa.deepClone(test.args);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
describe('deepClone(identity,maxDepth)', function() {
var res1 = {};
var res2 = {
A: {},
B: '100',
C: {},
D: {},
E: {}
};
var res3 = {
A: {
Example: {}
},
B: '100',
C: {
SamePropName: 'SamePropName2',
OtherProperty: [],
DepthTest: 'sameValue'
},
D: {
A: 100,
B: 'a string',
C: []
},
E:{
ANumber: 7,
OtherProperty: 'check this out'
}
};
var tests = [
{args: [complexObject,0], expected: res1 },
{args: [complexObject,1], expected: res2 },
{args: [complexObject,2], expected: res3 }
];
tests.forEach(function(test) {
it(opDescription(2,test.args,test.expected), function() {
var res = mitsuketa.deepClone(test.args[0],test.args[1]);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
describe('deepClone(identity,maxDepth,startDepth)', function() {
var res2 = [
{
unique_id: 299, factory_id: 'alpha', model: 'br-chrome', maker: 'breez TM',
year: '2017', type: 'racing',
status: { hasOwner: false, price: 345.99 },
specs : {
dimensions: { length: '1.68m', width: '13cm', height: '1.02m' },
usability: { grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
color: 'silver',
components: [ 'basket', 'chain', 'handle', 'seat' ]
},
sales: {
date_arrived: 'Nov 30, 2017', date_showcased: 'Dec 4, 2017', date_sold: false,
sale_opportunities: [
{ name: 'Donn Reddick', contact_info: '1-987-652-8775', date: 'Dec 19, 2017'},
{ name: 'Susan Boyle', contact_info: '1-555-101-9875', date: 'Dec 4, 2017'}
]
}
},
{
unique_id: 300, factory_id: 'beta', model: 'XV17', maker: 'hyperwheel',
year: '2017', type: 'city',
status: { hasOwner: true, price: 1100 },
specs : {
dimensions: { length: '1.65m', width: '13cm', height: '1.03m' },
usability: { grip : 5.5, speed : 3, accelaration : 5, weight: '', durability: 6 },
color: 'red',
components: [ 'basket', 'chain', 'handle', 'seat' ]
},
sales: {
date_arrived: 'Nov 13, 2017', date_showcased: 'Nov 16, 2017', date_sold: false,
sale_opportunities: [
{ name: 'Tom Stark', contact_info: 'N/A', date: ''},
{ name: "Jane O'Neil", contact_info: 'N/A', date: ''}
]
}
},
{
unique_id: 301, factory_id: 'gamma', model: 'XV15', maker: 'hyperwheel', year: '2017', type: 'sport',
status: { hasOwner: true, price: 1800 },
specs : {
dimensions: { length: '1.68m', width: '13cm', height: '1.02m' },
usability: { grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
color: 'red',
components: [ 'basket', 'chain', 'handle', 'seat', 'kinetic lights' ]
},
sales: {
date_arrived: 'Nov 28, 2017', date_showcased: 'Nov 29, 2017', date_sold: 'Nov 29, 2017',
sale_opportunities: []
}
},
{
unique_id: 302, factory_id: 'gamma',
model: '2019 pro', maker: 'hyperwheel', year: '2018', type: 'racing',
status: { hasOwner: false, price: 1499 },
specs : {
dimensions: { length: '1.69m', width: '11cm', height: '0.95m' },
usability: { grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
color: 'pink',
components: [ 'basket', 'chain', 'handle', 'seat', 'reflector lights', 'usb charger' ]
},
sales: {
date_arrived: false, date_showcased: false, date_sold: false,
sale_opportunities: []
}
}
];
var res3 = [
{ hasOwner: false, price: 345.99 },
{
dimensions: { length: '1.68m', width: '13cm', height: '1.02m' },
usability: { grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
color: 'silver',
components: [ 'basket', 'chain', 'handle', 'seat' ]
},
{
date_arrived: 'Nov 30, 2017', date_showcased: 'Dec 4, 2017', date_sold: false,
sale_opportunities: [
{ name: 'Donn Reddick', contact_info: '1-987-652-8775', date: 'Dec 19, 2017'},
{ name: 'Susan Boyle', contact_info: '1-555-101-9875', date: 'Dec 4, 2017'}
]
},
{ hasOwner: true, price: 1100 },
{
dimensions: { length: '1.65m', width: '13cm', height: '1.03m' },
usability: { grip : 5.5, speed : 3, accelaration : 5, weight: '', durability: 6 },
color: 'red',
components: [ 'basket', 'chain', 'handle', 'seat' ]
},
{
date_arrived: 'Nov 13, 2017', date_showcased: 'Nov 16, 2017', date_sold: false,
sale_opportunities: [
{ name: 'Tom Stark', contact_info: 'N/A', date: ''},
{ name: "Jane O'Neil", contact_info: 'N/A', date: ''}
]
},
{ hasOwner: true, price: 1800 },
{
dimensions: { length: '1.68m', width: '13cm', height: '1.02m' },
usability: { grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
color: 'red',
components: [ 'basket', 'chain', 'handle', 'seat', 'kinetic lights' ]
},
{
date_arrived: 'Nov 28, 2017', date_showcased: 'Nov 29, 2017', date_sold: 'Nov 29, 2017',
sale_opportunities: []
},
{ hasOwner: false, price: 1499 },
{
dimensions: { length: '1.69m', width: '11cm', height: '0.95m' },
usability: { grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
color: 'pink',
components: [ 'basket', 'chain', 'handle', 'seat', 'reflector lights', 'usb charger' ]
},
{
date_arrived: false, date_showcased: false, date_sold: false,
sale_opportunities: []
}
];
var res4 = [
{ length: '1.68m', width: '13cm', height: '1.02m' },
{ grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
[ 'basket', 'chain', 'handle', 'seat' ],
[
{ name: 'Donn Reddick', contact_info: '1-987-652-8775', date: 'Dec 19, 2017'},
{ name: 'Susan Boyle', contact_info: '1-555-101-9875', date: 'Dec 4, 2017'}
],
{ length: '1.65m', width: '13cm', height: '1.03m' },
{ grip : 5.5, speed : 3, accelaration : 5, weight: '', durability: 6 },
[ 'basket', 'chain', 'handle', 'seat' ],
[
{ name: 'Tom Stark', contact_info: 'N/A', date: ''},
{ name: "Jane O'Neil", contact_info: 'N/A', date: ''}
],
{ length: '1.68m', width: '13cm', height: '1.02m' },
{ grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
[ 'basket', 'chain', 'handle', 'seat', 'kinetic lights' ],
{ length: '1.69m', width: '11cm', height: '0.95m' },
{ grip : 5, speed : 4, accelaration : 8, weight: '', durability: 10 },
[ 'basket', 'chain', 'handle', 'seat', 'reflector lights', 'usb charger' ]
];
var res5 = [
{ name: 'Donn Reddick', contact_info: '1-987-652-8775', date: 'Dec 19, 2017' },
{ name: 'Susan Boyle', contact_info: '1-555-101-9875', date: 'Dec 4, 2017' },
{ name: 'Tom Stark', contact_info: 'N/A', date: '' },
{ name: "Jane O'Neil", contact_info: 'N/A', date: '' }
];
tests = [
{args: [bicycles,null,0], expected: bicycles },
{args: [bicycles,null,1], expected: res2 },
{args: [bicycles,null,2], expected: res3 },
{args: [bicycles,null,3], expected: res4 },
{args: [bicycles,null,4], expected: res5 },
{args: [bicycles,null,5], expected: [] }
];
tests.forEach(function(test) {
it(opDescription(3,test.args,test.expected), function() {
var res = mitsuketa.deepClone(test.args[0],test.args[1],test.args[2]);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
var obj = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
C : 'some text',
sameProp: 'sameProp test'
};
var res3 = {
A : { _someprp : 'Hello', NewName: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
C : 'some text',
sameProp: 'sameProp test'
};
describe('renameKey(identity,keyName,newKeyName)', function() {
var res1 = {
A : { FirstWord : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
C : 'some text',
sameProp: 'sameProp test'
};
var res2 = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', last_property: 'another same test' },
C : 'some text',
sameProp: 'sameProp test'
};
var tests = [
{args: [obj,'_someprp','FirstWord'], expected: res1 },
{args: [obj,'C','last_property'], expected: res2 },
{args: [obj,'sameProp','NewName'], expected: res3 }
];
tests.forEach(function(test) {
it(opDescription(3,test.args,test.expected), function() {
var res = mitsuketa.renameKey(test.args[0],test.args[1],test.args[2]);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
describe('renameKey(identity,keyName,newKeyName,maxDepth)', function() {
var res1 = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', GotIt: 'another same test' },
C : 'some text',
sameProp: 'sameProp test'
};
var res2 = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
GotIt : 'some text',
sameProp: 'sameProp test'
};
var tests = [
{args: [obj,'_someprp','FirstWord',0], expected: obj },
{args: [obj,'C','GotIt',2], expected: res1 },
{args: [obj,'C','GotIt',1], expected: res2 },
{args: [obj,'C','GotIt',0], expected: obj }
];
tests.forEach(function(test) {
it(opDescription(4,test.args,test.expected), function() {
var res = mitsuketa.renameKey(test.args[0],test.args[1],test.args[2],test.args[3]);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
describe('renameKeys(identity,keyName,newKeyName)', function() {
var res1 = {
A : { FirstWord : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
C : 'some text',
sameProp: 'sameProp test'
};
var res2 = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', last_property: 'another same test' },
last_property : 'some text',
sameProp: 'sameProp test'
};
var res3 = {
A : { _someprp : 'Hello', NewName: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
C : 'some text',
NewName: 'sameProp test'
};
var tests = [
{args: [obj,'_someprp','FirstWord'], expected: res1 },
{args: [obj,'C','last_property'], expected: res2 },
{args: [obj,'sameProp','NewName'], expected: res3 }
];
tests.forEach(function(test) {
it(opDescription(3,test.args,test.expected), function() {
var res = mitsuketa.renameKeys(test.args[0],test.args[1],test.args[2]);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
describe('renameKeys(identity,keyName,newKeyName,maxDepth)', function() {
var res1 = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
last_property : 'some text',
sameProp: 'sameProp test'
};
var res2 = {
A : { _someprp : 'Hello', sameProp: 'sameProp test' },
B : { SecondWord : 'World', last_property: 'another same test' },
last_property : 'some text',
sameProp: 'sameProp test'
};
var res3 = {
A : { _someprp : 'Hello', NewName: 'sameProp test' },
B : { SecondWord : 'World', C: 'another same test' },
C : 'some text',
NewName: 'sameProp test'
};
var tests = [
{args: [obj,'C','last_property',0], expected: obj },
{args: [obj,'C','last_property',1], expected: res1 },
{args: [obj,'C','last_property',2], expected: res2 },
{args: [obj,'sameProp',null,null], expected: undefined },
{args: [obj,'sameProp',undefined,null], expected: undefined },
{args: [obj,'sameProp',[1,2],null], expected: undefined }
];
tests.forEach(function(test) {
it(opDescription(3,test.args,test.expected), function() {
var res = mitsuketa.renameKeys(test.args[0],test.args[1],test.args[2],test.args[3]);
assert.equal(stringify(res), stringify(test.expected));
});
});
});
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