Skip to content

Commit

Permalink
type: module
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 3, 2021
1 parent 566fee0 commit 0fa5d17
Show file tree
Hide file tree
Showing 9 changed files with 588 additions and 395 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true
"es6": true
}
}
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 . --format=compact
- run: yarn test
23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
"name": "Mike Bostock",
"url": "https://bost.ocks.org/mike"
},
"main": "dist/internmap.js",
"unpkg": "dist/internmap.min.js",
"type": "module",
"main": "src/index.js",
"module": "src/index.js",
"jsdelivr": "dist/internmap.min.js",
"unpkg": "dist/internmap.min.js",
"exports": {
"umd": "./dist/internmap.min.js",
"default": "./src/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/mbostock/internmap.git"
Expand All @@ -20,17 +26,18 @@
"src/**/*.js"
],
"scripts": {
"pretest": "rollup -c",
"test": "tape test/**/*-test.js && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test",
"test": "mocha --experimental-modules 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
"postpublish": "git push && git push --tags"
},
"sideEffects": false,
"devDependencies": {
"eslint": "^7.18.0",
"mocha": "8",
"rollup": "^2.37.1",
"rollup-plugin-terser": "^7.0.2",
"tape": "^4.13.3",
"tape-await": "^0.1.2"
"rollup-plugin-terser": "^7.0.2"
},
"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
}
}
48 changes: 24 additions & 24 deletions test/InternMap-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {InternMap} = require("../");
const tape = require("tape-await");
import {InternMap} from "../src/index.js";
import assert from "assert";

class Value {
constructor(value) {
Expand All @@ -10,12 +10,12 @@ class Value {
}
}

tape("InternMap is an instanceof Map", test => {
test.ok(new InternMap() instanceof Map);
it("InternMap is an instanceof Map", () => {
assert(new InternMap() instanceof Map);
});

tape("InternMap(null) returns an empty set", test => {
test.equal(new InternMap(null).size, 0);
it("InternMap(null) returns an empty set", () => {
assert.strictEqual(new InternMap(null).size, 0);
});

basicTest("strings", "a", "b", "c");
Expand All @@ -24,31 +24,31 @@ basicTest("dates", new Date(Date.UTC(2001, 0, 1)), new Date(Date.UTC(2002, 0, 1)
basicTest("custom", new Value(1), new Value(2), new Value(3));

function basicTest(name, a, b, c) {
tape(`InternMap(${name})`, test => {
it(`InternMap(${name})`, () => {
const entries = [[a, 1], [b, 2]];
const map = new InternMap([[a, 1], [b, 1], [b, 2]]);
test.deepEquals([...map], entries);
test.deepEquals([...map.keys()], entries.map(([key]) => key));
test.deepEquals([...map.values()], entries.map(([, value]) => value));
test.deepEquals([...map.entries()], entries);
test.equals(map.has(a), true);
test.equals(map.has(b), true);
test.equals(map.has(c), false);
test.equals(map.get(a), 1);
test.equals(map.get(b), 2);
test.equals(map.get(c), undefined);
assert.deepStrictEqual([...map], entries);
assert.deepStrictEqual([...map.keys()], entries.map(([key]) => key));
assert.deepStrictEqual([...map.values()], entries.map(([, value]) => value));
assert.deepStrictEqual([...map.entries()], entries);
assert.strictEqual(map.has(a), true);
assert.strictEqual(map.has(b), true);
assert.strictEqual(map.has(c), false);
assert.strictEqual(map.get(a), 1);
assert.strictEqual(map.get(b), 2);
assert.strictEqual(map.get(c), undefined);
});
}

tape(`InternMap(entries) returns the first of heterogeneous types`, test => {
it(`InternMap(entries) returns the first of heterogeneous types`, () => {
const x = Date.UTC(2001, 0, 1);
const d = new Date(x);
const map1 = new InternMap([[x, "number"], [d, "date"]]);
test.equals(map1.get(x), "date");
test.equals(map1.get(d), "date");
test.deepEquals([...map1.entries()], [[x, "date"]]);
assert.strictEqual(map1.get(x), "date");
assert.strictEqual(map1.get(d), "date");
assert.deepStrictEqual([...map1.entries()], [[x, "date"]]);
const map2 = new InternMap([[d, "date"], [x, "number"]]);
test.equals(map2.get(x), "number");
test.equals(map2.get(d), "number");
test.deepEquals([...map2.entries()], [[d, "number"]]);
assert.strictEqual(map2.get(x), "number");
assert.strictEqual(map2.get(d), "number");
assert.deepStrictEqual([...map2.entries()], [[d, "number"]]);
});
40 changes: 20 additions & 20 deletions test/InternSet-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {InternSet} = require("../");
const tape = require("tape-await");
import assert from "assert";
import {InternSet} from "../src/index.js";

class Value {
constructor(value) {
Expand All @@ -10,12 +10,12 @@ class Value {
}
}

tape("InternSet is an instanceof Set", test => {
test.ok(new InternSet() instanceof Set);
it("InternSet is an instanceof Set", () => {
assert(new InternSet() instanceof Set);
});

tape("InternSet(null) returns an empty set", test => {
test.equal(new InternSet(null).size, 0);
it("InternSet(null) returns an empty set", () => {
assert.strictEqual(new InternSet(null).size, 0);
});

basicTest("strings", "a", "b", "c");
Expand All @@ -24,27 +24,27 @@ basicTest("dates", new Date(Date.UTC(2001, 0, 1)), new Date(Date.UTC(2002, 0, 1)
basicTest("custom", new Value(1), new Value(2), new Value(3));

function basicTest(name, a, b, c) {
tape(`InternSet(${name})`, test => {
it(`InternSet(${name})`, () => {
const values = [a, b];
const set = new InternSet([a, b, b]);
test.deepEquals([...set], values);
test.deepEquals([...set.values()], values);
test.deepEquals([...set.entries()], values.map(value => [value, value]));
test.equals(set.has(a), true);
test.equals(set.has(b), true);
test.equals(set.has(c), false);
assert.deepStrictEqual([...set], values);
assert.deepStrictEqual([...set.values()], values);
assert.deepStrictEqual([...set.entries()], values.map(value => [value, value]));
assert.strictEqual(set.has(a), true);
assert.strictEqual(set.has(b), true);
assert.strictEqual(set.has(c), false);
});
}

tape(`InternSet(values) returns the first of heterogeneous types`, test => {
it(`InternSet(values) returns the first of heterogeneous types`, () => {
const x = Date.UTC(2001, 0, 1);
const d = new Date(x);
const set1 = new InternSet([x, d]);
test.equals(set1.has(x), true);
test.equals(set1.has(d), true);
test.deepEquals([...set1.values()], [x]);
assert.strictEqual(set1.has(x), true);
assert.strictEqual(set1.has(d), true);
assert.deepStrictEqual([...set1.values()], [x]);
const set2 = new InternSet([d, x]);
test.equals(set2.has(x), true);
test.equals(set2.has(d), true);
test.deepEquals([...set2.values()], [d]);
assert.strictEqual(set2.has(x), true);
assert.strictEqual(set2.has(d), true);
assert.deepStrictEqual([...set2.values()], [d]);
});
Empty file removed test/value.js
Empty file.
Loading

0 comments on commit 0fa5d17

Please sign in to comment.