Last active
December 22, 2015 14:18
-
-
Save comilab/6484652 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
var getRandomName = function() { | |
// ゥは使わない気がするので一旦外す | |
var chars = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンヴガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポァィェォッャュョー'.split(''), | |
prefixes1 = 'ウクグツフブプヴ'.split(''), | |
prefixes2 = 'ツフブプヴ'.split(''), | |
prefixes3 = 'キシチニヒミリギジヂビピ'.split(''), | |
length = Math.max(Math.floor(Math.random() * 9), 2), | |
name = ''; | |
while (name.length < length) { | |
var rand = Math.floor(Math.random() * chars.length), | |
c = chars[rand]; | |
if (('ンァィゥェォッャュョー'.match(c) && !name.length) | |
|| ('ァィゥェォャュョ'.match(c) && (name.length + 2) == length) | |
|| ('ッ'.match(c) && (name.length + 1) == length) | |
) { | |
continue; | |
} | |
if ('ィェォ'.match(c)) { | |
var rand = Math.floor(Math.random() * prefixes1.length); | |
name += prefixes1[rand] + c; | |
} else if ('ァ'.match(c)) { | |
var rand = Math.floor(Math.random() * prefixes2.length); | |
name += prefixes2[rand] + c; | |
} else if ('ャュョ'.match(c)) { | |
var rand = Math.floor(Math.random() * prefixes3.length); | |
name += prefixes3[rand] + c; | |
} else { | |
name += prev = c; | |
} | |
} | |
return name; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment