Skip to content

Commit 47f9564

Browse files
Filmbostock
andauthored
Adopt type=module (#93)
* Adopt type=module follow changes in d3-format: * type=module * add exports * remove zip * license: ISC * update dependencies * es6 rather than no-undef * remove Sublime project * add eslint.json * update dependencies * cleaner imports * Update README Co-authored-by: Mike Bostock <mbostock@gmail.com>
1 parent e8cddfd commit 47f9564

33 files changed

Lines changed: 1598 additions & 1589 deletions

.eslintrc.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"ecmaVersion": 8
66
},
77
"env": {
8-
"es6": true,
9-
"node": true,
10-
"browser": true
8+
"es6": true
119
},
1210
"rules": {
1311
"no-cond-assign": 0

.github/eslint.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "eslint-compact",
5+
"pattern": [
6+
{
7+
"regexp": "^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(Error|Warning|Info)\\s-\\s(.+)\\s\\((.+)\\)$",
8+
"file": 1,
9+
"line": 2,
10+
"column": 3,
11+
"severity": 4,
12+
"message": 5,
13+
"code": 6
14+
}
15+
]
16+
}
17+
]
18+
}

.github/workflows/node.js.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
2+
3+
name: Node.js CI
4+
5+
on:
6+
push:
7+
branches: [ main ]
8+
pull_request:
9+
branches: [ main ]
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
node-version: [14.x]
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- run: yarn --frozen-lockfile
27+
- run: |
28+
echo ::add-matcher::.github/eslint.json
29+
yarn run eslint src test --format=compact
30+
- run: yarn test

LICENSE

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
Copyright 2010-2016 Mike Bostock
2-
All rights reserved.
3-
4-
Redistribution and use in source and binary forms, with or without modification,
5-
are permitted provided that the following conditions are met:
6-
7-
* Redistributions of source code must retain the above copyright notice, this
8-
list of conditions and the following disclaimer.
9-
10-
* Redistributions in binary form must reproduce the above copyright notice,
11-
this list of conditions and the following disclaimer in the documentation
12-
and/or other materials provided with the distribution.
13-
14-
* Neither the name of the author nor the names of contributors may be used to
15-
endorse or promote products derived from this software without specific prior
16-
written permission.
17-
18-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25-
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1+
Copyright 2010-2021 Mike Bostock
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose
4+
with or without fee is hereby granted, provided that the above copyright notice
5+
and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13+
THIS SOFTWARE.

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This module provides a variety of interpolation methods for blending between two values. Values may be numbers, colors, strings, arrays, or even deeply-nested objects. For example:
44

55
```js
6-
var i = d3.interpolateNumber(10, 20);
6+
const i = d3.interpolateNumber(10, 20);
77
i(0.0); // 10
88
i(0.2); // 12
99
i(0.5); // 15
@@ -21,7 +21,7 @@ d3.interpolateLab("steelblue", "brown")(0.5); // "rgb(142, 92, 109)"
2121
Here’s a more elaborate example demonstrating type inference used by [interpolate](#interpolate):
2222

2323
```js
24-
var i = d3.interpolate({colors: ["red", "blue"]}, {colors: ["white", "black"]});
24+
const i = d3.interpolate({colors: ["red", "blue"]}, {colors: ["white", "black"]});
2525
i(0.0); // {colors: ["rgb(255, 0, 0)", "rgb(0, 0, 255)"]}
2626
i(0.5); // {colors: ["rgb(255, 128, 128)", "rgb(0, 0, 128)"]}
2727
i(1.0); // {colors: ["rgb(255, 255, 255)", "rgb(0, 0, 0)"]}
@@ -31,16 +31,26 @@ Note that the generic value interpolator detects not only nested objects and arr
3131

3232
## Installing
3333

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

