Skip to content

Commit

Permalink
Merge 0577c93 into 8894cd9
Browse files Browse the repository at this point in the history
  • Loading branch information
ekilah committed Aug 25, 2020
2 parents 8894cd9 + 0577c93 commit bc946f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/parsimmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,17 @@ function formatError(input, error) {
}

function flags(re) {
var s = "" + re;
return s.slice(s.lastIndexOf("/") + 1);
if (re.flags !== undefined) {
return re.flags;
}
// legacy browser support
return [
re.global ? "g" : "",
re.ignoreCase ? "i" : "",
re.multiline ? "m" : "",
re.unicode ? "u" : "",
re.sticky ? "y" : ""
].join("");
}

function anchoredRegexp(re) {
Expand Down Expand Up @@ -1356,6 +1365,7 @@ Parsimmon.empty = empty;
Parsimmon.end = end;
Parsimmon.eof = eof;
Parsimmon.fail = fail;
Parsimmon.flags = flags;
Parsimmon.formatError = formatError;
Parsimmon.index = index;
Parsimmon.isParser = isParser;
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"Parsimmon": true,
"assert": true,
"testSetScenario": true
},
"rules": {
"no-invalid-regexp": ["error", {"allowConstructorFlags": ["u", "y"]}]
}
}
35 changes: 35 additions & 0 deletions test/core/flags.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

testSetScenario(function() {
describe("Parsimmon.flags()", function() {
it("works in modern browsers", function() {
// eslint-disable-next-line no-invalid-regexp
var flags = Parsimmon.flags(new RegExp("a", "gimuy"));
assert.strictEqual(flags, "gimuy");
});

it("works on legacy browsers without Regexp.flags property with flags", function() {
// eslint-disable-next-line no-invalid-regexp
var oldRegExp = new RegExp("a", "gimuy");

// Simulate old RegExp without the flags property
Object.defineProperty(oldRegExp, "flags", { value: undefined });
assert.strictEqual(oldRegExp.flags, undefined);

var flags = Parsimmon.flags(oldRegExp);
assert.strictEqual(flags, "gimuy");
});

it("works on legacy browsers without Regexp.flags property without flags", function() {
// eslint-disable-next-line no-invalid-regexp
var oldRegExp = new RegExp("a", "");

// Simulate old RegExp without the flags property
Object.defineProperty(oldRegExp, "flags", { value: undefined });
assert.strictEqual(oldRegExp.flags, undefined);

var flags = Parsimmon.flags(oldRegExp);
assert.strictEqual(flags, "");
});
});
});

0 comments on commit bc946f4

Please sign in to comment.