1 | /*! |
||
2 | strip-json-comments |
||
3 | Strip comments from JSON. Lets you use comments in your JSON files! |
||
4 | https://github.com/sindresorhus/strip-json-comments |
||
5 | by Sindre Sorhus |
||
6 | MIT License |
||
7 | */ |
||
8 | (function () { |
||
9 | 'use strict'; |
||
10 | |||
11 | function stripJsonComments(str) { |
||
12 | var currentChar; |
||
13 | var nextChar; |
||
14 | var insideString = false; |
||
15 | var insideComment = false; |
||
16 | var ret = ''; |
||
17 | |||
18 | for (var i = 0; i < str.length; i++) { |
||
19 | currentChar = str[i]; |
||
20 | nextChar = str[i + 1]; |
||
21 | |||
22 | if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') { |
||
23 | insideString = !insideString; |
||
24 | } |
||
25 | |||
26 | if (insideString) { |
||
27 | ret += currentChar; |
||
28 | continue; |
||
29 | } |
||
30 | |||
31 | if (!insideComment && currentChar + nextChar === '//') { |
||
32 | insideComment = 'single'; |
||
33 | i++; |
||
0 ignored issues
–
show
Complexity
Coding Style
introduced
by
![]() |
|||
34 | } else if (insideComment === 'single' && currentChar + nextChar === '\r\n') { |
||
35 | insideComment = false; |
||
36 | i++; |
||
0 ignored issues
–
show
|
|||
37 | View Code Duplication | } else if (insideComment === 'single' && currentChar === '\n') { |
|
0 ignored issues
–
show
|
|||
38 | insideComment = false; |
||
39 | } else if (!insideComment && currentChar + nextChar === '/*') { |
||
40 | insideComment = 'multi'; |
||
41 | i++; |
||
0 ignored issues
–
show
|
|||
42 | continue; |
||
43 | } else if (insideComment === 'multi' && currentChar + nextChar === '*/') { |
||
44 | insideComment = false; |
||
45 | i++; |
||
0 ignored issues
–
show
|
|||
46 | continue; |
||
47 | } |
||
48 | |||
49 | if (insideComment) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | ret += currentChar; |
||
54 | } |
||
55 | |||
56 | return ret; |
||
57 | } |
||
58 | |||
59 | if (typeof module !== 'undefined' && module.exports) { |
||
60 | module.exports = stripJsonComments; |
||
61 | } else { |
||
62 | window.stripJsonComments = stripJsonComments; |
||
63 | } |
||
64 | })(); |
||
65 |