36-
In vanilla, a `d3` global is exported. (If using [color interpolation](#color-spaces), also load [d3-color](https://github.com/d3/d3-color).)
36+
```html
37+
<script type="module">
38+
39+
import {interpolateRgb} from "https://cdn.skypack.dev/d3-interpolate@3";
40+
41+
const interpolate = interpolateRgb("steelblue", "brown");
42+
43+
</script>
44+
```
45+
46+
For legacy environments, you can load d3-interpolate’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported. (If using [color interpolation](#color-spaces), also load [d3-color](https://github.com/d3/d3-color).)
3747

3848
```html
39-
<script src="https://d3js.org/d3-color.v2.min.js"></script>
40-
<script src="https://d3js.org/d3-interpolate.v2.min.js"></script>
49+
<script src="https://cdn.jsdelivr.net/npm/d3-color@3"></script>
50+
<script src="https://cdn.jsdelivr.net/npm/d3-interpolate@3"></script>
4151
<script>
4252
43-
var interpolate = d3.interpolateRgb("steelblue", "brown");
53+
const interpolate = d3.interpolateRgb("steelblue", "brown");
4454
4555
</script>
4656
```
@@ -224,7 +234,7 @@ Like [interpolateCubehelix](#interpolateCubehelix), but does not use the shortes
224234
Given that *interpolate* is one of [interpolateRgb](#interpolateRgb), [interpolateCubehelix](#interpolateCubehelix) or [interpolateCubehelixLong](#interpolateCubehelixLong), returns a new interpolator factory of the same type using the specified *gamma*. For example, to interpolate from purple to orange with a gamma of 2.2 in RGB space:
225235

226236
```js
227-
var interpolator = d3.interpolateRgb.gamma(2.2)("purple", "orange");
237+
const interpolator = d3.interpolateRgb.gamma(2.2)("purple", "orange");
228238
```
229239

230240
See Eric Brasseur’s article, [Gamma error in picture scaling](http://www.ericbrasseur.org/gamma.html), for more on gamma correction.
@@ -252,7 +262,7 @@ Returns a uniform nonrational B-spline interpolator through the specified array
252262
Returns a piecewise interpolator, composing interpolators for each adjacent pair of *values*. The returned interpolator maps *t* in [0, 1 / (*n* - 1)] to *interpolate*(*values*[0], *values*[1]), *t* in [1 / (*n* - 1), 2 / (*n* - 1)] to *interpolate*(*values*[1], *values*[2]), and so on, where *n* = *values*.length. In effect, this is a lightweight [linear scale](https://github.com/d3/d3-scale/blob/master/README.md#linear-scales). For example, to blend through red, green and blue:
253263

254264
```js
255-
var interpolate = d3.piecewise(d3.interpolateRgb.gamma(2.2), ["red", "green", "blue"]);
265+
const interpolate = d3.piecewise(d3.interpolateRgb.gamma(2.2), ["red", "green", "blue"]);
256266
```
257267

258268
If *interpolate* is not specified, defaults to [d3.interpolate](#interpolate).

d3-interpolate.sublime-project

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,52 @@
22
"name": "d3-interpolate",
33
"version": "2.0.1",
44
"description": "Interpolate numbers, colors, strings, arrays, objects, whatever!",
5+
"homepage": "https://d3js.org/d3-interpolate/",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/d3/d3-interpolate.git"
9+
},
510
"keywords": [
611
"d3",
712
"d3-module",
813
"interpolate",
914
"interpolation",
1015
"color"
1116
],
12-
"homepage": "https://d3js.org/d3-interpolate/",
13-
"license": "BSD-3-Clause",
17+
"license": "ISC",
1418
"author": {
1519
"name": "Mike Bostock",
1620
"url": "http://bost.ocks.org/mike"
1721
},
18-
"main": "dist/d3-interpolate.js",
19-
"unpkg": "dist/d3-interpolate.min.js",
20-
"jsdelivr": "dist/d3-interpolate.min.js",
21-
"module": "src/index.js",
22-
"repository": {
23-
"type": "git",
24-
"url": "https://github.com/d3/d3-interpolate.git"
25-
},
22+
"type": "module",
2623
"files": [
2724
"dist/**/*.js",
2825
"src/**/*.js"
2926
],
30-
"scripts": {
31-
"pretest": "rollup -c",
32-
"test": "tape 'test/**/*-test.js' && eslint src",
33-
"prepublishOnly": "rm -rf dist && yarn test",
34-
"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"
27+
"module": "src/index.js",
28+
"main": "src/index.js",
29+
"jsdelivr": "dist/d3-interpolate.min.js",
30+
"unpkg": "dist/d3-interpolate.min.js",
31+
"exports": {
32+
"umd": "./dist/d3-interpolate.min.js",
33+
"default": "./src/index.js"
3534
},
35+
"sideEffects": false,
3636
"dependencies": {
37-
"d3-color": "1 - 2"
37+
"d3-color": "1 - 3"
3838
},
39-
"sideEffects": false,
4039
"devDependencies": {
41-
"eslint": "6",
42-
"rollup": "1",
43-
"rollup-plugin-terser": "5",
44-
"tape": "4"
40+
"eslint": "7",
41+
"mocha": "8",
42+
"rollup": "2",
43+
"rollup-plugin-terser": "7"
44+
},
45+
"scripts": {
46+
"test": "mocha 'test/**/*-test.js' && eslint src test",
47+
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
48+
"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 -"
49+
},
50+
"engines": {
51+
"node": ">=12"
4552
}
4653
}

test/.eslintrc.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 8
6+
},
7+
"env": {
8+
"es6": true,
9+
"mocha": true
10+
},
11+
"rules": {
12+
"no-cond-assign": 0
13+
}
14+
}

test/array-test.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
1-
var tape = require("tape"),
2-
interpolate = require("../");
1+
import assert from "assert";
2+
import {interpolateArray} from "../src/index.js";
33

4-
tape("interpolateArray(a, b) interpolates defined elements in a and b", function(test) {
5-
test.deepEqual(interpolate.interpolateArray([2, 12], [4, 24])(0.5), [3, 18]);
6-
test.end();
4+
it("interpolateArray(a, b) interpolates defined elements in a and b", () => {
5+
assert.deepStrictEqual(interpolateArray([2, 12], [4, 24])(0.5), [3, 18]);
76
});
87

9-
tape("interpolateArray(a, b) interpolates nested objects and arrays", function(test) {
10-
test.deepEqual(interpolate.interpolateArray([[2, 12]], [[4, 24]])(0.5), [[3, 18]]);
11-
test.deepEqual(interpolate.interpolateArray([{foo: [2, 12]}], [{foo: [4, 24]}])(0.5), [{foo: [3, 18]}]);
12-
test.end();
8+
it("interpolateArray(a, b) interpolates nested objects and arrays", () => {
9+
assert.deepStrictEqual(interpolateArray([[2, 12]], [[4, 24]])(0.5), [[3, 18]]);
10+
assert.deepStrictEqual(interpolateArray([{foo: [2, 12]}], [{foo: [4, 24]}])(0.5), [{foo: [3, 18]}]);
1311
});
1412

15-
tape("interpolateArray(a, b) ignores elements in a that are not in b", function(test) {
16-
test.deepEqual(interpolate.interpolateArray([2, 12, 12], [4, 24])(0.5), [3, 18]);
17-
test.end();
13+
it("interpolateArray(a, b) ignores elements in a that are not in b", () => {
14+
assert.deepStrictEqual(interpolateArray([2, 12, 12], [4, 24])(0.5), [3, 18]);
1815
});
1916

20-
tape("interpolateArray(a, b) uses constant elements in b that are not in a", function(test) {
21-
test.deepEqual(interpolate.interpolateArray([2, 12], [4, 24, 12])(0.5), [3, 18, 12]);
22-
test.end();
17+
it("interpolateArray(a, b) uses constant elements in b that are not in a", () => {
18+
assert.deepStrictEqual(interpolateArray([2, 12], [4, 24, 12])(0.5), [3, 18, 12]);
2319
});
2420

25-
tape("interpolateArray(a, b) treats undefined as an empty array", function(test) {
26-
test.deepEqual(interpolate.interpolateArray(undefined, [2, 12])(0.5), [2, 12]);
27-
test.deepEqual(interpolate.interpolateArray([2, 12], undefined)(0.5), []);
28-
test.deepEqual(interpolate.interpolateArray(undefined, undefined)(0.5), []);
29-
test.end();
21+
it("interpolateArray(a, b) treats undefined as an empty array", () => {
22+
assert.deepStrictEqual(interpolateArray(undefined, [2, 12])(0.5), [2, 12]);
23+
assert.deepStrictEqual(interpolateArray([2, 12], undefined)(0.5), []);
24+
assert.deepStrictEqual(interpolateArray(undefined, undefined)(0.5), []);
3025
});
3126

32-
tape("interpolateArray(a, b) interpolates array-like objects", function(test) {
33-
var array = new Float64Array(2),
34-
args = (function() { return arguments; })(2, 12);
27+
it("interpolateArray(a, b) interpolates array-like objects", () => {
28+
const array = new Float64Array(2);
29+
const args = (function() { return arguments; })(2, 12);
3530
array[0] = 2;
3631
array[1] = 12;
37-
test.deepEqual(interpolate.interpolateArray(array, [4, 24])(0.5), [3, 18]);
38-
test.deepEqual(interpolate.interpolateArray(args, [4, 24])(0.5), [3, 18]);
39-
test.end();
32+
assert.deepStrictEqual(interpolateArray(array, [4, 24])(0.5), [3, 18]);
33+
assert.deepStrictEqual(interpolateArray(args, [4, 24])(0.5), [3, 18]);
4034
});
4135

42-
tape("interpolateArray(a, b) gives exact ends for t=0 and t=1", function(test) {
43-
var a = [2e+42], b = [335];
44-
test.deepEqual(interpolate.interpolateArray(a, b)(1), b);
45-
test.deepEqual(interpolate.interpolateArray(a, b)(0), a);
46-
test.end();
36+
it("interpolateArray(a, b) gives exact ends for t=0 and t=1", () => {
37+
const a = [2e+42], b = [335];
38+
assert.deepStrictEqual(interpolateArray(a, b)(1), b);
39+
assert.deepStrictEqual(interpolateArray(a, b)(0), a);
4740
});

test/asserts.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import assert from "assert";
2+
3+
export function assertInDelta(actual, expected, delta = 1e-6) {
4+
assert(inDelta(actual, expected, delta), `${actual} should be within ${delta} of ${expected}`);
5+
}
6+
7+
function inDelta(actual, expected, delta) {
8+
return (Array.isArray(expected) ? inDeltaArray
9+
: typeof expected === "object" ? inDeltaObject
10+
: inDeltaNumber)(actual, expected, delta);
11+
}
12+
13+
function inDeltaArray(actual, expected, delta) {
14+
let n = expected.length, i = -1;
15+
if (actual.length !== n) return false;
16+
while (++i < n) if (!inDelta(actual[i], expected[i], delta)) return false;
17+
return true;
18+
}
19+
20+
function inDeltaObject(actual, expected, delta) {
21+
for (let i in expected) if (!inDelta(actual[i], expected[i], delta)) return false;
22+
for (let i in actual) if (!(i in expected)) return false;
23+
return true;
24+
}
25+
26+
function inDeltaNumber(actual, expected, delta) {
27+
return actual >= expected - delta && actual <= expected + delta;
28+
}

0 commit comments

Comments
 (0)