Remove duplicate words and add unit tests

This commit is contained in:
Quantum 2018-11-27 19:56:46 -05:00
parent e8ed9a64e9
commit 338f564f8d
6 changed files with 656 additions and 1187 deletions

1769
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,7 @@
"scripts": {
"build": "rimraf dist && cross-env NODE_ENV=production webpack",
"serve": "webpack-dev-server",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha tests"
},
"keywords": [],
"author": "Quantum",
@ -17,15 +17,16 @@
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.4.5",
"mocha": "^5.2.0",
"node-sass": "^4.10.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"postcss-loader": "^3.0.0",
"rimraf": "^2.6.2",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"terser-webpack-plugin": "latest",
"webpack": "^4.26.1",
"webpack-cli": "^3.1.2",
"terser-webpack-plugin": "latest"
"webpack-cli": "^3.1.2"
},
"dependencies": {
"bootstrap": "^4.1.3",

View file

@ -1,20 +1,19 @@
import words from './words'
const words = require('./words')
const digits = '0123456789'
const symbols = '`~!@#$%^&*()_+-=,./<>?;:|'
export function getWordList (name) {
function getWordList (name) {
if (['small'].includes(name)) {
return words[name]
}
throw new Error(`Invalid word list: ${name}`)
}
export function getWords (list, indices) {
function getWords (list, indices) {
return Array.from(indices).map(index => list[index % list.length])
}
export function capitalize (string) {
function capitalize (string) {
return string[0].toUpperCase() + string.slice(1)
}
@ -30,7 +29,7 @@ function pickChar (options) {
return options[array[0] % options.length]
}
export function generate (options) {
function generate (options) {
let words = pickWords(getWordList(options.list), options.count)
if (options.capitalize) {
@ -48,11 +47,11 @@ export function generate (options) {
return words.join('')
}
export function lengthBits (list) {
function lengthBits (list) {
return Math.log2(list.length)
}
export function computeBits (options) {
function computeBits (options) {
const wordBits = lengthBits(getWordList(options.list))
const capsBits = options.capitalize ? 1 : 0
const symbolBits = options.symbol ? lengthBits(symbols) : 0

View file

@ -1,4 +1,4 @@
export default [
module.exports = [
'ability',
'able',
'about',
@ -293,6 +293,7 @@ export default [
'chief',
'child',
'childhood',
'chin',
'choice',
'choose',
'chose',
@ -425,7 +426,6 @@ export default [
'daughter',
'dawn',
'day',
'day',
'dead',
'deal',
'dear',
@ -485,7 +485,6 @@ export default [
'discovery',
'discussion',
'disease',
'disgrace',
'disk',
'display',
'disposition',
@ -626,6 +625,7 @@ export default [
'express',
'expression',
'exquisite',
'extensive',
'extent',
'external',
'extraordinary',
@ -755,6 +755,7 @@ export default [
'gallant',
'game',
'garden',
'gas',
'gate',
'gather',
'gave',
@ -772,7 +773,6 @@ export default [
'giant',
'gift',
'girl',
'girl',
'give',
'glad',
'glance',
@ -780,6 +780,7 @@ export default [
'gloom',
'glorious',
'glory',
'glow',
'god',
'goes',
'gold',
@ -927,6 +928,7 @@ export default [
'indignation',
'individual',
'industry',
'inevitable',
'inferior',
'infinite',
'influence',
@ -1000,19 +1002,19 @@ export default [
'larger',
'last',
'late',
'later',
'latter',
'laugh',
'laughter',
'law',
'lawyer',
'lay',
'leader',
'leaf',
'learn',
'least',
'leave',
'left',
'leg',
'leisure',
'length',
'lesson',
'lest',
@ -1136,7 +1138,6 @@ export default [
'morning',
'most',
'mother',
'mother',
'motion',
'motive',
'mountain',
@ -1197,7 +1198,6 @@ export default [
'obedience',
'obey',
'object',
'obs',
'observation',
'observe',
'obtain',
@ -1249,6 +1249,7 @@ export default [
'pair',
'palace',
'pale',
'papa',
'paper',
'paradise',
'paragraph',
@ -1403,6 +1404,8 @@ export default [
'rage',
'rain',
'raise',
'ran',
'rang',
'range',
'rank',
'rapid',
@ -1582,7 +1585,6 @@ export default [
'sharp',
'she',
'sheep',
'sheet',
'shelter',
'ship',
'shock',
@ -1820,7 +1822,6 @@ export default [
'thereof',
'these',
'they',
'they',
'thick',
'thin',
'thing',
@ -1975,7 +1976,6 @@ export default [
'western',
'wet',
'what',
'what',
'whatever',
'whatsoever',
'wheel',

View file

@ -1,5 +1,3 @@
import small from './2048'
console.assert(small.length === 2048)
export default { small }
module.exports = {
small: require('./2048')
}

22
tests/words.js Normal file
View file

@ -0,0 +1,22 @@
const assert = require('assert').strict
const words = require('../src/words')
describe('Word List Tests', () => {
const lists = ['small']
it('length', () => {
assert.equal(words.small.length, 2048, 'small list size incorrect')
})
lists.forEach(name => {
it(`${name} uniqueness`, () => {
const store = {}
words[name].forEach(word => {
const key = `word_${word}`
assert(!store[key], `word ${word} is duplicated`)
store[key] = true
})
})
})
})