@@ -10,6 +10,7 @@ import {
10
10
makePathArray ,
11
11
mergeOpts ,
12
12
setBy ,
13
+ uuid ,
13
14
} from '../src/index'
14
15
15
16
describe ( 'getBy' , ( ) => {
@@ -769,3 +770,34 @@ describe('mergeOpts', () => {
769
770
expect ( mergeOpts ( original , { } ) ) . toEqual ( { foo : 'test' } )
770
771
} )
771
772
} )
773
+
774
+ describe ( 'uuid' , ( ) => {
775
+ it ( 'should return a string' , ( ) => {
776
+ const id = uuid ( )
777
+ expect ( typeof id ) . toBe ( 'string' )
778
+ } )
779
+
780
+ it ( 'should match UUID v4 format' , ( ) => {
781
+ const id = uuid ( )
782
+ const uuidV4Regex =
783
+ / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - 4 [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ /
784
+ expect ( id ) . toMatch ( uuidV4Regex )
785
+ } )
786
+
787
+ it ( 'should generate different values on multiple calls' , ( ) => {
788
+ const ids = new Set ( Array . from ( { length : 100 } , ( ) => uuid ( ) ) )
789
+ expect ( ids . size ) . toBe ( 100 )
790
+ } )
791
+
792
+ it ( 'should always produce 36 characters' , ( ) => {
793
+ const id = uuid ( )
794
+ expect ( id . length ) . toBe ( 36 )
795
+ } )
796
+
797
+ it ( 'should set correct version (4) and variant bits' , ( ) => {
798
+ const id = uuid ( )
799
+ const parts = id . split ( '-' )
800
+ expect ( parts [ 2 ] ?. [ 0 ] ) . toBe ( '4' )
801
+ expect ( [ '8' , '9' , 'a' , 'b' ] ) . toContain ( parts [ 3 ] ?. [ 0 ] )
802
+ } )
803
+ } )
0 commit comments