1- import { encode , is_buffer , maybe_map } from './utils' ;
2- import { default_format , formatters } from './formats' ;
1+ import { encode , is_buffer , maybe_map , has } from './utils' ;
2+ import { default_format , default_formatter , formatters } from './formats' ;
33import type { NonNullableProperties , StringifyOptions } from './types' ;
4-
5- const has = Object . prototype . hasOwnProperty ;
4+ import { isArray } from '../utils/values' ;
65
76const array_prefix_generators = {
87 brackets ( prefix : PropertyKey ) {
@@ -17,13 +16,11 @@ const array_prefix_generators = {
1716 } ,
1817} ;
1918
20- const is_array = Array . isArray ;
21- const push = Array . prototype . push ;
2219const push_to_array = function ( arr : any [ ] , value_or_array : any ) {
23- push . apply ( arr , is_array ( value_or_array ) ? value_or_array : [ value_or_array ] ) ;
20+ Array . prototype . push . apply ( arr , isArray ( value_or_array ) ? value_or_array : [ value_or_array ] ) ;
2421} ;
2522
26- const to_ISO = Date . prototype . toISOString ;
23+ let toISOString ;
2724
2825const defaults = {
2926 addQueryPrefix : false ,
@@ -38,11 +35,11 @@ const defaults = {
3835 encoder : encode ,
3936 encodeValuesOnly : false ,
4037 format : default_format ,
41- formatter : formatters [ default_format ] ,
38+ formatter : default_formatter ,
4239 /** @deprecated */
4340 indices : false ,
4441 serializeDate ( date ) {
45- return to_ISO . call ( date ) ;
42+ return ( toISOString ??= Function . prototype . call . bind ( Date . prototype . toISOString ) ) ( date ) ;
4643 } ,
4744 skipNulls : false ,
4845 strictNullHandling : false ,
@@ -105,7 +102,7 @@ function inner_stringify(
105102 obj = filter ( prefix , obj ) ;
106103 } else if ( obj instanceof Date ) {
107104 obj = serializeDate ?.( obj ) ;
108- } else if ( generateArrayPrefix === 'comma' && is_array ( obj ) ) {
105+ } else if ( generateArrayPrefix === 'comma' && isArray ( obj ) ) {
109106 obj = maybe_map ( obj , function ( value ) {
110107 if ( value instanceof Date ) {
111108 return serializeDate ?.( value ) ;
@@ -148,14 +145,14 @@ function inner_stringify(
148145 }
149146
150147 let obj_keys ;
151- if ( generateArrayPrefix === 'comma' && is_array ( obj ) ) {
148+ if ( generateArrayPrefix === 'comma' && isArray ( obj ) ) {
152149 // we need to join elements in
153150 if ( encodeValuesOnly && encoder ) {
154151 // @ts -expect-error values only
155152 obj = maybe_map ( obj , encoder ) ;
156153 }
157154 obj_keys = [ { value : obj . length > 0 ? obj . join ( ',' ) || null : void undefined } ] ;
158- } else if ( is_array ( filter ) ) {
155+ } else if ( isArray ( filter ) ) {
159156 obj_keys = filter ;
160157 } else {
161158 const keys = Object . keys ( obj ) ;
@@ -165,9 +162,9 @@ function inner_stringify(
165162 const encoded_prefix = encodeDotInKeys ? String ( prefix ) . replace ( / \. / g, '%2E' ) : String ( prefix ) ;
166163
167164 const adjusted_prefix =
168- commaRoundTrip && is_array ( obj ) && obj . length === 1 ? encoded_prefix + '[]' : encoded_prefix ;
165+ commaRoundTrip && isArray ( obj ) && obj . length === 1 ? encoded_prefix + '[]' : encoded_prefix ;
169166
170- if ( allowEmptyArrays && is_array ( obj ) && obj . length === 0 ) {
167+ if ( allowEmptyArrays && isArray ( obj ) && obj . length === 0 ) {
171168 return adjusted_prefix + '[]' ;
172169 }
173170
@@ -184,7 +181,7 @@ function inner_stringify(
184181 // @ts -ignore
185182 const encoded_key = allowDots && encodeDotInKeys ? ( key as any ) . replace ( / \. / g, '%2E' ) : key ;
186183 const key_prefix =
187- is_array ( obj ) ?
184+ isArray ( obj ) ?
188185 typeof generateArrayPrefix === 'function' ?
189186 generateArrayPrefix ( adjusted_prefix , encoded_key )
190187 : adjusted_prefix
@@ -205,7 +202,7 @@ function inner_stringify(
205202 skipNulls ,
206203 encodeDotInKeys ,
207204 // @ts -ignore
208- generateArrayPrefix === 'comma' && encodeValuesOnly && is_array ( obj ) ? null : encoder ,
205+ generateArrayPrefix === 'comma' && encodeValuesOnly && isArray ( obj ) ? null : encoder ,
209206 filter ,
210207 sort ,
211208 allowDots ,
@@ -244,15 +241,15 @@ function normalize_stringify_options(
244241
245242 let format = default_format ;
246243 if ( typeof opts . format !== 'undefined' ) {
247- if ( ! has . call ( formatters , opts . format ) ) {
244+ if ( ! has ( formatters , opts . format ) ) {
248245 throw new TypeError ( 'Unknown format option provided.' ) ;
249246 }
250247 format = opts . format ;
251248 }
252249 const formatter = formatters [ format ] ;
253250
254251 let filter = defaults . filter ;
255- if ( typeof opts . filter === 'function' || is_array ( opts . filter ) ) {
252+ if ( typeof opts . filter === 'function' || isArray ( opts . filter ) ) {
256253 filter = opts . filter ;
257254 }
258255
@@ -316,7 +313,7 @@ export function stringify(object: any, opts: StringifyOptions = {}) {
316313 if ( typeof options . filter === 'function' ) {
317314 filter = options . filter ;
318315 obj = filter ( '' , obj ) ;
319- } else if ( is_array ( options . filter ) ) {
316+ } else if ( isArray ( options . filter ) ) {
320317 filter = options . filter ;
321318 obj_keys = filter ;
322319 }
0 commit comments