Configurable utility for coercing values.
Install the library via NPM:
npm install @InlineManual/coerce --saveThen 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
nullandunefinedto an empty array ([]). - Leaves
arrayunchanged.
Used to handle timestamps.
- Leaves
numberunchanged. - Converts
nowto current timestamp. - Converts strings like
-1 hourto relative timestamp. Seeparse-relative-timefor more details.
- Treats
stringas CSS selector, returns first matched element (if any). - Returns
objectunchanged.
- Converts
undefinedtotrue. - Converts
nulltotrue. - Converts
arraytotrueif empty, otherwisefalse. - Converts
stringtotrueif empty, otherwisefalse. - Converts
numbertotrueif value is0, otherwisefalse. - Converts
booleantotrueiffalse, otherwisefalse. (Yeah, I know this is a bit confusing, but if you think about it, it makes sense.) - Converts
objecttotrueif it has no keys (e.g.{}), otherwisefalse. - Converts
functionto false.
Same as empty, but always converts number to false.
Same as empty, but always converts number to true.
- Leaves
numberunchanged. - Converts
undefinedto0. - Converts
nullto0. - Converts
falseto0,trueto1. 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
stringunchanged. - Converts
undefinedto""(empty string). - Converts
nullto""(empty string). - Converts
numberto string. - Converts
falseto"false",trueto"true"(textual representation).
- Leaves
functionunchanged. - Converts all other types to a function that returns original input.
- Leaves
booleanunchanged. - Converts
undefinedandnulltofalse. - Converts zero (
0), empty string (""), empty array ([]) and empty object ({}) tofalse, otherwise totrue. - Evaluates
functionand 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 [email protected].
Coerce is published under the MIT license.