Created
July 13, 2019 03:08
-
-
Save nortonwong/0f02c368d102e70220084674c49d1355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(({ strict = false, sort = true, trim = true, search = false } = {}) => { | |
const suffixes = ['s', 'ly', 'ing', 'd', 'ed', 'er', 'r', 'est', 'ist', 'al', 'ism', 'ity', 'ic']; | |
const isSuffixed = (string, strings, suffix) => string.endsWith(suffix) && strings.includes(string.substring(0, string.length - suffix.length)); | |
const matchSearch = typeof search == 'string' ? e => e.includes(search) | |
: search instanceof RegExp ? e => e.match(search) | |
: search === false ? e => true | |
: search instanceof Array ? e => search.includes(e) | |
: typeof search == 'function' ? search | |
: (() => { throw new TypeError(`Search argument was ${typeof search}: ${search}`); })(); | |
const words = strict | |
? document.body.innerText.split(/\s+/) | |
: document.body.innerText.toLowerCase().split(/[^a-z]/); | |
const vocab = [...new Set(words)] | |
.filter(e => e.length > 1) | |
.filter((e, i, a) => e.length < 4 || !trim || suffixes.every(suffix => !isSuffixed(e, a, suffix))) | |
.filter(e => matchSearch(e)); | |
if (sort) { vocab.sort(); } | |
return `${vocab.length}: ${vocab.join(' ') || '[]'}`; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment