diff --git a/src/plugins/props/types/map.js b/src/plugins/props/types/map.js index f0718d88..130998c6 100644 --- a/src/plugins/props/types/map.js +++ b/src/plugins/props/types/map.js @@ -1,8 +1,7 @@ import PropType from "../util/PropType.js"; +import { split } from "../util/split.js"; import Iterable from "./iterable.js"; -const entrySplitter = /(?= 2) { k = parts.shift(); v = parts.join(":"); diff --git a/src/plugins/props/util/split.js b/src/plugins/props/util/split.js index df0c0a5d..ebf7e522 100644 --- a/src/plugins/props/util/split.js +++ b/src/plugins/props/util/split.js @@ -15,10 +15,14 @@ function regexEscape (string) { return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); } +function unescapeSeparator (value, separator) { + return separator ? value.replaceAll(`\\${separator}`, separator) : value; +} + /** * Split a value by a separator, respecting pairs (parens, strings, etc.) but - * failing back gracefully for malformed input. Yields each top-level part as - * a trimmed string. + * failing back gracefully for malformed input. The separator can be escaped + * with a backslash. Yields each top-level part as a trimmed string. * * @param {string} value * @param {object} [options] @@ -34,7 +38,7 @@ export function* split (value, { separator = ",", pairs = defaultPairs } = {}) { let isSeparatorWhitespace = !separator; let separatorRegex = isSeparatorWhitespace ? /\s+/g - : RegExp(regexEscape(separator).replace(/^\s*|\s*$/g, "\\s*"), "g"); + : RegExp(`(? unescapeSeparator(p, separator)); return; } @@ -72,7 +79,7 @@ export function* split (value, { separator = ",", pairs = defaultPairs } = {}) { } else if (matched.trim() === separator) { if (stack.length === 0) { - yield value.slice(lastIndex, index).trim(); + yield unescapeSeparator(value.slice(lastIndex, index).trim(), separator); lastIndex = index + matched.length; } } @@ -98,6 +105,6 @@ export function* split (value, { separator = ",", pairs = defaultPairs } = {}) { } if (lastIndex < value.length) { - yield value.slice(lastIndex).trim(); + yield unescapeSeparator(value.slice(lastIndex).trim(), separator); } } diff --git a/test/split.js b/test/split.js index 7c9eeb9f..83f40c8f 100644 --- a/test/split.js +++ b/test/split.js @@ -50,5 +50,20 @@ export default { arg: `a ", b, c`, expect: [`a "`, "b", "c"], }, + { + name: "Escaped separator", + tests: [ + { + name: "Default", + arg: "a\\, b, c", + expect: ["a, b", "c"], + }, + { + name: "Custom", + args: ["foo\\:bar: baz", { separator: ":" }], + expect: ["foo:bar", "baz"], + }, + ], + }, ], };