Last active
March 9, 2016 15:19
-
-
Save zzandy/8343e63c59affe65e473 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface INameSource{ | |
getName():string; | |
} | |
class EnglishisNameSource implements INameSource | |
{ | |
private prefix:string[]=[]; | |
private suffix:string[]=[]; | |
private recent:string[] = []; | |
private recentSuffix:string[] = []; | |
private recentNo = 10; | |
constructor(){ | |
const both = (s:string) => { | |
let a = s.split(' '); | |
[].push.apply(this.prefix, a); | |
[].push.apply(this.suffix, a); | |
}; | |
const start = (s:string)=>[].push.apply(this.prefix, s.split(' ')); | |
const end = (s:string)=>[].push.apply(this.suffix, s.split(' ')); | |
start('saint red green wet dry old white black dark silver gold golden shady hot cold yellow crystal clear') | |
both('church cliff heap tor rise range ridge down up nock port gate mound cross hill castle valley marsh park grove copse timber slough mire moss fen forest sand sea lake river creek rock stone wood field cove') | |
end('fell fall town ton landing warf refuge shelter road yard') | |
this.recentNo = Math.min(this.prefix.length, this.suffix.length)/2 | 0; | |
} | |
private isCool(pref:string, suff:string):boolean{ | |
let nomatch = [ | |
'keco', 'ckco', 'shs', /[b-df-hj-np-tv-z]{4,}/i, /([abcd])(?:\1+)/i | |
]; | |
return pref != suff | |
&& this.recent.indexOf(pref)==-1 | |
&& this.recent.indexOf(suff)==-1 | |
&& nomatch.every(nm=>(nm instanceof RegExp | |
?(pref+suff).search(nm) | |
:(pref+suff).indexOf(nm) )==-1); | |
} | |
getName(){ | |
const rnd = <T>(a:T[]) => a[(Math.random()*a.length)|0]; | |
const push = (item:string)=>{ | |
let a = this.recent; | |
if(a.indexOf(item)==-1) | |
a.push(item); | |
if(a.length > this.recentNo) | |
a.splice(a.length-this.recentNo, this.recentNo); | |
}; | |
while(true){ | |
let pref = rnd(this.prefix); | |
let suff = rnd(this.suffix); | |
if(this.isCool(pref, suff)){ | |
this.recent.push(pref); | |
push(pref) | |
push(suff) | |
let str = pref + suff; | |
return str.charAt(0).toUpperCase() + str.substr(1); | |
} | |
} | |
} | |
} | |
function repeat(n:number, callback:()=>any){ | |
while(n-->0) | |
callback(); | |
} | |
var ns = new EnglishisNameSource(); | |
repeat(40, ()=>document.write(ns.getName() + '<br/>')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment