Skip to content

Commit d77f9a5

Browse files
committedAug 19, 2021
Require Node.js 12.20 and move to ESM
1 parent 8fb3824 commit d77f9a5

File tree

8 files changed

+60
-68
lines changed

8 files changed

+60
-68
lines changed
 

Diff for: ‎.github/workflows/main.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

Diff for: ‎benchmark.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* globals bench, set */
2-
'use strict';
3-
const fs = require('fs');
4-
const stripJsonComments = require('.');
2+
import fs from 'node:fs';
3+
import stripJsonComments from './index.js';
54

65
const json = fs.readFileSync('sample.json', 'utf8');
76
const bigJson = fs.readFileSync('sample-big.json', 'utf8');

Diff for: ‎index.d.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
declare namespace stripJsonComments {
2-
interface Options {
3-
/**
4-
Replace comments with whitespace instead of stripping them entirely.
1+
export interface Options {
2+
/**
3+
Replace comments with whitespace instead of stripping them entirely.
54
6-
@default true
7-
*/
8-
readonly whitespace?: boolean;
9-
}
5+
@default true
6+
*/
7+
readonly whitespace?: boolean;
108
}
119

1210
/**
@@ -19,7 +17,7 @@ It will replace single-line comments `//` and multi-line comments `/**\/` with w
1917
2018
@example
2119
```
22-
import stripJsonComments = require('strip-json-comments');
20+
import stripJsonComments from 'strip-json-comments';
2321
2422
const json = `{
2523
// Rainbows
@@ -30,9 +28,7 @@ JSON.parse(stripJsonComments(json));
3028
//=> {unicorn: 'cake'}
3129
```
3230
*/
33-
declare function stripJsonComments(
31+
export default function stripJsonComments(
3432
jsonString: string,
35-
options?: stripJsonComments.Options
33+
options?: Options
3634
): string;
37-
38-
export = stripJsonComments;

Diff for: ‎index.js

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'use strict';
21
const singleComment = Symbol('singleComment');
32
const multiComment = Symbol('multiComment');
3+
44
const stripWithoutWhitespace = () => '';
55
const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/\S/g, ' ');
66

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

19-
module.exports = (jsonString, options = {}) => {
19+
export default function stripJsonComments(jsonString, {whitespace = true} = {}) {
2020
if (typeof jsonString !== 'string') {
2121
throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);
2222
}
2323

24-
const strip = options.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
24+
const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
2525

26-
let insideString = false;
27-
let insideComment = false;
26+
let isInsideString = false;
27+
let isInsideComment = false;
2828
let offset = 0;
2929
let result = '';
3030

31-
for (let i = 0; i < jsonString.length; i++) {
32-
const currentCharacter = jsonString[i];
33-
const nextCharacter = jsonString[i + 1];
31+
for (let index = 0; index < jsonString.length; index++) {
32+
const currentCharacter = jsonString[index];
33+
const nextCharacter = jsonString[index + 1];
3434

35-
if (!insideComment && currentCharacter === '"') {
36-
const escaped = isEscaped(jsonString, i);
35+
if (!isInsideComment && currentCharacter === '"') {
36+
const escaped = isEscaped(jsonString, index);
3737
if (!escaped) {
38-
insideString = !insideString;
38+
isInsideString = !isInsideString;
3939
}
4040
}
4141

42-
if (insideString) {
42+
if (isInsideString) {
4343
continue;
4444
}
4545

46-
if (!insideComment && currentCharacter + nextCharacter === '//') {
47-
result += jsonString.slice(offset, i);
48-
offset = i;
49-
insideComment = singleComment;
50-
i++;
51-
} else if (insideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
52-
i++;
53-
insideComment = false;
54-
result += strip(jsonString, offset, i);
55-
offset = i;
46+
if (!isInsideComment && currentCharacter + nextCharacter === '//') {
47+
result += jsonString.slice(offset, index);
48+
offset = index;
49+
isInsideComment = singleComment;
50+
index++;
51+
} else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
52+
index++;
53+
isInsideComment = false;
54+
result += strip(jsonString, offset, index);
55+
offset = index;
5656
continue;
57-
} else if (insideComment === singleComment && currentCharacter === '\n') {
58-
insideComment = false;
59-
result += strip(jsonString, offset, i);
60-
offset = i;
61-
} else if (!insideComment && currentCharacter + nextCharacter === '/*') {
62-
result += jsonString.slice(offset, i);
63-
offset = i;
64-
insideComment = multiComment;
65-
i++;
57+
} else if (isInsideComment === singleComment && currentCharacter === '\n') {
58+
isInsideComment = false;
59+
result += strip(jsonString, offset, index);
60+
offset = index;
61+
} else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
62+
result += jsonString.slice(offset, index);
63+
offset = index;
64+
isInsideComment = multiComment;
65+
index++;
6666
continue;
67-
} else if (insideComment === multiComment && currentCharacter + nextCharacter === '*/') {
68-
i++;
69-
insideComment = false;
70-
result += strip(jsonString, offset, i + 1);
71-
offset = i + 1;
67+
} else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
68+
index++;
69+
isInsideComment = false;
70+
result += strip(jsonString, offset, index + 1);
71+
offset = index + 1;
7272
continue;
7373
}
7474
}
7575

76-
return result + (insideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
77-
};
76+
return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
77+
}

Diff for: ‎index.test-d.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {expectType} from 'tsd';
2-
import stripJsonComments = require('.');
3-
4-
const options: stripJsonComments.Options = {};
2+
import stripJsonComments from './index.js';
53

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

Diff for: ‎package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd",
@@ -39,9 +41,9 @@
3941
"jsonc"
4042
],
4143
"devDependencies": {
42-
"ava": "^1.4.1",
44+
"ava": "^3.15.0",
4345
"matcha": "^0.7.0",
44-
"tsd": "^0.7.2",
45-
"xo": "^0.24.0"
46+
"tsd": "^0.17.0",
47+
"xo": "^0.44.0"
4648
}
4749
}

Diff for: ‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $ npm install strip-json-comments
2424
## Usage
2525

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

2929
const json = `{
3030
// Rainbows

Diff for: ‎test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import stripJsonComments from '.';
2+
import stripJsonComments from './index.js';
33

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

0 commit comments

Comments
 (0)
Please sign in to comment.