-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (34 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const {inspect} = require('util');
const {readFileSync} = require('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 === 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] = {encoding: 'utf8', ...options};
}
} else if (argLen === 1) {
args.push('utf8');
} else {
throw new RangeError(`Expected 1 or 2 arguments (path[, options]), but got ${
argLen === 0 ? 'no' : argLen
} arguments.`);
}
const str = readFileSync(...args);
return JSON.parse(str.charCodeAt(0) === 65279 /* 0xFEFF */ ? str.slice(1) : str);
};