/
core.adapters.ts
133 lines (116 loc) · 3.74 KB
/
core.adapters.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @namespace Chart._adapters
* @since 2.8.0
* @private
*/
import type {AnyObject} from '../types/basic.js';
import type {ChartOptions} from '../types/index.js';
export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
export interface DateAdapter<T extends AnyObject = AnyObject> {
readonly options: T;
/**
* Will called with chart options after adapter creation.
*/
init(this: DateAdapter<T>, chartOptions: ChartOptions): void;
/**
* Returns a map of time formats for the supported formatting units defined
* in Unit as well as 'datetime' representing a detailed date/time string.
*/
formats(this: DateAdapter<T>): Record<string, string>;
/**
* Parses the given `value` and return the associated timestamp.
* @param value - the value to parse (usually comes from the data)
* @param [format] - the expected data format
*/
parse(this: DateAdapter<T>, value: unknown, format?: TimeUnit): number | null;
/**
* Returns the formatted date in the specified `format` for a given `timestamp`.
* @param timestamp - the timestamp to format
* @param format - the date/time token
*/
format(this: DateAdapter<T>, timestamp: number, format: TimeUnit): string;
/**
* Adds the specified `amount` of `unit` to the given `timestamp`.
* @param timestamp - the input timestamp
* @param amount - the amount to add
* @param unit - the unit as string
*/
add(this: DateAdapter<T>, timestamp: number, amount: number, unit: TimeUnit): number;
/**
* Returns the number of `unit` between the given timestamps.
* @param a - the input timestamp (reference)
* @param b - the timestamp to subtract
* @param unit - the unit as string
*/
diff(this: DateAdapter<T>, a: number, b: number, unit: TimeUnit): number;
/**
* Returns start of `unit` for the given `timestamp`.
* @param timestamp - the input timestamp
* @param unit - the unit as string
* @param [weekday] - the ISO day of the week with 1 being Monday
* and 7 being Sunday (only needed if param *unit* is `isoWeek`).
*/
startOf(this: DateAdapter<T>, timestamp: number, unit: TimeUnit | 'isoWeek', weekday?: number): number;
/**
* Returns end of `unit` for the given `timestamp`.
* @param timestamp - the input timestamp
* @param unit - the unit as string
*/
endOf(this: DateAdapter<T>, timestamp: number, unit: TimeUnit | 'isoWeek'): number;
}
function abstract<T = void>(): T {
throw new Error('This method is not implemented: Check that a complete date adapter is provided.');
}
/**
* Date adapter (current used by the time scale)
* @namespace Chart._adapters._date
* @memberof Chart._adapters
* @private
*/
class DateAdapterBase implements DateAdapter {
/**
* Override default date adapter methods.
* Accepts type parameter to define options type.
* @example
* Chart._adapters._date.override<{myAdapterOption: string}>({
* init() {
* console.log(this.options.myAdapterOption);
* }
* })
*/
static override<T extends AnyObject = AnyObject>(
members: Partial<Omit<DateAdapter<T>, 'options'>>
) {
Object.assign(DateAdapterBase.prototype, members);
}
readonly options: AnyObject;
constructor(options: AnyObject) {
this.options = options || {};
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
init() {}
formats(): Record<string, string> {
return abstract();
}
parse(): number | null {
return abstract();
}
format(): string {
return abstract();
}
add(): number {
return abstract();
}
diff(): number {
return abstract();
}
startOf(): number {
return abstract();
}
endOf(): number {
return abstract();
}
}
export default {
_date: DateAdapterBase
};