diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1870cf..441975c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,12 +10,10 @@ jobs: fail-fast: false matrix: node-version: - - 14 - - 12 - - 10 + - 16 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index aec0f1e..a7a8c6d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,23 +1,21 @@ -declare namespace yn { - interface Options { - /** - Use a key distance-based score to leniently accept typos of `yes` and `no`. - - @default false - */ - readonly lenient?: boolean; - - /** - Default value if no match was found. - - @default undefined - */ - readonly default?: boolean | undefined; - } - - interface OptionsWithDefault extends Options { - default: boolean; - } +export interface Options { + /** + Use a key distance-based score to leniently accept typos of `yes` and `no`. + + @default false + */ + readonly lenient?: boolean; + + /** + The default value if no match was found. + + @default undefined + */ + readonly default?: boolean | undefined; +} + +export interface OptionsWithDefault extends Options { + readonly default: boolean; } /** @@ -25,12 +23,12 @@ Parse yes/no like values. The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`, 'on', 'off' -@param input - Value that should be converted. +@param input - The value that should be converted. @returns The parsed input if it can be parsed or the default value defined in the `default` option. @example ``` -import yn = require('yn'); +import yn from 'yn'; yn('y'); //=> true @@ -51,7 +49,5 @@ yn('mo', {lenient: true}); //=> false ``` */ -declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean; -declare function yn(input: unknown, options?: yn.Options): boolean | undefined; - -export = yn; +export default function yn(input: unknown, options: OptionsWithDefault): boolean; +export default function yn(input: unknown, options?: Options): boolean | undefined; diff --git a/index.js b/index.js index e97c181..cfaf973 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,9 @@ -'use strict'; -const lenientFunction = require('./lenient'); +import lenientFunction from './lenient.js'; -const yn = (value, { +export default function yn(value, { lenient = false, - default: default_ -} = {}) => { + default: default_, +} = {}) { value = String(value).trim(); if (default_ !== undefined && typeof default_ !== 'boolean') { @@ -24,6 +23,4 @@ const yn = (value, { } return default_; -}; - -module.exports = yn; +} diff --git a/index.test-d.ts b/index.test-d.ts index 92ce6d5..e2b8543 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import yn = require('.'); +import yn from './index.js'; expectType(yn('y')); expectType(yn('mo', {lenient: true})); diff --git a/lenient.js b/lenient.js index 68012d7..d018f77 100644 --- a/lenient.js +++ b/lenient.js @@ -1,5 +1,3 @@ -'use strict'; - const YES_MATCH_SCORE_THRESHOLD = 2; const NO_MATCH_SCORE_THRESHOLD = 1.25; @@ -12,9 +10,10 @@ const yMatch = new Map([ ['u', 0.75], ['g', 0.25], ['h', 0.25], - ['j', 0.25] + ['j', 0.25], ]); +// eslint-disable-next-line unicorn/prevent-abbreviations const eMatch = new Map([ [2, 0.25], [3, 0.25], @@ -24,7 +23,7 @@ const eMatch = new Map([ ['r', 0.75], ['s', 0.25], ['d', 0.25], - ['f', 0.25] + ['f', 0.25], ]); const sMatch = new Map([ @@ -36,7 +35,7 @@ const sMatch = new Map([ ['d', 0.75], ['z', 0.25], ['x', 0.25], - ['c', 0.25] + ['c', 0.25], ]); const nMatch = new Map([ @@ -45,7 +44,7 @@ const nMatch = new Map([ ['k', 0.25], ['b', 0.75], ['n', 1], - ['m', 0.75] + ['m', 0.75], ]); const oMatch = new Map([ @@ -55,10 +54,11 @@ const oMatch = new Map([ ['o', 1], ['p', 0.75], ['k', 0.25], - ['l', 0.25] + ['l', 0.25], ]); function getYesMatchScore(value) { + // eslint-disable-next-line unicorn/prevent-abbreviations const [y, e, s] = value; let score = 0; @@ -92,7 +92,7 @@ function getNoMatchScore(value) { return score; } -module.exports = (input, default_) => { +export default function lenient(input, default_) { if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) { return true; } @@ -102,4 +102,4 @@ module.exports = (input, default_) => { } return default_; -}; +} diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index cf3b358..e4e09c1 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Parse yes/no like values", "license": "MIT", "repository": "sindresorhus/yn", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=10" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd" @@ -35,8 +38,8 @@ "lenient" ], "devDependencies": { - "ava": "^2.4.0", - "tsd": "^0.11.0", - "xo": "^0.25.3" + "ava": "^3.15.0", + "tsd": "^0.17.0", + "xo": "^0.44.0" } } diff --git a/readme.md b/readme.md index aef3a02..f0328b2 100644 --- a/readme.md +++ b/readme.md @@ -14,18 +14,16 @@ The following case-insensitive values are recognized: *Enable lenient mode to gracefully handle typos.* - ## Install ``` $ npm install yn ``` - ## Usage ```js -const yn = require('yn'); +import yn from 'yn'; yn('y'); //=> true @@ -48,7 +46,6 @@ yn('mo', {lenient: true}); Unrecognized values return `undefined`. - ## API ### yn(input, options?) @@ -57,7 +54,7 @@ Unrecognized values return `undefined`. Type: `unknown` -Value that should be converted. +The value that should be converted. #### options @@ -75,4 +72,4 @@ Use a key distance-based score to leniently accept typos of `yes` and `no`. Type: `boolean`\ Default: `undefined` -Default value if no match was found. +The default value if no match was found. diff --git a/test.js b/test.js index 0d6fc8f..5b8bba2 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import yn from '.'; +import yn from './index.js'; const truthyCases = [ 'y', @@ -13,7 +13,7 @@ const truthyCases = [ true, '1', 1, - 'on' + 'on', ]; test('truthy cases', t => { for (const case_ of truthyCases) { @@ -34,7 +34,7 @@ const falseyCases = [ false, '0', 0, - 'off' + 'off', ]; test('falsey cases', t => { for (const case_ of falseyCases) { @@ -45,7 +45,7 @@ test('falsey cases', t => { const undefinedCases = [ // Falsey cases that don't work - NaN, + Number.NaN, null, undefined, '', @@ -66,7 +66,7 @@ const undefinedCases = [ 'n o', 'yn', // Other - 'unicorn' + 'unicorn', ]; test('undefined cases', t => { for (const case_ of undefinedCases) { @@ -92,7 +92,9 @@ test('lenient option - falsey value cases', t => { test('default option throws error if not a boolean type', t => { t.throws(() => { yn('10', {default: 10}); - }, 'Expected the `default` option to be of type `boolean`, got `number`'); + }, { + message: 'Expected the `default` option to be of type `boolean`, got `number`', + }); }); test('default option', t => {