require-json5
Advanced tools
Comparing version 1.1.0 to 1.2.0
67
index.js
@@ -5,12 +5,12 @@ /** | ||
* @license MIT | ||
* @version 1.1.0 | ||
* @version 1.2.0 | ||
* @author Dumitru Uzun (DUzun.Me) | ||
*/ | ||
var VERSION = '1.1.0'; | ||
const VERSION = '1.2.0'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
var JSON5 = require('json5'); | ||
const JSON5 = require('json5'); | ||
@@ -21,10 +21,20 @@ /// Require a JSON file with comments | ||
const extensions = ['.json5', '.json']; | ||
for(var i=0, l = extensions.length, ext; i<l; ++i) { | ||
for (let i=0, l = extensions.length, ext; i<l; ++i) { | ||
ext = extensions[i]; | ||
if(fs.existsSync(filename + ext)) { | ||
filename += ext; | ||
const fn = filename + ext; | ||
if (fs.existsSync(fn)) { | ||
filename = fn; | ||
break; | ||
} | ||
} | ||
// ES5 alternative | ||
// ['.json5', '.json'].some((ext) => { | ||
// const fn = filename + ext; | ||
// if (!fs.existsSync(fn)) return; | ||
// filename = fn; | ||
// return true; | ||
// }); | ||
} | ||
try { | ||
@@ -39,13 +49,39 @@ return JSON5.parse(stripBOM(fs.readFileSync(filename, 'utf8'))); | ||
function require_hook(module, filename) { | ||
module.exports = requireJSON5(filename); | ||
} | ||
const _backup_require_hooks = {}; | ||
/// Override require for .json extension | ||
function replace_require() { | ||
require.extensions['.json'] = function(module, filename) { | ||
module.exports = requireJSON5(filename); | ||
}; | ||
function replace_require(ext) { | ||
if (ext == undefined) ext = '.json'; | ||
const bak = require.extensions[ext]; | ||
if (bak === require_hook) return; | ||
_backup_require_hooks[ext] = bak; | ||
require.extensions[ext] = require_hook; | ||
} | ||
/// Restore the original require for .json extension | ||
function restore_require(ext) { | ||
if (ext == undefined) ext = '.json'; | ||
const bak = _backup_require_hooks[ext]; | ||
if (!(bak && ext in _backup_require_hooks)) return false; | ||
delete _backup_require_hooks[ext]; | ||
if (bak) { | ||
require.extensions[ext] = bak; | ||
return true; | ||
} | ||
return delete require.extensions[ext]; | ||
} | ||
/// Register .json5 extension for require | ||
require.extensions['.json5'] = function(module, filename) { | ||
module.exports = requireJSON5(filename); | ||
}; | ||
replace_require('.json5'); | ||
@@ -57,2 +93,3 @@ /// Exports: | ||
requireJSON5.replace = replace_require; | ||
requireJSON5.restore = restore_require; | ||
requireJSON5.VERSION = VERSION; | ||
@@ -59,0 +96,0 @@ |
{ | ||
"name": "require-json5", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Require JSON5 files in node - a better JSON for ES5 era", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,6 +0,6 @@ | ||
# require-json5 [![Build Status](https://travis-ci.org/duzun/require-json5.svg?branch=master)](https://travis-ci.org/duzun/require-json5) | ||
# require-json5 [![Build Status](https://travis-ci.com/duzun/require-json5.svg?branch=master)](https://app.travis-ci.com/duzun/require-json5) | ||
Require JSON5 files in node - a better JSON for ES5 era | ||
Require JSON5 files in node - a better JSON for the JSNext era | ||
JSON5 is more human friendly, can contain comments, trailing commas, unquoted keys amd more! | ||
JSON5 is more human friendly, can contain comments, trailing commas, unquoted keys and more! | ||
@@ -10,3 +10,3 @@ ## Install | ||
```sh | ||
npm i require-json5 | ||
npm i -S require-json5 | ||
``` | ||
@@ -19,3 +19,3 @@ | ||
```js | ||
var requireJSON5 = require('require-json5'); | ||
const requireJSON5 = require('require-json5'); | ||
``` | ||
@@ -26,12 +26,14 @@ | ||
```js | ||
var config = require("./config.json5"); | ||
let config = require("./config.json5"); | ||
// or w/o the extension, when "./config.json5" exists and there is no "./config.json", nor "./config.js" | ||
let config = require("./config"); | ||
``` | ||
2) Load a `.json` file in JSON5 format | ||
2) Explicitly load a `.json` file in JSON5 format | ||
```js | ||
var config = requireJSON5("./config.json"); | ||
let config = requireJSON5("./config.json"); | ||
``` | ||
3) Load a .js file as JSON5 format. | ||
3) Load a .js file as JSON5 format. | ||
This is useful if you don't like the `.json5` file extension | ||
@@ -41,3 +43,3 @@ and prefer to keep JSON5 in `.js` files. | ||
```js | ||
var config = requireJSON5("./config.js"); | ||
let config = requireJSON5("./config.js"); | ||
``` | ||
@@ -48,3 +50,3 @@ | ||
```js | ||
var config = requireJSON5.parse('{ name: /*a very important option*/ "value" }'); | ||
let config = requireJSON5.parse('{ name: /*a very important option*/ "value" }'); | ||
``` | ||
@@ -56,5 +58,10 @@ | ||
require('require-json5').replace(); | ||
var config = require("./config"); // can be config.json, config.json5 or config.js | ||
let config = require("./config"); // can be config.json, config.json5 or config.js | ||
``` | ||
5) Restore the original `require(.json)` | ||
```js | ||
require('require-json5').restore(); | ||
``` | ||
## Example of JSON5 | ||
@@ -91,1 +98,3 @@ | ||
``` | ||
For more details on the `JSON5` format see the [json5](https://www.npmjs.com/package/json5) library. |
@@ -26,3 +26,4 @@ | ||
// 4. | ||
var config = require("./config"); // .json file | ||
var config = require("./config"); // .json file containing .json5 | ||
const path = require('path'); | ||
asert.equal(config.name, 'json', 'yes'); | ||
@@ -33,2 +34,16 @@ asert.equal(config.unicorn, 'cake'); | ||
// 5. | ||
requireJSON.restore(); | ||
delete require.cache[path.resolve(__dirname, './config.json')]; | ||
try { | ||
var config = require("./config"); // should throw | ||
asert.equal(config.name, 'unknown'); // and never get here | ||
} | ||
catch(err) { | ||
asert.notEqual(err.code, 'ERR_ASSERTION'); | ||
asert.equal(/Unexpected token/.test(err.message), true); | ||
} | ||
// 6. | ||
var configStr = fs.readFileSync(__dirname + "/config5.json5", 'utf8'); | ||
@@ -40,3 +55,2 @@ var config = requireJSON.parse(configStr); | ||
console.log(config); |
Sorry, the diff of this file is not supported yet
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
10165
134
94