read-json-sync
Advanced tools
Comparing version 2.0.0-2 to 2.0.0-3
34
index.js
'use strict'; | ||
const {inspect} = require('util'); | ||
const {readFileSync} = require('graceful-fs'); | ||
const CODE = 'ERR_INVALID_OPT_VALUE_ENCODING'; | ||
const ENCODING_ERROR = 'read-json-sync expected the encoding option to be a <string> (defaulting to \'utf8\' if nothing is specified) so as to convert file contents from <Buffer> to <string> before parsing it as JSON'; | ||
module.exports = function readJsonSync(...args) { | ||
const argLen = args.length; | ||
if (argLen !== 1 && argLen !== 2) { | ||
if (argLen === 2) { | ||
const options = args[1]; | ||
const isObject = typeof options === 'object'; | ||
if (options === null || (isObject && options.encoding !== undefined && typeof options.encoding !== 'string')) { | ||
const encoding = options === null ? null : options.encoding; | ||
const error = new TypeError(`${ENCODING_ERROR}, but a non-string value ${inspect(encoding)} was provided.`); | ||
error.code = CODE; | ||
throw error; | ||
} | ||
if (options === '' || (isObject && options.encoding === '')) { | ||
const error = new TypeError(`${ENCODING_ERROR.replace('<', 'non-empty <')}, but '' (empty string) was provided.`); | ||
error.code = CODE; | ||
throw error; | ||
} | ||
if (isObject || options === undefined) { | ||
args[1] = Object.assign({encoding: 'utf8'}, options); | ||
} | ||
} else if (argLen === 1) { | ||
args.push('utf8'); | ||
} else { | ||
throw new RangeError(`Expected 1 or 2 arguments (path[, options]), but got ${ | ||
@@ -14,5 +42,5 @@ argLen === 0 ? 'no' : argLen | ||
const str = readFileSync(...args).toString(); | ||
const str = readFileSync(...args); | ||
return JSON.parse(str.charCodeAt(0) === 65279 ? /* 0xFEFF */ str.slice(1) : str); | ||
return JSON.parse(str.charCodeAt(0) === 65279 /* 0xFEFF */ ? str.slice(1) : str); | ||
}; |
{ | ||
"name": "read-json-sync", | ||
"version": "2.0.0-2", | ||
"version": "2.0.0-3", | ||
"description": "Read and parse a JSON file synchronously", | ||
@@ -33,3 +33,3 @@ "repository": "shinnn/read-json-sync", | ||
"chalk": "^2.3.0", | ||
"eslint": "^4.10.0", | ||
"eslint": "^4.11.0", | ||
"istanbul": "^0.4.5", | ||
@@ -36,0 +36,0 @@ "tape": "^4.8.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
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
4523
34