diff --git a/index.js b/index.js index e6be6f3..43991c3 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const writeFileAtomic = require('write-file-atomic'); const sortKeys = require('sort-keys'); const makeDir = require('make-dir'); const detectIndent = require('detect-indent'); +const isPlainObj = require('is-plain-obj'); const readFile = promisify(fs.readFile); @@ -24,7 +25,7 @@ const init = (fn, filePath, data, options) => { ...options }; - if (options.sortKeys) { + if (options.sortKeys && isPlainObj(data)) { data = sortKeys(data, { deep: true, compare: typeof options.sortKeys === 'function' ? options.sortKeys : undefined diff --git a/package.json b/package.json index 3c8dd44..8756e07 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "dependencies": { "detect-indent": "^6.0.0", "graceful-fs": "^4.1.15", + "is-plain-obj": "^2.0.0", "make-dir": "^3.0.0", "sort-keys": "^3.0.0", "write-file-atomic": "^3.0.0" diff --git a/test.js b/test.js index 37458be..7da9624 100644 --- a/test.js +++ b/test.js @@ -39,6 +39,9 @@ test('async - {sortKeys: true}', async t => { const tempFile = tempy.file(); await writeJsonFile(tempFile, {c: true, b: true, a: true}, {sortKeys: true}); t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"a": true,\n\t"b": true,\n\t"c": true\n}\n'); + + await writeJsonFile(tempFile, ['c', 'b', 'a'], {sortKeys: true}); + t.is(fs.readFileSync(tmp, 'utf8'), '[\n\t"c",\n\t"b",\n\t"a"\n]\n'); }); test('async - {sortKeys: false}', async t => {