@marianmeres/parse-boolean
v2.0.4
Published
[](https://www.npmjs.com/package/@marianmeres/parse-boolean) [](https://jsr.io/@marianmeres/parse-boolean)
Readme
@marianmeres/parse-boolean
A little utility which parses any input to boolean. Almost as simple as (v) => !!v
except that it differently handles strings.
Only truthy strings are true, t, yes, y, on, ok, enable, enabled and
numeric ones except zero. All others are considered falsey. This dictionary can be globally
extended to your own needs. Case insensitive.
Mainly useful for string-to-boolean conversion from text config files, html form values, or similar...
Install
deno add jsr:@marianmeres/parse-booleannpm install @marianmeres/parse-booleanUsage
parseBoolean(value: any): booleanExamples
parseBoolean('yEs'); // true
parseBoolean('ON'); // true
parseBoolean(''); // false
parseBoolean('foo'); // false
parseBoolean('-0.0'); // false
parseBoolean('{}'); // false
// all non-strings are casted as !!v
parseBoolean({}); // true
parseBoolean(NaN); // false
parseBoolean(123); // trueCustom dictionary
You can extend the truthy dictionary with your own values:
parseBoolean('yo'); // false
parseBoolean.addTruthy('yo');
parseBoolean('YO'); // true (case insensitive)Reset the dictionary to remove all custom values:
parseBoolean.addTruthy('custom');
parseBoolean('custom'); // true
parseBoolean.reset();
parseBoolean('custom'); // false
parseBoolean('yes'); // true (default values still work)API
parseBoolean(value: any): boolean
Parses any input value to a boolean.
- Non-string values: Uses standard JavaScript truthy/falsy conversion (
!!value) - Numeric strings: Parsed as numbers (zero is false, non-zero is true)
- String dictionary:
"true","t","yes","y","on","ok","enable","enabled"(case insensitive) - All other strings: Considered falsy
parseBoolean.addTruthy(value: string): void
Adds a custom string to the truthy dictionary. Values are normalized (lowercased and trimmed).
parseBoolean.reset(): void
Resets the truthy dictionary to default values, removing all custom additions.
Package Identity
- Name: @marianmeres/parse-boolean
- Author: Marian Meres
- Repository: https://github.com/marianmeres/parse-boolean
- License: MIT
