12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 'use strict';
- const Hoek = require('@hapi/hoek');
- const Any = require('../any');
- const internals = {
- Set: require('../../set')
- };
- internals.Boolean = class extends Any {
- constructor() {
- super();
- this._type = 'boolean';
- this._flags.insensitive = true;
- this._inner.truthySet = new internals.Set();
- this._inner.falsySet = new internals.Set();
- }
- _base(value, state, options) {
- const result = {
- value
- };
- if (typeof value === 'string' &&
- options.convert) {
- const normalized = this._flags.insensitive ? value.toLowerCase() : value;
- result.value = (normalized === 'true' ? true
- : (normalized === 'false' ? false : value));
- }
- if (typeof result.value !== 'boolean') {
- result.value = (this._inner.truthySet.has(value, null, null, this._flags.insensitive) ? true
- : (this._inner.falsySet.has(value, null, null, this._flags.insensitive) ? false : value));
- }
- result.errors = (typeof result.value === 'boolean') ? null : this.createError('boolean.base', { value }, state, options);
- return result;
- }
- truthy(...values) {
- const obj = this.clone();
- values = Hoek.flatten(values);
- for (let i = 0; i < values.length; ++i) {
- const value = values[i];
- Hoek.assert(value !== undefined, 'Cannot call truthy with undefined');
- obj._inner.truthySet.add(value);
- }
- return obj;
- }
- falsy(...values) {
- const obj = this.clone();
- values = Hoek.flatten(values);
- for (let i = 0; i < values.length; ++i) {
- const value = values[i];
- Hoek.assert(value !== undefined, 'Cannot call falsy with undefined');
- obj._inner.falsySet.add(value);
- }
- return obj;
- }
- insensitive(enabled) {
- const insensitive = enabled === undefined ? true : !!enabled;
- if (this._flags.insensitive === insensitive) {
- return this;
- }
- const obj = this.clone();
- obj._flags.insensitive = insensitive;
- return obj;
- }
- describe() {
- const description = super.describe();
- description.truthy = [true, ...this._inner.truthySet.values()];
- description.falsy = [false, ...this._inner.falsySet.values()];
- return description;
- }
- };
- module.exports = new internals.Boolean();
|