Skip to content

Commit

Permalink
following d3/d3-format#112
Browse files Browse the repository at this point in the history
* adopt type="module"
* modernize tooling
* change default branch to main
* license ISC
  • Loading branch information
Fil committed May 29, 2021
1 parent b2fad5c commit 49590cc
Show file tree
Hide file tree
Showing 10 changed files with 821 additions and 757 deletions.
9 changes: 0 additions & 9 deletions .eslintrc.json
Expand Up @@ -3,14 +3,5 @@
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true,
"browser": true
},
"rules": {
"no-cond-assign": 0,
"no-constant-condition": 0
}
}
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
@@ -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
13 changes: 13 additions & 0 deletions LICENSE
@@ -0,0 +1,13 @@
Copyright 2013-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.
14 changes: 0 additions & 14 deletions LICENSE.txt

This file was deleted.

16 changes: 12 additions & 4 deletions README.md
Expand Up @@ -52,13 +52,21 @@ versor.rotation(q01); // [90, 0, 90]

```

In a browser:
If you use npm, `npm install versor`. You can also download the [latest release on GitHub](https://github.com/d3/versor/releases/latest). For vanilla HTML in modern browsers, import versor from Skypack:

```html
<!DOCTYPE html>
<script src="https://unpkg.com/versor"></script>
<script>
<script type="module">
import versor from "https://cdn.skypack.dev/versor@0.2";
const t = versor([90,0,0]);
</script>
```

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

```html
<script src="https://cdn.jsdelivr.net/npm/versor@0.2"></script>
<script>
versor([90,0,0]);
</script>
Expand Down
50 changes: 28 additions & 22 deletions package.json
@@ -1,39 +1,45 @@
{
"name": "versor",
"version": "0.1.2",
"version": "0.2.0",
"description": "Versor",
"homepage": "https://github.com/Fil/versor",
"repository": {
"type": "git",
"url": "https://github.com/Fil/versor.git"
},
"keywords": [
"d3",
"d3-module",
"d3-geo",
"versor"
],
"license": "GPL-3.0",
"main": "dist/versor.js",
"unpkg": "dist/versor.min.js",
"jsdelivr": "dist/versor.min.js",
"module": "src/index.js",
"jsnext:main": "src/index.js",
"homepage": "https://github.com/Fil/versor",
"repository": {
"type": "git",
"url": "https://github.com/Fil/versor.git"
},
"license": "ISC",
"author": {
"name": "Mike Bostock",
"url": "https://github.com/mbostock"
},
"scripts": {
"pretest": "rollup -c",
"test": "tape 'test/**/*-test.js' && eslint src",
"prepublishOnly": "rm -rf dist && yarn test",
"postpublish": "zip -j dist/versor.zip -- LICENSE.txt README.md dist/versor.js dist/versor.min.js"
"type": "module",
"module": "src/index.js",
"main": "src/index.js",
"jsdelivr": "dist/versor.min.js",
"unpkg": "dist/versor.min.js",
"exports": {
"umd": "./dist/versor.min.js",
"default": "./src/index.js"
},
"dependencies": {},
"devDependencies": {
"eslint": "6",
"rollup": "1",
"rollup-plugin-terser": "5",
"tape": "4"
}
"eslint": "7",
"mocha": "8",
"rollup": "2",
"rollup-plugin-terser": "5"
},
"scripts": {
"test": "mocha 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test && rollup -c"
},
"engines": {
"node": ">=12"
},
"jsnext:main": "src/index.js"
}
10 changes: 5 additions & 5 deletions src/index.js
@@ -1,4 +1,4 @@
var acos = Math.acos,
const acos = Math.acos,
asin = Math.asin,
atan2 = Math.atan2,
cos = Math.cos,
Expand Down Expand Up @@ -85,7 +85,7 @@ class Versor {
*/

// Returns the unit quaternion for the given Euler rotation angles [λ, φ, γ].
var versor = Versor.fromAngles;
const versor = Versor.fromAngles;

// Returns the quaternion that represents q0 * q1.
versor.multiply = Versor.multiply;
Expand All @@ -98,7 +98,7 @@ versor.interpolate = Versor.interpolateAngles;

// Returns Cartesian coordinates [x, y, z] given spherical coordinates [λ, φ].
versor.cartesian = function(e) {
var l = e[0] * radians, p = e[1] * radians, cp = cos(p);
const l = e[0] * radians, p = e[1] * radians, cp = cos(p);
return [cp * cos(l), cp * sin(l), sin(p)];
};

Expand All @@ -116,9 +116,9 @@ versor.delta = function(v0, v1, alpha) {
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
}

var w = cross(v0, v1), l = sqrt(dot(w, w));
const w = cross(v0, v1), l = sqrt(dot(w, w));
if (!l) return [1, 0, 0, 0];
var t = alpha * acos(max(-1, min(1, dot(v0, v1)))) / 2, s = sin(t); // t = θ / 2
const t = alpha * acos(max(-1, min(1, dot(v0, v1)))) / 2, s = sin(t); // t = θ / 2
return [cos(t), w[2] / l * s, -w[1] / l * s, w[0] / l * s];
};

Expand Down
10 changes: 10 additions & 0 deletions test/.eslintrc.json
@@ -0,0 +1,10 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"mocha": true
}
}
53 changes: 24 additions & 29 deletions test/versor-test.js
@@ -1,52 +1,47 @@
var tape = require("tape");
var versor = require("../");
import assert from "assert";
import versor from "../src/index.js";

