Allow custom format for random values #3
							
								
								
									
										21
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								index.js
									
									
									
									
									
								
							| @ -23,7 +23,7 @@ | |||||||
|     return shuffle(numbers); |     return shuffle(numbers); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function random() { |   function populate() { | ||||||
|     if (!adjectives.length) { |     if (!adjectives.length) { | ||||||
|       adjectives = shuffle(lists.adjectives.slice(0)); |       adjectives = shuffle(lists.adjectives.slice(0)); | ||||||
|     } |     } | ||||||
| @ -33,13 +33,32 @@ | |||||||
|     if (!numbers.length) { |     if (!numbers.length) { | ||||||
|       numbers = shuffle(genNumbers()); |       numbers = shuffle(genNumbers()); | ||||||
|     } |     } | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|  |   function random() { | ||||||
|  |     populate(); | ||||||
|     return adjectives.pop() |     return adjectives.pop() | ||||||
|       + '-' + animals.pop() |       + '-' + animals.pop() | ||||||
|       + '-' + numbers.pop() |       + '-' + numbers.pop() | ||||||
|       ; |       ; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   // Accepts a format as a stirng of 'a' for adjective, 'n' for noun, and '#' for number
 | ||||||
|  |   function get(format) { | ||||||
|  |     populate(); | ||||||
|  |     hri = new Array(); | ||||||
|  |     for (var i = 0; i < format.length; i++) { | ||||||
|  |       switch (format[i]) { | ||||||
|  |         case 'a': hri.push(adjectives.pop()); break; | ||||||
|  |         case 'n': hri.push(animals.pop()); break; | ||||||
|  |         case '#': hri.push(adjectives.pop()); break; | ||||||
|  |         default: | ||||||
|  |           throw 'Unexpected value ' + format[i] + '. Expected a,n,#'; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |     return hri.join('-'); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   exports.humanReadableIds = { random: random }; |   exports.humanReadableIds = { random: random }; | ||||||
|   exports.hri = exports.humanReadableIds; |   exports.hri = exports.humanReadableIds; | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user