Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

oneblink/cast-property-types.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cast-property-types.js

coerce properties within an Object to other types

npm module travis-ci

What is this?

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).

API

castPropertyTypes()

  • @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.

About

coerce properties within an Object to other types

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published