Comparing version 1.13.1 to 1.14.0
1.14.0 / 2018-06-29 | ||
================== | ||
**features** | ||
* [[`d401917`](http://github.com/node-modules/utility/commit/d401917f20f89c52be237279d575c695f1bf6ae0)] - feat: add replacer and space to writeJSON* (#34) (Khaidi Chu <<i@2333.moe>>) | ||
1.13.1 / 2017-10-17 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -22,7 +22,13 @@ 'use strict'; | ||
exports.writeJSONSync = function(filepath, str) { | ||
exports.writeJSONSync = function(filepath, str, options) { | ||
options = options || {}; | ||
if (!('space' in options)) { | ||
options.space = 2; | ||
} | ||
mkdirp.sync(path.dirname(filepath)); | ||
if (typeof str === 'object') { | ||
str = JSON.stringify(str, null, 2) + '\n'; | ||
str = JSON.stringify(str, options.replacer, options.space) + '\n'; | ||
} | ||
fs.writeFileSync(filepath, str); | ||
@@ -44,6 +50,12 @@ }; | ||
exports.writeJSON = function(filepath, str) { | ||
exports.writeJSON = function(filepath, str, options) { | ||
options = options || {}; | ||
if (!('space' in options)) { | ||
options.space = 2; | ||
} | ||
if (typeof str === 'object') { | ||
str = JSON.stringify(str, null, 2) + '\n'; | ||
str = JSON.stringify(str, options.replacer, options.space) + '\n'; | ||
} | ||
return mkdir(path.dirname(filepath)) | ||
@@ -50,0 +62,0 @@ .then(function() { |
{ | ||
"name": "utility", | ||
"version": "1.13.1", | ||
"version": "1.14.0", | ||
"description": "A collection of useful utilities.", | ||
@@ -5,0 +5,0 @@ "main": "lib/utility.js", |
@@ -227,3 +227,6 @@ # utility | ||
const pkg = utils.readJSONSync('package.json'); | ||
utils.writeJSONSync('package.json', pkg); | ||
utils.writeJSONSync('package.json', pkg, { | ||
replacer: null, | ||
space: '\t', | ||
}); | ||
``` | ||
@@ -240,2 +243,14 @@ | ||
> **Hint:** In `utils.writeJSON*()`, if `pkg` is an object, the **optional** third parameter `options` may contain two | ||
> keys. | ||
> | ||
> + `replacer`: Equals to `JSON.stringify()`'s second parameter; | ||
> + `space`: Equals to `JSON.stringify()`'s third parameter. Defaults to `2`. | ||
> | ||
> Refs: | ||
> | ||
> + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter | ||
> + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument | ||
### Object.assign | ||
@@ -242,0 +257,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37876
273405
817
309
1