Skip to content

Instantly share code, notes, and snippets.

@nortonwong
Created July 13, 2019 03:08
Show Gist options
  • Save nortonwong/0f02c368d102e70220084674c49d1355 to your computer and use it in GitHub Desktop.
Save nortonwong/0f02c368d102e70220084674c49d1355 to your computer and use it in GitHub Desktop.
(({ 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