File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change 1
1
export { uneval } from './src/uneval.js' ;
2
- export { parse } from './src/parse.js' ;
2
+ export { parse , unflatten } from './src/parse.js' ;
3
3
export { stringify } from './src/stringify.js' ;
Original file line number Diff line number Diff line change @@ -12,8 +12,14 @@ import {
12
12
* @param {string } serialized
13
13
*/
14
14
export function parse ( serialized ) {
15
- const parsed = JSON . parse ( serialized ) ;
15
+ return unflatten ( JSON . parse ( serialized ) ) ;
16
+ }
16
17
18
+ /**
19
+ * Revive a value flattened with `devalue.flatten`
20
+ * @param {number | any[] } parsed
21
+ */
22
+ export function unflatten ( parsed ) {
17
23
if ( typeof parsed === 'number' ) return hydrate ( parsed , true ) ;
18
24
19
25
if ( ! Array . isArray ( parsed ) || parsed . length === 0 ) {
Original file line number Diff line number Diff line change 1
1
import * as vm from 'vm' ;
2
2
import * as assert from 'uvu/assert' ;
3
3
import * as uvu from 'uvu' ;
4
- import { uneval , parse , stringify } from '../index.js' ;
4
+ import { uneval , unflatten , parse , stringify } from '../index.js' ;
5
5
6
6
const fixtures = {
7
7
basics : [
@@ -406,6 +406,23 @@ for (const [name, tests] of Object.entries(fixtures)) {
406
406
test . run ( ) ;
407
407
}
408
408
409
+ for ( const [ name , tests ] of Object . entries ( fixtures ) ) {
410
+ const test = uvu . suite ( `unflatten: ${ name } ` ) ;
411
+ for ( const t of tests ) {
412
+ test ( t . name , ( ) => {
413
+ const actual = unflatten ( JSON . parse ( t . json ) ) ;
414
+ const expected = t . value ;
415
+
416
+ if ( t . validate ) {
417
+ t . validate ( actual ) ;
418
+ } else {
419
+ assert . equal ( actual , expected ) ;
420
+ }
421
+ } ) ;
422
+ }
423
+ test . run ( ) ;
424
+ }
425
+
409
426
const invalid = [
410
427
{
411
428
name : 'empty string' ,
You can’t perform that action at this time.
0 commit comments