@@ -72,6 +72,74 @@ describe('stringifyInfo()', () => {
7272 } ) ;
7373 } ) ;
7474
75+ describe ( 'mode option' , ( ) => {
76+ it ( 'json by default' , ( ) => {
77+ assert . deepStrictEqual ( stringifyInfo ( [ { a : 1 } , { a : 2 } ] ) , {
78+ bytes : '[{"a":1},{"a":2}]' . length ,
79+ spaceBytes : 0 ,
80+ circular : [ ]
81+ } ) ;
82+ } ) ;
83+
84+ it ( 'jsonl mode computes newline-delimited sizes' , ( ) => {
85+ // {"a":1}\n{"a":2}\n3 = 7+1+7+1+1 = 17
86+ assert . deepStrictEqual ( stringifyInfo ( [ { a : 1 } , { a : 2 } , 3 ] , { mode : 'jsonl' } ) , {
87+ bytes : 17 ,
88+ spaceBytes : 0 ,
89+ circular : [ ]
90+ } ) ;
91+ } ) ;
92+
93+ it ( 'jsonl mode with non-array value treats as single value' , ( ) => {
94+ assert . deepStrictEqual ( stringifyInfo ( { a : 1 } , { mode : 'jsonl' } ) , {
95+ bytes : '{"a":1}' . length ,
96+ spaceBytes : 0 ,
97+ circular : [ ]
98+ } ) ;
99+ } ) ;
100+
101+ it ( 'jsonl mode with empty array' , ( ) => {
102+ assert . deepStrictEqual ( stringifyInfo ( [ ] , { mode : 'jsonl' } ) , {
103+ bytes : 0 ,
104+ spaceBytes : 0 ,
105+ circular : [ ]
106+ } ) ;
107+ } ) ;
108+
109+ it ( 'jsonl mode with single element array' , ( ) => {
110+ assert . deepStrictEqual ( stringifyInfo ( [ 42 ] , { mode : 'jsonl' } ) , {
111+ bytes : 2 ,
112+ spaceBytes : 0 ,
113+ circular : [ ]
114+ } ) ;
115+ } ) ;
116+
117+ it ( 'jsonl mode with space option' , ( ) => {
118+ // Each root is formatted independently
119+ const info = stringifyInfo ( [ { a : 1 } , { b : 2 } ] , { mode : 'jsonl' , space : 2 } ) ;
120+ const root1 = JSON . stringify ( { a : 1 } , null , 2 ) ;
121+ const root2 = JSON . stringify ( { b : 2 } , null , 2 ) ;
122+ const expected = root1 + '\n' + root2 ;
123+
124+ assert . strictEqual ( info . bytes , Buffer . byteLength ( expected , 'utf8' ) ) ;
125+ } ) ;
126+
127+ it ( 'jsonl mode with replacer' , ( ) => {
128+ assert . deepStrictEqual ( stringifyInfo ( [ { a : 1 , b : 2 } , { a : 3 , b : 4 } ] , { mode : 'jsonl' , replacer : [ 'a' ] } ) , {
129+ bytes : '{"a":1}\n{"a":3}' . length ,
130+ spaceBytes : 0 ,
131+ circular : [ ]
132+ } ) ;
133+ } ) ;
134+
135+ it ( 'throws on invalid mode' , ( ) => {
136+ assert . throws (
137+ ( ) => stringifyInfo ( [ 1 , 2 ] , { mode : 'auto' } ) ,
138+ / I n v a l i d o p t i o n s : ` m o d e ` s h o u l d b e " j s o n " o r " j s o n l " /
139+ ) ;
140+ } ) ;
141+ } ) ;
142+
75143 describe ( 'circular' , ( ) => {
76144 it ( 'should stop on first circular reference by default' , ( ) => {
77145 const circularRef = { } ;
0 commit comments