Skip to content

Commit 7d76b29

Browse files
committed
feat: add option to prioritize some keys
1 parent 42ec32e commit 7d76b29

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ Options:
2828
-q, --quotingStyle Strings will be quoted using this quoting style [choices: "single", "double"] [default: "single"]
2929
-f, --forceQuotes Force quotes for all scalar values [boolean] [default: false]
3030
-w, --lineWidth Wrap line width (-1 for unlimited width) [number] [default: 80]
31+
-p, --prioritize Comma seperated list of keys to prioritize [string]
3132
-h, --help Show help [boolean]
3233
--version Show version number [boolean]
3334
3435
Examples:
3536
yaml-sort --input config.yml Sorts alphabetically and overwrites the file config.yml
3637
yaml-sort --input config.yml --lineWidth 100 --stdout Sorts the file config.yml and output result to STDOUT wrapped to 100 columns
3738
yaml-sort --input config.yml --indent 4 --output sorted.yml Indents with 4 spaces and outputs result to file sorted.yml
38-
yaml-sort --input config.yml --forceQuotes --quotingStyle double Forces double quotes for all scalar values
39+
yaml-sort --input config.yml --prioritize name Sorts alphabetically, keeps "name" key at the top
3940
cat config.yml | yaml-sort Sorts alphabetically from STDIN
4041
```

test/test-order.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
b:
2+
c: 'ccc'
3+
d: 'ddd'
4+
e: 'eee'
5+
a: 'Lorem ipsum...'

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,27 @@ test('CLI multiple YAML documents --check SUCCESS', (t) => {
298298
proc.stderr.match('')
299299
proc.end()
300300
})
301+
302+
test('CLI w order (full)', (t) => {
303+
const proc = spawn(t, 'cat test-order.yml | ../yaml-sort.js --prioritize e,d,c', opts)
304+
proc.exitCode(0)
305+
proc.stdout.match('a: Lorem ipsum...\n' +
306+
'b:\n' +
307+
' e: eee\n' +
308+
' d: ddd\n' +
309+
' c: ccc\n')
310+
proc.stderr.match('')
311+
proc.end()
312+
})
313+
314+
test('CLI w order (partial)', (t) => {
315+
const proc = spawn(t, 'cat test-order.yml | ../yaml-sort.js --prioritize f,e,b', opts)
316+
proc.exitCode(0)
317+
proc.stdout.match('b:\n' +
318+
' e: eee\n' +
319+
' c: ccc\n' +
320+
' d: ddd\n' +
321+
'a: Lorem ipsum...\n')
322+
proc.stderr.match('')
323+
proc.end()
324+
})

yaml-sort.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const argv = yargs
1515
'Sorts the file config.yml and output result to STDOUT wrapped to 100 columns'],
1616
['$0 --input config.yml --indent 4 --output sorted.yml',
1717
'Indents with 4 spaces and outputs result to file sorted.yml'],
18+
['$0 --input config.yml --prioritize name', 'Sorts alphabetically, keeps "name" key at the top'],
1819
['cat config.yml | $0',
1920
'Sorts alphabetically from STDIN']
2021
])
@@ -75,6 +76,11 @@ const argv = yargs
7576
describe: 'Wrap line width (-1 for unlimited width)',
7677
number: true
7778
})
79+
.option('prioritize', {
80+
alias: 'p',
81+
describe: 'Comma seperated list of keys to prioritize',
82+
string: true
83+
})
7884
.help('h')
7985
.alias('h', 'help')
8086
.version()
@@ -101,8 +107,16 @@ argv.input.forEach((file) => {
101107

102108
const documents = yaml.loadAll(content)
103109

110+
const prioritize = argv.prioritize ? argv.prioritize.split(',').reverse() : []
111+
104112
const sortedDocuments = documents.map(doc => yaml.dump(doc, {
105-
sortKeys: true,
113+
sortKeys: function (a, b) {
114+
const ia = prioritize.indexOf(a)
115+
const ib = prioritize.indexOf(b)
116+
return ia !== -1 || ib !== -1
117+
? (ib < ia ? -1 : ib > ia)
118+
: (a < b ? -1 : a > b)
119+
},
106120
indent: argv.indent,
107121
lineWidth: argv.lineWidth,
108122
quotingType: argv.quotingStyle === 'double' ? '"' : "'",

0 commit comments

Comments
 (0)