Skip to content

Commit

Permalink
adopt InternMap for ordinal scales
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Mar 15, 2021
1 parent c7efc99 commit b9bd7b6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -34,7 +34,7 @@
},
"sideEffects": false,
"dependencies": {
"d3-array": "^2.3.0",
"d3-array": "^2.12.0",
"d3-format": "1 - 2",
"d3-interpolate": "1.2.0 - 2",
"d3-time": "1 - 2",
Expand Down
17 changes: 8 additions & 9 deletions src/ordinal.js
@@ -1,29 +1,28 @@
import {initRange} from "./init.js";
import {InternMap} from "d3-array";

export const implicit = Symbol("implicit");

export default function ordinal() {
var index = new Map(),
var index = new InternMap(),
domain = [],
range = [],
unknown = implicit;

function scale(d) {
var key = d + "", i = index.get(key);
if (!i) {
if (!index.has(d)) {
if (unknown !== implicit) return unknown;
index.set(key, i = domain.push(d));
index.set(d, domain.push(d));
}
return range[(i - 1) % range.length];
return range[(index.get(d) - 1) % range.length];
}

scale.domain = function(_) {
if (!arguments.length) return domain.slice();
domain = [], index = new Map();
domain = [], index = new InternMap();
for (const value of _) {
const key = value + "";
if (index.has(key)) continue;
index.set(key, domain.push(value));
if (index.has(value)) continue;
index.set(value, domain.push(value));
}
return scale;
};
Expand Down
23 changes: 23 additions & 0 deletions test/ordinal-test.js
Expand Up @@ -87,6 +87,29 @@ tape("ordinal.domain() does not coerce domain values to strings", function(test)
test.end();
});

tape("ordinal() accepts dates", function(test) {
var s = scale.scaleOrdinal();
s(new Date(1970, 2, 1));
s(new Date(2001, 4, 13));
s(new Date(1970, 2, 1));
s(new Date(2001, 4, 13));
test.deepEqual(s.domain(), [new Date(1970, 2, 1), new Date(2001, 4, 13)]);
test.end();
});

tape("ordinal.domain() accepts dates", function(test) {
var s = scale.scaleOrdinal().domain([
new Date(1970, 2, 1),
new Date(2001, 4, 13),
new Date(1970, 2, 1),
new Date(2001, 4, 13)
]);
s(new Date(1970, 2, 1));
s(new Date(1999, 11, 31));
test.deepEqual(s.domain(), [new Date(1970, 2, 1), new Date(2001, 4, 13), new Date(1999, 11, 31)]);
test.end();
});

tape("ordinal.domain() does not barf on object built-ins", function(test) {
var s = scale.scaleOrdinal().domain(["__proto__", "hasOwnProperty"]).range([42, 43]);
test.equal(s("__proto__"), 42);
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Expand Up @@ -169,10 +169,12 @@ cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"

d3-array@^2.3.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-2.8.0.tgz#f76e10ad47f1f4f75f33db5fc322eb9ffde5ef23"
integrity sha512-6V272gsOeg7+9pTW1jSYOR1QE37g95I3my1hBmY+vOUNHRrk9yt4OTz/gK7PMkVAVDrYYq4mq3grTiZ8iJdNIw==
d3-array@^2.12.0:
version "2.12.0"
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-2.12.0.tgz#68a74d153e52d6bf4608d9f6388cca48b42a4c3f"
integrity sha512-T6H/qNldyD/1OlRkJbonb3u3MPhNwju8OPxYv0YSjDb/B2RUeeBEHzIpNrYiinwpmz8+am+puMrpcrDWgY9wRg==
dependencies:
internmap "^1.0.0"

"d3-color@1 - 2":
version "2.0.0"
Expand Down Expand Up @@ -548,6 +550,11 @@ inquirer@^6.4.1:
strip-ansi "^5.1.0"
through "^2.3.6"

internmap@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/internmap/-/internmap-1.0.1.tgz#0017cc8a3b99605f0302f2b198d272e015e5df95"
integrity sha512-lDB5YccMydFBtasVtxnZ3MRBHuaoE8GKsppq+EchKL2U4nK/DmEpPHNH8MZe5HkMtpSiTSOZwfN0tzYjO/lJEw==

is-callable@^1.1.3, is-callable@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.4.tgz#1e1adf219e1eeb684d691f9d6a05ff0d30a24d75"
Expand Down

0 comments on commit b9bd7b6

Please sign in to comment.