Created
April 21, 2016 10:30
-
-
Save geshan/c6839f26c44d4cf91df524e60f34fc9b to your computer and use it in GitHub Desktop.
generator
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 codes = generateUniqueCodes(10000000); | |
| var query = printQuery(codes); | |
| console.log(query); | |
| function unique(arr) { | |
| var sorted = arr; | |
| sorted.sort(); | |
| return sorted.filter(function(value, index, arr){ | |
| if(index < 1) | |
| return true; | |
| else | |
| return value != arr[index-1]; | |
| }); | |
| } | |
| function printQuery(codes) { | |
| var query = 'INSERT INTO referral_code(code) VALUES ' | |
| for(var i = 0; i < codes.length; i++) { | |
| query += '(\'RF' + codes[i] + '\'),'; | |
| } | |
| return query.substring(0,query.length - 1) + ';'; | |
| } | |
| function generateUniqueCodes(n) { | |
| var codes = []; | |
| for (var i = 1; i <= n; i++) { | |
| codes.push(generate(5)); | |
| } | |
| //codes = codes.filter(onlyUnique); | |
| return unique(codes); | |
| } | |
| function generate(limit) { | |
| var dictionary = 'qwertyuiopasdfghjklzxcvbnm1234567890'; | |
| var maxOffset = dictionary.length; | |
| var code = ''; | |
| for (var length = limit; length > 0; length--) { | |
| code += dictionary[Math.floor(Math.random() * maxOffset)]; | |
| } | |
| return code; | |
| } | |
| function onlyUnique(value, index, self) { | |
| return self.indexOf(value) === index; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment