@@ -3,8 +3,10 @@ import { NullType } from './type/null.type'
3
3
import { UndefinedType } from './type/undefined.type'
4
4
import {
5
5
detectCollectionTypeFromValue ,
6
+ detectType ,
6
7
isCollection ,
7
8
isHomogeneous ,
9
+ isNode ,
8
10
isSet ,
9
11
typeName ,
10
12
typeOf ,
@@ -88,6 +90,49 @@ describe('Util', () => {
88
90
} )
89
91
} )
90
92
93
+ describe ( 'detect type' , ( ) => {
94
+ it ( 'detects string' , ( ) => {
95
+ expect ( detectType ( 'aString' ) ) . toBe ( 'S' )
96
+ expect ( detectType ( String ( 'aString' ) ) ) . toBe ( 'S' )
97
+ // tslint:disable-next-line:no-construct
98
+ expect ( detectType ( new String ( 'aString' ) ) ) . toBe ( 'S' )
99
+ } )
100
+ it ( 'detects number' , ( ) => {
101
+ expect ( detectType ( 3 ) ) . toBe ( 'N' )
102
+ expect ( detectType ( Number ( - 5 ) ) ) . toBe ( 'N' )
103
+ // tslint:disable-next-line:no-construct
104
+ expect ( detectType ( new Number ( 83 ) ) ) . toBe ( 'N' )
105
+ } )
106
+ it ( 'detects binary' , ( ) => {
107
+ let buffer : any
108
+ if ( isNode ( ) ) {
109
+ buffer = Buffer . alloc ( 5 )
110
+ } else {
111
+ buffer = new ArrayBuffer ( 8 )
112
+ }
113
+ expect ( detectType ( buffer ) ) . toBe ( 'B' )
114
+ } )
115
+ it ( 'detects null' , ( ) => {
116
+ expect ( detectType ( null ) ) . toBe ( 'NULL' )
117
+ } )
118
+ it ( 'detects bool' , ( ) => {
119
+ expect ( detectType ( true ) ) . toBe ( 'BOOL' )
120
+ expect ( detectType ( false ) ) . toBe ( 'BOOL' )
121
+ } )
122
+ it ( 'detects collection' , ( ) => {
123
+ expect ( detectType ( new Set ( [ 'a' ] ) ) ) . toBe ( 'SS' )
124
+ expect ( detectType ( new Set ( [ 2 ] ) ) ) . toBe ( 'NS' )
125
+ expect ( detectType ( [ 0 , 1 , 1 , 2 , 3 , 5 ] ) ) . toBe ( 'L' )
126
+ } )
127
+ it ( 'detects object' , ( ) => {
128
+ expect ( detectType ( { } ) ) . toBe ( 'M' )
129
+ expect ( detectType ( { foo : 'bar' } ) ) . toBe ( 'M' )
130
+ } )
131
+ it ( 'throws if not such a type' , ( ) => {
132
+ expect ( ( ) => detectType ( undefined ) ) . toThrow ( )
133
+ } )
134
+ } )
135
+
91
136
describe ( 'type name' , ( ) => {
92
137
it ( 'String' , ( ) => {
93
138
expect ( typeName ( String ) ) . toBe ( 'String' )
0 commit comments