@jasurbek2208/toboolean
v1.5.5
Published
Lightweight utility to convert inputs to booleans, handling case-insensitive strings and edge cases
Readme
@jasurbek2208/toboolean
Lightweight utility to convert inputs to booleans, handling case-insensitive strings and edge cases
Install
npm install @jasurbek2208/tobooleanUsage
import { toBoolean, toBooleanOnly } from '@jasurbek2208/toboolean'
//More permissive:
toBoolean('true'|'str'|{a:2}|true|[1]|1)//true
toBoolean(' FalSE'|''|{}|[]|0|null|undefined)//false
//Strict string/boolean parsing:
toBooleanOnly('true'|true)//true
//the rest is falseAPI
toBoolean(text: string | boolean | number | object | null | undefined): boolean
- Input: String, boolean, number, object, array,
null, orundefined - Returns:
truefor'true'(case-insensitive), non-empty arrays/objects, or truthy non-strings;falseotherwise
toBooleanOnly(text: string | boolean | null | undefined): boolean
- Input: String, boolean,
null, orundefined - Returns:
trueif the input is the booleantrueor the string'true'(case-insensitive);falseotherwise
Features
- Case-insensitive, trims whitespace
- Handles
null,undefined, invalid inputs - Lightweight
