@@ -32,7 +32,7 @@ function strEscape (str) {
3232 return JSON . stringify ( str )
3333}
3434
35- function insertSort ( array , comparator ) {
35+ function sort ( array , comparator ) {
3636 // Insertion sort is very efficient for small input sizes, but it has a bad
3737 // worst case complexity. Thus, use native array sort for bigger values.
3838 if ( array . length > 2e2 || comparator ) {
@@ -97,21 +97,15 @@ function getCircularValueOption (options) {
9797 return '"[Circular]"'
9898}
9999
100- function getDeterministicOption ( options , key ) {
100+ function getDeterministicOption ( options ) {
101101 let value
102- if ( hasOwnProperty . call ( options , key ) ) {
103- value = options [ key ]
104- if ( typeof value === 'boolean' ) {
105- return value
102+ if ( hasOwnProperty . call ( options , 'deterministic' ) ) {
103+ value = options . deterministic
104+ if ( typeof value !== 'boolean' && typeof value !== 'function ') {
105+ throw new TypeError ( 'The "deterministic" argument must be of type boolean or comparator function' )
106106 }
107- if ( typeof value === 'function' ) {
108- return value
109- }
110- }
111- if ( value === undefined ) {
112- return true
113107 }
114- throw new TypeError ( `The " ${ key } " argument must be of type boolean or comparator function` )
108+ return value === undefined ? true : value
115109}
116110
117111function getBooleanOption ( options , key ) {
@@ -188,7 +182,7 @@ function configure (options) {
188182 }
189183 const circularValue = getCircularValueOption ( options )
190184 const bigint = getBooleanOption ( options , 'bigint' )
191- const deterministic = getDeterministicOption ( options , 'deterministic' )
185+ const deterministic = getDeterministicOption ( options )
192186 const comparator = typeof deterministic === 'function' ? deterministic : undefined
193187 const maximumDepth = getPositiveIntegerOption ( options , 'maximumDepth' )
194188 const maximumBreadth = getPositiveIntegerOption ( options , 'maximumBreadth' )
@@ -266,7 +260,7 @@ function configure (options) {
266260 }
267261 const maximumPropertiesToStringify = Math . min ( keyLength , maximumBreadth )
268262 if ( deterministic && ! isTypedArrayWithEntries ( value ) ) {
269- keys = insertSort ( keys , comparator )
263+ keys = sort ( keys , comparator )
270264 }
271265 stack . push ( value )
272266 for ( let i = 0 ; i < maximumPropertiesToStringify ; i ++ ) {
@@ -465,7 +459,7 @@ function configure (options) {
465459 separator = join
466460 }
467461 if ( deterministic ) {
468- keys = insertSort ( keys , comparator )
462+ keys = sort ( keys , comparator )
469463 }
470464 stack . push ( value )
471465 for ( let i = 0 ; i < maximumPropertiesToStringify ; i ++ ) {
@@ -527,7 +521,8 @@ function configure (options) {
527521
528522 let res = ''
529523
530- if ( Array . isArray ( value ) ) {
524+ const hasLength = value . length !== undefined
525+ if ( hasLength && Array . isArray ( value ) ) {
531526 if ( value . length === 0 ) {
532527 return '[]'
533528 }
@@ -562,14 +557,14 @@ function configure (options) {
562557 }
563558 let separator = ''
564559 let maximumPropertiesToStringify = Math . min ( keyLength , maximumBreadth )
565- if ( isTypedArrayWithEntries ( value ) ) {
560+ if ( hasLength && isTypedArrayWithEntries ( value ) ) {
566561 res += stringifyTypedArray ( value , ',' , maximumBreadth )
567562 keys = keys . slice ( value . length )
568563 maximumPropertiesToStringify -= value . length
569564 separator = ','
570565 }
571566 if ( deterministic ) {
572- keys = insertSort ( keys , comparator )
567+ keys = sort ( keys , comparator )
573568 }
574569 stack . push ( value )
575570 for ( let i = 0 ; i < maximumPropertiesToStringify ; i ++ ) {
0 commit comments