Created
March 15, 2016 03:32
-
-
Save Leko/e9131a436c88ebd4dcd4 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
'use strict' | |
const shuffle = (list) => { | |
let i, j, temp | |
if (typeof list === 'string') { | |
list = list.split('') | |
} else if(Array.isArray(list)) { | |
list = list.slice() | |
} | |
i = list.length | |
if (i === 0) { | |
return list | |
} | |
while (--i) { | |
j = Math.floor(Math.random() * (i + 1)) | |
temp = list[i] | |
list[i] = list[j] | |
list[j] = temp | |
} | |
return list | |
} | |
const serialize = (len = 16, prefix = '') => { | |
// I, 1, 0, Oなど勘違いしやすい文字を除外 | |
const seed = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789' | |
return prefix + shuffle(seed).slice(0, len - prefix.length).join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment