Skip to content

Commit ce3a285

Browse files
committed
Only sort keys on objects
Fixes #20.
1 parent f08874c commit ce3a285

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sortKeys = require('sort-keys');
66
const makeDir = require('make-dir');
77
const pify = require('pify');
88
const detectIndent = require('detect-indent');
9+
const isPlainObj = require('is-plain-obj');
910

1011
const init = (fn, filePath, data, options) => {
1112
if (!filePath) {
@@ -21,7 +22,7 @@ const init = (fn, filePath, data, options) => {
2122
sortKeys: false
2223
}, options);
2324

24-
if (options.sortKeys) {
25+
if (options.sortKeys && isPlainObj(data)) {
2526
data = sortKeys(data, {
2627
deep: true,
2728
compare: typeof options.sortKeys === 'function' ? options.sortKeys : undefined

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"detect-indent": "^5.0.0",
3838
"graceful-fs": "^4.1.2",
39+
"is-plain-obj": "^1.1.0",
3940
"make-dir": "^1.0.0",
4041
"pify": "^4.0.0",
4142
"sort-keys": "^2.0.0",

test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ test('async - {sortKeys: true}', async t => {
4040
const tmp = path.join(tempfile(), 'foo');
4141
await m(tmp, {c: true, b: true, a: true}, {sortKeys: true});
4242
t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"a": true,\n\t"b": true,\n\t"c": true\n}\n');
43+
await m(tmp, ['a', 'b', 'c'], {sortKeys: true});
44+
t.is(fs.readFileSync(tmp, 'utf8'), '[\n\t"a",\n\t"b",\n\t"c"\n]\n');
4345
});
4446

4547
test('async - {sortKeys: false}', async t => {

0 commit comments

Comments
 (0)