Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
sindresorhus committed Aug 14, 2021
1 parent 7e8adfc commit 2ac2b7d
Showing 9 changed files with 59 additions and 66 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
48 changes: 22 additions & 26 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
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;
}

/**
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;
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import yn = require('.');
import yn from './index.js';

expectType<boolean | undefined>(yn('y'));
expectType<boolean | undefined>(yn('mo', {lenient: true}));
18 changes: 9 additions & 9 deletions lenient.js
Original file line number Diff line number Diff line change
@@ -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_;
};
}
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 8 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -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 => {

0 comments on commit 2ac2b7d

Please sign in to comment.