File tree Expand file tree Collapse file tree 3 files changed +66
-4
lines changed Expand file tree Collapse file tree 3 files changed +66
-4
lines changed Original file line number Diff line number Diff line change @@ -56,3 +56,29 @@ const arr = [
56
56
{ index: 4 , name: ' foo' },
57
57
]
58
58
```
59
+
60
+ #### Sort Object of Objects
61
+
62
+ Uses optional inline JSON configuration (like [ Sort Array of Objects] ( #sort-array-of-objects ) ) to define sort keys.
63
+
64
+ ``` js
65
+ // / keep-sorted { "keys": ["index","label"] }
66
+ const obj = {
67
+ a: { index: 3 , label: ' banana' },
68
+ b: { index: 2 , label: ' cherry' },
69
+ c: { index: 2 , label: ' apple' },
70
+ d: { index: 1 , label: ' berry' }
71
+ }
72
+ ```
73
+
74
+ Will be converted to:
75
+
76
+ ``` js
77
+ // / keep-sorted { "keys": ["index","label"] }
78
+ const obj = {
79
+ d: { index: 1 , label: ' berry' },
80
+ c: { index: 2 , label: ' apple' },
81
+ b: { index: 2 , label: ' cherry' },
82
+ a: { index: 3 , label: ' banana' },
83
+ }
84
+ ```
Original file line number Diff line number Diff line change @@ -405,4 +405,26 @@ run(
405
405
` ,
406
406
errors : [ 'command-error' ] ,
407
407
} ,
408
+ {
409
+ description : 'Sort object of objects' ,
410
+ code : $ `
411
+ /// keep-sorted { "keys": ["index","label"] }
412
+ const obj = {
413
+ a: { index: 3, label: 'banana' },
414
+ b: { index: 2, label: 'cherry' },
415
+ c: { index: 2, label: 'apple' },
416
+ d: { index: 1, label: 'berry' }
417
+ }
418
+ ` ,
419
+ output : $ `
420
+ /// keep-sorted { "keys": ["index","label"] }
421
+ const obj = {
422
+ d: { index: 1, label: 'berry' },
423
+ c: { index: 2, label: 'apple' },
424
+ b: { index: 2, label: 'cherry' },
425
+ a: { index: 3, label: 'banana' },
426
+ }
427
+ ` ,
428
+ errors : [ 'command-fix' ] ,
429
+ } ,
408
430
)
Original file line number Diff line number Diff line change @@ -72,17 +72,31 @@ export const keepSorted: Command = {
72
72
...( options ?. keys || [ ] ) ,
73
73
] . filter ( x => x != null ) as string [ ]
74
74
75
- if ( objectKeys . length > 0 && node . type !== 'ArrayExpression' )
76
- return ctx . reportError ( `Only arrays can be sorted by keys, but got ${ node . type } ` )
75
+ if ( objectKeys . length > 0 && node . type !== 'ArrayExpression' && node . type !== 'ObjectExpression' )
76
+ return ctx . reportError ( `Only arrays and objects can be sorted by keys, but got ${ node . type } ` )
77
77
78
78
if ( node . type === 'ObjectExpression' ) {
79
79
return sort (
80
80
ctx ,
81
81
node ,
82
- node . properties ,
82
+ node . properties . filter ( Boolean ) as ( Tree . ObjectExpression | Tree . Property ) [ ] ,
83
83
( prop ) => {
84
- if ( prop . type === 'Property' )
84
+ if ( objectKeys . length ) {
85
+ if ( prop . type === 'Property' && prop . value . type === 'ObjectExpression' ) {
86
+ const objectProp = prop . value
87
+ return objectKeys . map ( ( key ) => {
88
+ for ( const innerProp of objectProp . properties ) {
89
+ if ( innerProp . type === 'Property' && getString ( innerProp . key ) === key ) {
90
+ return getString ( innerProp . value )
91
+ }
92
+ }
93
+ return null
94
+ } )
95
+ }
96
+ }
97
+ else if ( prop . type === 'Property' ) {
85
98
return getString ( prop . key )
99
+ }
86
100
return null
87
101
} ,
88
102
)
You can’t perform that action at this time.
0 commit comments