Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/plugins/props/types/map.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import PropType from "../util/PropType.js";
import { split } from "../util/split.js";
import Iterable from "./iterable.js";

const entrySplitter = /(?<!\\):/;

/**
* Concrete type for `Map`, which also serves as the canonical dictionary in
* JS. Extends {@link Iterable}: reuses the parent's `parseItems` to grab
Expand Down Expand Up @@ -37,7 +36,7 @@ const MapType = PropType.register({
for (let item of this.parseItems(value)) {
let k, v;
if (typeof item === "string") {
let parts = item.split(entrySplitter);
let parts = [...split(item, { separator: ":" })];
if (parts.length >= 2) {
k = parts.shift();
v = parts.join(":");
Expand Down
19 changes: 13 additions & 6 deletions src/plugins/props/util/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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(`(?<!\\\\)${regexEscape(separator).replace(/^\s*|\s*$/g, "\\s*")}`, "g");

let pairStrings = new Set([
...Object.keys(pairs.nest),
Expand All @@ -46,7 +50,10 @@ export function* split (value, { separator = ",", pairs = defaultPairs } = {}) {

if (!pairRegex.test(value)) {
// value contains no pairs, just split
yield* value.trim().split(separatorRegex);
yield* value
.trim()
.split(separatorRegex)
.map(p => unescapeSeparator(p, separator));
return;
}

Expand All @@ -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;
}
}
Expand All @@ -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);
}
}
15 changes: 15 additions & 0 deletions test/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
},
],
};