Skip to content

Commit 54755e3

Browse files
authored
enchancement(scripts) Improve sorting of update-dictionary (gatsbyjs#25663)
* Make update-dictionary better * better word compare
1 parent ea48d44 commit 54755e3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

scripts/update-dictionary.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
const { execSync } = require(`child_process`)
66
const fs = require(`fs`)
77

8+
// Strip the string
9+
function trim(str) {
10+
// `*` is used to negate words, so negated words should be put with the original word
11+
// e.g. npm, *NPM
12+
return str.replace(/^\*/, ``)
13+
}
14+
15+
function compareWords(a, b) {
16+
// The empty string should go at the end
17+
if (a === ``) return 1
18+
if (b === ``) return -1
19+
// Compare the two words
20+
return trim(a).localeCompare(trim(b))
21+
}
22+
823
// Get our current list of words
924
const words = new Set(fs.readFileSync(`./dictionary.txt`, `utf-8`).split(`\n`))
1025

@@ -34,7 +49,7 @@ for (const match of matches) {
3449

3550
const wordList = [...words]
3651
// Sort in alphabetical order for convenience
37-
wordList.sort((a, b) => a.localeCompare(b))
52+
wordList.sort(compareWords)
3853

3954
// Write back to the dictionary file
4055
fs.writeFileSync(`./dictionary.txt`, wordList.join(`\n`))

0 commit comments

Comments
 (0)