-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
sindresorhus
merged 1 commit into
sindresorhus:master
from
BendingBender:typescript-defs
Apr 30, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '12' | ||
- '10' | ||
- '8' | ||
- '6' | ||
- '4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\\"\""}'); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups.