• File: index.js
  • Full Path: /var/www/nodejs/daily_store_reports_nodejs/node_modules/yn/index.js
  • Date Modified: 02/04/2023 9:31 PM
  • File size: 578 bytes
  • MIME-type: text/plain
  • Charset: utf-8
'use strict';
const lenient = require('./lenient');

module.exports = (val, opts) => {
	val = String(val).trim();
	opts = Object.assign({
		lenient: false,
		default: null
	}, opts);

	if (opts.default !== null && typeof opts.default !== 'boolean') {
		throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof opts.default}\``);
	}

	if (/^(?:y|yes|true|1)$/i.test(val)) {
		return true;
	}

	if (/^(?:n|no|false|0)$/i.test(val)) {
		return false;
	}

	if (opts.lenient === true) {
		return lenient(val, opts);
	}

	return opts.default;
};