1.2.0 • Published 2 years ago
@simonedelpopolo/to-bool v1.2.0
to-bool
Converts only the strings "true" and "false" to boolean respectively
javascript ESM module
Other functionalities
Installation
npm install @simonedelpopolo/to-bool
Usage
ESM
import { bool } from '@simonedelpopolo/to-bool'
const converted = await bool( 'true' )
console.log( converted )
Other Functionalities
Function map( object, string OR number )
Given an object it will map the property name to the corresponding boolean value set.
It accepts only type object for the logic
parameter, and it will check if any of the value set for the property is a boolean and not any other type.
It accepts only type string or number for the against
parameter
Usage
ESM
This will return false
import { map } from '@simonedelpopolo/to-bool'
/**
* @type {string}
*/
const against = 'no'
/**
* @type {{no: boolean, yes: boolean, true: boolean, false: boolean}}
*/
const logic = {
true: true,
false: false,
yes: true,
no: false
}
const matchBool = await map( logic, against )
console.log( matchBool ) // return 'false'
This will reject with:
your against
value haven't matched any of your logic
object. given against
: ok
import { map } from '@simonedelpopolo/to-bool'
/**
* @type {string}
*/
const against = 'ok'
/**
* @type {{no: boolean, yes: boolean, true: boolean, false: boolean}}
*/
const logic = {
true: true,
false: false,
yes: true,
no: false
}
const matchBool = await map( logic, against )
console.log( matchBool ) // reject -> your `against` value haven't matched any of your `logic` object. given `against`: ok