firefly-utils
v1.0.2
Published
Flatten object, unflatten object and find n level value and much more
Downloads
5
Maintainers
Readme
firefly-utils
Common Object utility to make life easier.
APIs
Setup
$ npm i --save firefly-utils
ObjectUtils
const utils = require('firefly-utils').ObjectUtils;
flatten
Converts a nested obeject structure to one level.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
};
utils.flatten(data);
/* output
{
name.firstName:'John',
name.lastName:'Doe',
email:'[email protected]',
contact.code:'+91',
contact.number:1231231230
}
*/
// you can provide your own separator
utils.flatten(data, '#');
/* output
{
name#firstName:'John',
name#lastName:'Doe',
email:'[email protected]',
contact#code:'+91',
contact#number:1231231230
}
*/
unFlatten
Converts an object from flatten state to its normal form.
const data = {
'name.firstName':'John',
'name.lastName':'Doe',
'email':'[email protected]',
'contact.code':'+91',
'contact.number':1231231230
};
utils.unFlatten(data);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
}
*/
// you can provide your own separator
const data = {
'name$firstName':'John',
'name$lastName':'Doe',
'email':'[email protected]',
'contact$code':'+91',
'contact$number':1231231230
};
utils.unFlatten(data, '$');
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
}
*/
getValue
Retrivies value from a nested object using key as path.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
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
*/
setValue
Sets value in a nested object using key as path.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
};
utils.setValue('name.title', data, 'Mr.');
/* data object is updated to
{
name:{
title:'Mr',
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
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:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
}
*/
deleteValue
Delete value from a nested object using key as path.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
};
utils.deleteValue('contact.code', data);
/* data object is updated to
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
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:'[email protected]',
contact:{
number:1231231230
}
}
*/
deepmerge
Merge two objects.
const data1 = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]'
};
const data2 = {
contact:{
code:'+91',
number:1231231230
}
};
utils.deepmerge(data1, data2);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
}
*/
clone
Makes an exact copy of the provided object.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
};
utils.clone(data);
/* output
{
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
}
*/
convertToArray
Converts and Object to an Array.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
};
utils.convertToArray(data);
/* output
[
{
key:'name',
value:{
firstName:'John',
lastName:'Doe'
}
},
{
key:'email',
value:'[email protected]'
},
{
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:'[email protected]'
},
{
key:'contact',
value:[
{
key:'code',
value:'+91'
},
{
key:'number',
value:1231231230
}
]
}
]
*/
CryptUtils
const utils = require('firefly-utils').CryptUtils;
encrypt
Encrypts anyting.
const data = {
name:{
firstName:'John',
lastName:'Doe'
},
email:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
};
const secret = 'MY_SECRET_KEY';
utils.encrypt(data,secret);
/* output
ENCRYPTED_STRING
*/
decrypt
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:'[email protected]',
contact:{
code:'+91',
number:1231231230
}
}
*/