1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Ordinal = exports.defaultUnknown = void 0;
|
4 | const base_1 = require("./base");
|
5 | exports.defaultUnknown = Symbol('defaultUnknown');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function updateIndexMap(target, arr, key) {
|
14 | for (let i = 0; i < arr.length; i += 1) {
|
15 | if (!target.has(arr[i])) {
|
16 | target.set(key(arr[i]), i);
|
17 | }
|
18 | }
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function mapBetweenArrByMapIndex(options) {
|
28 | const { value, from, to, mapper, notFoundReturn } = options;
|
29 | let mappedIndex = mapper.get(value);
|
30 |
|
31 |
|
32 |
|
33 | if (mappedIndex === undefined) {
|
34 | if (notFoundReturn !== exports.defaultUnknown) {
|
35 | return notFoundReturn;
|
36 | }
|
37 | mappedIndex = from.push(value) - 1;
|
38 | mapper.set(value, mappedIndex);
|
39 | }
|
40 | return to[mappedIndex % to.length];
|
41 | }
|
42 | function createKey(d) {
|
43 | if (d instanceof Date)
|
44 | return (d) => `${d}`;
|
45 | if (typeof d === 'object')
|
46 | return (d) => JSON.stringify(d);
|
47 | return (d) => d;
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | class Ordinal extends base_1.Base {
|
60 |
|
61 | getDefaultOptions() {
|
62 | return {
|
63 | domain: [],
|
64 | range: [],
|
65 | unknown: exports.defaultUnknown,
|
66 | };
|
67 | }
|
68 |
|
69 | constructor(options) {
|
70 | super(options);
|
71 | }
|
72 | map(x) {
|
73 | if (this.domainIndexMap.size === 0) {
|
74 | updateIndexMap(this.domainIndexMap, this.getDomain(), this.domainKey);
|
75 | }
|
76 | return mapBetweenArrByMapIndex({
|
77 | value: this.domainKey(x),
|
78 | mapper: this.domainIndexMap,
|
79 | from: this.getDomain(),
|
80 | to: this.getRange(),
|
81 | notFoundReturn: this.options.unknown,
|
82 | });
|
83 | }
|
84 | invert(y) {
|
85 | if (this.rangeIndexMap.size === 0) {
|
86 | updateIndexMap(this.rangeIndexMap, this.getRange(), this.rangeKey);
|
87 | }
|
88 | return mapBetweenArrByMapIndex({
|
89 | value: this.rangeKey(y),
|
90 | mapper: this.rangeIndexMap,
|
91 | from: this.getRange(),
|
92 | to: this.getDomain(),
|
93 | notFoundReturn: this.options.unknown,
|
94 | });
|
95 | }
|
96 |
|
97 | rescale(options) {
|
98 | const [d] = this.options.domain;
|
99 | const [r] = this.options.range;
|
100 | this.domainKey = createKey(d);
|
101 | this.rangeKey = createKey(r);
|
102 |
|
103 | if (!this.rangeIndexMap) {
|
104 | this.rangeIndexMap = new Map();
|
105 | this.domainIndexMap = new Map();
|
106 | return;
|
107 | }
|
108 |
|
109 | if (!options || options.range) {
|
110 | this.rangeIndexMap.clear();
|
111 | }
|
112 | if (!options || options.domain || options.compare) {
|
113 | this.domainIndexMap.clear();
|
114 | this.sortedDomain = undefined;
|
115 | }
|
116 | }
|
117 | clone() {
|
118 | return new Ordinal(this.options);
|
119 | }
|
120 | getRange() {
|
121 | return this.options.range;
|
122 | }
|
123 | getDomain() {
|
124 |
|
125 | if (this.sortedDomain)
|
126 | return this.sortedDomain;
|
127 | const { domain, compare } = this.options;
|
128 | this.sortedDomain = compare ? [...domain].sort(compare) : domain;
|
129 | return this.sortedDomain;
|
130 | }
|
131 | }
|
132 | exports.Ordinal = Ordinal;
|
133 |
|
\ | No newline at end of file |