hzob-obj-morph
v0.1.0
Published
Javascript Object properties transformation functions
Downloads
1
Maintainers
Readme
Hzob Object Morph
Change object values, conditionally check what values to change.
Instalation
npm install hzob-obj-morph --save
First steps
This package exports a function that have two parameters, first one is the data object, and the second is the configuration object
import objMorph from "hzob-obj-morph";
const testData = {
name: 'John Doe',
age: 29,
email: '[email protected]'
}
const config = {
checkValue: function(data, key, wholeObject) {
return key === 'name';
},
processValue: function(data, key, wholeObject) {
return 'Dr. '+data;
}
};
const result = objMorph(testData, config);
console.log(result);
// result:
// {
// "name": "Dr. John Doe",
// "age": 29,
// "email": "[email protected]"
// }
Configuration
Configuration object accept the follow parameters:
Parameter | Type | Description
----- | ----- | -----
checkValue | function(data: any, key: string, wholeObject: any): boolean | Check for values to be processed, returns true
to execute processValue
to mutate that value
processValue | function(data: any, key: string, wholeObject: any): any | For the values that returned true
on checkValue
, execute this function, and expect the new value on return
ignore | function(data: any, key: string, wholeObject: boolean): any | If returns true
, it will skip this property, usefull for third party objects like moment, or custom objects.