@@ -246,6 +246,35 @@ async function validateOffsetEncoding(string, inp, group) {
246246 }
247247}
248248
249+ async function parseJUSTC ( str ) {
250+ try {
251+ const result = JUSTC . parse ( str ) ;
252+
253+ if ( result && typeof result . then === 'function' ) {
254+ return await result ;
255+ }
256+
257+ return result ;
258+ } catch ( err ) {
259+ if ( typeof window !== 'undefined' ) { /* Browsers */
260+ try {
261+ await JUSTC . initialize ( ) ;
262+
263+ const retry = JUSTC . parse ( str ) ;
264+ if ( retry && typeof retry . then === 'function' ) {
265+ return await retry ;
266+ }
267+
268+ return retry ;
269+ } catch {
270+ return null ;
271+ }
272+ }
273+
274+ return null ;
275+ }
276+ }
277+
249278/**
250279 * **JavaScript String Compressor - compress function.**
251280 * @param {string|object|number } input string
@@ -269,6 +298,7 @@ export async function compress(input, options) {
269298 minifiedworker : true ,
270299 depthlimit : 10 ,
271300 workerlimit : 2 ,
301+ jsonstring : false ,
272302 debug : false ,
273303
274304 depth : 0 ,
@@ -325,9 +355,15 @@ export async function compress(input, options) {
325355 } else
326356 /* JSON Object (as object) */
327357 if ( typeof str == 'object' ) try {
358+ let JUSTCstr = undefined ;
328359 if ( opts . justc ) {
329360 const JUSTCobj = await toJUSTC ( str ) ;
330- str = JUSTCobj ;
361+ const JSONstr = JSON . stringify ( await parseJUSTC ( JUSTCobj ) ) ;
362+ if ( JSONstr == JSON . stringify ( str ) ) JUSTCstr = JUSTCobj ;
363+ }
364+
365+ if ( typeof JUSTCstr != 'undefined' ) {
366+ str = JUSTCstr ;
331367 code3 = 2 ;
332368 } else {
333369 str = JSON . stringify ( str ) ;
@@ -342,16 +378,21 @@ export async function compress(input, options) {
342378 const obj = JSON . parse ( str ) ;
343379 if ( ! Array . isArray ( obj ) && typeof obj == 'object' ) {
344380
345- const JUSTCobj = opts . justc ? await toJUSTC ( obj ) : false ;
381+ let JUSTCstr = undefined ;
382+ if ( opts . justc && opts . jsonstring ) {
383+ const JUSTCobj = await toJUSTC ( obj ) ;
384+ const JSONstr = JSON . stringify ( await parseJUSTC ( JUSTCobj ) ) ;
385+ if ( JSONstr == JSON . stringify ( str ) ) JUSTCstr = JUSTCobj ;
386+ }
346387
347- if ( JUSTCobj && JUSTCobj . length < str . length && str == JSON . stringify ( obj ) ) {
348- str = JUSTCobj ;
388+ if ( typeof JUSTCstr != 'undefined' && JUSTCstr . length < str . length && str == JSON . stringify ( obj ) ) {
389+ str = JUSTCstr ;
349390 code3 = 1 ;
350391 } else {
351392 str = str . slice ( 1 , - 1 ) ;
352393 code3 = 5 ;
353394 }
354- } else if ( typeof obj == 'object' ) {
395+ } else if ( typeof obj == 'object' && Array . isArray ( obj ) ) {
355396 /* JSON Array (as string) */
356397 str = str . slice ( 1 , - 1 ) ;
357398 code3 = 3 ;
@@ -514,35 +555,6 @@ function characterEncodings(id, realstr) {
514555 }
515556}
516557
517- async function parseJUSTC ( str ) {
518- try {
519- const result = JUSTC . parse ( str ) ;
520-
521- if ( result && typeof result . then === 'function' ) {
522- return await result ;
523- }
524-
525- return result ;
526- } catch ( err ) {
527- if ( typeof window !== 'undefined' ) { /* Browsers */
528- try {
529- await JUSTC . initialize ( ) ;
530-
531- const retry = JUSTC . parse ( str ) ;
532- if ( retry && typeof retry . then === 'function' ) {
533- return await retry ;
534- }
535-
536- return retry ;
537- } catch {
538- return null ;
539- }
540- }
541-
542- return null ;
543- }
544- }
545-
546558function offsetDecoding ( str , group ) {
547559 const offset = group * 32 ;
548560 const result = [ ] ;
0 commit comments