@@ -9,28 +9,30 @@ test('qsParse', () => {
99 expect ( qsParse ( 'a=1&a=2&b=1' ) ) . toEqual ( { a : [ '1' , '2' ] , b : '1' } ) ;
1010 expect ( qsParse ( 'a=1&a=2&a=3&a=4' ) ) . toEqual ( { a : [ '1' , '2' , '3' , '4' ] } ) ;
1111} ) ;
12+ // 因有时区问题,仅在中国境内执行
13+ if ( Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone === 'Asia/Shanghai' ) {
14+ test ( 'qsStringify 默认 replacer' , ( ) => {
15+ const i = new Date ( 2020 , 0 , 1 , 0 , 0 , 0 , 0 ) ;
16+ const query = { a : 1 , b : [ 2 , 3 ] , c : '4' , d : undefined , e : null , f : true , g : false , i } ;
17+ const string = 'a=1&b=2&b=3&c=4&f=true&g=false&i=2019-12-31T16%3A00%3A00.000Z' ;
18+ expect ( qsStringify ( query ) ) . toBe ( string ) ;
19+ } ) ;
1220
13- test ( 'qsStringify 默认 replacer' , ( ) => {
14- const i = new Date ( 2020 , 0 , 1 , 0 , 0 , 0 , 0 ) ;
15- const query = { a : 1 , b : [ 2 , 3 ] , c : '4' , d : undefined , e : null , f : true , g : false , i } ;
16- const string = 'a=1&b=2&b=3&c=4&f=true&g=false&i=2019-12-31T16%3A00%3A00.000Z' ;
17- expect ( qsStringify ( query ) ) . toBe ( string ) ;
18- } ) ;
19-
20- test ( 'qsStringify 复写 replacer' , ( ) => {
21- const replacer : Replacer = value => {
22- if ( isString ( value ) ) return `string-${ value } ` ;
23- if ( isNumber ( value ) ) return `number-${ value } ` ;
24- if ( isBoolean ( value ) ) return `boolean-${ value ? 'true' : 'false' } ` ;
25- if ( isUndefined ( value ) ) return 'undefined' ;
26- if ( isNull ( value ) ) return 'null' ;
27- if ( isDate ( value ) ) return 'date-' + formatDate ( new Date ( value ) , 'YYYY-MM-DD HH:mm:ss' ) ;
28- return null ;
29- } ;
30- const i = new Date ( 2020 , 0 , 1 , 0 , 0 , 0 , 0 ) ;
31- const query = { a : 1 , b : [ 2 , 3 ] , c : '4' , d : undefined , e : null , f : true , g : false , i } ;
32- const string =
33- 'a=number-1&b=number-2&b=number-3&c=string-4&d=undefined&e=null&' +
34- 'f=boolean-true&g=boolean-false&i=date-2020-01-01+00%3A00%3A00' ;
35- expect ( qsStringify ( query , replacer ) ) . toBe ( string ) ;
36- } ) ;
21+ test ( 'qsStringify 复写 replacer' , ( ) => {
22+ const replacer : Replacer = value => {
23+ if ( isString ( value ) ) return `string-${ value } ` ;
24+ if ( isNumber ( value ) ) return `number-${ value } ` ;
25+ if ( isBoolean ( value ) ) return `boolean-${ value ? 'true' : 'false' } ` ;
26+ if ( isUndefined ( value ) ) return 'undefined' ;
27+ if ( isNull ( value ) ) return 'null' ;
28+ if ( isDate ( value ) ) return 'date-' + formatDate ( new Date ( value ) , 'YYYY-MM-DD HH:mm:ss' ) ;
29+ return null ;
30+ } ;
31+ const i = new Date ( 2020 , 0 , 1 , 0 , 0 , 0 , 0 ) ;
32+ const query = { a : 1 , b : [ 2 , 3 ] , c : '4' , d : undefined , e : null , f : true , g : false , i } ;
33+ const string =
34+ 'a=number-1&b=number-2&b=number-3&c=string-4&d=undefined&e=null&' +
35+ 'f=boolean-true&g=boolean-false&i=date-2020-01-01+00%3A00%3A00' ;
36+ expect ( qsStringify ( query , replacer ) ) . toBe ( string ) ;
37+ } ) ;
38+ }
0 commit comments