-
Notifications
You must be signed in to change notification settings - Fork 287
/
ordinal.js
46 lines (37 loc) · 1.06 KB
/
ordinal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {InternMap} from "d3-array";
import {initRange} from "./init.js";
export const implicit = Symbol("implicit");
export default function ordinal() {
var index = new InternMap(),
domain = [],
range = [],
unknown = implicit;
function scale(d) {
let i = index.get(d);
if (i === undefined) {
if (unknown !== implicit) return unknown;
index.set(d, i = domain.push(d) - 1);
}
return range[i % range.length];
}
scale.domain = function(_) {
if (!arguments.length) return domain.slice();
domain = [], index = new InternMap();
for (const value of _) {
if (index.has(value)) continue;
index.set(value, domain.push(value) - 1);
}
return scale;
};
scale.range = function(_) {
return arguments.length ? (range = Array.from(_), scale) : range.slice();
};
scale.unknown = function(_) {
return arguments.length ? (unknown = _, scale) : unknown;
};
scale.copy = function() {
return ordinal(domain, range).unknown(unknown);
};
initRange.apply(scale, arguments);
return scale;
}