Methods
(static) addType(typeName, castFunction)
Add a new type to cast. This new type can then be used as MyValue((typeName))
Parameters:
Name | Type | Description |
---|---|---|
typeName |
string
|
New type name to add. It will be used in the "(( ))" |
castFunction |
CastFunction
|
Example
Cast.addType('boolean2', value => value === 'true')
//Then it can be used as "true((boolean2))"
(static) array(array)
Casts an array of values.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array.<*>
|
(static) object(object) → {Object}
Casts object all properties.
Parameters:
Name | Type | Description |
---|---|---|
object |
Object
|
The object containing values to cast |
Returns:
- Type:
-
Object
The object with casted values
(static) objects(objects)
Casts an array of objects.
Parameters:
Name | Type | Description |
---|---|---|
objects |
Array.<Object>
|
Example
Cast.objects([
{ username: 'plouc((string))', is_active: 'true((boolean))', age: '25((number))' },
{ username: 'john((string))', is_active: 'false((boolean))', age: '32((number))' },
])
// output
// > [
// > { username: 'plouc', is_active: true, age: 25 },
// > { username: 'john', is_active: false, age: 32 },
// > ]
(static) value(value) → {*}
Casts a value according to type directives.
Supports the following types:
- undefined
- null
- number
- boolean
- array
- date
- string
Parameters:
Name | Type | Description |
---|---|---|
value |
string
|
The value to cast |
Returns:
- Type:
-
*
The casted value or untouched value if no casting directive found
Example
Cast.value('2((number))')
Cast.value('true((boolean))')
Cast.value('((null))')
Cast.value('raw')
// output
// > 2
// > true
// > null
// > 'raw'