Skip to content

Commit 2ac2b7d

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent 7e8adfc commit 2ac2b7d

File tree

9 files changed

+59
-66
lines changed

9 files changed

+59
-66
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
13+
- 16
1614
steps:
1715
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
1917
with:
2018
node-version: ${{ matrix.node-version }}
2119
- run: npm install

index.d.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
declare namespace yn {
2-
interface Options {
3-
/**
4-
Use a key distance-based score to leniently accept typos of `yes` and `no`.
5-
6-
@default false
7-
*/
8-
readonly lenient?: boolean;
9-
10-
/**
11-
Default value if no match was found.
12-
13-
@default undefined
14-
*/
15-
readonly default?: boolean | undefined;
16-
}
17-
18-
interface OptionsWithDefault extends Options {
19-
default: boolean;
20-
}
1+
export interface Options {
2+
/**
3+
Use a key distance-based score to leniently accept typos of `yes` and `no`.
4+
5+
@default false
6+
*/
7+
readonly lenient?: boolean;
8+
9+
/**
10+
The default value if no match was found.
11+
12+
@default undefined
13+
*/
14+
readonly default?: boolean | undefined;
15+
}
16+
17+
export interface OptionsWithDefault extends Options {
18+
readonly default: boolean;
2119
}
2220

2321
/**
2422
Parse yes/no like values.
2523
2624
The following case-insensitive values are recognized: `'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0`, 'on', 'off'
2725
28-
@param input - Value that should be converted.
26+
@param input - The value that should be converted.
2927
@returns The parsed input if it can be parsed or the default value defined in the `default` option.
3028
3129
@example
3230
```
33-
import yn = require('yn');
31+
import yn from 'yn';
3432
3533
yn('y');
3634
//=> true
@@ -51,7 +49,5 @@ yn('mo', {lenient: true});
5149
//=> false
5250
```
5351
*/
54-
declare function yn(input: unknown, options: yn.OptionsWithDefault): boolean;
55-
declare function yn(input: unknown, options?: yn.Options): boolean | undefined;
56-
57-
export = yn;
52+
export default function yn(input: unknown, options: OptionsWithDefault): boolean;
53+
export default function yn(input: unknown, options?: Options): boolean | undefined;

index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use strict';
2-
const lenientFunction = require('./lenient');
1+
import lenientFunction from './lenient.js';
32

4-
const yn = (value, {
3+
export default function yn(value, {
54
lenient = false,
6-
default: default_
7-
} = {}) => {
5+
default: default_,
6+
} = {}) {
87
value = String(value).trim();
98

109
if (default_ !== undefined && typeof default_ !== 'boolean') {
@@ -24,6 +23,4 @@ const yn = (value, {
2423
}
2524

2625
return default_;
27-
};
28-
29-
module.exports = yn;
26+
}

index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import yn = require('.');
2+
import yn from './index.js';
33

44
expectType<boolean | undefined>(yn('y'));
55
expectType<boolean | undefined>(yn('mo', {lenient: true}));

lenient.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const YES_MATCH_SCORE_THRESHOLD = 2;
42
const NO_MATCH_SCORE_THRESHOLD = 1.25;
53

@@ -12,9 +10,10 @@ const yMatch = new Map([
1210
['u', 0.75],
1311
['g', 0.25],
1412
['h', 0.25],
15-
['j', 0.25]
13+
['j', 0.25],
1614
]);
1715

16+
// eslint-disable-next-line unicorn/prevent-abbreviations
1817
const eMatch = new Map([
1918
[2, 0.25],
2019
[3, 0.25],
@@ -24,7 +23,7 @@ const eMatch = new Map([
2423
['r', 0.75],
2524
['s', 0.25],
2625
['d', 0.25],
27-
['f', 0.25]
26+
['f', 0.25],
2827
]);
2928

3029
const sMatch = new Map([
@@ -36,7 +35,7 @@ const sMatch = new Map([
3635
['d', 0.75],
3736
['z', 0.25],
3837
['x', 0.25],
39-
['c', 0.25]
38+
['c', 0.25],
4039
]);
4140

4241
const nMatch = new Map([
@@ -45,7 +44,7 @@ const nMatch = new Map([
4544
['k', 0.25],
4645
['b', 0.75],
4746
['n', 1],
48-
['m', 0.75]
47+
['m', 0.75],
4948
]);
5049

5150
const oMatch = new Map([
@@ -55,10 +54,11 @@ const oMatch = new Map([
5554
['o', 1],
5655
['p', 0.75],
5756
['k', 0.25],
58-
['l', 0.25]
57+
['l', 0.25],
5958
]);
6059

6160
function getYesMatchScore(value) {
61+
// eslint-disable-next-line unicorn/prevent-abbreviations
6262
const [y, e, s] = value;
6363
let score = 0;
6464

@@ -92,7 +92,7 @@ function getNoMatchScore(value) {
9292
return score;
9393
}
9494

95-
module.exports = (input, default_) => {
95+
export default function lenient(input, default_) {
9696
if (getYesMatchScore(input) >= YES_MATCH_SCORE_THRESHOLD) {
9797
return true;
9898
}
@@ -102,4 +102,4 @@ module.exports = (input, default_) => {
102102
}
103103

104104
return default_;
105-
};
105+
}

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
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:
66

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Parse yes/no like values",
55
"license": "MIT",
66
"repository": "sindresorhus/yn",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "[email protected]",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=10"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -35,8 +38,8 @@
3538
"lenient"
3639
],
3740
"devDependencies": {
38-
"ava": "^2.4.0",
39-
"tsd": "^0.11.0",
40-
"xo": "^0.25.3"
41+
"ava": "^3.15.0",
42+
"tsd": "^0.17.0",
43+
"xo": "^0.44.0"
4144
}
4245
}

readme.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@ The following case-insensitive values are recognized:
1414

1515
*Enable lenient mode to gracefully handle typos.*
1616

17-
1817
## Install
1918

2019
```
2120
$ npm install yn
2221
```
2322

24-
2523
## Usage
2624

2725
```js
28-
const yn = require('yn');
26+
import yn from 'yn';
2927

3028
yn('y');
3129
//=> true
@@ -48,7 +46,6 @@ yn('mo', {lenient: true});
4846

4947
Unrecognized values return `undefined`.
5048

51-
5249
## API
5350

5451
### yn(input, options?)
@@ -57,7 +54,7 @@ Unrecognized values return `undefined`.
5754

5855
Type: `unknown`
5956

60-
Value that should be converted.
57+
The value that should be converted.
6158

6259
#### options
6360

@@ -75,4 +72,4 @@ Use a key distance-based score to leniently accept typos of `yes` and `no`.
7572
Type: `boolean`\
7673
Default: `undefined`
7774

78-
Default value if no match was found.
75+
The default value if no match was found.

test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import yn from '.';
2+
import yn from './index.js';
33

44
const truthyCases = [
55
'y',
@@ -13,7 +13,7 @@ const truthyCases = [
1313
true,
1414
'1',
1515
1,
16-
'on'
16+
'on',
1717
];
1818
test('truthy cases', t => {
1919
for (const case_ of truthyCases) {
@@ -34,7 +34,7 @@ const falseyCases = [
3434
false,
3535
'0',
3636
0,
37-
'off'
37+
'off',
3838
];
3939
test('falsey cases', t => {
4040
for (const case_ of falseyCases) {
@@ -45,7 +45,7 @@ test('falsey cases', t => {
4545

4646
const undefinedCases = [
4747
// Falsey cases that don't work
48-
NaN,
48+
Number.NaN,
4949
null,
5050
undefined,
5151
'',
@@ -66,7 +66,7 @@ const undefinedCases = [
6666
'n o',
6767
'yn',
6868
// Other
69-
'unicorn'
69+
'unicorn',
7070
];
7171
test('undefined cases', t => {
7272
for (const case_ of undefinedCases) {
@@ -92,7 +92,9 @@ test('lenient option - falsey value cases', t => {
9292
test('default option throws error if not a boolean type', t => {
9393
t.throws(() => {
9494
yn('10', {default: 10});
95-
}, 'Expected the `default` option to be of type `boolean`, got `number`');
95+
}, {
96+
message: 'Expected the `default` option to be of type `boolean`, got `number`',
97+
});
9698
});
9799

98100
test('default option', t => {

0 commit comments

Comments
 (0)