wishthis/node_modules/strip-json-comments/index.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict';
2022-06-08 10:36:39 +00:00
const singleComment = Symbol('singleComment');
const multiComment = Symbol('multiComment');
const stripWithoutWhitespace = () => '';
const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/\S/g, ' ');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
const isEscaped = (jsonString, quotePosition) => {
let index = quotePosition - 1;
let backslashCount = 0;
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
while (jsonString[index] === '\\') {
index -= 1;
backslashCount += 1;
}
return Boolean(backslashCount % 2);
};
module.exports = (jsonString, options = {}) => {
if (typeof jsonString !== 'string') {
throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);
}
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
const strip = options.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
let insideString = false;
let insideComment = false;
let offset = 0;
let result = '';
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
for (let i = 0; i < jsonString.length; i++) {
const currentCharacter = jsonString[i];
const nextCharacter = jsonString[i + 1];
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
if (!insideComment && currentCharacter === '"') {
const escaped = isEscaped(jsonString, i);
2022-01-21 08:28:41 +00:00
if (!escaped) {
insideString = !insideString;
}
}
if (insideString) {
continue;
}
2022-06-08 10:36:39 +00:00
if (!insideComment && currentCharacter + nextCharacter === '//') {
result += jsonString.slice(offset, i);
2022-01-21 08:28:41 +00:00
offset = i;
insideComment = singleComment;
i++;
2022-06-08 10:36:39 +00:00
} else if (insideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
2022-01-21 08:28:41 +00:00
i++;
insideComment = false;
2022-06-08 10:36:39 +00:00
result += strip(jsonString, offset, i);
2022-01-21 08:28:41 +00:00
offset = i;
continue;
2022-06-08 10:36:39 +00:00
} else if (insideComment === singleComment && currentCharacter === '\n') {
2022-01-21 08:28:41 +00:00
insideComment = false;
2022-06-08 10:36:39 +00:00
result += strip(jsonString, offset, i);
2022-01-21 08:28:41 +00:00
offset = i;
2022-06-08 10:36:39 +00:00
} else if (!insideComment && currentCharacter + nextCharacter === '/*') {
result += jsonString.slice(offset, i);
2022-01-21 08:28:41 +00:00
offset = i;
insideComment = multiComment;
i++;
continue;
2022-06-08 10:36:39 +00:00
} else if (insideComment === multiComment && currentCharacter + nextCharacter === '*/') {
2022-01-21 08:28:41 +00:00
i++;
insideComment = false;
2022-06-08 10:36:39 +00:00
result += strip(jsonString, offset, i + 1);
2022-01-21 08:28:41 +00:00
offset = i + 1;
continue;
}
}
2022-06-08 10:36:39 +00:00
return result + (insideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
2022-01-21 08:28:41 +00:00
};