index.js 578 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. const lenient = require('./lenient');
  3. module.exports = (val, opts) => {
  4. val = String(val).trim();
  5. opts = Object.assign({
  6. lenient: false,
  7. default: null
  8. }, opts);
  9. if (opts.default !== null && typeof opts.default !== 'boolean') {
  10. throw new TypeError(`Expected the \`default\` option to be of type \`boolean\`, got \`${typeof opts.default}\``);
  11. }
  12. if (/^(?:y|yes|true|1)$/i.test(val)) {
  13. return true;
  14. }
  15. if (/^(?:n|no|false|0)$/i.test(val)) {
  16. return false;
  17. }
  18. if (opts.lenient === true) {
  19. return lenient(val, opts);
  20. }
  21. return opts.default;
  22. };