Configurable utility for coercing values.
Install the library via NPM:
npm install @InlineManual/coerce --save
Then use in your project like this:
import constructCoertor from 'coerce';
You can use predefined coercion configs:
var coerceText = constructCoertor('text');
coerceText(); // "" (empty string)
coerceText('aaa'); // "aaa"
coerceText(123); // "123" (number converted to string)
coerceText(true); // "true"
Or you can define your own coercion config:
var coerceCustom = constructCoertor({
number: 'some number',
text: function (input) {return 'aaa' + input;}
});
// if value is any other type than function, it will be returned for
// that type
coerceCustom(123); // "some number"
// function will be used to transform the input value
coerceCustom('bbb'); // "aaabbb"
// if value for any type is not defined, `null` will be used
coerceCustom(true); // null
- Converts
null
andunefined
to an empty array ([]
). - Leaves
array
unchanged.
Used to handle timestamps.
- Leaves
number
unchanged. - Converts
now
to current timestamp. - Converts strings like
-1 hour
to relative timestamp. Seeparse-relative-time
for more details.
- Treats
string
as CSS selector, returns first matched element (if any). - Returns
object
unchanged.
- Converts
undefined
totrue
. - Converts
null
totrue
. - Converts
array
totrue
if empty, otherwisefalse
. - Converts
string
totrue
if empty, otherwisefalse
. - Converts
number
totrue
if value is0
, otherwisefalse
. - Converts
boolean
totrue
iffalse
, otherwisefalse
. (Yeah, I know this is a bit confusing, but if you think about it, it makes sense.) - Converts
object
totrue
if it has no keys (e.g.{}
), otherwisefalse
. - Converts
function
to false.
Same as empty
, but always converts number
to false
.
Same as empty
, but always converts number
to true
.
- Leaves
number
unchanged. - Converts
undefined
to0
. - Converts
null
to0
. - Converts
false
to0
,true
to1
. string
- If empty, converts to
0
. - If it is possible to parse the string to number, returns parsed number.
- Otherwise returns
null
.
- If empty, converts to
Alias: string
- Leaves
string
unchanged. - Converts
undefined
to""
(empty string). - Converts
null
to""
(empty string). - Converts
number
to string. - Converts
false
to"false"
,true
to"true"
(textual representation).
- Leaves
function
unchanged. - Converts all other types to a function that returns original input.
- Leaves
boolean
unchanged. - Converts
undefined
andnull
tofalse
. - Converts zero (
0
), empty string (""
), empty array ([]
) and empty object ({}
) tofalse
, otherwise totrue
. - Evaluates
function
and converts returned result to boolean.
Constructs function that will coerce any input according to config.
Parameters
config
[(string | coercionConfig)] Identifier of pre-made coercion config (string) or custom config (object).
Returns Function
Parameters
config
(optional, default{}
)
Parameters
config
(optional, default{}
)
If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at riki@fczbkk.com.
Coerce is published under the MIT license.