Skip to content

Commit

Permalink
fix(ordinal): update map only with unset unknown (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored Sep 19, 2022
1 parent 8908324 commit 5535fc8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions __tests__/unit/scales/band.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ describe('band scale', () => {
expect(bandScale.map('wow')).toStrictEqual(0);

bandScale.update({
unknown: 'hello world~',
unknown: null,
});
expect(bandScale.map('bar')).toStrictEqual('hello world~');
expect(bandScale.map('bar')).toStrictEqual(null);
});

test('test padding option', () => {
Expand Down
13 changes: 7 additions & 6 deletions __tests__/unit/scales/ordinal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Ordinal, Comparator } from '../../../src';
import { defaultUnknown } from '../../../src/scales/ordinal';

describe('ordinal scale', () => {
test('ordinal has no expected defaults', () => {
test('ordinal has expected defaults', () => {
const c = new Ordinal();
expect(c.getOptions().domain).toStrictEqual([]);
expect(c.getOptions().range).toStrictEqual([]);
expect(c.getOptions().unknown).toBeUndefined();
expect(c.getOptions().unknown).toBe(defaultUnknown);
});

test('default options', () => {
Expand Down Expand Up @@ -42,7 +43,7 @@ describe('ordinal scale', () => {

// 不存在的值 我们将新值插入 domain 中,并更新 map
expect(scale.map('3')).toStrictEqual('February');
expect(scale.getOptions().domain.length).toStrictEqual(6);
expect(scale.getOptions().domain?.length).toStrictEqual(6);

// NaN / undefined / null 做法同上
expect(scale.map(NaN)).toStrictEqual('January');
Expand Down Expand Up @@ -147,10 +148,10 @@ describe('ordinal scale', () => {
});
expect(scale.map('D')).toStrictEqual('a');
scale.update({
unknown: 'hello world',
unknown: null,
});
expect(scale.map('E')).toStrictEqual('hello world');
expect(scale.invert('foo')).toStrictEqual('hello world');
expect(scale.map('E')).toStrictEqual(null);
expect(scale.invert('foo')).toStrictEqual(null);
});

test('duplicate data in domain or range', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/scales/band.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InternMap } from '../utils';
import { BandOptions, Domain } from '../types';
import { Ordinal } from './ordinal';
import { Ordinal, defaultUnknown } from './ordinal';

function normalize(array: number[]): number[] {
const min = Math.min(...array);
Expand Down Expand Up @@ -216,7 +216,7 @@ export class Band<O extends BandOptions = BandOptions> extends Ordinal<O> {
paddingInner: 0,
paddingOuter: 0,
padding: 0,
unknown: undefined,
unknown: defaultUnknown,
flex: [],
} as O;
}
Expand Down
9 changes: 7 additions & 2 deletions src/scales/ordinal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type MapBetweenArrOptions = {
notFoundReturn?: any;
};

export const defaultUnknown = Symbol('defaultUnknown');

/**
* 更新 indexMap
*
Expand All @@ -42,9 +44,11 @@ function mapBetweenArrByMapIndex(options: MapBetweenArrOptions) {
const { value, from, to, mapper, notFoundReturn } = options;
let mappedIndex = mapper.get(value);

// index 不存在时,我们将 value 添加到原数组, 并更新 Map
// index 不存在时,
// 如果用户显式设置了 unknown 的值,那么就返回 unknown 的值
// 否者我们将 value 添加到原数组, 并更新 Map
if (mappedIndex === undefined) {
if (notFoundReturn) {
if (notFoundReturn !== defaultUnknown) {
return notFoundReturn;
}
mappedIndex = from.push(value) - 1;
Expand Down Expand Up @@ -91,6 +95,7 @@ export class Ordinal<O extends OrdinalOptions = OrdinalOptions> extends Base<O>
return {
domain: [],
range: [],
unknown: defaultUnknown,
} as O;
}

Expand Down
3 changes: 2 additions & 1 deletion src/scales/point.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Band } from './band';
import { defaultUnknown } from './ordinal';
import { PointOptions, BandOptions } from '../types';

/**
Expand Down Expand Up @@ -29,7 +30,7 @@ export class Point extends Band<PointOptions & BandOptions> {
align: 0.5,
round: false,
padding: 0,
unknown: undefined,
unknown: defaultUnknown,
paddingInner: 1,
paddingOuter: 0,
};
Expand Down

0 comments on commit 5535fc8

Please sign in to comment.