1- var assert = require ( 'assert' )
2- var lolex = require ( 'lolex' )
3- var ULID = require ( './lib/index.umd.js' )
4- var ulid = ULID . factory ( )
5-
1+ var child_process = require ( "child_process" )
2+ var path = require ( "path" )
3+ var assert = require ( "assert" )
4+ var lolex = require ( "lolex" )
65
7- describe ( 'ulid' , function ( ) {
8-
9- describe ( 'prng' , function ( ) {
6+ var ULID = require ( "./lib/index.umd.js" )
7+ var ulid = ULID . factory ( )
108
9+ describe ( "ulid" , function ( ) {
10+ describe ( "prng" , function ( ) {
1111 var prng = ULID . detectPrng ( )
1212
13- it ( ' should produce a number' , function ( ) {
13+ it ( " should produce a number" , function ( ) {
1414 assert . strictEqual ( false , isNaN ( prng ( ) ) )
1515 } )
1616
17- it ( ' should be between 0 and 1' , function ( ) {
17+ it ( " should be between 0 and 1" , function ( ) {
1818 var num = prng ( )
1919 assert ( num >= 0 && num <= 1 )
2020 } )
21-
2221 } )
2322
24- describe ( 'incremenet base32' , function ( ) {
25-
26- it ( 'increments correctly' , function ( ) {
27- assert . strictEqual ( 'A109D' , ULID . incrementBase32 ( 'A109C' ) )
23+ describe ( "incremenet base32" , function ( ) {
24+ it ( "increments correctly" , function ( ) {
25+ assert . strictEqual ( "A109D" , ULID . incrementBase32 ( "A109C" ) )
2826 } )
2927
30- it ( ' carries correctly' , function ( ) {
31- assert . strictEqual ( ' A1Z00' , ULID . incrementBase32 ( ' A1YZZ' ) )
28+ it ( " carries correctly" , function ( ) {
29+ assert . strictEqual ( " A1Z00" , ULID . incrementBase32 ( " A1YZZ" ) )
3230 } )
3331
34- it ( ' double increments correctly' , function ( ) {
35- assert . strictEqual ( ' A1Z01' , ULID . incrementBase32 ( ULID . incrementBase32 ( ' A1YZZ' ) ) )
32+ it ( " double increments correctly" , function ( ) {
33+ assert . strictEqual ( " A1Z01" , ULID . incrementBase32 ( ULID . incrementBase32 ( " A1YZZ" ) ) )
3634 } )
3735
38- it ( ' throws when it cannot increment' , function ( ) {
36+ it ( " throws when it cannot increment" , function ( ) {
3937 assert . throws ( function ( ) {
40- ULID . incrementBase32 ( ' ZZZ' )
38+ ULID . incrementBase32 ( " ZZZ" )
4139 } )
4240 } )
43-
4441 } )
4542
46- describe ( 'randomChar' , function ( ) {
47-
43+ describe ( "randomChar" , function ( ) {
4844 var sample = { }
4945 var prng = ULID . detectPrng ( )
5046
@@ -56,186 +52,178 @@ describe('ulid', function() {
5652 sample [ char ] += 1
5753 }
5854
59- it ( ' should never return undefined' , function ( ) {
60- assert . strictEqual ( undefined , sample [ ' undefined' ] )
55+ it ( " should never return undefined" , function ( ) {
56+ assert . strictEqual ( undefined , sample [ " undefined" ] )
6157 } )
6258
63- it ( ' should never return an empty string' , function ( ) {
64- assert . strictEqual ( undefined , sample [ '' ] )
59+ it ( " should never return an empty string" , function ( ) {
60+ assert . strictEqual ( undefined , sample [ "" ] )
6561 } )
66-
6762 } )
6863
69- describe ( 'encodeTime' , function ( ) {
70-
71- it ( 'should return expected encoded result' , function ( ) {
72- assert . strictEqual ( '01ARYZ6S41' , ULID . encodeTime ( 1469918176385 , 10 ) )
64+ describe ( "encodeTime" , function ( ) {
65+ it ( "should return expected encoded result" , function ( ) {
66+ assert . strictEqual ( "01ARYZ6S41" , ULID . encodeTime ( 1469918176385 , 10 ) )
7367 } )
7468
75- it ( ' should change length properly' , function ( ) {
76- assert . strictEqual ( ' 0001AS99AA60' , ULID . encodeTime ( 1470264322240 , 12 ) )
69+ it ( " should change length properly" , function ( ) {
70+ assert . strictEqual ( " 0001AS99AA60" , ULID . encodeTime ( 1470264322240 , 12 ) )
7771 } )
7872
79- it ( ' should truncate time if not enough length' , function ( ) {
80- assert . strictEqual ( ' AS4Y1E11' , ULID . encodeTime ( 1470118279201 , 8 ) )
73+ it ( " should truncate time if not enough length" , function ( ) {
74+ assert . strictEqual ( " AS4Y1E11" , ULID . encodeTime ( 1470118279201 , 8 ) )
8175 } )
8276
83- describe ( 'should throw an error' , function ( ) {
84-
85- it ( 'if time greater than (2 ^ 48) - 1' , function ( ) {
77+ describe ( "should throw an error" , function ( ) {
78+ it ( "if time greater than (2 ^ 48) - 1" , function ( ) {
8679 assert . throws ( function ( ) {
8780 ULID . encodeTime ( Math . pow ( 2 , 48 ) , 8 )
8881 } , Error )
8982 } )
9083
91- it ( ' if time is not a number' , function ( ) {
84+ it ( " if time is not a number" , function ( ) {
9285 assert . throws ( function ( ) {
93- ULID . encodeTime ( ' test' )
86+ ULID . encodeTime ( " test" )
9487 } , Error )
9588 } )
9689
97- it ( ' if time is infinity' , function ( ) {
90+ it ( " if time is infinity" , function ( ) {
9891 assert . throws ( function ( ) {
9992 ULID . encodeTime ( Infinity )
10093 } , Error )
10194 } )
10295
103- it ( ' if time is negative' , function ( ) {
96+ it ( " if time is negative" , function ( ) {
10497 assert . throws ( function ( ) {
10598 ULID . encodeTime ( - 1 )
10699 } , Error )
107100 } )
108101
109- it ( ' if time is a float' , function ( ) {
102+ it ( " if time is a float" , function ( ) {
110103 assert . throws ( function ( ) {
111104 ULID . encodeTime ( 100.1 )
112105 } , Error )
113106 } )
114-
115107 } )
116-
117108 } )
118109
119- describe ( 'encodeRandom' , function ( ) {
120-
110+ describe ( "encodeRandom" , function ( ) {
121111 var prng = ULID . detectPrng ( )
122112
123- it ( ' should return correct length' , function ( ) {
113+ it ( " should return correct length" , function ( ) {
124114 assert . strictEqual ( 12 , ULID . encodeRandom ( 12 , prng ) . length )
125115 } )
126-
127116 } )
128117
129- describe ( 'decodeTime' , function ( ) {
130-
131- it ( 'should return correct timestamp' , function ( ) {
118+ describe ( "decodeTime" , function ( ) {
119+ it ( "should return correct timestamp" , function ( ) {
132120 var timestamp = Date . now ( )
133121 var id = ulid ( timestamp )
134122 assert . strictEqual ( timestamp , ULID . decodeTime ( id ) )
135123 } )
136124
137- it ( ' should accept the maximum allowed timestamp' , function ( ) {
138- assert . strictEqual ( 281474976710655 , ULID . decodeTime ( ' 7ZZZZZZZZZZZZZZZZZZZZZZZZZ' ) )
125+ it ( " should accept the maximum allowed timestamp" , function ( ) {
126+ assert . strictEqual ( 281474976710655 , ULID . decodeTime ( " 7ZZZZZZZZZZZZZZZZZZZZZZZZZ" ) )
139127 } )
140128
141- describe ( 'should reject' , function ( ) {
142-
143- it ( 'malformed strings of incorrect length' , function ( ) {
129+ describe ( "should reject" , function ( ) {
130+ it ( "malformed strings of incorrect length" , function ( ) {
144131 assert . throws ( function ( ) {
145- ULID . decodeTime ( ' FFFF' )
132+ ULID . decodeTime ( " FFFF" )
146133 } , Error )
147134 } )
148135
149- it ( ' strings with timestamps that are too high' , function ( ) {
136+ it ( " strings with timestamps that are too high" , function ( ) {
150137 assert . throws ( function ( ) {
151- ULID . decodeTime ( ' 80000000000000000000000000' )
138+ ULID . decodeTime ( " 80000000000000000000000000" )
152139 } , Error )
153140 } )
154-
155141 } )
156-
157142 } )
158143
159- describe ( 'ulid' , function ( ) {
160-
161- it ( 'should return correct length' , function ( ) {
144+ describe ( "ulid" , function ( ) {
145+ it ( "should return correct length" , function ( ) {
162146 assert . strictEqual ( 26 , ulid ( ) . length )
163147 } )
164148
165- it ( ' should return expected encoded time component result' , function ( ) {
166- assert . strictEqual ( ' 01ARYZ6S41' , ulid ( 1469918176385 ) . substring ( 0 , 10 ) )
149+ it ( " should return expected encoded time component result" , function ( ) {
150+ assert . strictEqual ( " 01ARYZ6S41" , ulid ( 1469918176385 ) . substring ( 0 , 10 ) )
167151 } )
168-
169152 } )
170153
171- describe ( 'monotonicity' , function ( ) {
172-
154+ describe ( "monotonicity" , function ( ) {
173155 function stubbedPrng ( ) {
174156 return 0.96
175157 }
176158
177159 var stubbedUlid = ULID . factory ( stubbedPrng )
178160
179- describe ( 'without seedTime' , function ( ) {
180-
161+ describe ( "without seedTime" , function ( ) {
181162 var stubbedUlid = ULID . monotonicFactory ( stubbedPrng )
182163 var clock
183164
184165 before ( function ( ) {
185- clock = lolex . install ( {
186- now : 1469918176385 ,
187- toFake : [ ' Date' ]
188- } )
166+ clock = lolex . install ( {
167+ now : 1469918176385 ,
168+ toFake : [ " Date" ] ,
169+ } )
189170 } )
190171
191172 after ( function ( ) {
192- clock . uninstall ( )
173+ clock . uninstall ( )
193174 } )
194175
195- it ( ' first call' , function ( ) {
196- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYYY' , stubbedUlid ( ) )
176+ it ( " first call" , function ( ) {
177+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYYY" , stubbedUlid ( ) )
197178 } )
198179
199- it ( ' second call' , function ( ) {
200- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYYZ' , stubbedUlid ( ) )
180+ it ( " second call" , function ( ) {
181+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYYZ" , stubbedUlid ( ) )
201182 } )
202183
203- it ( ' third call' , function ( ) {
204- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYZ0' , stubbedUlid ( ) )
184+ it ( " third call" , function ( ) {
185+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYZ0" , stubbedUlid ( ) )
205186 } )
206187
207- it ( ' fourth call' , function ( ) {
208- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYZ1' , stubbedUlid ( ) )
188+ it ( " fourth call" , function ( ) {
189+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYZ1" , stubbedUlid ( ) )
209190 } )
210-
211191 } )
212192
213- describe ( 'with seedTime' , function ( ) {
214-
193+ describe ( "with seedTime" , function ( ) {
215194 var stubbedUlid = ULID . monotonicFactory ( stubbedPrng )
216195
217- it ( ' first call' , function ( ) {
218- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYYY' , stubbedUlid ( 1469918176385 ) )
196+ it ( " first call" , function ( ) {
197+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYYY" , stubbedUlid ( 1469918176385 ) )
219198 } )
220199
221- it ( ' second call with the same' , function ( ) {
222- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYYZ' , stubbedUlid ( 1469918176385 ) )
200+ it ( " second call with the same" , function ( ) {
201+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYYZ" , stubbedUlid ( 1469918176385 ) )
223202 } )
224203
225- it ( ' third call with less than' , function ( ) {
226- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYZ0' , stubbedUlid ( 100000000 ) )
204+ it ( " third call with less than" , function ( ) {
205+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYZ0" , stubbedUlid ( 100000000 ) )
227206 } )
228207
229- it ( ' fourth call with even more less than' , function ( ) {
230- assert . strictEqual ( ' 01ARYZ6S41YYYYYYYYYYYYYYZ1' , stubbedUlid ( 10000 ) )
208+ it ( " fourth call with even more less than" , function ( ) {
209+ assert . strictEqual ( " 01ARYZ6S41YYYYYYYYYYYYYYZ1" , stubbedUlid ( 10000 ) )
231210 } )
232211
233- it ( ' fifth call with 1 greater than' , function ( ) {
234- assert . strictEqual ( ' 01ARYZ6S42YYYYYYYYYYYYYYYY' , stubbedUlid ( 1469918176386 ) )
212+ it ( " fifth call with 1 greater than" , function ( ) {
213+ assert . strictEqual ( " 01ARYZ6S42YYYYYYYYYYYYYYYY" , stubbedUlid ( 1469918176386 ) )
235214 } )
236-
237215 } )
238-
239216 } )
240217
218+ describe ( "command line" , function ( ) {
219+ it ( "should return a valid ULID" , function ( done ) {
220+ child_process . exec ( path . join ( __dirname , "bin/cli.js" ) , ( error , stdout , stderr ) => {
221+ if ( stdout . length === 26 && ! error && ! stderr ) {
222+ done ( )
223+ } else {
224+ done ( error || stderr )
225+ }
226+ } )
227+ } )
228+ } )
241229} )
0 commit comments