strip-json-comments
Advanced tools
Comparing version 2.0.1 to 3.0.0
61
index.js
'use strict'; | ||
var singleComment = 1; | ||
var multiComment = 2; | ||
const singleComment = Symbol('singleComment'); | ||
const multiComment = Symbol('multiComment'); | ||
const stripWithoutWhitespace = () => ''; | ||
const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/\S/g, ' '); | ||
function stripWithoutWhitespace() { | ||
return ''; | ||
} | ||
module.exports = (jsonString, options = {}) => { | ||
const strip = options.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace; | ||
function stripWithWhitespace(str, start, end) { | ||
return str.slice(start, end).replace(/\S/g, ' '); | ||
} | ||
let insideString = false; | ||
let insideComment = false; | ||
let offset = 0; | ||
let result = ''; | ||
module.exports = function (str, opts) { | ||
opts = opts || {}; | ||
for (let i = 0; i < jsonString.length; i++) { | ||
const currentCharacter = jsonString[i]; | ||
const nextCharacter = jsonString[i + 1]; | ||
var currentChar; | ||
var nextChar; | ||
var insideString = false; | ||
var insideComment = false; | ||
var offset = 0; | ||
var ret = ''; | ||
var strip = opts.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace; | ||
for (var i = 0; i < str.length; i++) { | ||
currentChar = str[i]; | ||
nextChar = str[i + 1]; | ||
if (!insideComment && currentChar === '"') { | ||
var escaped = str[i - 1] === '\\' && str[i - 2] !== '\\'; | ||
if (!insideComment && currentCharacter === '"') { | ||
const escaped = jsonString[i - 1] === '\\' && jsonString[i - 2] !== '\\'; | ||
if (!escaped) { | ||
@@ -39,19 +30,19 @@ insideString = !insideString; | ||
if (!insideComment && currentChar + nextChar === '//') { | ||
ret += str.slice(offset, i); | ||
if (!insideComment && currentCharacter + nextCharacter === '//') { | ||
result += jsonString.slice(offset, i); | ||
offset = i; | ||
insideComment = singleComment; | ||
i++; | ||
} else if (insideComment === singleComment && currentChar + nextChar === '\r\n') { | ||
} else if (insideComment === singleComment && currentCharacter + nextCharacter === '\r\n') { | ||
i++; | ||
insideComment = false; | ||
ret += strip(str, offset, i); | ||
result += strip(jsonString, offset, i); | ||
offset = i; | ||
continue; | ||
} else if (insideComment === singleComment && currentChar === '\n') { | ||
} else if (insideComment === singleComment && currentCharacter === '\n') { | ||
insideComment = false; | ||
ret += strip(str, offset, i); | ||
result += strip(jsonString, offset, i); | ||
offset = i; | ||
} else if (!insideComment && currentChar + nextChar === '/*') { | ||
ret += str.slice(offset, i); | ||
} else if (!insideComment && currentCharacter + nextCharacter === '/*') { | ||
result += jsonString.slice(offset, i); | ||
offset = i; | ||
@@ -61,6 +52,6 @@ insideComment = multiComment; | ||
continue; | ||
} else if (insideComment === multiComment && currentChar + nextChar === '*/') { | ||
} else if (insideComment === multiComment && currentCharacter + nextCharacter === '*/') { | ||
i++; | ||
insideComment = false; | ||
ret += strip(str, offset, i + 1); | ||
result += strip(jsonString, offset, i + 1); | ||
offset = i + 1; | ||
@@ -71,3 +62,3 @@ continue; | ||
return ret + (insideComment ? strip(str.substr(offset)) : str.substr(offset)); | ||
return result + (insideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset)); | ||
}; |
{ | ||
"name": "strip-json-comments", | ||
"version": "2.0.1", | ||
"description": "Strip comments from JSON. Lets you use comments in your JSON files!", | ||
"license": "MIT", | ||
"repository": "sindresorhus/strip-json-comments", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"json", | ||
"strip", | ||
"remove", | ||
"delete", | ||
"trim", | ||
"comments", | ||
"multiline", | ||
"parse", | ||
"config", | ||
"configuration", | ||
"conf", | ||
"settings", | ||
"util", | ||
"env", | ||
"environment" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "strip-json-comments", | ||
"version": "3.0.0", | ||
"description": "Strip comments from JSON. Lets you use comments in your JSON files!", | ||
"license": "MIT", | ||
"repository": "sindresorhus/strip-json-comments", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd", | ||
"bench": "matcha benchmark.js" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"json", | ||
"strip", | ||
"comments", | ||
"remove", | ||
"delete", | ||
"trim", | ||
"multiline", | ||
"parse", | ||
"config", | ||
"configuration", | ||
"settings", | ||
"util", | ||
"env", | ||
"environment" | ||
], | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"matcha": "^0.7.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # strip-json-comments [![Build Status](https://travis-ci.org/sindresorhus/strip-json-comments.svg?branch=master)](https://travis-ci.org/sindresorhus/strip-json-comments) | ||
{ | ||
// rainbows | ||
// Rainbows | ||
"unicorn": /* ❤ */ "cake" | ||
@@ -17,3 +17,3 @@ } | ||
Also available as a [gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin. | ||
Also available as a [Gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[Grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[Broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin. | ||
@@ -24,3 +24,3 @@ | ||
``` | ||
$ npm install --save strip-json-comments | ||
$ npm install strip-json-comments | ||
``` | ||
@@ -32,3 +32,6 @@ | ||
```js | ||
const json = '{/*rainbows*/"unicorn":"cake"}'; | ||
const json = `{ | ||
// Rainbows | ||
"unicorn": /* ❤ */ "cake" | ||
}`; | ||
@@ -42,5 +45,5 @@ JSON.parse(stripJsonComments(json)); | ||
### stripJsonComments(input, [options]) | ||
### stripJsonComments(jsonString, [options]) | ||
#### input | ||
#### jsonString | ||
@@ -53,5 +56,7 @@ Type: `string` | ||
Type: `object` | ||
##### whitespace | ||
Type: `boolean` | ||
Type: `boolean`<br> | ||
Default: `true` | ||
@@ -62,2 +67,9 @@ | ||
## Benchmark | ||
``` | ||
$ npm run bench | ||
``` | ||
## Related | ||
@@ -71,2 +83,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
6137
5
83
77
4