1- 'use strict' ;
21const singleComment = Symbol ( 'singleComment' ) ;
32const multiComment = Symbol ( 'multiComment' ) ;
3+
44const stripWithoutWhitespace = ( ) => '' ;
55const 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+ }
0 commit comments