@@ -104,45 +104,65 @@ var Generator = (function() {
104104 function Generator ( options ) {
105105 options = options || { } ;
106106 this . options = { } ;
107- this . options . digits = int ( options . digits , 6 ) ;
108- this . options . letters = int ( options . letters , 3 ) ;
107+ this . keys = { } ;
109108 this . options . port = int ( options . port , 9876 ) ;
110- this . options . store = typeof ( options . store ) === "function"
109+ this . options . autoAddKeys = options . autoAddKeys ? true : false ;
110+ this . server ;
111+ this . _started ;
112+ this . _stopped ;
113+ this . add ( '__default' , options ) ;
114+ }
115+
116+ Generator . prototype . add = function ( key , options ) {
117+ options = options || { } ;
118+ if ( this . keys [ key ] ) { return false ; }
119+ this . keys [ key ] = { } ;
120+ this . keys [ key ] . options = { } ;
121+ this . keys [ key ] . options . digits = int ( options . digits , 6 ) ;
122+ this . keys [ key ] . options . letters = int ( options . letters , 3 ) ;
123+ this . keys [ key ] . options . store = typeof ( options . store ) === "function"
111124 ? options . store : function ( ) { }
112- this . options . store_freq = int ( options . store_freq , 1 ) ;
113- this . options . restore = options . restore || null ;
114- this . numbers = - 1 ;
115- this . letters = "A" ;
125+ this . keys [ key ] . options . store_freq = int ( options . store_freq , 1 ) ;
126+ this . keys [ key ] . options . restore = options . restore || null ;
127+ this . keys [ key ] . numbers = - 1 ;
128+ this . keys [ key ] . letters = "A" ;
116129 // workaround to get A's generated as first ids when
117130 // options.digits is 0
118131 if ( options . digits === 0 ) {
119- this . letters = "@" ;
132+ this . keys [ key ] . letters = "@" ;
120133 }
121134 if ( options . restore ) {
122135 var result = parseId ( options . restore ) ;
123136 if ( result ) {
124- this . numbers = parseInt ( result . numbers ) ;
125- this . options . digits = result . numbers . length ;
126- this . letters = result . letters ;
127- this . options . letters = result . letters . length ;
137+ this . keys [ key ] . numbers = parseInt ( result . numbers ) ;
138+ this . keys [ key ] . options . digits = result . numbers . length ;
139+ this . keys [ key ] . letters = result . letters ;
140+ this . keys [ key ] . options . letters = result . letters . length ;
128141 }
129142 }
130- this . generatedIds = [ ] ;
131- this . unsavedIds = [ ] ;
132- this . server ;
133- this . _online ;
134- }
143+ this . keys [ key ] . generatedIds = [ ] ;
144+ this . keys [ key ] . unsavedIds = [ ] ;
135145
136- Generator . prototype . generate = function ( ) {
137- var _new = generateId ( this . letters , this . options . letters ,
138- this . numbers , this . options . digits ) ;
139- this . letters = _new . letters ;
140- this . numbers = _new . numbers ;
141- this . generatedIds . push ( _new . id ) ;
142- this . unsavedIds . push ( _new . id ) ;
143- if ( this . options . store_freq === this . unsavedIds . length ) {
144- this . options . store ( this . unsavedIds ) ;
145- this . unsavedIds = [ ] ;
146+ return true ;
147+ } ;
148+
149+ Generator . prototype . generate = function ( key ) {
150+ if ( ! key ) { key = '__default' ; }
151+ if ( ! this . keys [ key ] ) {
152+ if ( ! this . options . autoAddKeys ) {
153+ return null ;
154+ }
155+ this . add ( key ) ;
156+ }
157+ var _new = generateId ( this . keys [ key ] . letters , this . keys [ key ] . options . letters ,
158+ this . keys [ key ] . numbers , this . keys [ key ] . options . digits ) ;
159+ this . keys [ key ] . letters = _new . letters ;
160+ this . keys [ key ] . numbers = _new . numbers ;
161+ this . keys [ key ] . generatedIds . push ( _new . id ) ;
162+ this . keys [ key ] . unsavedIds . push ( _new . id ) ;
163+ if ( this . keys [ key ] . options . store_freq === this . keys [ key ] . unsavedIds . length ) {
164+ this . keys [ key ] . options . store ( this . keys [ key ] . unsavedIds ) ;
165+ this . keys [ key ] . unsavedIds = [ ] ;
146166 }
147167 return _new . id ;
148168 } ;
@@ -151,26 +171,32 @@ var Generator = (function() {
151171 done = done || function ( ) { } ;
152172 if ( this . _online ) return ;
153173 this . server = http . Server ( function ( req , res ) {
154- switch ( req . url ) {
155- case "/next" :
156- res . end ( this . generate ( ) ) ;
157- break ;
158- case "/ping" :
159- res . end ( "pong" ) ;
160- break ;
174+ if ( req . url . match ( / ( ^ \/ \w + ) (?: \/ ( \w + ) ) ? / ) ) {
175+ var action = RegExp . $1 , key = RegExp . $2 || '__default' ;
176+ switch ( action ) {
177+ case "/next" :
178+ res . end ( this . generate ( key ) ) ;
179+ break ;
180+ case "/ping" :
181+ res . end ( "pong" ) ;
182+ break ;
183+ }
161184 }
162185 } . bind ( this ) ) ;
163186 this . server . listen ( this . options . port , done ) ;
164187 this . _online = true ;
165188 } ;
166189
167- Generator . prototype . store = function ( ) {
168- if ( this . unsavedIds . length > 0 ) this . options . store ( this . unsavedIds ) ;
190+ Generator . prototype . store = function ( key ) {
191+ if ( ! key ) { key = '__default' ; }
192+ if ( this . keys [ key ] . unsavedIds . length > 0 ) this . keys [ key ] . options . store ( this . keys [ key ] . unsavedIds ) ;
169193 } ;
170194
171195 Generator . prototype . stop = function ( ) {
172196 if ( ! this . _online ) return ;
173- this . store ( ) ;
197+ for ( var key in this . keys ) {
198+ this . store ( key ) ;
199+ }
174200 this . server . close ( ) ;
175201 this . _online = false ;
176202 } ;
0 commit comments