Skip to content

Commit

Permalink
Fixed time typings
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-korobko committed Jul 4, 2023
1 parent f80b81e commit 16890a9
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 51 deletions.
20 changes: 7 additions & 13 deletions src/api/series-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CreatePriceLineOptions, PriceLineOptions } from '../model/price-line-op
import { RangeImpl } from '../model/range-impl';
import { Series } from '../model/series';
import { SeriesPlotRow } from '../model/series-data';
import { SeriesMarker } from '../model/series-markers';
import { convertSeriesMarker, SeriesMarker } from '../model/series-markers';
import {
SeriesOptionsMap,
SeriesPartialOptionsMap,
Expand Down Expand Up @@ -137,8 +137,8 @@ export class SeriesApi<
if (dataFirstBarInRange !== null && dataLastBarInRange !== null) {
// result.from = dataFirstBarInRange.time.businessDay || dataFirstBarInRange.time.timestamp;
// result.to = dataLastBarInRange.time.businessDay || dataLastBarInRange.time.timestamp;
result.from = this._horzScaleBehavior.convertInternalToHorzItem(dataFirstBarInRange.time);
result.to = this._horzScaleBehavior.convertInternalToHorzItem(dataLastBarInRange.time);
result.from = dataFirstBarInRange.originalTime as HorzScaleItem;
result.to = dataLastBarInRange.originalTime as HorzScaleItem;
}

return result;
Expand Down Expand Up @@ -187,21 +187,15 @@ export class SeriesApi<
public setMarkers(data: SeriesMarker<HorzScaleItem>[]): void {
checkItemsAreOrdered(data, this._horzScaleBehavior, true);

const convertedMarkers = data.map((marker: SeriesMarker<HorzScaleItem>) => ({
...marker as Omit<SeriesMarker<HorzScaleItem>, 'time' | 'originalTime'>,
originalTime: marker.time,
time: this._horzScaleBehavior.convertHorzItemToInternal(marker.time),
}));
const convertedMarkers = data.map((marker: SeriesMarker<HorzScaleItem>) =>
convertSeriesMarker<HorzScaleItem, InternalHorzScaleItem>(marker, this._horzScaleBehavior.convertHorzItemToInternal(marker.time), marker.time)
);
this._series.setMarkers(convertedMarkers);
}

public markers(): SeriesMarker<HorzScaleItem>[] {
return this._series.markers().map<SeriesMarker<HorzScaleItem>>((internalItem: SeriesMarker<InternalHorzScaleItem>) => {
const { originalTime, time, ...item } = internalItem;
return {
time: originalTime as HorzScaleItem,
...item as Omit<SeriesMarker<InternalHorzScaleItem>, 'time' | 'originalTIme'>,
};
return convertSeriesMarker<InternalHorzScaleItem, HorzScaleItem>(internalItem, internalItem.originalTime as HorzScaleItem, undefined);
});
}

Expand Down
14 changes: 7 additions & 7 deletions src/api/time-scale-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { clone, DeepPartial } from '../helpers/strict-type-checks';

import { ChartModel } from '../model/chart-model';
import { Coordinate } from '../model/coordinate';
import { IHorzScaleBehavior } from '../model/ihorz-scale-behavior';
import { Logical, LogicalRange, Range, TimePointIndex, TimePointsRange } from '../model/time-data';
import { IHorzScaleBehavior, InternalHorzScaleItem } from '../model/ihorz-scale-behavior';
import { Logical, LogicalRange, Range, TimePointIndex } from '../model/time-data';
import { HorzScaleOptions, TimeScale } from '../model/time-scale';

import {
Expand Down Expand Up @@ -78,13 +78,13 @@ export class TimeScaleApi<HorzScaleItem> implements ITimeScaleApi<HorzScaleItem>
}

return {
from: this._horzScaleBehavior.convertInternalToHorzItem(timeRange.from),
to: this._horzScaleBehavior.convertInternalToHorzItem(timeRange.to),
from: timeRange.from.originalTime as HorzScaleItem,
to: timeRange.to.originalTime as HorzScaleItem,
};
}

