Skip to content

Commit

Permalink
Adopt type=module (#31)
Browse files Browse the repository at this point in the history
* Adopt type=module

follow changes in d3-format:
* type=module
* add exports
* remove zip
* license: ISC
* update dependencies

* remove Sublime project

* update dependencies

* update dependencies

* Update README

* cleaner imports

* add eslint.json

Co-authored-by: Mike Bostock <mbostock@gmail.com>
  • Loading branch information
Fil and mbostock authored Jun 4, 2021
1 parent 4bb4493 commit 8050da0
Show file tree
Hide file tree
Showing 14 changed files with 998 additions and 858 deletions.
8 changes: 0 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true,
"browser": true
},
"rules": {
"no-cond-assign": 0
}
}
18 changes: 18 additions & 0 deletions .github/eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"problemMatcher": [
{
"owner": "eslint-compact",
"pattern": [
{
"regexp": "^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(Error|Warning|Info)\\s-\\s(.+)\\s\\((.+)\\)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5,
"code": 6
}
]
}
]
}
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn --frozen-lockfile
- run: |
echo ::add-matcher::.github/eslint.json
yarn run eslint src test --format=compact
- run: yarn test
40 changes: 13 additions & 27 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
Copyright 2010-2016 Mike Bostock
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the author nor the names of contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Copyright 2010-2021 Mike Bostock

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,36 @@ This module provides a few basic geometric operations for two-dimensional polygo

## Installing

If you use NPM, `npm install d3-polygon`. Otherwise, download the [latest release](https://github.com/d3/d3-polygon/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-polygon.v1.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
If you use npm, `npm install d3-polygon`. You can also download the [latest release on GitHub](https://github.com/d3/d3-polygon/releases/latest). For vanilla HTML in modern browsers, import d3-polygon from Skypack:

```html
<script src="https://d3js.org/d3-polygon.v2.min.js"></script>
<script type="module">
import {polygonHull} from "https://cdn.skypack.dev/d3-polygon@3";
const hull = polygonHull(points);
</script>
```

For legacy environments, you can load d3-polygon’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:

```html
<script src="https://cdn.jsdelivr.net/npm/d3-polygon@3"></script>
<script>
var hull = d3.polygonHull(points);
const hull = d3.polygonHull(points);
</script>
```

## API Reference

<a href="#polygonArea" name="polygonArea">#</a> d3.<b>polygonArea</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/area.js#L1 "Source Code")
<a href="#polygonArea" name="polygonArea">#</a> d3.<b>polygonArea</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/area.js "Source Code")

Returns the signed area of the specified *polygon*. If the vertices of the polygon are in counterclockwise order (assuming a coordinate system where the origin ⟨0,0⟩ is in the top-left corner), the returned area is positive; otherwise it is negative, or zero.

<a href="#polygonCentroid" name="polygonCentroid">#</a> d3.<b>polygonCentroid</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/centroid.js#L1 "Source Code")
<a href="#polygonCentroid" name="polygonCentroid">#</a> d3.<b>polygonCentroid</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/centroid.js "Source Code")

Returns the [centroid](https://en.wikipedia.org/wiki/Centroid) of the specified *polygon*.

Expand All @@ -31,10 +43,10 @@ Returns the [centroid](https://en.wikipedia.org/wiki/Centroid) of the specified

Returns the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) of the specified *points* using [Andrew’s monotone chain algorithm](http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain). The returned hull is represented as an array containing a subset of the input *points* arranged in counterclockwise order. Returns null if *points* has fewer than three elements.

<a href="#polygonContains" name="polygonContains">#</a> d3.<b>polygonContains</b>(<i>polygon</i>, <i>point</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/contains.js#L1 "Source Code")
<a href="#polygonContains" name="polygonContains">#</a> d3.<b>polygonContains</b>(<i>polygon</i>, <i>point</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/contains.js "Source Code")

Returns true if and only if the specified *point* is inside the specified *polygon*.

<a href="#polygonLength" name="polygonLength">#</a> d3.<b>polygonLength</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/length.js#L1 "Source Code")
<a href="#polygonLength" name="polygonLength">#</a> d3.<b>polygonLength</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/length.js "Source Code")

Returns the length of the perimeter of the specified *polygon*.
17 changes: 0 additions & 17 deletions d3-polygon.sublime-project

This file was deleted.

45 changes: 26 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "d3-polygon",
"version": "2.0.0",
"description": "Operations for two-dimensional polygons.",
"homepage": "https://d3js.org/d3-polygon/",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-polygon.git"
},
"keywords": [
"d3",
"d3-module",
Expand All @@ -10,35 +15,37 @@
"geometry",
"graphics"
],
"homepage": "https://d3js.org/d3-polygon/",
"license": "BSD-3-Clause",
"license": "ISC",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "dist/d3-polygon.js",
"unpkg": "dist/d3-polygon.min.js",
"jsdelivr": "dist/d3-polygon.min.js",
"module": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-polygon.git"
},
"type": "module",
"files": [
"dist/**/*.js",
"src/**/*.js"
],
"scripts": {
"pretest": "rollup -c",
"test": "tape 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test",
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd - && zip -j dist/${npm_package_name}.zip -- LICENSE README.md dist/${npm_package_name}.js dist/${npm_package_name}.min.js"
"module": "src/index.js",
"main": "src/index.js",
"jsdelivr": "dist/d3-polygon.min.js",
"unpkg": "dist/d3-polygon.min.js",
"exports": {
"umd": "./dist/d3-polygon.min.js",
"default": "./src/index.js"
},
"sideEffects": false,
"devDependencies": {
"eslint": "6",
"rollup": "1",
"rollup-plugin-terser": "5",
"tape": "4"
"eslint": "7",
"mocha": "8",
"rollup": "2",
"rollup-plugin-terser": "7"
},
"scripts": {
"test": "mocha 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd -"
},
"engines": {
"node": ">=12"
}
}
10 changes: 10 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"mocha": true
}
}
48 changes: 21 additions & 27 deletions test/area-test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
var tape = require("tape"),
polygon = require("../");
import assert from "assert";
import {polygonArea} from "../src/index.js";

