coerce properties within an Object to other types
This is a utility function to take an Object that has properties with String values, and cast those Strings to other type (e.g. Booleans and Numbers).
- @param {Object} input key-value pairs to cast
- @param {Object} example key-value pairs where values are of desired types
- @returns {Object} a new Object with same properties as input but with cast values
castPropertyTypes()
will take the values from the first parameter, and cast
those values using the types found in the second parameter.
For example, castPropertyTypes({ value: '123' }, { value: 0 })
will return:
{
"value": 123
}
We use the built-in parseFloat()
method to cast values to a Number, so be
aware that values that are not numeric may have unexpected results.