Skip to content

Commit

Permalink
Adopt type=module (#93)
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

* es6 rather than no-undef

* remove Sublime project

* add eslint.json

* update dependencies

* cleaner imports

* Update README

Co-authored-by: Mike Bostock <[email protected]>
  • Loading branch information
Fil and mbostock authored Jun 4, 2021
1 parent e8cddfd commit 47f9564
Show file tree
Hide file tree
Showing 33 changed files with 1,598 additions and 1,589 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true,
"browser": true
"es6": true
},
"rules": {
"no-cond-assign": 0
Expand Down
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.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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:

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

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

## Installing

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.
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:

In vanilla, a `d3` global is exported. (If using [color interpolation](#color-spaces), also load [d3-color](https://github.com/d3/d3-color).)
```html
<script type="module">
import {interpolateRgb} from "https://cdn.skypack.dev/d3-interpolate@3";
const interpolate = interpolateRgb("steelblue", "brown");
</script>
```

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).)

```html
<script src="https://d3js.org/d3-color.v2.min.js"></script>
<script src="https://d3js.org/d3-interpolate.v2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-color@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-interpolate@3"></script>
<script>
var interpolate = d3.interpolateRgb("steelblue", "brown");
const interpolate = d3.interpolateRgb("steelblue", "brown");
</script>
```
Expand Down Expand Up @@ -224,7 +234,7 @@ Like [interpolateCubehelix](#interpolateCubehelix), but does not use the shortes
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:

```js
var interpolator = d3.interpolateRgb.gamma(2.2)("purple", "orange");
const interpolator = d3.interpolateRgb.gamma(2.2)("purple", "orange");
```

See Eric Brasseur’s article, [Gamma error in picture scaling](http://www.ericbrasseur.org/gamma.html), for more on gamma correction.
Expand Down Expand Up @@ -252,7 +262,7 @@ Returns a uniform nonrational B-spline interpolator through the specified array
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:

```js
var interpolate = d3.piecewise(d3.interpolateRgb.gamma(2.2), ["red", "green", "blue"]);
const interpolate = d3.piecewise(d3.interpolateRgb.gamma(2.2), ["red", "green", "blue"]);
```

If *interpolate* is not specified, defaults to [d3.interpolate](#interpolate).
17 changes: 0 additions & 17 deletions d3-interpolate.sublime-project

This file was deleted.

49 changes: 28 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,52 @@
"name": "d3-interpolate",
"version": "2.0.1",
"description": "Interpolate numbers, colors, strings, arrays, objects, whatever!",
"homepage": "https://d3js.org/d3-interpolate/",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-interpolate.git"
},
"keywords": [
"d3",
"d3-module",
"interpolate",
"interpolation",
"color"
],
"homepage": "https://d3js.org/d3-interpolate/",
"license": "BSD-3-Clause",
"license": "ISC",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "dist/d3-interpolate.js",
"unpkg": "dist/d3-interpolate.min.js",
"jsdelivr": "dist/d3-interpolate.min.js",
"module": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-interpolate.git"
},
"type": "module",
"files": [
"dist/**/*.js",
"src/**/*.js"
],
"scripts": {
"pretest": "rollup -c",
"test": "tape 'test/**/*-test.js' && eslint src",
"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-interpolate.min.js",
"unpkg": "dist/d3-interpolate.min.js",
"exports": {
"umd": "./dist/d3-interpolate.min.js",
"default": "./src/index.js"
},
"sideEffects": false,
"dependencies": {
"d3-color": "1 - 2"
"d3-color": "1 - 3"
},
"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"
}
}
14 changes: 14 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"es6": true,
"mocha": true
},
"rules": {
"no-cond-assign": 0
}
}
55 changes: 24 additions & 31 deletions test/array-test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
var tape = require("tape"),
interpolate = require("../");
import assert from "assert";
import {interpolateArray} from "../src/index.js";

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

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

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

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

tape("interpolateArray(a, b) treats undefined as an empty array", function(test) {
test.deepEqual(interpolate.interpolateArray(undefined, [2, 12])(0.5), [2, 12]);
test.deepEqual(interpolate.interpolateArray([2, 12], undefined)(0.5), []);
test.deepEqual(interpolate.interpolateArray(undefined, undefined)(0.5), []);
test.end();
it("interpolateArray(a, b) treats undefined as an empty array", () => {
assert.deepStrictEqual(interpolateArray(undefined, [2, 12])(0.5), [2, 12]);
assert.deepStrictEqual(interpolateArray([2, 12], undefined)(0.5), []);
assert.deepStrictEqual(interpolateArray(undefined, undefined)(0.5), []);
});

tape("interpolateArray(a, b) interpolates array-like objects", function(test) {
var array = new Float64Array(2),
args = (function() { return arguments; })(2, 12);
it("interpolateArray(a, b) interpolates array-like objects", () => {
const array = new Float64Array(2);
const args = (function() { return arguments; })(2, 12);
array[0] = 2;
array[1] = 12;
test.deepEqual(interpolate.interpolateArray(array, [4, 24])(0.5), [3, 18]);
test.deepEqual(interpolate.interpolateArray(args, [4, 24])(0.5), [3, 18]);
test.end();
assert.deepStrictEqual(interpolateArray(array, [4, 24])(0.5), [3, 18]);
assert.deepStrictEqual(interpolateArray(args, [4, 24])(0.5), [3, 18]);
});

tape("interpolateArray(a, b) gives exact ends for t=0 and t=1", function(test) {
var a = [2e+42], b = [335];
test.deepEqual(interpolate.interpolateArray(a, b)(1), b);
test.deepEqual(interpolate.interpolateArray(a, b)(0), a);
test.end();
it("interpolateArray(a, b) gives exact ends for t=0 and t=1", () => {
const a = [2e+42], b = [335];
assert.deepStrictEqual(interpolateArray(a, b)(1), b);
assert.deepStrictEqual(interpolateArray(a, b)(0), a);
});
28 changes: 28 additions & 0 deletions test/asserts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import assert from "assert";

export function assertInDelta(actual, expected, delta = 1e-6) {
assert(inDelta(actual, expected, delta), `${actual} should be within ${delta} of ${expected}`);
}

function inDelta(actual, expected, delta) {
return (Array.isArray(expected) ? inDeltaArray
: typeof expected === "object" ? inDeltaObject
: inDeltaNumber)(actual, expected, delta);
}

function inDeltaArray(actual, expected, delta) {
let n = expected.length, i = -1;
if (actual.length !== n) return false;
while (++i < n) if (!inDelta(actual[i], expected[i], delta)) return false;
return true;
}

function inDeltaObject(actual, expected, delta) {
for (let i in expected) if (!inDelta(actual[i], expected[i], delta)) return false;
for (let i in actual) if (!(i in expected)) return false;
return true;
}

function inDeltaNumber(actual, expected, delta) {
return actual >= expected - delta && actual <= expected + delta;
}
Loading

0 comments on commit 47f9564

Please sign in to comment.