public setVisibleRange(range: Range<HorzScaleItem>): void {
const convertedRange: TimePointsRange = {
const convertedRange: Range<InternalHorzScaleItem> = {
from: this._horzScaleBehavior.convertHorzItemToInternal(range.from),
to: this._horzScaleBehavior.convertHorzItemToInternal(range.to),
};
Expand Down Expand Up @@ -149,12 +149,12 @@ export class TimeScaleApi<HorzScaleItem> implements ITimeScaleApi<HorzScaleItem>
public coordinateToTime(x: number): HorzScaleItem | null {
const timeScale = this._model.timeScale();
const timePointIndex = timeScale.coordinateToIndex(x as Coordinate);
const timePoint = timeScale.indexToTime(timePointIndex);
const timePoint = timeScale.indexToTimeScalePoint(timePointIndex);
if (timePoint === null) {
return null;
}

return this._horzScaleBehavior.convertInternalToHorzItem(timePoint);
return timePoint.originalTime as HorzScaleItem;
}

public width(): number {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const customSeriesDefaultOptions: CustomSeriesOptions = {
...customStyleDefaults,
};

export { createChart } from './api/create-chart';
export { createChart, createChartEx } from './api/create-chart';

/**
* Returns the current version as a string. For example `'3.3.0'`.
Expand Down
14 changes: 5 additions & 9 deletions src/model/horz-scale-behavior-time/horz-scale-behavior-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ import { BusinessDay, isBusinessDay, isUTCTimestamp, TickMarkType, TickMarkWeigh
type TimeConverter = (time: Time) => InternalHorzScaleItem;

function businessDayConverter(time: Time): InternalHorzScaleItem {
let businessDay = time;
if (isString(time)) {
time = stringToBusinessDay(time);
businessDay = stringToBusinessDay(time);
}
if (!isBusinessDay(time)) {
if (!isBusinessDay(businessDay)) {
throw new Error('time must be of type BusinessDay');
}

const date = new Date(Date.UTC(time.year, time.month - 1, time.day, 0, 0, 0, 0));
const date = new Date(Date.UTC(businessDay.year, businessDay.month - 1, businessDay.day, 0, 0, 0, 0));

return {
timestamp: Math.round(date.getTime() / 1000) as UTCTimestamp,
businessDay: time,
businessDay,
} as unknown as InternalHorzScaleItem;
}

Expand Down Expand Up @@ -181,11 +182,6 @@ export class HorzScaleBehaviorTime implements IHorzScaleBehavior<Time> {
return ensureNotNull(selectTimeConverter(data));
}

public convertInternalToHorzItem(item: InternalHorzScaleItem): Time {
const tp = item as unknown as TimePoint;
return tp.businessDay ?? tp.timestamp;
}

public key(item: InternalHorzScaleItem | Time): InternalHorzScaleItemKey {
// eslint-disable-next-line no-restricted-syntax
if (typeof item === 'object' && 'timestamp' in item) {
Expand Down
7 changes: 0 additions & 7 deletions src/model/ihorz-scale-behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ export interface IHorzScaleBehavior<HorzScaleItem> {
* @returns InternalHorzScaleItem
*/
convertHorzItemToInternal(item: HorzScaleItem): InternalHorzScaleItem;
/**
* Convert an internal horizontal item into a horizontal scale item.
*
* @param item - item to be converted
* @returns HorzScaleItem
*/
convertInternalToHorzItem(item: InternalHorzScaleItem): HorzScaleItem;
/**
* Creates and returns a converter for changing series data into internal horizontal scale items.
*
Expand Down
14 changes: 14 additions & 0 deletions src/model/series-markers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ export interface SeriesMarker<TimeType> {
export interface InternalSeriesMarker<TimeType> extends SeriesMarker<TimeType> {
internalId: number;
}

export function convertSeriesMarker<InTimeType, OutTimeType>(sm: SeriesMarker<InTimeType>, newTime: OutTimeType, originalTime?: unknown): SeriesMarker<OutTimeType> {
const { time: inTime, originalTime: inOriginalTime, ...values } = sm;
/* eslint-disable @typescript-eslint/consistent-type-assertions */
const res = {
time: newTime,
...values,
} as SeriesMarker<OutTimeType>;
/* eslint-enable @typescript-eslint/consistent-type-assertions */
if (originalTime !== undefined) {
res.originalTime = originalTime;
}
return res;
}
2 changes: 1 addition & 1 deletion src/model/time-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface Range<T> {
to: T;
}

export type TimePointsRange = Range<InternalHorzScaleItem>;
export type TimePointsRange = Range<Omit<TimeScalePoint, 'timeWeight'>>;

/**
* Index for a point on the horizontal (time) scale.
Expand Down
7 changes: 4 additions & 3 deletions src/model/time-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TickMark, TickMarks } from './tick-marks';
import {
Logical,
LogicalRange,
Range,
SeriesItemsIndexesRange,
TickMarkWeightValue,
TimedValue,
Expand Down Expand Up @@ -342,12 +343,12 @@ export class TimeScale<HorzScaleItem> implements ITimeScale {
const lastIndex = ensureNotNull(this._lastIndex());

return {
from: ensureNotNull(this.indexToTime(Math.max(firstIndex, from) as TimePointIndex)),
to: ensureNotNull(this.indexToTime(Math.min(lastIndex, to) as TimePointIndex)),
from: ensureNotNull(this.indexToTimeScalePoint(Math.max(firstIndex, from) as TimePointIndex)),
to: ensureNotNull(this.indexToTimeScalePoint(Math.min(lastIndex, to) as TimePointIndex)),
};
}

public logicalRangeForTimeRange(range: TimePointsRange): LogicalRange {
public logicalRangeForTimeRange(range: Range<InternalHorzScaleItem>): LogicalRange {
return {
from: ensureNotNull(this.timeToIndex(range.from, true)) as number as Logical,
to: ensureNotNull(this.timeToIndex(range.to, true)) as number as Logical,
Expand Down
4 changes: 0 additions & 4 deletions tests/e2e/coverage/test-cases/chart/horizontal-scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class HorzScaleBehaviorPrice {
return price => price;
}

convertInternalToHorzItem(item) {
return item;
}

key(item) {
return item;
}
Expand Down
18 changes: 16 additions & 2 deletions tests/e2e/graphics/test-cases/api/series-markers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function reorderField(obj) {
const keys = Object.keys(obj);
keys.sort();
const res = {};
for (const key of keys) {
res[key] = obj[key];
}
return res;
}

function reorderFieldInArray(arr) {
return arr.map(reorderField);
}

function compare(markers, seriesApiMarkers) {
return JSON.stringify(markers) === JSON.stringify(seriesApiMarkers);
return JSON.stringify(reorderFieldInArray(markers)) === JSON.stringify(reorderFieldInArray(seriesApiMarkers));
}

function runTestCase(container) {
Expand Down Expand Up @@ -34,5 +48,5 @@ function runTestCase(container) {
},
});

console.assert(compare(markers, seriesApiMarkers), `series.markers() should return exactly the same that was provided to series.setMarkers()\n${JSON.stringify(seriesApiMarkers)}`);
console.assert(compare(markers, seriesApiMarkers), `series.markers() should return exactly the same that was provided to series.setMarkers()\n${JSON.stringify(seriesApiMarkers)}\n${JSON.stringify(markers)}`);
}
4 changes: 0 additions & 4 deletions tests/e2e/graphics/test-cases/horizontal-price-scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class HorzScaleBehaviorPrice {
return price => price;
}

convertInternalToHorzItem(item) {
return item;
}

key(item) {
return item;
}
Expand Down

0 comments on commit 16890a9

Please sign in to comment.