Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 8, add TypeScript definition #40

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
- '4'
36 changes: 36 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
declare namespace stripJsonComments {
interface Options {
/**
Replace comments with whitespace instead of stripping them entirely.

@default true
*/
readonly whitespace?: boolean;
}
}

/**
Strip comments from JSON. Lets you use comments in your JSON files!

It will replace single-line comments `//` and multi-line comments `/**\/` with whitespace. This allows JSON error positions to remain as close as possible to the original source.

@param jsonString - Accepts a string with JSON.
@returns A JSON string without comments.

@example
```
const json = `{
// rainbows
"unicorn": "cake"
}`;

JSON.parse(stripJsonComments(json));
//=> {unicorn: 'cake'}
```
*/
declare function stripJsonComments(
jsonString: string,
options?: stripJsonComments.Options
): string;

export = stripJsonComments;
46 changes: 22 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
'use strict';
const singleComment = 1;
const multiComment = 2;
const singleComment = Symbol('singleComment');
const multiComment = Symbol('multiComment');
const stripWithoutWhitespace = () => '';
const stripWithWhitespace = (str, start, end) => str.slice(start, end).replace(/\S/g, ' ');
const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/\S/g, ' ');

module.exports = (str, opts) => {
opts = opts || {};

const strip = opts.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
module.exports = (jsonString, options = {}) => {
const strip = options.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;

let insideString = false;
let insideComment = false;
let offset = 0;
let ret = '';
let result = '';

for (let i = 0; i < str.length; i++) {
const currentChar = str[i];
const nextChar = str[i + 1];
for (let i = 0; i < jsonString.length; i++) {
const currentCharacter = jsonString[i];
const nextCharacter = jsonString[i + 1];

if (!insideComment && currentChar === '"') {
const escaped = str[i - 1] === '\\' && str[i - 2] !== '\\';
if (!insideComment && currentCharacter === '"') {
const escaped = jsonString[i - 1] === '\\' && jsonString[i - 2] !== '\\';
if (!escaped) {
insideString = !insideString;
}
Expand All @@ -29,35 +27,35 @@ module.exports = (str, opts) => {
continue;
}

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;
insideComment = multiComment;
i++;
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;
continue;
}
}

return ret + (insideComment ? strip(str.substr(offset)) : str.substr(offset));
return result + (insideComment ? strip(jsonString.substr(offset)) : jsonString.substr(offset));
};
9 changes: 9 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {expectType} from 'tsd';
import stripJsonComments = require('.');

const options: stripJsonComments.Options = {};

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

expectType<string>(stripJsonComments(json));
expectType<string>(stripJsonComments(json, {whitespace: true}));
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to add tsd here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups.

Expand All @@ -37,8 +37,9 @@
"environment"
],
"devDependencies": {
"ava": "^0.20.0",
"ava": "^1.4.1",
"matcha": "^0.7.0",
"xo": "^0.18.0"
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ $ npm install strip-json-comments
## Usage

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

JSON.parse(stripJsonComments(json));
//=> {unicorn: 'cake'}
Expand All @@ -35,9 +38,9 @@ JSON.parse(stripJsonComments(json));

## API

### stripJsonComments(input, [options])
### stripJsonComments(jsonString, [options])

#### input
#### jsonString

Type: `string`

Expand Down
68 changes: 34 additions & 34 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
import test from 'ava';
import m from '.';
import stripJsonComments from '.';

test('replace comments with whitespace', t => {
t.is(m('//comment\n{"a":"b"}'), ' \n{"a":"b"}');
t.is(m('/*//comment*/{"a":"b"}'), ' {"a":"b"}');
t.is(m('{"a":"b"//comment\n}'), '{"a":"b" \n}');
t.is(m('{"a":"b"/*comment*/}'), '{"a":"b" }');
t.is(m('{"a"/*\n\n\ncomment\r\n*/:"b"}'), '{"a" \n\n\n \r\n :"b"}');
t.is(m('/*!\n * comment\n */\n{"a":"b"}'), ' \n \n \n{"a":"b"}');
t.is(m('{/*comment*/"a":"b"}'), '{ "a":"b"}');
t.is(stripJsonComments('//comment\n{"a":"b"}'), ' \n{"a":"b"}');
t.is(stripJsonComments('/*//comment*/{"a":"b"}'), ' {"a":"b"}');
t.is(stripJsonComments('{"a":"b"//comment\n}'), '{"a":"b" \n}');
t.is(stripJsonComments('{"a":"b"/*comment*/}'), '{"a":"b" }');
t.is(stripJsonComments('{"a"/*\n\n\ncomment\r\n*/:"b"}'), '{"a" \n\n\n \r\n :"b"}');
t.is(stripJsonComments('/*!\n * comment\n */\n{"a":"b"}'), ' \n \n \n{"a":"b"}');
t.is(stripJsonComments('{/*comment*/"a":"b"}'), '{ "a":"b"}');
});

test('remove comments', t => {
const opts = {whitespace: false};
t.is(m('//comment\n{"a":"b"}', opts), '\n{"a":"b"}');
t.is(m('/*//comment*/{"a":"b"}', opts), '{"a":"b"}');
t.is(m('{"a":"b"//comment\n}', opts), '{"a":"b"\n}');
t.is(m('{"a":"b"/*comment*/}', opts), '{"a":"b"}');
t.is(m('{"a"/*\n\n\ncomment\r\n*/:"b"}', opts), '{"a":"b"}');
t.is(m('/*!\n * comment\n */\n{"a":"b"}', opts), '\n{"a":"b"}');
t.is(m('{/*comment*/"a":"b"}', opts), '{"a":"b"}');
const options = {whitespace: false};
t.is(stripJsonComments('//comment\n{"a":"b"}', options), '\n{"a":"b"}');
t.is(stripJsonComments('/*//comment*/{"a":"b"}', options), '{"a":"b"}');
t.is(stripJsonComments('{"a":"b"//comment\n}', options), '{"a":"b"\n}');
t.is(stripJsonComments('{"a":"b"/*comment*/}', options), '{"a":"b"}');
t.is(stripJsonComments('{"a"/*\n\n\ncomment\r\n*/:"b"}', options), '{"a":"b"}');
t.is(stripJsonComments('/*!\n * comment\n */\n{"a":"b"}', options), '\n{"a":"b"}');
t.is(stripJsonComments('{/*comment*/"a":"b"}', options), '{"a":"b"}');
});

test('doesn\'t strip comments inside strings', t => {
t.is(m('{"a":"b//c"}'), '{"a":"b//c"}');
t.is(m('{"a":"b/*c*/"}'), '{"a":"b/*c*/"}');
t.is(m('{"/*a":"b"}'), '{"/*a":"b"}');
t.is(m('{"\\"/*a":"b"}'), '{"\\"/*a":"b"}');
t.is(stripJsonComments('{"a":"b//c"}'), '{"a":"b//c"}');
t.is(stripJsonComments('{"a":"b/*c*/"}'), '{"a":"b/*c*/"}');
t.is(stripJsonComments('{"/*a":"b"}'), '{"/*a":"b"}');
t.is(stripJsonComments('{"\\"/*a":"b"}'), '{"\\"/*a":"b"}');
});

test('consider escaped slashes when checking for escaped string quote', t => {
t.is(m('{"\\\\":"https://foobar.com"}'), '{"\\\\":"https://foobar.com"}');
t.is(m('{"foo\\"":"https://foobar.com"}'), '{"foo\\"":"https://foobar.com"}');
t.is(stripJsonComments('{"\\\\":"https://foobar.com"}'), '{"\\\\":"https://foobar.com"}');
t.is(stripJsonComments('{"foo\\"":"https://foobar.com"}'), '{"foo\\"":"https://foobar.com"}');
});

test('line endings - no comments', t => {
t.is(m('{"a":"b"\n}'), '{"a":"b"\n}');
t.is(m('{"a":"b"\r\n}'), '{"a":"b"\r\n}');
t.is(stripJsonComments('{"a":"b"\n}'), '{"a":"b"\n}');
t.is(stripJsonComments('{"a":"b"\r\n}'), '{"a":"b"\r\n}');
});

test('line endings - single line comment', t => {
t.is(m('{"a":"b"//c\n}'), '{"a":"b" \n}');
t.is(m('{"a":"b"//c\r\n}'), '{"a":"b" \r\n}');
t.is(stripJsonComments('{"a":"b"//c\n}'), '{"a":"b" \n}');
t.is(stripJsonComments('{"a":"b"//c\r\n}'), '{"a":"b" \r\n}');
});

test('line endings - single line block comment', t => {
t.is(m('{"a":"b"/*c*/\n}'), '{"a":"b" \n}');
t.is(m('{"a":"b"/*c*/\r\n}'), '{"a":"b" \r\n}');
t.is(stripJsonComments('{"a":"b"/*c*/\n}'), '{"a":"b" \n}');
t.is(stripJsonComments('{"a":"b"/*c*/\r\n}'), '{"a":"b" \r\n}');
});

test('line endings - multi line block comment', t => {
t.is(m('{"a":"b",/*c\nc2*/"x":"y"\n}'), '{"a":"b", \n "x":"y"\n}');
t.is(m('{"a":"b",/*c\r\nc2*/"x":"y"\r\n}'), '{"a":"b", \r\n "x":"y"\r\n}');
t.is(stripJsonComments('{"a":"b",/*c\nc2*/"x":"y"\n}'), '{"a":"b", \n "x":"y"\n}');
t.is(stripJsonComments('{"a":"b",/*c\r\nc2*/"x":"y"\r\n}'), '{"a":"b", \r\n "x":"y"\r\n}');
});

test('line endings - works at EOF', t => {
const opts = {whitespace: false};
t.is(m('{\r\n\t"a":"b"\r\n} //EOF'), '{\r\n\t"a":"b"\r\n} ');
t.is(m('{\r\n\t"a":"b"\r\n} //EOF', opts), '{\r\n\t"a":"b"\r\n} ');
const options = {whitespace: false};
t.is(stripJsonComments('{\r\n\t"a":"b"\r\n} //EOF'), '{\r\n\t"a":"b"\r\n} ');
t.is(stripJsonComments('{\r\n\t"a":"b"\r\n} //EOF', options), '{\r\n\t"a":"b"\r\n} ');
});

test.failing('handles weird escaping', t => {
// eslint-disable-next-line no-useless-escape
t.is(m('{"x":"x \"sed -e \\\"s/^.\\\\{46\\\\}T//\\\" -e \\\"s/#033/\\\\x1b/g\\\"\""}'), '{"x":"x \"sed -e \\\"s/^.\\\\{46\\\\}T//\\\" -e \\\"s/#033/\\\\x1b/g\\\"\""}');
t.is(stripJsonComments('{"x":"x \"sed -e \\\"s/^.\\\\{46\\\\}T//\\\" -e \\\"s/#033/\\\\x1b/g\\\"\""}'), '{"x":"x \"sed -e \\\"s/^.\\\\{46\\\\}T//\\\" -e \\\"s/#033/\\\\x1b/g\\\"\""}');
});