1.1.7 • Published 5 months ago

@marianmeres/parse-boolean v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@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

$ npm i @marianmeres/parse-boolean

Usage

parseBoolean(value: any): boolean

Examples

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); // true

Custom dictionary example

parseBoolean('yo'); // false
parseBoolean.addTruthy('yo');
parseBoolean('YO'); // true
1.1.7

5 months ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago