Skip to content

Commit

Permalink
fix: Fix support for %% escape
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jun 24, 2021
1 parent 289009e commit 324777a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
23 changes: 18 additions & 5 deletions parse.js
Expand Up @@ -12,7 +12,8 @@ var digitChars = primitiveSet.apply(null, aFrom("0123456789"))
, flagChars = primitiveSet.apply(null, aFrom("#0-+ 'I"))
, lengthChars = primitiveSet.apply(null, aFrom("hlLzjt"));

var formatString, char, index, state, literalStart, literals, placeholders;
var formatString, char, index, state, literalStart, literals, placeholders
, afterEscapeLiteralIndexes;
var literalEnd, placeholder, currentTokenStart;

var states = {
Expand All @@ -35,11 +36,10 @@ var states = {
state = "parameter";
}
} else if (char === "%") {
placeholder.type = "%";
literals.push(formatString.slice(literalStart, literalEnd));
afterEscapeLiteralIndexes.push(
literals.push(formatString.slice(literalStart, literalEnd) + "%")
);
literalStart = ++index;
placeholder.content = formatString.slice(literalEnd, index);
placeholders.push(placeholder);
state = "literal";
} else {
state = "flagsStart";
Expand Down Expand Up @@ -204,6 +204,7 @@ module.exports = function (input) {
literalStart = 0;
literals = [];
placeholders = [];
afterEscapeLiteralIndexes = [];
var length = input.length;

// eslint-disable-next-line no-unmodified-loop-condition
Expand All @@ -213,6 +214,18 @@ module.exports = function (input) {
}
literals.push(formatString.slice(literalStart, length));

if (afterEscapeLiteralIndexes.length) {
for (
var i = afterEscapeLiteralIndexes.length - 1, literalIndex;
(literalIndex = afterEscapeLiteralIndexes[i]);
--i
) {
literals[literalIndex - 1] += literals[literalIndex];
delete literals[literalIndex];
}
literals = literals.filter(function (literal) { return typeof literal === "string"; });
}

return {
literals: literals,
placeholders: placeholders,
Expand Down
31 changes: 10 additions & 21 deletions test/parse/escape.js
Expand Up @@ -7,41 +7,30 @@ test("parse", function (t) {
t.test("Should parse text with", function (t) {
t.deepEqual(
parse("foo %%"),
{
literals: ["foo ", ""],
placeholders: [{ type: "%", content: "%%" }],
isParameterIndexingValid: true
},
{ literals: ["foo %"], placeholders: [], isParameterIndexingValid: true },
"Escape at the end"
);

t.deepEqual(
parse("%% foo"),
{
literals: ["", " foo"],
placeholders: [{ type: "%", content: "%%" }],
isParameterIndexingValid: true
},
{ literals: ["% foo"], placeholders: [], isParameterIndexingValid: true },
"Escape at the beginning"
);

t.deepEqual(
parse("foo %% bar"),
{
literals: ["foo ", " bar"],
placeholders: [{ type: "%", content: "%%" }],
isParameterIndexingValid: true
},
{ literals: ["foo % bar"], placeholders: [], isParameterIndexingValid: true },
"Escape in a middle"
);

t.deepEqual(
parse("%%"),
{
literals: ["", ""],
placeholders: [{ type: "%", content: "%%" }],
isParameterIndexingValid: true
},
parse("%%%% foo %%%% bar %%%%"),
{ literals: ["%% foo %% bar %%"], placeholders: [], isParameterIndexingValid: true },
"Doubled escapes"
);

t.deepEqual(
parse("%%"), { literals: ["%"], placeholders: [], isParameterIndexingValid: true },
"Just escape"
);
t.end();
Expand Down

0 comments on commit 324777a

Please sign in to comment.