Socket
Socket
Sign inDemoInstall

strip-json-comments

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strip-json-comments - npm Package Compare versions

Comparing version 4.0.0 to 5.0.0

9

index.d.ts
export interface Options {
/**
Replace comments with whitespace instead of stripping them entirely.
Strip trailing commas in addition to comments.
@default true
*/
readonly trailingCommas?: boolean;
/**
Replace comments and trailing commas with whitespace instead of stripping them entirely.
@default true
*/
readonly whitespace?: boolean;

@@ -8,0 +15,0 @@ }

@@ -19,3 +19,3 @@ const singleComment = Symbol('singleComment');

export default function stripJsonComments(jsonString, {whitespace = true} = {}) {
export default function stripJsonComments(jsonString, {whitespace = true, trailingCommas = false} = {}) {
if (typeof jsonString !== 'string') {

@@ -30,3 +30,5 @@ throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);

let offset = 0;
let buffer = '';
let result = '';
let commaIndex = -1;

@@ -38,2 +40,3 @@ for (let index = 0; index < jsonString.length; index++) {

if (!isInsideComment && currentCharacter === '"') {
// Enter or exit string
const escaped = isEscaped(jsonString, index);

@@ -50,3 +53,4 @@ if (!escaped) {

if (!isInsideComment && currentCharacter + nextCharacter === '//') {
result += jsonString.slice(offset, index);
// Enter single-line comment
buffer += jsonString.slice(offset, index);
offset = index;

@@ -56,13 +60,16 @@ isInsideComment = singleComment;

} else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
// Exit single-line comment via \r\n
index++;
isInsideComment = false;
result += strip(jsonString, offset, index);
buffer += strip(jsonString, offset, index);
offset = index;
continue;
} else if (isInsideComment === singleComment && currentCharacter === '\n') {
// Exit single-line comment via \n
isInsideComment = false;
result += strip(jsonString, offset, index);
buffer += strip(jsonString, offset, index);
offset = index;
} else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
result += jsonString.slice(offset, index);
// Enter multiline comment
buffer += jsonString.slice(offset, index);
offset = index;

@@ -73,11 +80,34 @@ isInsideComment = multiComment;

} else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
// Exit multiline comment
index++;
isInsideComment = false;
result += strip(jsonString, offset, index + 1);
buffer += strip(jsonString, offset, index + 1);
offset = index + 1;
continue;
} else if (trailingCommas && !isInsideComment) {
if (commaIndex !== -1) {
if (currentCharacter === '}' || currentCharacter === ']') {
// Strip trailing comma
buffer += jsonString.slice(offset, index);
result += strip(buffer, 0, 1) + buffer.slice(1);
buffer = '';
offset = index;
commaIndex = -1;
} else if (currentCharacter !== ' ' && currentCharacter !== '\t' && currentCharacter !== '\r' && currentCharacter !== '\n') {
// Hit non-whitespace following a comma; comma is not trailing
buffer += jsonString.slice(offset, index);
offset = index;
commaIndex = -1;
}
} else if (currentCharacter === ',') {
// Flush buffer prior to this point, and save new comma index
result += buffer + jsonString.slice(offset, index);
buffer = '';
offset = index;
commaIndex = index;
}
}
}
return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
return result + buffer + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
}

16

package.json
{
"name": "strip-json-comments",
"version": "4.0.0",
"version": "5.0.0",
"description": "Strip comments from JSON. Lets you use comments in your JSON files!",

@@ -15,4 +15,5 @@ "license": "MIT",

"exports": "./index.js",
"types": "./index.d.ts",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=14.16"
},

@@ -45,7 +46,12 @@ "scripts": {

"devDependencies": {
"ava": "^3.15.0",
"ava": "^4.3.1",
"matcha": "^0.7.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
"tsd": "^0.22.0",
"xo": "^0.51.0"
},
"xo": {
"rules": {
"complexity": "off"
}
}
}

@@ -20,5 +20,5 @@ # strip-json-comments

```sh
npm install strip-json-comments
```
$ npm install strip-json-comments
```

@@ -53,2 +53,9 @@ ## Usage

##### trailingCommas
Type: `boolean`\
Default: `false`
Strip trailing commas in addition to comments.
##### whitespace

@@ -59,9 +66,9 @@

Replace comments with whitespace instead of stripping them entirely.
Replace comments and trailing commas with whitespace instead of stripping them entirely.
## Benchmark
```sh
npm run bench
```
$ npm run bench
```

@@ -68,0 +75,0 @@ ## Related

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc