Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / exotic   js

Repository URL to install this package:

Version: 2.0.8 

/ src / types / primitive / boolean / cast / toBoolean.ts

import ecmaToBoolean from './ecmaToBoolean'

const matchTrue = /^("|')?(true)("|')?$/i
const matchFalse = /^("|')?(false)("|')?$/i

// @todo need to export
const isTruish = (x: any): x is true | 'true' | '"true"' | `'true'` =>
  x === true || (typeof x === 'string' && matchTrue.test(x))
const isFalsish = (x): x is false | 'false' | '"false"' | `'false'` =>
  x === false || (typeof x === 'string' && matchFalse.test(x))

const fromIshToBoolean = (x: any): boolean => {
  if (isTruish(x)) {
    return true
  } else if (isFalsish(x)) {
    return false
  } else {
    return ecmaToBoolean(x)
  }
}

export { fromIshToBoolean }
export default fromIshToBoolean