a47976fd51b4c07f241db53a6fc1c6d235dfb9c0
[platform/framework/web/crosswalk-tizen.git] /
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++;
34                         } else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
35                                 insideComment = false;
36                                 i++;
37                                 ret += currentChar;
38                                 ret += nextChar;
39                                 continue;
40                         } else if (insideComment === 'single' && currentChar === '\n') {
41                                 insideComment = false;
42                         } else if (!insideComment && currentChar + nextChar === '/*') {
43                                 insideComment = 'multi';
44                                 i++;
45                                 continue;
46                         } else if (insideComment === 'multi' && currentChar + nextChar === '*/') {
47                                 insideComment = false;
48                                 i++;
49                                 continue;
50                         }
51
52                         if (insideComment) {
53                                 continue;
54                         }
55
56                         ret += currentChar;
57                 }
58
59                 return ret;
60         }
61
62         if (typeof module !== 'undefined' && module.exports) {
63                 module.exports = stripJsonComments;
64         } else {
65                 window.stripJsonComments = stripJsonComments;
66         }
67 })();