Skip to content

Commit 5ba0ad6

Browse files
authored
fix(stock): avoid mutating stock data item (#3349)
1 parent c6917f0 commit 5ba0ad6

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

__tests__/unit/plots/stock/data-spec.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TREND_FIELD, TREND_UP, TREND_DOWN, Y_FIELD } from '../../../../src/plot
33

44
describe('stock data', () => {
55
it('stock data', () => {
6-
const yField = ['start', 'end', 'max', 'min'];
6+
const yField: [string, string, string, string] = ['start', 'end', 'max', 'min'];
77
const originalData = [
88
{ date: '2015-11-19', start: 8.18, max: 8.33, min: 7.98, end: 8.32, volumn: 1810, money: 14723.56 },
99
{ date: '2015-11-18', start: 8.37, max: 8.6, min: 8.03, end: 8.09, volumn: 2790.37, money: 23309.19 },
@@ -49,7 +49,7 @@ describe('stock data', () => {
4949
});
5050

5151
it('start = end', () => {
52-
const yField = ['start', 'end', 'max', 'min'];
52+
const yField: [string, string, string, string] = ['start', 'end', 'max', 'min'];
5353
const originalData = [
5454
{ date: '2015-11-19', start: 8.18, max: 8.33, min: 7.98, end: 8.18, volumn: 1810, money: 14723.56 },
5555
];
@@ -70,7 +70,7 @@ describe('stock data', () => {
7070
});
7171

7272
it('contain invalid value', () => {
73-
const yField = ['start', 'end', 'max', 'min'];
73+
const yField: [string, string, string, string] = ['start', 'end', 'max', 'min'];
7474
const originalData = [
7575
{ date: '2015-11-19', start: undefined, max: 8.33, min: 7.98, end: null, volumn: 1810, money: 14723.56 },
7676
];
@@ -89,4 +89,33 @@ describe('stock data', () => {
8989
},
9090
]);
9191
});
92+
93+
it('does not mutate original data item', () => {
94+
const yField: [string, string, string, string] = ['start', 'end', 'max', 'min'];
95+
const originalData = [
96+
{ date: '2015-11-19', start: 8.18, max: 8.33, min: 7.98, end: 8.18, volumn: 1810, money: 14723.56 },
97+
];
98+
const data = getStockData(originalData, yField);
99+
expect(data[0]).not.toBe(originalData[0]);
100+
expect(data).toEqual([
101+
{
102+
date: '2015-11-19',
103+
start: 8.18,
104+
max: 8.33,
105+
min: 7.98,
106+
end: 8.18,
107+
volumn: 1810,
108+
money: 14723.56,
109+
[TREND_FIELD]: TREND_UP,
110+
[Y_FIELD]: [8.18, 8.18, 8.33, 7.98],
111+
},
112+
]);
113+
});
114+
115+
it('contain invalid data item', () => {
116+
const yField: [string, string, string, string] = ['start', 'end', 'max', 'min'];
117+
const originalData = [null] as unknown as Record<string, any>[];
118+
const data = getStockData(originalData, yField);
119+
expect(data).toEqual([null]);
120+
});
92121
});

src/plots/stock/utils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { TREND_FIELD, TREND_DOWN, TREND_UP, Y_FIELD } from './constant';
66
* @param data
77
* @param yField
88
*/
9-
export function getStockData(data, yField) {
10-
return map(data, (obj) => {
11-
if (isArray(yField)) {
9+
export function getStockData(data: Record<string, any>[], yField: [string, string, string, string]) {
10+
return map(data, (item) => {
11+
const obj = item && { ...item };
12+
if (isArray(yField) && obj) {
1213
const [open, close, high, low] = yField;
1314
obj[TREND_FIELD] = obj[open] <= obj[close] ? TREND_UP : TREND_DOWN;
1415
obj[Y_FIELD] = [obj[open], obj[close], obj[high], obj[low]];

0 commit comments

Comments
 (0)