mirror of
https://github.com/nodejs/node.git
synced 2025-05-18 22:53:45 +00:00

PR-URL: https://github.com/iojs/io.js/pull/1539 Fixes: https://github.com/iojs/io.js/issues/1253 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
68 lines
1.5 KiB
JavaScript
68 lines
1.5 KiB
JavaScript
/*!
|
|
strip-json-comments
|
|
Strip comments from JSON. Lets you use comments in your JSON files!
|
|
https://github.com/sindresorhus/strip-json-comments
|
|
by Sindre Sorhus
|
|
MIT License
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function stripJsonComments(str) {
|
|
var currentChar;
|
|
var nextChar;
|
|
var insideString = false;
|
|
var insideComment = false;
|
|
var ret = '';
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
|
currentChar = str[i];
|
|
nextChar = str[i + 1];
|
|
|
|
if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') {
|
|
insideString = !insideString;
|
|
}
|
|
|
|
if (insideString) {
|
|
ret += currentChar;
|
|
continue;
|
|
}
|
|
|
|
if (!insideComment && currentChar + nextChar === '//') {
|
|
insideComment = 'single';
|
|
i++;
|
|
} else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
|
|
insideComment = false;
|
|
i++;
|
|
ret += currentChar;
|
|
ret += nextChar;
|
|
continue;
|
|
} else if (insideComment === 'single' && currentChar === '\n') {
|
|
insideComment = false;
|
|
} else if (!insideComment && currentChar + nextChar === '/*') {
|
|
insideComment = 'multi';
|
|
i++;
|
|
continue;
|
|
} else if (insideComment === 'multi' && currentChar + nextChar === '*/') {
|
|
insideComment = false;
|
|
i++;
|
|
continue;
|
|
}
|
|
|
|
if (insideComment) {
|
|
continue;
|
|
}
|
|
|
|
ret += currentChar;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
if (typeof module !== 'undefined' && module.exports) {
|
|
module.exports = stripJsonComments;
|
|
} else {
|
|
window.stripJsonComments = stripJsonComments;
|
|
}
|
|
})();
|