UNPKG

@yobta/validator

Version:
18 lines (17 loc) 567 B
import { rule } from '../rule/rule.js'; const truthySet = new Set(['1', 'true', 'yes']); const falsySet = new Set(['0', 'false', 'no', 'null']); export const booleanMessage = 'It should be a boolean'; export const boolean = (message = booleanMessage) => rule((input = '') => { if (input === '') { return undefined; } const lowerCasedInput = String(input).toLowerCase(); if (falsySet.has(lowerCasedInput)) { return false; } else if (truthySet.has(lowerCasedInput)) { return true; } throw new Error(message); });