From 1cda612b129fcde8d7ba913cd9edf8e1c2e33399 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 27 Jun 2024 13:00:13 +0300 Subject: [PATCH] switch from CommonJS to ESM --- bench/basic.js | 19 ++++++++++++------- bench/bench.js | 18 +++++++++++------- package.json | 9 ++++++++- src/earcut.js | 14 +++++--------- test/test.js | 19 +++++++++---------- 5 files changed, 45 insertions(+), 34 deletions(-) diff --git a/bench/basic.js b/bench/basic.js index d0ec251..163a513 100644 --- a/bench/basic.js +++ b/bench/basic.js @@ -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`); diff --git a/bench/bench.js b/bench/bench.js index 3273a6e..1fbe520 100644 --- a/bench/bench.js +++ b/bench/bench.js @@ -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 { @@ -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() diff --git a/package.json b/package.json index 95bdb48..17949cd 100644 --- a/package.json +++ b/package.json @@ -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" ], @@ -34,7 +37,11 @@ "watchify": "^4.0.0" }, "eslintConfig": { - "extends": "mourner" + "extends": "mourner", + "parserOptions": { + "sourceType": "module", + "ecmaVersion": 2020 + } }, "repository": { "type": "git", diff --git a/src/earcut.js b/src/earcut.js index 61a4202..fba8c80 100644 --- a/src/earcut.js +++ b/src/earcut.js @@ -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; @@ -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; @@ -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; @@ -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; @@ -678,4 +674,4 @@ earcut.flatten = function (data) { } } return result; -}; +} diff --git a/test/test.js b/test/test.js index 66132d5..9533669 100644 --- a/test/test.js +++ b/test/test.js @@ -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]); @@ -26,9 +25,9 @@ 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; @@ -36,8 +35,8 @@ Object.keys(expected.triangles).forEach(function (id) { 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();