1
1
import { describe , expect , test } from "bun:test" ;
2
2
import { Perf } from "../../src/core/utils" ;
3
- import * as reqres from "../../src/core/utils/reqres" ;
4
- import * as strings from "../../src/core/utils/strings" ;
3
+ import * as utils from "../../src/core/utils" ;
5
4
6
5
async function wait ( ms : number ) {
7
6
return new Promise ( ( resolve ) => {
@@ -13,7 +12,7 @@ describe("Core Utils", async () => {
13
12
describe ( "[core] strings" , async ( ) => {
14
13
test ( "objectToKeyValueArray" , async ( ) => {
15
14
const obj = { a : 1 , b : 2 , c : 3 } ;
16
- const result = strings . objectToKeyValueArray ( obj ) ;
15
+ const result = utils . objectToKeyValueArray ( obj ) ;
17
16
expect ( result ) . toEqual ( [
18
17
{ key : "a" , value : 1 } ,
19
18
{ key : "b" , value : 2 } ,
@@ -22,24 +21,24 @@ describe("Core Utils", async () => {
22
21
} ) ;
23
22
24
23
test ( "snakeToPascalWithSpaces" , async ( ) => {
25
- const result = strings . snakeToPascalWithSpaces ( "snake_to_pascal" ) ;
24
+ const result = utils . snakeToPascalWithSpaces ( "snake_to_pascal" ) ;
26
25
expect ( result ) . toBe ( "Snake To Pascal" ) ;
27
26
} ) ;
28
27
29
28
test ( "randomString" , async ( ) => {
30
- const result = strings . randomString ( 10 ) ;
29
+ const result = utils . randomString ( 10 ) ;
31
30
expect ( result ) . toHaveLength ( 10 ) ;
32
31
} ) ;
33
32
34
33
test ( "pascalToKebab" , async ( ) => {
35
- const result = strings . pascalToKebab ( "PascalCase" ) ;
34
+ const result = utils . pascalToKebab ( "PascalCase" ) ;
36
35
expect ( result ) . toBe ( "pascal-case" ) ;
37
36
} ) ;
38
37
39
38
test ( "replaceSimplePlaceholders" , async ( ) => {
40
39
const str = "Hello, {$name}!" ;
41
40
const vars = { name : "John" } ;
42
- const result = strings . replaceSimplePlaceholders ( str , vars ) ;
41
+ const result = utils . replaceSimplePlaceholders ( str , vars ) ;
43
42
expect ( result ) . toBe ( "Hello, John!" ) ;
44
43
} ) ;
45
44
} ) ;
@@ -49,7 +48,7 @@ describe("Core Utils", async () => {
49
48
const headers = new Headers ( ) ;
50
49
headers . append ( "Content-Type" , "application/json" ) ;
51
50
headers . append ( "Authorization" , "Bearer 123" ) ;
52
- const obj = reqres . headersToObject ( headers ) ;
51
+ const obj = utils . headersToObject ( headers ) ;
53
52
expect ( obj ) . toEqual ( {
54
53
"content-type" : "application/json" ,
55
54
authorization : "Bearer 123"
@@ -59,21 +58,21 @@ describe("Core Utils", async () => {
59
58
test ( "replaceUrlParam" , ( ) => {
60
59
const url = "/api/:id/:name" ;
61
60
const params = { id : "123" , name : "test" } ;
62
- const result = reqres . replaceUrlParam ( url , params ) ;
61
+ const result = utils . replaceUrlParam ( url , params ) ;
63
62
expect ( result ) . toBe ( "/api/123/test" ) ;
64
63
} ) ;
65
64
66
65
test ( "encode" , ( ) => {
67
66
const obj = { id : "123" , name : "test" } ;
68
- const result = reqres . encodeSearch ( obj ) ;
67
+ const result = utils . encodeSearch ( obj ) ;
69
68
expect ( result ) . toBe ( "id=123&name=test" ) ;
70
69
71
70
const obj2 = { id : "123" , name : [ "test1" , "test2" ] } ;
72
- const result2 = reqres . encodeSearch ( obj2 ) ;
71
+ const result2 = utils . encodeSearch ( obj2 ) ;
73
72
expect ( result2 ) . toBe ( "id=123&name=test1&name=test2" ) ;
74
73
75
74
const obj3 = { id : "123" , name : { test : "test" } } ;
76
- const result3 = reqres . encodeSearch ( obj3 , { encode : true } ) ;
75
+ const result3 = utils . encodeSearch ( obj3 , { encode : true } ) ;
77
76
expect ( result3 ) . toBe ( "id=123&name=%7B%22test%22%3A%22test%22%7D" ) ;
78
77
} ) ;
79
78
} ) ;
@@ -108,4 +107,91 @@ describe("Core Utils", async () => {
108
107
expect ( count ) . toBe ( 2 ) ;
109
108
} ) ;
110
109
} ) ;
110
+
111
+ describe ( "objects" , ( ) => {
112
+ test ( "omitKeys" , ( ) => {
113
+ const objects = [
114
+ [ { a : 1 , b : 2 , c : 3 } , [ "a" ] , { b : 2 , c : 3 } ] ,
115
+ [ { a : 1 , b : 2 , c : 3 } , [ "b" ] , { a : 1 , c : 3 } ] ,
116
+ [ { a : 1 , b : 2 , c : 3 } , [ "c" ] , { a : 1 , b : 2 } ] ,
117
+ [ { a : 1 , b : 2 , c : 3 } , [ "a" , "b" ] , { c : 3 } ] ,
118
+ [ { a : 1 , b : 2 , c : 3 } , [ "a" , "b" , "c" ] , { } ]
119
+ ] as [ object , string [ ] , object ] [ ] ;
120
+
121
+ for ( const [ obj , keys , expected ] of objects ) {
122
+ const result = utils . omitKeys ( obj , keys as any ) ;
123
+ expect ( result ) . toEqual ( expected ) ;
124
+ }
125
+ } ) ;
126
+
127
+ test ( "isEqual" , ( ) => {
128
+ const objects = [
129
+ [ 1 , 1 , true ] ,
130
+ [ 1 , "1" , false ] ,
131
+ [ 1 , 2 , false ] ,
132
+ [ "1" , "1" , true ] ,
133
+ [ "1" , "2" , false ] ,
134
+ [ true , true , true ] ,
135
+ [ true , false , false ] ,
136
+ [ false , false , true ] ,
137
+ [ 1 , NaN , false ] ,
138
+ [ NaN , NaN , true ] ,
139
+ [ null , null , true ] ,
140
+ [ null , undefined , false ] ,
141
+ [ undefined , undefined , true ] ,
142
+ [ new Map ( [ [ "a" , 1 ] ] ) , new Map ( [ [ "a" , 1 ] ] ) , true ] ,
143
+ [ new Map ( [ [ "a" , 1 ] ] ) , new Map ( [ [ "a" , 2 ] ] ) , false ] ,
144
+ [ new Map ( [ [ "a" , 1 ] ] ) , new Map ( [ [ "b" , 1 ] ] ) , false ] ,
145
+ [
146
+ new Map ( [ [ "a" , 1 ] ] ) ,
147
+ new Map ( [
148
+ [ "a" , 1 ] ,
149
+ [ "b" , 2 ]
150
+ ] ) ,
151
+ false
152
+ ] ,
153
+ [ { a : 1 } , { a : 1 } , true ] ,
154
+ [ { a : 1 } , { a : 2 } , false ] ,
155
+ [ { a : 1 } , { b : 1 } , false ] ,
156
+ [ { a : "1" } , { a : "1" } , true ] ,
157
+ [ { a : "1" } , { a : "2" } , false ] ,
158
+ [ { a : "1" } , { b : "1" } , false ] ,
159
+ [ { a : 1 } , { a : 1 , b : 2 } , false ] ,
160
+ [ { a : [ 1 , 2 , 3 ] } , { a : [ 1 , 2 , 3 ] } , true ] ,
161
+ [ { a : [ 1 , 2 , 3 ] } , { a : [ 1 , 2 , 4 ] } , false ] ,
162
+ [ { a : [ 1 , 2 , 3 ] } , { a : [ 1 , 2 , 3 , 4 ] } , false ] ,
163
+ [ { a : { b : 1 } } , { a : { b : 1 } } , true ] ,
164
+ [ { a : { b : 1 } } , { a : { b : 2 } } , false ] ,
165
+ [ { a : { b : 1 } } , { a : { c : 1 } } , false ] ,
166
+ [ { a : { b : 1 } } , { a : { b : 1 , c : 2 } } , false ] ,
167
+ [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , true ] ,
168
+ [ [ 1 , 2 , 3 ] , [ 1 , 2 , 4 ] , false ] ,
169
+ [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 , 4 ] , false ] ,
170
+ [ [ { a : 1 } ] , [ { a : 1 } ] , true ] ,
171
+ [ [ { a : 1 } ] , [ { a : 2 } ] , false ] ,
172
+ [ [ { a : 1 } ] , [ { b : 1 } ] , false ]
173
+ ] as [ any , any , boolean ] [ ] ;
174
+
175
+ for ( const [ a , b , expected ] of objects ) {
176
+ const result = utils . isEqual ( a , b ) ;
177
+ expect ( result ) . toEqual ( expected ) ;
178
+ }
179
+ } ) ;
180
+
181
+ test ( "getPath" , ( ) => {
182
+ const tests = [
183
+ [ { a : 1 , b : 2 , c : 3 } , "a" , 1 ] ,
184
+ [ { a : 1 , b : 2 , c : 3 } , "b" , 2 ] ,
185
+ [ { a : { b : 1 } } , "a.b" , 1 ] ,
186
+ [ { a : { b : 1 } } , "a.b.c" , null , null ] ,
187
+ [ { a : { b : 1 } } , "a.b.c" , 1 , 1 ] ,
188
+ [ [ [ 1 ] ] , "0.0" , 1 ]
189
+ ] as [ object , string , any , any ] [ ] ;
190
+
191
+ for ( const [ obj , path , expected , defaultValue ] of tests ) {
192
+ const result = utils . getPath ( obj , path , defaultValue ) ;
193
+ expect ( result ) . toEqual ( expected ) ;
194
+ }
195
+ } ) ;
196
+ } ) ;
111
197
} ) ;
0 commit comments