Change object values, conditionally check what values to change.
npm install hzob-obj-morph --save
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: 'john.doe@somecompany.com'
}
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": "john.doe@somecompany.com"
// }
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. |