function distance2 (a,b) {
return a
.map(function(x,i) { return (x - b[i]) * (x - b[i]); })
.reduce(function(a,b) { return a + b; }, 0);
}

tape("versor offers functions", function(test) {
test.equal(typeof versor, 'function');
test.equal(typeof versor.cartesian, 'function');
test.equal(typeof versor.rotation, 'function');
test.equal(typeof versor.delta, 'function');
test.end();
it("versor offers functions", () => {
assert.strictEqual(typeof versor, 'function');
assert.strictEqual(typeof versor.cartesian, 'function');
assert.strictEqual(typeof versor.rotation, 'function');
assert.strictEqual(typeof versor.delta, 'function');
});

tape("90° versors", function(test) {
test.ok(distance2(versor([ 90, 0, 0 ]), [ Math.sqrt(2)/2, Math.sqrt(2)/2, 0, 0 ]) < 1e-8);
test.ok(distance2(versor([ -90, 0, 0 ]), [ Math.sqrt(2)/2, -Math.sqrt(2)/2, 0, 0 ]) < 1e-8);
test.ok(distance2(versor([ 0, 90, 0 ]), [ Math.sqrt(2)/2, 0, Math.sqrt(2)/2, 0 ]) < 1e-8);
test.ok(distance2(versor([ 0, -90, 0 ]), [ Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, 0 ]) < 1e-8);
test.ok(distance2(versor([ 0, 0, 90 ]), [ Math.sqrt(2)/2, 0, 0, Math.sqrt(2)/2 ]) < 1e-8);
test.ok(distance2(versor([ 0, 0, -90 ]), [ Math.sqrt(2)/2, 0, 0, -Math.sqrt(2)/2 ]) < 1e-8);
test.end();
it("90° versors", () => {
assert(distance2(versor([ 90, 0, 0 ]), [ Math.sqrt(2)/2, Math.sqrt(2)/2, 0, 0 ]) < 1e-8);
assert(distance2(versor([ -90, 0, 0 ]), [ Math.sqrt(2)/2, -Math.sqrt(2)/2, 0, 0 ]) < 1e-8);
assert(distance2(versor([ 0, 90, 0 ]), [ Math.sqrt(2)/2, 0, Math.sqrt(2)/2, 0 ]) < 1e-8);
assert(distance2(versor([ 0, -90, 0 ]), [ Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, 0 ]) < 1e-8);
assert(distance2(versor([ 0, 0, 90 ]), [ Math.sqrt(2)/2, 0, 0, Math.sqrt(2)/2 ]) < 1e-8);
assert(distance2(versor([ 0, 0, -90 ]), [ Math.sqrt(2)/2, 0, 0, -Math.sqrt(2)/2 ]) < 1e-8);
});


tape("delta for large angles https://github.com/Fil/versor/issues/8", function(test){
test.ok(distance2(versor.delta([0.5963903847916048, -0.48599667744373487, -0.6388471949079623], [0.2185627727412969, 0.8848373533648707, -0.4114525154399803]), [ 0.6939654197615048,
it("delta for large angles https://github.com/Fil/versor/issues/8", function(){
assert(distance2(versor.delta([0.5963903847916048, -0.48599667744373487, -0.6388471949079623], [0.2185627727412969, 0.8848373533648707, -0.4114525154399803]), [ 0.6939654197615048,
0.45674413520077356,
-0.07619840032229953,
0.551353421067883 ]) < 1e-8);
test.end();
});

tape("delta", function(test) {
var a = versor.cartesian([ 0, 0 ]), b = versor.cartesian([ 90, 0 ]);
test.ok(distance2(versor.delta(a,b), [ 0.7071, 0.7071, 0, 0 ]) < 1e-8);
it("delta", () => {
let a = versor.cartesian([ 0, 0 ]), b = versor.cartesian([ 90, 0 ]);
assert(distance2(versor.delta(a,b), [ 0.7071, 0.7071, 0, 0 ]) < 1e-8);
b = versor.cartesian([ 0, 90 ]);
test.ok(distance2(versor.delta(a,b), [ 0.7071, 0, 0.7071, 0 ]) < 1e-8);
assert(distance2(versor.delta(a,b), [ 0.7071, 0, 0.7071, 0 ]) < 1e-8);

a = versor.cartesian([ 0, 0 ]), b = versor.cartesian([ 180, 0 ]);
test.ok(distance2(versor.delta(a,b), [ 0, 1, 0, 0 ]) < 1e-8);
test.end();
assert(distance2(versor.delta(a,b), [ 0, 1, 0, 0 ]) < 1e-8);
});

tape("delta tweening", function(test) {
var a = versor.cartesian([ 0, 0 ]), b = versor.cartesian([ 90, 0 ]);
test.ok(distance2(versor.delta(a,b,0.5), [ 0.9239, 0.3827, 0, 0 ]) < 1e-8);
test.end();
it("delta tweening", () => {
const a = versor.cartesian([ 0, 0 ]), b = versor.cartesian([ 90, 0 ]);
assert(distance2(versor.delta(a,b,0.5), [ 0.9239, 0.3827, 0, 0 ]) < 1e-8);
});

0 comments on commit 49590cc

Please sign in to comment.