From 6a4181995b3007601903e2f207447db76a5fd56b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 28 May 2019 16:17:55 +0700 Subject: [PATCH 1/3] Create funding.yml --- .github/funding.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/funding.yml diff --git a/.github/funding.yml b/.github/funding.yml new file mode 100644 index 0000000..1a630e9 --- /dev/null +++ b/.github/funding.yml @@ -0,0 +1,3 @@ +github: sindresorhus +open_collective: sindresorhus +custom: https://sindresorhus.com/donate From 24d119b72cdccf44710816f4096b57ce1d74e7c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20M=C3=A5rtensson?= Date: Mon, 1 Jul 2019 07:38:00 +0200 Subject: [PATCH 2/3] Only sort keys on objects (#21) --- index.js | 3 ++- package.json | 1 + test.js | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) 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..26a7a3e 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(tempFile, 'utf8'), '[\n\t"c",\n\t"b",\n\t"a"\n]\n'); }); test('async - {sortKeys: false}', async t => { From 5b66b0eb5f5ddba9b44d6f6f66c4b7b90950efea Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 1 Jul 2019 12:39:43 +0700 Subject: [PATCH 3/3] 4.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8756e07..e3b6208 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "write-json-file", - "version": "4.1.0", + "version": "4.1.1", "description": "Stringify and write JSON to a file atomically", "license": "MIT", "repository": "sindresorhus/write-json-file",