tape("polygonArea(polygon) returns the expected value for closed counterclockwise polygons", function(test) {
test.equal(polygon.polygonArea([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), 1);
test.end();
it("polygonArea(polygon) returns the expected value for closed counterclockwise polygons", () => {
assert.strictEqual(polygonArea([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), 1);
});

tape("polygonArea(polygon) returns the expected value for closed clockwise polygons", function(test) {
test.equal(polygon.polygonArea([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), -1);
test.equal(polygon.polygonArea([[1, 1], [3, 2], [2, 3], [1, 1]]), -1.5);
test.end();
it("polygonArea(polygon) returns the expected value for closed clockwise polygons", () => {
assert.strictEqual(polygonArea([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), -1);
assert.strictEqual(polygonArea([[1, 1], [3, 2], [2, 3], [1, 1]]), -1.5);
});

tape("polygonArea(polygon) returns the expected value for open counterclockwise polygons", function(test) {
test.equal(polygon.polygonArea([[0, 0], [0, 1], [1, 1], [1, 0]]), 1);
test.end();
it("polygonArea(polygon) returns the expected value for open counterclockwise polygons", () => {
assert.strictEqual(polygonArea([[0, 0], [0, 1], [1, 1], [1, 0]]), 1);
});

tape("polygonArea(polygon) returns the expected value for open clockwise polygons", function(test) {
test.equal(polygon.polygonArea([[0, 0], [1, 0], [1, 1], [0, 1]]), -1);
test.equal(polygon.polygonArea([[1, 1], [3, 2], [2, 3]]), -1.5);
test.end();
it("polygonArea(polygon) returns the expected value for open clockwise polygons", () => {
assert.strictEqual(polygonArea([[0, 0], [1, 0], [1, 1], [0, 1]]), -1);
assert.strictEqual(polygonArea([[1, 1], [3, 2], [2, 3]]), -1.5);
});

tape("polygonArea(polygon) returns the expected value for a very large polygon", function(test) {
var stop = 1e8,
step = 1e4,
points = [],
value;
for (value = 0; value < stop; value += step) points.push([0, value]);
for (value = 0; value < stop; value += step) points.push([value, stop]);
for (value = stop - step; value >= 0; value -= step) points.push([stop, value]);
for (value = stop - step; value >= 0; value -= step) points.push([value, 0]);
test.equal(polygon.polygonArea(points), 1e16 - 5e7);
test.end();
it("polygonArea(polygon) returns the expected value for a very large polygon", () => {
const stop = 1e8;
const step = 1e4;
const points = [];
for (let value = 0; value < stop; value += step) points.push([0, value]);
for (let value = 0; value < stop; value += step) points.push([value, stop]);
for (let value = stop - step; value >= 0; value -= step) points.push([stop, value]);
for (let value = stop - step; value >= 0; value -= step) points.push([value, 0]);
assert.strictEqual(polygonArea(points), 1e16 - 5e7);
});
48 changes: 21 additions & 27 deletions test/centroid-test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
var tape = require("tape"),
polygon = require("../");
import assert from "assert";
import {polygonCentroid} from "../src/index.js";

tape("polygonCentroid(points) returns the expected value for closed counterclockwise polygons", function(test) {
test.deepEqual(polygon.polygonCentroid([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), [.5, .5]);
test.end();
it("polygonCentroid(points) returns the expected value for closed counterclockwise polygons", () => {
assert.deepStrictEqual(polygonCentroid([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), [0.5, 0.5]);
});

tape("polygonCentroid(points) returns the expected value for closed clockwise polygons", function(test) {
test.deepEqual(polygon.polygonCentroid([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), [.5, .5]);
test.deepEqual(polygon.polygonCentroid([[1, 1], [3, 2], [2, 3], [1, 1]]), [2, 2]);
test.end();
it("polygonCentroid(points) returns the expected value for closed clockwise polygons", () => {
assert.deepStrictEqual(polygonCentroid([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), [0.5, 0.5]);
assert.deepStrictEqual(polygonCentroid([[1, 1], [3, 2], [2, 3], [1, 1]]), [2, 2]);
});

tape("polygonCentroid(points) returns the expected value for open counterclockwise polygons", function(test) {
test.deepEqual(polygon.polygonCentroid([[0, 0], [0, 1], [1, 1], [1, 0]]), [.5, .5]);
test.end();
it("polygonCentroid(points) returns the expected value for open counterclockwise polygons", () => {
assert.deepStrictEqual(polygonCentroid([[0, 0], [0, 1], [1, 1], [1, 0]]), [0.5, 0.5]);
});

tape("polygonCentroid(points) returns the expected value for open counterclockwise polygons", function(test) {
test.deepEqual(polygon.polygonCentroid([[0, 0], [1, 0], [1, 1], [0, 1]]), [.5, .5]);
test.deepEqual(polygon.polygonCentroid([[1, 1], [3, 2], [2, 3]]), [2, 2]);
test.end();
it("polygonCentroid(points) returns the expected value for open counterclockwise polygons", () => {
assert.deepStrictEqual(polygonCentroid([[0, 0], [1, 0], [1, 1], [0, 1]]), [0.5, 0.5]);
assert.deepStrictEqual(polygonCentroid([[1, 1], [3, 2], [2, 3]]), [2, 2]);
});

tape("polygonCentroid(polygon) returns the expected value for a very large polygon", function(test) {
var stop = 1e8,
step = 1e4,
points = [],
value;
for (value = 0; value < stop; value += step) points.push([0, value]);
for (value = 0; value < stop; value += step) points.push([value, stop]);
for (value = stop - step; value >= 0; value -= step) points.push([stop, value]);
for (value = stop - step; value >= 0; value -= step) points.push([value, 0]);
test.deepEqual(polygon.polygonCentroid(points), [49999999.75000187, 49999999.75001216]);
test.end();
it("polygonCentroid(polygon) returns the expected value for a very large polygon", () => {
const stop = 1e8;
const step = 1e4;
const points = [];
for (let value = 0; value < stop; value += step) points.push([0, value]);
for (let value = 0; value < stop; value += step) points.push([value, stop]);
for (let value = stop - step; value >= 0; value -= step) points.push([stop, value]);
for (let value = stop - step; value >= 0; value -= step) points.push([value, 0]);
assert.deepStrictEqual(polygonCentroid(points), [49999999.75000187, 49999999.75001216]);
});
Loading

0 comments on commit 8050da0

Please sign in to comment.