Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node-version: [10.x, 12.x]
node-version: [12.x, 14.x]

runs-on: ${{ matrix.os }}

Expand Down
5 changes: 0 additions & 5 deletions src/constant.js

This file was deleted.

48 changes: 31 additions & 17 deletions src/library.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {require as requireDefault} from "d3-require";
import constant from "./constant.js";
import DOM from "./dom/index.js";
import Files from "./files/index.js";
import {NoFileAttachments} from "./fileAttachment.js";
Expand All @@ -13,24 +12,39 @@ import resolve from "./resolve.js";
import requirer from "./require.js";
import svg from "./svg.js";
import tex from "./tex.js";
import vegalite from "./vegalite.js";
import width from "./width.js";

export default Object.assign(function Library(resolver) {
const require = requirer(resolver);
Object.defineProperties(this, {
DOM: {value: DOM, writable: true, enumerable: true},
FileAttachment: {value: constant(NoFileAttachments), writable: true, enumerable: true},
Files: {value: Files, writable: true, enumerable: true},
Generators: {value: Generators, writable: true, enumerable: true},
html: {value: constant(html), writable: true, enumerable: true},
md: {value: md(require), writable: true, enumerable: true},
Mutable: {value: constant(Mutable), writable: true, enumerable: true},
now: {value: now, writable: true, enumerable: true},
Promises: {value: Promises, writable: true, enumerable: true},
require: {value: constant(require), writable: true, enumerable: true},
resolve: {value: constant(resolve), writable: true, enumerable: true},
svg: {value: constant(svg), writable: true, enumerable: true},
tex: {value: tex(require), writable: true, enumerable: true},
width: {value: width, writable: true, enumerable: true}
});
Object.defineProperties(this, properties({
DOM: () => DOM,
FileAttachment: () => NoFileAttachments,
Files: () => Files,
Generators: () => Generators,
Inputs: () => require("@observablehq/inputs@0.7.21/dist/inputs.umd.min.js"),
Mutable: () => Mutable,
Plot: () => require("@observablehq/plot@0.1.0/dist/plot.umd.min.js"),
Promises: () => Promises,
_: () => require("lodash@4.17.21/lodash.min.js"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is sort of not-like-the-others in this set. I assume that it is very commonly used and stable in notebooks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lodash is the most popular library on npm according to this analysis:

https://gist.github.com/anvaka/8e8fa57c7ee1350e3491

It appears to be referenced in 2.5K public notebooks according to this query:

https://observablehq.com/search?query=lodash

That’s less than Vega (9K) and D3 (10K+), but still quite popular, and I vouch for its quality.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question regarding evolvability and compatibility: I started out using Ramda because of its elegant “immutable auto-curried iteratee-first data-last methods”. However, including the much more popular lodash library conveniently and lazily by default urges me to switch to lodash. Plus, I just learned that there also is lodash/fp, that enjoys the same feature as Ramda above. Is there a way to include lodash/fp by default the same way as just lodash? Or should I fall back to an explicit require?

d3: () => require("d3@6.7.0/dist/d3.min.js"),
htl: () => require("htl@0.2.5/dist/htl.min.js"),
html: () => html,
md: md(require),
now: now,
require: () => require,
resolve: () => resolve,
svg: () => svg,
tex: tex(require),
vl: vegalite(require),
width: width
}));
}, {resolve: requireDefault.resolve});

function properties(values) {
return Object.fromEntries(Object.entries(values).map(property));
}

function property([key, value]) {
return [key, ({value, writable: true, enumerable: true})];
}
6 changes: 2 additions & 4 deletions src/promises/when.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import constant from "../constant.js";

var timeouts = new Map;

function timeout(now, time) {
Expand All @@ -16,7 +14,7 @@ function timeout(now, time) {

export default function when(time, value) {
var now;
return (now = timeouts.get(time = +time)) ? now.then(constant(value))
return (now = timeouts.get(time = +time)) ? now.then(() => value)
: (now = Date.now()) >= time ? Promise.resolve(value)
: timeout(now, time).then(constant(value));
: timeout(now, time).then(() => value);
}
10 changes: 10 additions & 0 deletions src/vegalite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function vl(require) {
return async () => {
const [vega, vegalite, api] = await Promise.all([
"vega@5.20.2/build/vega.min.js",
"vega-lite@5.1.0/build/vega-lite.min.js",
"vega-lite-api@5.0.0/build/vega-lite-api.min.js"
].map(module => require(module)));
return api.register(vega, vegalite);
};
}
6 changes: 6 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ test("new Library returns a library with the expected keys", async t => {
"FileAttachment",
"Files",
"Generators",
"Inputs",
"Mutable",
"Plot",
"Promises",
"_",
"d3",
"htl",
"html",
"md",
"now",
"require",
"resolve",
"svg",
"tex",
"vl",
"width"
]);
t.end();
Expand Down