strip-json-comments
Advanced tools
Comparing version 3.1.1 to 4.0.0
@@ -1,10 +0,8 @@ | ||
declare namespace stripJsonComments { | ||
interface Options { | ||
/** | ||
Replace comments with whitespace instead of stripping them entirely. | ||
export interface Options { | ||
/** | ||
Replace comments with whitespace instead of stripping them entirely. | ||
@default true | ||
*/ | ||
readonly whitespace?: boolean; | ||
} | ||
@default true | ||
*/ | ||
readonly whitespace?: boolean; | ||
} | ||
@@ -22,2 +20,4 @@ | ||
``` | ||
import stripJsonComments from 'strip-json-comments'; | ||
const json = `{ | ||
@@ -32,7 +32,5 @@ // Rainbows | ||
*/ | ||
declare function stripJsonComments( | ||
export default function stripJsonComments( | ||
jsonString: string, | ||
options?: stripJsonComments.Options | ||
options?: Options | ||
): string; | ||
export = stripJsonComments; |
76
index.js
@@ -1,4 +0,4 @@ | ||
'use strict'; | ||
const singleComment = Symbol('singleComment'); | ||
const multiComment = Symbol('multiComment'); | ||
const stripWithoutWhitespace = () => ''; | ||
@@ -19,3 +19,3 @@ const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/\S/g, ' '); | ||
module.exports = (jsonString, options = {}) => { | ||
export default function stripJsonComments(jsonString, {whitespace = true} = {}) { | ||
if (typeof jsonString !== 'string') { | ||
@@ -25,50 +25,50 @@ throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``); | ||
const strip = options.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace; | ||
const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace; | ||
let insideString = false; | ||
let insideComment = false; | ||
let isInsideString = false; | ||
let isInsideComment = false; | ||
let offset = 0; | ||
let result = ''; | ||
for (let i = 0; i < jsonString.length; i++) { | ||
const currentCharacter = jsonString[i]; | ||
const nextCharacter = jsonString[i + 1]; | ||
for (let index = 0; index < jsonString.length; index++) { | ||
const currentCharacter = jsonString[index]; | ||
const nextCharacter = jsonString[index + 1]; | ||
if (!insideComment && currentCharacter === '"') { | ||
const escaped = isEscaped(jsonString, i); | ||
if (!isInsideComment && currentCharacter === '"') { | ||
const escaped = isEscaped(jsonString, index); | ||
if (!escaped) { | ||
insideString = !insideString; | ||
isInsideString = !isInsideString; | ||
} | ||
} | ||
if (insideString) { | ||
if (isInsideString) { | ||
continue; | ||
} | ||
if (!insideComment && currentCharacter + nextCharacter === '//') { | ||
result += jsonString.slice(offset, i); | ||
offset = i; | ||
insideComment = singleComment; | ||
i++; | ||
} else if (insideComment === singleComment && currentCharacter + nextCharacter === '\r\n') { | ||
i++; | ||
insideComment = false; | ||
result += strip(jsonString, offset, i); | ||
offset = i; | ||
if (!isInsideComment && currentCharacter + nextCharacter === '//') { | ||
result += jsonString.slice(offset, index); | ||
offset = index; | ||
isInsideComment = singleComment; | ||
index++; | ||
} else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') { | ||
index++; | ||
isInsideComment = false; | ||
result += strip(jsonString, offset, index); | ||
offset = index; | ||
continue; | ||
} else if (insideComment === singleComment && currentCharacter === '\n') { | ||
insideComment = false; | ||
result += strip(jsonString, offset, i); | ||
offset = i; | ||
} else if (!insideComment && currentCharacter + nextCharacter === '/*') { | ||
result += jsonString.slice(offset, i); | ||
offset = i; | ||
insideComment = multiComment; | ||
i++; | ||
} else if (isInsideComment === singleComment && currentCharacter === '\n') { | ||
isInsideComment = false; | ||
result += strip(jsonString, offset, index); | ||
offset = index; | ||
} else if (!isInsideComment && currentCharacter + nextCharacter === '/*') { | ||
result += jsonString.slice(offset, index); | ||
offset = index; | ||
isInsideComment = multiComment; | ||
index++; | ||
continue; | ||
} else if (insideComment === multiComment && currentCharacter + nextCharacter === '*/') { | ||
i++; | ||
insideComment = false; | ||
result += strip(jsonString, offset, i + 1); | ||
offset = i + 1; | ||
} else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') { | ||
index++; | ||
isInsideComment = false; | ||
result += strip(jsonString, offset, index + 1); | ||
offset = index + 1; | ||
continue; | ||
@@ -78,3 +78,3 @@ } | ||
return result + (insideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset)); | ||
}; | ||
return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset)); | ||
} |
{ | ||
"name": "strip-json-comments", | ||
"version": "3.1.1", | ||
"version": "4.0.0", | ||
"description": "Strip comments from JSON. Lets you use comments in your JSON files!", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -43,7 +45,7 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"ava": "^3.15.0", | ||
"matcha": "^0.7.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# strip-json-comments [![Build Status](https://travis-ci.com/sindresorhus/strip-json-comments.svg?branch=master)](https://travis-ci.com/github/sindresorhus/strip-json-comments) | ||
# strip-json-comments | ||
@@ -27,2 +27,4 @@ > Strip comments from JSON. Lets you use comments in your JSON files! | ||
```js | ||
import stripJsonComments from 'strip-json-comments'; | ||
const json = `{ | ||
@@ -29,0 +31,0 @@ // Rainbows |
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
7023
81
Yes
92