Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 19, 2021
1 parent 8fb3824 commit d77f9a5
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 68 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
5 changes: 2 additions & 3 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* globals bench, set */
'use strict';
const fs = require('fs');
const stripJsonComments = require('.');
import fs from 'node:fs';
import stripJsonComments from './index.js';

const json = fs.readFileSync('sample.json', 'utf8');
const bigJson = fs.readFileSync('sample-big.json', 'utf8');
Expand Down
22 changes: 9 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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;
}

/**
Expand All @@ -19,7 +17,7 @@ It will replace single-line comments `//` and multi-line comments `/**\/` with w
@example
```
import stripJsonComments = require('strip-json-comments');
import stripJsonComments from 'strip-json-comments';
const json = `{
// Rainbows
Expand All @@ -30,9 +28,7 @@ JSON.parse(stripJsonComments(json));
//=> {unicorn: 'cake'}
```
*/
declare function stripJsonComments(
export default function stripJsonComments(
jsonString: string,
options?: stripJsonComments.Options
options?: Options
): string;

export = stripJsonComments;
76 changes: 38 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const singleComment = Symbol('singleComment');
const multiComment = Symbol('multiComment');

const stripWithoutWhitespace = () => '';
const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/\S/g, ' ');

Expand All @@ -16,62 +16,62 @@ const isEscaped = (jsonString, quotePosition) => {
return Boolean(backslashCount % 2);
};

module.exports = (jsonString, options = {}) => {
export default function stripJsonComments(jsonString, {whitespace = true} = {}) {
if (typeof jsonString !== 'string') {
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;
}
}

return result + (insideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
};
return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
}
4 changes: 1 addition & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {expectType} from 'tsd';
import stripJsonComments = require('.');

const options: stripJsonComments.Options = {};
import stripJsonComments from './index.js';

const json = '{/*rainbows*/"unicorn":"cake"}';

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd",
Expand Down Expand Up @@ -39,9 +41,9 @@
"jsonc"
],
"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"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ npm install strip-json-comments
## Usage

```js
const stripJsonComments = require('strip-json-comments');
import stripJsonComments from 'strip-json-comments';

const json = `{
// Rainbows
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import stripJsonComments from '.';
import stripJsonComments from './index.js';

test('replace comments with whitespace', t => {
t.is(stripJsonComments('//comment\n{"a":"b"}'), ' \n{"a":"b"}');
Expand Down

0 comments on commit d77f9a5

Please sign in to comment.