@@ -67,6 +67,7 @@ export interface Pluralizable {
interface WordOptions {
interface WordOptions {
plural: boolean;
plural: boolean;
capital: boolean;
capital: boolean;
allCaps: boolean;
proper: boolean;
proper: boolean;
kind: NounKind;
kind: NounKind;
vowel: VowelSound;
vowel: VowelSound;
@@ -74,6 +75,7 @@ interface WordOptions {
}
}
const emptyConfig: WordOptions = {
const emptyConfig: WordOptions = {
allCaps: false,
capital: false,
capital: false,
count: false,
count: false,
kind: NounKind.Specific,
kind: NounKind.Specific,
@@ -117,6 +119,12 @@ export abstract class Word {
// These functions are pure; they don't mutate the original object.
// These functions are pure; they don't mutate the original object.
// This is necessary to avoid causing chaos.
// This is necessary to avoid causing chaos.
get allCaps (): this {
const opts: WordOptions = Object.assign({}, this.opt)
opts.allCaps = true
return this.configure(opts) as this
}
get capital (): this {
get capital (): this {
const opts: WordOptions = Object.assign({}, this.opt)
const opts: WordOptions = Object.assign({}, this.opt)
opts.capital = true
opts.capital = true
@@ -167,15 +175,14 @@ export abstract class Word {
}
}
export class RandomWord extends Word {
export class RandomWord extends Word {
private last: number
constructor (public choices: Array<Word>, opt: WordOptions = emptyConfig) {
private history: { last: number }
constructor (public choices: Array<Word>, opt: WordOptions = emptyConfig, history: { last: number } = { last: -1 }) {
super(opt)
super(opt)
this.last = -1
this.history = history
}
}
configure (opts: WordOptions): Word {
configure (opts: WordOptions): Word {
return new RandomWord(this.choices, opts)
return new RandomWord(this.choices, opts, this.history )
}
}
toString (): string {
toString (): string {
@@ -183,9 +190,9 @@ export class RandomWord extends Word {
do {
do {
choice = Math.floor(Math.random() * this.choices.length)
choice = Math.floor(Math.random() * this.choices.length)
} while (choice === this.last)
} while (choice === this.history. last)
this.last = choice
this.history. last = choice
return this.choices[choice].configure(this.opt).toString()
return this.choices[choice].configure(this.opt).toString()
}
}
}
}
@@ -234,7 +241,9 @@ export class Noun extends Word {
}
}
}
}
if (this.options.capital) {
if (this.options.allCaps) {
result = result.toUpperCase()
} else if (this.options.capital) {
result = result.slice(0, 1).toUpperCase() + result.slice(1)
result = result.slice(0, 1).toUpperCase() + result.slice(1)
}
}
@@ -243,14 +252,14 @@ export class Noun extends Word {
}
}
export class ImproperNoun extends Noun {
export class ImproperNoun extends Noun {
constructor (singularNoun: string, pluralNoun: string) {
super(singularNoun, pluralNoun, { plural: false, capital: false, proper: false, kind: NounKind.Specific, vowel: VowelSound.Default, count: true })
constructor (singularNoun: string, pluralNoun: string = singularNoun ) {
super(singularNoun, pluralNoun, { plural: false, allCaps: false, capital: false, proper: false, kind: NounKind.Specific, vowel: VowelSound.Default, count: true })
}
}
}
}
export class ProperNoun extends Noun {
export class ProperNoun extends Noun {
constructor (singularNoun: string) {
constructor (singularNoun: string) {
super(singularNoun, null, { plural: false, capital: false, proper: true, kind: NounKind.Specific, vowel: VowelSound.Default, count: true })
super(singularNoun, null, { plural: false, allCaps: false, capital: false, proper: true, kind: NounKind.Specific, vowel: VowelSound.Default, count: true })
}
}
}
}