Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 30, 2024
1 parent 9d562fe commit 5ce6abf
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 73 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
60 changes: 28 additions & 32 deletions fetch-words.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
const fs = require('fs');
const got = require('got');
import process from 'node:process';
import fs from 'node:fs';
import got from 'got';
import badWords_ from 'badwords-list';
import leoProfanity from 'leo-profanity';

const isVerbose = process.argv.includes('--verbose');
const {array: badWords} = badWords_;

const {array: badWords} = require('badwords-list');
const leoProfanity = require('leo-profanity');
const isVerbose = process.argv.includes('--verbose');

leoProfanity.loadDictionary('en');

Expand Down Expand Up @@ -244,52 +245,47 @@ const ourBadWordsSet = new Set([
'zoophiles',
'zoophilias',
'zoophilic',
'zoophilies'
'zoophilies',
]);

const url = 'https://raw.githubusercontent.com/atebits/Words/master/Words/en.txt';

const filters = [
{
name: 'leo-profanity',
getReason: word => leoProfanity.check(word) ? 'is marked bad by leo-profanity' : undefined
getReason: word => leoProfanity.check(word) ? 'is marked bad by leo-profanity' : undefined,
},
{
name: 'badwords-list exact',
getReason: word => badWordsSet.has(word) ? 'is in badwords-list' : undefined
getReason: word => badWordsSet.has(word) ? 'is in badwords-list' : undefined,
},
{
name: 'our bad words list',
getReason: word => ourBadWordsSet.has(word) ? 'is in our bad words list' : undefined
}
getReason: word => ourBadWordsSet.has(word) ? 'is in our bad words list' : undefined,
},
];

(async () => {
const {body} = await got(url);
let words = body.trim().split('\n');
const {body} = await got(url);
let words = body.trim().split('\n');

const originalLength = words.length;
const originalLength = words.length;

for (const filter of filters) {
const previousLength = words.length;
for (const filter of filters) {
const previousLength = words.length;

words = words.filter(word => {
const reason = filter.getReason(word);
words = words.filter(word => {
const reason = filter.getReason(word);

if (reason !== undefined && isVerbose) {
console.log(`word \`${word}\` excluded because it ${reason}`);
}
if (reason !== undefined && isVerbose) {
console.log(`word \`${word}\` excluded because it ${reason}`);
}

return reason === undefined;
});
return reason === undefined;
});

console.log(`filtered ${previousLength - words.length} bad words with filter '${filter.name}'`);
}
console.log(`filtered ${previousLength - words.length} bad words with filter '${filter.name}'`);
}

console.log(`filtered ${originalLength - words.length} bad words total out of ${originalLength} original words`);
console.log(`filtered ${originalLength - words.length} bad words total out of ${originalLength} original words`);

fs.writeFileSync('words.txt', words.join('\n'));
})().catch(error => {
console.error(error);
process.exit(1); // eslint-disable-line unicorn/no-process-exit
});
fs.writeFileSync('words.txt', words.join('\n'));
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ List of [English words](https://github.com/atebits/Words/blob/master/Words/en.tx
@example
```
import * as fs from 'fs';
import fs from 'node:fs';
// Returns the path to the word list which is separated by `\n`
import wordListPath = require('word-list');
// Returns the path to the word list which is separated by `\n`.
import wordListPath from 'word-list';
const wordArray = fs.readFileSync(wordListPath, 'utf8').split('\n');
//=> […, 'abmhos', 'abnegate', …]
```
*/
declare const wordsListPath: string;

export = wordsListPath;
export default wordsListPath;
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
'use strict';
const path = require('path');
import {fileURLToPath} from 'node:url';
import path from 'node:path';

module.exports = path.join(__dirname, 'words.txt');
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const wordListPath = path.join(__dirname, 'words.txt');

export default wordListPath;
4 changes: 0 additions & 4 deletions index.test-d.ts

This file was deleted.

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:

Expand Down
22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
"description": "List of English words",
"license": "MIT",
"repository": "sindresorhus/word-list",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=8"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd",
"test": "xo && ava",
"build": "node fetch-words.js",
"prepare": "npm run build",
"pretest": "npm run build"
Expand All @@ -34,11 +41,10 @@
"dictionary"
],
"devDependencies": {
"ava": "^2.1.0",
"ava": "^6.1.2",
"badwords-list": "^1.0.0",
"got": "^9.6.0",
"leo-profanity": "^1.2.5",
"tsd": "^0.7.3",
"xo": "^0.24.0"
"got": "^13.0.0",
"leo-profanity": "^1.7.0",
"xo": "^0.58.0"
}
}
12 changes: 5 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ Used by [`word-stream`](https://github.com/sindresorhus/word-stream) and [`rando

One-letter words are not included. Many common bad words are also filtered out.


## Install

```sh
npm install word-list
```
$ npm install word-list
```


## Usage

```js
const fs = require('fs');
import fs from 'node:fs';

// Returns the path to the word list which is separated by `\n`
const wordListPath = require('word-list');
// Returns the path to the word list which is separated by `\n`.
import wordListPath from 'word-list';

const wordArray = fs.readFileSync(wordListPath, 'utf8').split('\n');
//=> […, 'abmhos', 'abnegate', …]
Expand Down
12 changes: 7 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs';
import fs from 'node:fs';
import test from 'ava';
import wordList from '.';
import wordList from './index.js';

const badWords = [
'bumfuck',
'bumfucks',
'ass'
'ass',
];

const goodWords = [
Expand All @@ -28,23 +28,25 @@ const goodWords = [
'pronoun',
'scrap',
'aerospace',
'backspace'
'backspace',
];

test('main', t => {
t.true(wordList.length > 0);
t.true(fs.statSync(wordList).size > 1000);
});

test('bad words, #2', t => {
test('bad words', t => {
const wordListText = fs.readFileSync(wordList, 'utf8');
const words = wordListText.split('\n');

for (const badWord of badWords) {
// eslint-disable-next-line ava/assertion-arguments
t.false(words.includes(badWord), badWord);
}

for (const goodWord of goodWords) {
// eslint-disable-next-line ava/assertion-arguments
t.true(words.includes(goodWord), goodWord);
}
});

0 comments on commit 5ce6abf

Please sign in to comment.