conversions
Source: conversions/
Methods
floatOr(defaultValue, data) → (Float or any type)
Converts passed data to float or returns the default value on failure. (curried)
Example
floatOr(-1,"x33x") // -> -1
floatOr(() => throw Errot("Ups!"),"x33x") // -> Error: Ups!
floatOr(-1)('45.553') // -> 45.553
Parameters
Name | Type | Optional | Description |
---|---|---|---|
defaultValue |
(Object or function()) |
|
default value or function to be returned if it fails. |
data |
any type |
|
any kind of data that we want to parse as float |
- See also
- Returns
-
(Float or any type)
Parsed value or the default one.
intOr(defaultValue, data) → (Int or any type)
Converts passed data to int or returns the default value on failure. (curried)
Example
intOr(-1,"x33x") // -> -1
intOr(() => throw Error("Ups!"),"x33x") // -> Error: Ups!
intOr(-1)('45.553') // -> 45
Parameters
Name | Type | Optional | Description |
---|---|---|---|
defaultValue |
(Object or function()) |
|
default value or function to be returned if it fails. |
data |
any type |
|
any kind of data that we want to parse as int |
- See also
- Returns
-
(Int or any type)
Parsed value or the default one.
jsonOr(defaultValue, data) → (Object or any type)
Converts passed data into a JSON or returns the default value on failure. (curried)
Example
jsonOr(-1,"sfdjl") // -> -1
jsonOr(() => throw Errot("Ups!"),"sfdjl") // -> Error: Ups!
jsonOr(-1)('{"a":1}') // -> {a:1}
Parameters
Name | Type | Optional | Description |
---|---|---|---|
defaultValue |
(Object or function()) |
|
default value or function to be returned if it fails. |
data |
any type |
|
any kind of data that we want to parse as JSON |
- See also
- Returns
-
(Object or any type)
Parsed value or the default one.
parseOr(parser, evaluator, defaultValue, data) → any type
Generic function. Parses a value using a parser function, then evaluates the result with an evaluator function. (curried)
If the parser throws any exception or the evaluator fails the default value is returned.
Example
jsonOr = parseOr(JSON.parse,isObject);
jsonOr(0,{}) // -> {}
jsonOr(0,null) // -> 0
Parameters
Name | Type | Optional | Description |
---|---|---|---|
parser |
function() |
|
parse function to transform the data input |
evaluator |
function() |
|
evaluates the output of the parser |
defaultValue |
any type |
|
value/function to be returned/executed if it fails |
data |
any type |
|
any kind of data that we want to parse |
- See also
- Returns
-
any type
parsed value or default one
setPrecisionOr(decimals, defaultValue, num) → Float
Fixes the number of decimals of a float. Returns the default value if non numeric value passed.(curried)
Example
setPrecisionOr(1,"fail", 3.44) // -> 3.4
setPrecisionOr(1,"fail", null) // -> "fail"
setPrecisionOr(0,"fail", 3.44) // -> 3
Parameters
Name | Type | Optional | Description |
---|---|---|---|
decimals |
Int |
|
number of decimals to fix. |
defaultValue |
(function() or any type) |
|
value to be returned if non number received |
num |
Float |
|
float number that we want to fix it's decimals. |
- See also
- Returns
-
Float