Skip to content

Commit

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

* update README

* assert message

* Update README

* Update README

* yarn upgrade

* cleaner imports

* cleaner imports

* cleaner imports

Co-authored-by: Mike Bostock <mbostock@gmail.com>
  • Loading branch information
Fil and mbostock committed Jun 4, 2021
1 parent a4c996f commit e5bd86e
Show file tree
Hide file tree
Showing 31 changed files with 1,319 additions and 1,362 deletions.
7 changes: 1 addition & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true,
"browser": true
},
"rules": {
"no-cond-assign": 0
"es6": true
}
}
6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ name: Node.js CI

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

jobs:
build:
Expand All @@ -15,7 +15,7 @@ jobs:

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

steps:
- uses: actions/checkout@v2
Expand Down
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.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ See the [d3-random collection on Observable](https://observablehq.com/collection

## Installing

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

```html
<script src="https://d3js.org/d3-random.v2.min.js"></script>
<script type="module">
import {randomUniform} from "https://cdn.skypack.dev/d3-random@3";
const random = randomUniform(1, 10);
</script>
```

For legacy environments, you can load d3-random’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-random@3"></script>
<script>
var random = d3.randomUniform(1, 10);
const random = d3.randomUniform(1, 10);
</script>
```
Expand Down Expand Up @@ -108,9 +120,10 @@ Returns a function for generating random numbers with a [Poisson distribution](h
Returns the same type of function for generating random numbers but where the given random number generator *source* is used as the source of randomness instead of Math.random. The given random number generator must implement the same interface as Math.random and only return values in the range [0, 1). This is useful when a seeded random number generator is preferable to Math.random. For example:

```js
const d3 = require("d3-random");
import {randomLcg, randomNumber} from "d3-random";

const seed = 0.44871573888282423; // any number in [0, 1)
const random = d3.randomNormal.source(d3.randomLcg(seed))(0, 1);
const random = randomNormal.source(randomLcg(seed))(0, 1);

random(); // -0.6253955998897069
```
Expand Down
42 changes: 24 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,50 @@
"name": "d3-random",
"version": "2.2.2",
"description": "Generate random numbers from various distributions.",
"homepage": "https://d3js.org/d3-random/",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-random.git"
},
"keywords": [
"d3",
"d3-module",
"random",
"rng"
],
"homepage": "https://d3js.org/d3-random/",
"license": "BSD-3-Clause",
"license": "ISC",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "dist/d3-random.js",
"unpkg": "dist/d3-random.min.js",
"jsdelivr": "dist/d3-random.min.js",
"module": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-random.git"
},
"type": "module",
"files": [
"dist/**/*.js",
"src/**/*.js"
],
"scripts": {
"pretest": "rollup -c",
"test": "./test/run.sh",
"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-random.min.js",
"unpkg": "dist/d3-random.min.js",
"exports": {
"umd": "./dist/d3-random.min.js",
"default": "./src/index.js"
},
"sideEffects": false,
"devDependencies": {
"d3-array": "1 - 2",
"eslint": "7",
"jsdom": "16",
"mocha": "8",
"rollup": "2",
"rollup-plugin-terser": "7",
"tape": "4",
"tape-await": "0.1"
"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"
}
}
11 changes: 11 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
},
"env": {
"es6": true,
"mocha": true
}
}
5 changes: 5 additions & 0 deletions test/asserts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assert from "assert";

export function assertInDelta(actual, expected, delta) {
assert(expected - delta <= actual && actual <= expected + delta, `${actual} should be within ${delta} of ${expected}`);
}
70 changes: 34 additions & 36 deletions test/bates-test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
var tape = require("tape-await"),
d3 = Object.assign({}, require("../"), require("d3-array")),
skewness = require("./skewness"),
kurtosis = require("./kurtosis");
import {mean, range, variance} from "d3-array";
import {randomBates, randomLcg} from "../src/index.js";
import {skewness, kurtosis} from "./statistics.js";
import {assertInDelta} from "./asserts.js";

require("./inDelta");

tape("d3.randomBates(n) returns random numbers with a mean of one-half", test => {
var randomBates = d3.randomBates.source(d3.randomLcg(0.6351090615932817));
test.inDelta(d3.mean(d3.range(10000).map(randomBates(1))), 0.5, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomBates(10))), 0.5, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomBates(1.5))), 0.5, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomBates(4.2))), 0.5, 0.05);
it("randomBates(n) returns random numbers with a mean of one-half", () => {
const r = randomBates.source(randomLcg(0.6351090615932817));
assertInDelta(mean(range(10000).map(r(1))), 0.5, 0.05);
assertInDelta(mean(range(10000).map(r(10))), 0.5, 0.05);
assertInDelta(mean(range(10000).map(r(1.5))), 0.5, 0.05);
assertInDelta(mean(range(10000).map(r(4.2))), 0.5, 0.05);
});

tape("d3.randomBates(n) returns random numbers with a variance of 1 / (12 * n)", test => {
var randomBates = d3.randomBates.source(d3.randomLcg(0.1284832084868286));
test.inDelta(d3.variance(d3.range(10000).map(randomBates(1))), 1 / 12, 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBates(10))), 1 / 120, 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBates(1.5))), 1 / 18, 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBates(4.2))), 1 / 50.4, 0.05);
it("randomBates(n) returns random numbers with a variance of 1 / (12 * n)", () => {
const r = randomBates.source(randomLcg(0.1284832084868286));
assertInDelta(variance(range(10000).map(r(1))), 1 / 12, 0.05);
assertInDelta(variance(range(10000).map(r(10))), 1 / 120, 0.05);
assertInDelta(variance(range(10000).map(r(1.5))), 1 / 18, 0.05);
assertInDelta(variance(range(10000).map(r(4.2))), 1 / 50.4, 0.05);
});

tape("d3.randomBates(n) returns random numbers with a skewness of 0", test => {
var randomBates = d3.randomBates.source(d3.randomLcg(0.051567609139606674));
test.inDelta(skewness(d3.range(10000).map(randomBates(1))), 0, 0.05);
test.inDelta(skewness(d3.range(10000).map(randomBates(10))), 0, 0.05);
test.inDelta(skewness(d3.range(10000).map(randomBates(1.5))), 0, 0.05);
test.inDelta(skewness(d3.range(10000).map(randomBates(4.2))), 0, 0.05);
it("randomBates(n) returns random numbers with a skewness of 0", () => {
const r = randomBates.source(randomLcg(0.051567609139606674));
assertInDelta(skewness(range(10000).map(r(1))), 0, 0.05);
assertInDelta(skewness(range(10000).map(r(10))), 0, 0.05);
assertInDelta(skewness(range(10000).map(r(1.5))), 0, 0.05);
assertInDelta(skewness(range(10000).map(r(4.2))), 0, 0.05);
});

tape("d3.randomBates(n) returns random numbers with a kurtosis of -6 / (5 * n)", test => {
var randomBates = d3.randomBates.source(d3.randomLcg(0.696913354780724));
test.inDelta(kurtosis(d3.range(10000).map(randomBates(1))), -6 / 5, 0.05);
test.inDelta(kurtosis(d3.range(10000).map(randomBates(10))), -6 / 50, 0.1);
test.inDelta(kurtosis(d3.range(10000).map(randomBates(1.5))), -6 / 7.5, 0.05);
test.inDelta(kurtosis(d3.range(10000).map(randomBates(4.2))), -6 / 21, 0.05);
it("randomBates(n) returns random numbers with a kurtosis of -6 / (5 * n)", () => {
const r = randomBates.source(randomLcg(0.696913354780724));
assertInDelta(kurtosis(range(10000).map(r(1))), -6 / 5, 0.05);
assertInDelta(kurtosis(range(10000).map(r(10))), -6 / 50, 0.1);
assertInDelta(kurtosis(range(10000).map(r(1.5))), -6 / 7.5, 0.05);
assertInDelta(kurtosis(range(10000).map(r(4.2))), -6 / 21, 0.05);
});

tape("d3.randomBates(0) is equivalent to d3.randomUniform()", test => {
var randomBates = d3.randomBates.source(d3.randomLcg(0.7717596603725383));
test.inDelta(d3.mean(d3.range(10000).map(randomBates(0))), 0.5, 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBates(0))), 1 / 12, 0.05);
test.inDelta(skewness(d3.range(10000).map(randomBates(0))), 0, 0.05);
test.inDelta(kurtosis(d3.range(10000).map(randomBates(0))), -6 / 5, 0.05);
it("randomBates(0) is equivalent to randomUniform()", () => {
const r = randomBates.source(randomLcg(0.7717596603725383));
assertInDelta(mean(range(10000).map(r(0))), 0.5, 0.05);
assertInDelta(variance(range(10000).map(r(0))), 1 / 12, 0.05);
assertInDelta(skewness(range(10000).map(r(0))), 0, 0.05);
assertInDelta(kurtosis(range(10000).map(r(0))), -6 / 5, 0.05);
});
62 changes: 30 additions & 32 deletions test/bernoulli-test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
var tape = require("tape-await"),
skewness = require("./skewness"),
kurtosis = require("./kurtosis"),
d3 = Object.assign({}, require("../"), require("d3-array"));
import {mean, range, variance} from "d3-array";
import {randomBernoulli, randomLcg} from "../src/index.js";
import {skewness, kurtosis} from "./statistics.js";
import {assertInDelta} from "./asserts.js";

require("./inDelta");

function mean(p) {
function dmean(p) {
return p;
}

function variance(p) {
function dvariance(p) {
return p * (1 - p);
}

function skew(p) {
return (1 - 2 * p) / Math.sqrt(variance(p));
return (1 - 2 * p) / Math.sqrt(dvariance(p));
}

function kurt(p) {
return (6 * Math.pow(p, 2) - 6 * p + 1) / (variance(p));
return (6 * Math.pow(p, 2) - 6 * p + 1) / (dvariance(p));
}

tape("randomBernoulli(p) returns random bernoulli distributed numbers with a mean of p", test => {
var randomBernoulli = d3.randomBernoulli.source(d3.randomLcg(0.48444190806583465));
test.inDelta(d3.mean(d3.range(10000).map(randomBernoulli(1))), mean(1), variance(1));
test.inDelta(d3.mean(d3.range(10000).map(randomBernoulli(.5))), mean(.5), variance(.5));
test.inDelta(d3.mean(d3.range(10000).map(randomBernoulli(.25))), mean(.25), variance(.25));
test.inDelta(d3.mean(d3.range(10000).map(randomBernoulli(0))), mean(0), variance(0));
it("randomBernoulli(p) returns random bernoulli distributed numbers with a mean of p", () => {
const r = randomBernoulli.source(randomLcg(0.48444190806583465));
assertInDelta(mean(range(10000).map(r(1))), dmean(1), dvariance(1));
assertInDelta(mean(range(10000).map(r(0.5))), dmean(0.5), dvariance(0.5));
assertInDelta(mean(range(10000).map(r(0.25))), dmean(0.25), dvariance(0.25));
assertInDelta(mean(range(10000).map(r(0))), dmean(0), dvariance(0));
});

tape("randomBernoulli(p) returns random bernoulli distributed numbers with a variance of p * (1 - p)", test => {
var randomBernoulli = d3.randomBernoulli.source(d3.randomLcg(0.9781605192898934));
test.inDelta(d3.variance(d3.range(10000).map(randomBernoulli(1))), variance(1), 0);
test.inDelta(d3.variance(d3.range(10000).map(randomBernoulli(.5))), variance(.5), 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBernoulli(.25))), variance(.25), 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBernoulli(0))), variance(0), 0);
it("randomBernoulli(p) returns random bernoulli distributed numbers with a variance of p * (1 - p)", () => {
const r = randomBernoulli.source(randomLcg(0.9781605192898934));
assertInDelta(variance(range(10000).map(r(1))), dvariance(1), 0);
assertInDelta(variance(range(10000).map(r(0.5))), dvariance(0.5), 0.05);
assertInDelta(variance(range(10000).map(r(0.25))), dvariance(0.25), 0.05);
assertInDelta(variance(range(10000).map(r(0))), dvariance(0), 0);
});

tape("randomBernoulli(p) returns random bernoulli distributed numbers with a skewness of (1 - 2 * p) / sqrt(p * (1 - p)).", test => {
var randomBernoulli = d3.randomBernoulli.source(d3.randomLcg(0.9776249148208429));
test.inDelta(skewness(d3.range(10000).map(randomBernoulli(.5))), skew(.5), 0.08);
test.inDelta(skewness(d3.range(10000).map(randomBernoulli(.25))), skew(.25), 0.05);
it("randomBernoulli(p) returns random bernoulli distributed numbers with a skewness of (1 - 2 * p) / sqrt(p * (1 - p)).", () => {
const r = randomBernoulli.source(randomLcg(0.9776249148208429));
assertInDelta(skewness(range(10000).map(r(0.5))), skew(0.5), 0.08);
assertInDelta(skewness(range(10000).map(r(0.25))), skew(0.25), 0.05);
});

tape("randomBernoulli(p) returns random bernoulli distributed numbers with a kurtosis excess of (6 * p^2 - 6 * p - 1) / (p * (1 - p)).", test => {
var randomBernoulli = d3.randomBernoulli.source(d3.randomLcg(0.8260973119979638));
test.inDelta(kurtosis(d3.range(10000).map(randomBernoulli(.05))), kurt(.05), kurt(.05) * 0.2);
test.inDelta(kurtosis(d3.range(10000).map(randomBernoulli(.10))), kurt(.10), kurt(.10) * 0.2);
test.inDelta(kurtosis(d3.range(10000).map(randomBernoulli(.15))), kurt(.15), kurt(.15) * 0.2);
test.inDelta(kurtosis(d3.range(50000).map(randomBernoulli(.20))), kurt(.20), kurt(.20) * 0.4);
it("randomBernoulli(p) returns random bernoulli distributed numbers with a kurtosis excess of (6 * p^2 - 6 * p - 1) / (p * (1 - p)).", () => {
const r = randomBernoulli.source(randomLcg(0.8260973119979638));
assertInDelta(kurtosis(range(10000).map(r(0.05))), kurt(0.05), kurt(0.05) * 0.2);
assertInDelta(kurtosis(range(10000).map(r(0.10))), kurt(0.10), kurt(0.10) * 0.2);
assertInDelta(kurtosis(range(10000).map(r(0.15))), kurt(0.15), kurt(0.15) * 0.2);
assertInDelta(kurtosis(range(50000).map(r(0.20))), kurt(0.20), kurt(0.20) * 0.4);
});

0 comments on commit e5bd86e

Please sign in to comment.