Common Object utility to make life easier.
$ npm i --save firefly-utils
const utils = require('firefly-utils').ObjectUtils;
Converts a nested obeject structure to one level.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
};
utils.flatten(data);
/* output
{
name.firstName:'John',
name.lastName:'Doe',
email:'john@doe.com',
contact.code:'+91',
contact.number:1231231230
}
*/
// you can provide your own separator
utils.flatten(data, '#');
/* output
{
name#firstName:'John',
name#lastName:'Doe',
email:'john@doe.com',
contact#code:'+91',
contact#number:1231231230
}
*/
Converts an object from flatten state to its normal form.
const data = {
'name.firstName':'John',
'name.lastName':'Doe',
'email':'john@doe.com',
'contact.code':'+91',
'contact.number':1231231230
};
utils.unFlatten(data);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/
// you can provide your own separator
const data = {
'name$firstName':'John',
'name$lastName':'Doe',
'email':'john@doe.com',
'contact$code':'+91',
'contact$number':1231231230
};
utils.unFlatten(data, '$');
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/
Retrivies value from a nested object using key as path.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
},
addressList:[
{
houseNo:'101',
street:'first street',
state:'john state'
},
{
houseNo:'102',
street:'second street',
state:'james state'
}
]
};
utils.getValue('name.firstName', data);
/* output
John
*/
// Use array as path
utils.getValue(['name','firstName'], data);
/* output
John
*/
// Accessing array data
utils.getValue(['addressList',0], data);
/* output
{
houseNo:'101',
street:'first street',
state:'john state'
}
*/
utils.getValue(['addressList',0,'houseNo'], data);
/* output
101
*/
// you can provide your own separator
utils.getValue('name@firstName', data, '@');
/* output
John
*/
Sets value in a nested object using key as path.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
};
utils.setValue('name.title', data, 'Mr.');
/* data object is updated to
{
name:{
title:'Mr',
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/
// you can provide your own separator
utils.setValue('name@title', data, 'Mr.', '@');
/* data object is updated to
{
name:{
title:'Mr',
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/
Delete value from a nested object using key as path.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
};
utils.deleteValue('contact.code', data);
/* data object is updated to
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
number:1231231230
}
}
*/
// you can provide your own separator
utils.deleteValue('contact@code', data, '@');
/* data object is updated to
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
number:1231231230
}
}
*/
Merge two objects.
const data1 = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com'
};
const data2 = {
contact:{
code:'+91',
number:1231231230
}
};
utils.deepmerge(data1, data2);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/
Makes an exact copy of the provided object.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
};
utils.clone(data);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/
Converts and Object to an Array.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
};
utils.convertToArray(data);
/* output
[
{
key:'name',
value:{
firstName:'John',
lastName:'Doe'
}
},
{
key:'email',
value:'john@doe.com'
},
{
key:'contact',
value:{
code:'+91',
number:1231231230
}
}
]
*/
// Convert recursively
utils.convertToArray(data, true);
/* output
[
{
key:'name',
value:[
{
key:'firstName',
value:'John'
},
{
key:'lastName',
value:'Doe'
}
]
},
{
key:'email',
value:'john@doe.com'
},
{
key:'contact',
value:[
{
key:'code',
value:'+91'
},
{
key:'number',
value:1231231230
}
]
}
]
*/
const utils = require('firefly-utils').CryptUtils;
Encrypts anyting.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
};
const secret = 'MY_SECRET_KEY';
utils.encrypt(data,secret);
/* output
ENCRYPTED_STRING
*/
Decrypts a string and returns as object if it is.
const data = 'ENCRYPTED_STRING';
const secret = 'MY_SECRET_KEY';
utils.decrypt(data,secret);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'john@doe.com',
contact:{
code:'+91',
number:1231231230
}
}
*/