1.1.2 • Published 7 years ago

qc-to_bool v1.1.2

Weekly downloads
18
License
ISC
Repository
github
Last release
7 years ago

qc-to_bool

Build Status Coverage Status License Downloads

npm badge

A simple JavaScript utility to convert various values to a boolean.

Installation

npm install --save qc-to_bool

Example Usage

import { toBool, toBoolOrNull } from 'qc-to_bool';

toBool(new Boolean(true));                  // `true`
toBool(new Boolean(false));                 // `false`
toBool(true);                               // `true`
toBool(false);                              // `false`
toBool(1);                                  // `true`
toBool(0);                                  // `false`
toBool('1');                                // `true`
toBool('0');                                // `false`
toBool('t');                                // `true`
toBool('f');                                // `false`
toBool('Y');                                // `true`
toBool('N');                                // `false`
toBool('on');                               // `true`
toBool('off');                              // `false`
toBool('true');                             // `true`
toBool('false');                            // `false`
toBool('Yes');                              // `true`
toBool('No');                               // `false`
toBool('other');                            // `'other'` (input)
toBool('other', false);                     // `false`
toBool('other', { def: false });            // `false`
toBool({});                                 // `{}` (input)
toBool({}, false);                          // `false`
toBool({}, true);                           // `true`
toBool({}, null);                           // `null`
toBool({}, { def: false });                 // `false`
toBool({ valueOf () { return 'yes'; } });   // `true`
toBoolOrNull(false);                        // `false`
toBoolOrNull('1');                          // `true`
toBoolOrNull('other');                      // `null`
toBoolOrNull({});                           // `null`

// Use different converter:
toBool('');                                 // `''` (input)
// Based on JavaScript truthiness:
toBool.setConverter(x => !!x);
toBool('');                                 // `false`
toBool({});                                 // `true`

// Based on Japanese language:
let converter = (input) => {
  let coercedInput, output;

  if (input !== undefined && input !== null) {
    if (typeof input.valueOf == 'function') {
      coercedInput = input.valueOf();
      if (coercedInput && typeof coercedInput.toString == 'function') {
        coercedInput = coercedInput.toString().toLowerCase();
      }
      output = converter.lut[coercedInput];
    }
  }
  return output;
};
converter.lut = {
  h: true,
  hai: true,
  i: false,
  iie: false,
};
toBool.setConverter(converter);
toBool('hai');                              // `true`
toBool('iie');                              // `false`

// Set back to default converter:
toBool.resetConverter();
1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago