Skip to content

Commit

Permalink
switch from CommonJS to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jun 27, 2024
1 parent 22ba5cd commit 1cda612
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
19 changes: 12 additions & 7 deletions bench/basic.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const earcut = require('../src/earcut');
import {earcut, flatten} from '../src/earcut.js';
import {readFileSync} from 'fs';

const {vertices, holes} = earcut.flatten(require('../test/fixtures/water.json'));
const data = JSON.parse(readFileSync(new URL('../test/fixtures/building.json', import.meta.url)));
const {vertices, holes} = flatten(data);

let start = Date.now(),
ops = 0;
let start = performance.now();
let ops = 0;
let passed = 0;

while (Date.now() - start < 1000) {
do {
earcut(vertices, holes);

ops++;
}
passed = performance.now() - start;
} while (passed < 1000);

console.log(Math.round(ops * 1000 / (Date.now() - start)) + ' ops/s');
console.log(`${Math.round(ops * 1000 / passed).toLocaleString()} ops/s`);
18 changes: 11 additions & 7 deletions bench/bench.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const earcut = require('../src/earcut');
import {earcut, flatten} from '../src/earcut.js';
import Benchmark from 'benchmark';
import {readFileSync} from 'fs';

function withoutHoles({vertices, holes, dimensions}) {
return {
Expand All @@ -7,15 +9,17 @@ function withoutHoles({vertices, holes, dimensions}) {
};
}

function getFixture(name) {
return flatten(JSON.parse(readFileSync(new URL(`../test/fixtures/${name}`, import.meta.url))));
}

const samples = {
'typical OSM building': earcut.flatten(require('../test/fixtures/building.json')),
'dude shape': withoutHoles(earcut.flatten(require('../test/fixtures/dude.json'))),
'dude shape with holes': earcut.flatten(require('../test/fixtures/dude.json')),
'complex OSM water': earcut.flatten(require('../test/fixtures/water.json'))
'typical OSM building': getFixture('building.json'),
'dude shape': withoutHoles(getFixture('dude.json')),
'dude shape with holes': getFixture('dude.json'),
'complex OSM water': getFixture('water.json')
};

const Benchmark = require('benchmark');

for (const name in samples) {
const {vertices, holes, dimensions} = samples[name];
new Benchmark.Suite()
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"version": "2.2.4",
"description": "The fastest and smallest JavaScript polygon triangulation library for your WebGL apps",
"main": "src/earcut.js",
"type": "module",
"exports": "./src/earcut.js",
"unpkg": "dist/earcut.min.js",
"jsdelivr": "dist/earcut.min.js",
"files": [
"src/earcut.js",
"dist/earcut.min.js",
"dist/earcut.dev.js"
],
Expand Down Expand Up @@ -34,7 +37,11 @@
"watchify": "^4.0.0"
},
"eslintConfig": {
"extends": "mourner"
"extends": "mourner",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
}
},
"repository": {
"type": "git",
Expand Down
14 changes: 5 additions & 9 deletions src/earcut.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

module.exports = earcut;
module.exports.default = earcut;

function earcut(data, holeIndices, dim) {
export function earcut(data, holeIndices, dim) {

dim = dim || 2;

Expand Down Expand Up @@ -626,7 +622,7 @@ function Node(i, x, y) {

// return a percentage difference between the polygon area and its triangulation area;
// used to verify correctness of triangulation
earcut.deviation = function (data, holeIndices, dim, triangles) {
export function deviation(data, holeIndices, dim, triangles) {
var hasHoles = holeIndices && holeIndices.length;
var outerLen = hasHoles ? holeIndices[0] * dim : data.length;

Expand All @@ -651,7 +647,7 @@ earcut.deviation = function (data, holeIndices, dim, triangles) {

return polygonArea === 0 && trianglesArea === 0 ? 0 :
Math.abs((trianglesArea - polygonArea) / polygonArea);
};
}

function signedArea(data, start, end, dim) {
var sum = 0;
Expand All @@ -663,7 +659,7 @@ function signedArea(data, start, end, dim) {
}

// turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
earcut.flatten = function (data) {
export function flatten(data) {
var dim = data[0][0].length,
result = {vertices: [], holes: [], dimensions: dim},
holeIndex = 0;
Expand All @@ -678,4 +674,4 @@ earcut.flatten = function (data) {
}
}
return result;
};
}
19 changes: 9 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

var test = require('tape'),
earcut = require('../src/earcut'),
fs = require('fs'),
path = require('path'),
expected = require('./expected.json');
import test from 'tape';
import {earcut, flatten, deviation} from '../src/earcut.js';
import fs from 'fs';

const expected = JSON.parse(fs.readFileSync(new URL('expected.json', import.meta.url)));

test('indices-2d', function (t) {
var indices = earcut([10, 0, 0, 50, 60, 60, 70, 10]);
Expand All @@ -26,18 +25,18 @@ test('empty', function (t) {
Object.keys(expected.triangles).forEach(function (id) {

test(id, function (t) {
var data = earcut.flatten(JSON.parse(fs.readFileSync(path.join(__dirname, '/fixtures/' + id + '.json')))),
var data = flatten(JSON.parse(fs.readFileSync(new URL(`fixtures/${id}.json`, import.meta.url)))),
indices = earcut(data.vertices, data.holes, data.dimensions),
deviation = earcut.deviation(data.vertices, data.holes, data.dimensions, indices),
err = deviation(data.vertices, data.holes, data.dimensions, indices),
expectedTriangles = expected.triangles[id],
expectedDeviation = expected.errors[id] || 0;

var numTriangles = indices.length / 3;
t.ok(numTriangles === expectedTriangles, numTriangles + ' triangles when expected ' + expectedTriangles);

if (expectedTriangles > 0) {
t.ok(deviation <= expectedDeviation,
'deviation ' + deviation + ' <= ' + expectedDeviation);
t.ok(err <= expectedDeviation,
'deviation ' + err + ' <= ' + expectedDeviation);
}

t.end();
Expand Down

0 comments on commit 1cda612

Please sign in to comment.