0.1.0 • Published 6 years ago

hzob-obj-morph v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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: '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

Configuration object accept the follow parameters:

ParameterTypeDescription
checkValuefunction(data: any, key: string, wholeObject: any): booleanCheck for values to be processed, returns true to execute processValue to mutate that value
processValuefunction(data: any, key: string, wholeObject: any): anyFor the values that returned true on checkValue, execute this function, and expect the new value on return
ignorefunction(data: any, key: string, wholeObject: boolean): anyIf returns true, it will skip this property, usefull for third party objects like moment, or custom objects.