Skip to content

Commit

Permalink
Lazy detect Set, use internal flag in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthemsteger committed Jul 8, 2020
1 parent 08ec6fc commit 1514352
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
17 changes: 11 additions & 6 deletions src/parsimmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ function bufferExists() {
return typeof Buffer !== "undefined";
}

function setExists() {
if (Parsimmon._supportsSet !== undefined) {
return Parsimmon._supportsSet;
}
var exists = typeof Set !== "undefined";
Parsimmon._supportsSet = exists;
return exists;
}

function ensureBuffer() {
if (!bufferExists()) {
throw new Error(
Expand Down Expand Up @@ -379,9 +388,9 @@ function makeLineColumnIndex(input, i) {
function union(xs, ys) {
// for newer browsers/node we can improve performance by using
// modern JS
if (Parsimmon.Set && Array.from) {
if (setExists() && Array.from) {
// eslint-disable-next-line no-undef
var set = new Parsimmon.Set(xs);
var set = new Set(xs);
for (var y = 0; y < ys.length; y++) {
set.add(ys[y]);
}
Expand Down Expand Up @@ -1397,8 +1406,4 @@ Parsimmon.Binary = {
doubleLE: doubleLE()
};

// For use later, we set Set if it is available
// eslint-disable-next-line no-undef
Parsimmon.Set = typeof Set !== "undefined" ? Set : undefined;

module.exports = Parsimmon;
21 changes: 0 additions & 21 deletions test/core/startup.test.js

This file was deleted.

14 changes: 7 additions & 7 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ globalThis.equivalentParsers = function equivalentParsers(p1, p2, inputs) {

globalThis.testSetScenario = function testSetScenario(fn) {
describe("", function() {
var origSet = Parsimmon.Set;
after(function() {
Parsimmon.Set = origSet;
});

fn();

if (typeof Set !== "undefined") {
describe("no Set", function() {
beforeEach(function() {
Parsimmon.Set = undefined;
before(function() {
Parsimmon._supportsSet = false;
});

after(function() {
Parsimmon._supportsSet = undefined;
});

fn();
});
}
Expand Down

0 comments on commit 1514352

Please sign in to comment.