Comparing version 0.3.2 to 0.3.3
@@ -6,4 +6,7 @@ "use strict"; | ||
const RE_SET_VERB = /^(export|set)\s+/; | ||
const RE_BOUNDING_QUOTES = /^"|"$/g; | ||
const RE_QUOTE_WITH_COMMENT = /(".*?")(\s*#\s*.*)?/; | ||
const RE_WHITE_SPACE = /\s+/g; | ||
const RE_BOUNDING_QUOTES_DOUBLE = /^"|"$/g; | ||
const RE_BOUNDING_QUOTES_SINGLE = /^'|'$/g; | ||
const RE_QUOTE_WITH_COMMENT = /(('.*?'|".*?")(\s*#\s*.*)?)$/; | ||
const RE_QUOTE = /("|')/; | ||
function parse(text) { | ||
@@ -23,10 +26,22 @@ const lines = text.trim().split("\n"); | ||
continue; | ||
if (/\s+/g.test(key)) | ||
if (RE_WHITE_SPACE.test(key)) | ||
continue; | ||
let value = v.join("=").trim(); | ||
if (value.startsWith('"') && !RE_QUOTE_WITH_COMMENT.test(value)) { | ||
let multiline_value = value.replace(RE_BOUNDING_QUOTES, ""); | ||
while (i < lines.length - 1 && !lines[i].endsWith('"')) { | ||
if (RE_QUOTE.test(value[0]) && !RE_QUOTE_WITH_COMMENT.test(value)) { | ||
const start_quote = value[0]; | ||
const quote_bound = start_quote === '"' | ||
? RE_BOUNDING_QUOTES_DOUBLE | ||
: RE_BOUNDING_QUOTES_SINGLE; | ||
const quote_end = new RegExp(start_quote + "$"); | ||
let multiline_value = value.replace(quote_bound, ""); | ||
while (i < lines.length - 1) { | ||
i += 1; | ||
multiline_value += "\n" + (0, strip_comment_1.stripComment)(lines[i]).replace(/"$/, ""); | ||
if (lines[i].includes(start_quote) && | ||
!/\\/.test(lines[i].charAt(lines[i].indexOf(start_quote) - 1))) { | ||
multiline_value += | ||
"\n" + | ||
(0, strip_comment_1.stripInlineComment)(lines[i], start_quote).replace(quote_end, ""); | ||
break; | ||
} | ||
multiline_value += "\n" + (0, strip_comment_1.stripComment)(lines[i]).replace(quote_end, ""); | ||
} | ||
@@ -33,0 +48,0 @@ value = multiline_value; |
@@ -5,4 +5,7 @@ /** | ||
* @example `foo # bar` -> "foo" | ||
* @example `"foo # bar"` -> "foo # bar" | ||
* @example `"foo"` -> "foo" | ||
* @example `'foo'` -> "foo" | ||
* @example `"foo'` -> `"foo'` | ||
*/ | ||
export declare function stripComment(text: string): string; | ||
export declare function stripInlineComment(text: string, quote_char: string): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stripComment = void 0; | ||
exports.stripInlineComment = exports.stripComment = void 0; | ||
const QUOTE_CHARS = ['"', "'"]; | ||
const CHAR_COMMENT = "#"; | ||
/** | ||
@@ -8,13 +10,53 @@ * Removes inline comments from a single-line string. | ||
* @example `foo # bar` -> "foo" | ||
* @example `"foo # bar"` -> "foo # bar" | ||
* @example `"foo"` -> "foo" | ||
* @example `'foo'` -> "foo" | ||
* @example `"foo'` -> `"foo'` | ||
*/ | ||
function stripComment(text) { | ||
const chars = text.split(""); | ||
const start = chars[0] === '"' ? 1 : 0; | ||
const break_char = chars[0] === '"' ? '"' : "#"; | ||
const first_char = chars[0]; | ||
const is_quote_start = QUOTE_CHARS.includes(first_char); | ||
const start = is_quote_start ? 1 : 0; | ||
const break_char = is_quote_start ? first_char : CHAR_COMMENT; | ||
let value = ""; | ||
for (let i = start; i < chars.length; i++) { | ||
const char = chars[i]; | ||
if (char === break_char) | ||
if (char === break_char) { | ||
if (is_quote_start) { | ||
// Break the loop if it's the last char of if | ||
// the next, non-empty character is a comment. | ||
if (i === chars.length - 1) | ||
break; | ||
const next_chars = chars.slice(i + 1); | ||
if (next_chars.filter((char) => char.trim())[0] === CHAR_COMMENT) | ||
break; | ||
} | ||
else { | ||
// If the comment is reached, break the loop. | ||
if (break_char === CHAR_COMMENT) | ||
break; | ||
} | ||
} | ||
value += char; | ||
if (is_quote_start && i === chars.length - 1) { | ||
// If the end of the string is reached and the quote is not closed, | ||
// prepend the first character to the value. | ||
if (char !== break_char) { | ||
value = first_char + value; | ||
} | ||
} | ||
} | ||
return value.trim(); | ||
} | ||
exports.stripComment = stripComment; | ||
function stripInlineComment(text, quote_char) { | ||
const chars = text.split(""); | ||
let value = ""; | ||
for (let i = 0; i < chars.length; i++) { | ||
const char = chars[i]; | ||
// If the current character is a quote and the previous character is not a backslash | ||
if (char === quote_char && chars[i - 1] !== "\\") | ||
break; | ||
if (char === "#" && chars[i - 1] !== "\\") | ||
break; | ||
value += char; | ||
@@ -24,2 +66,2 @@ } | ||
} | ||
exports.stripComment = stripComment; | ||
exports.stripInlineComment = stripInlineComment; |
{ | ||
"name": "didone", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Minimalist dotenv-like parser for the browser", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9913
166