@record-evolution/widget-linechart 1.4.9 → 1.4.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,242 +1,254 @@
1
- import { html, css, LitElement } from 'lit';
1
+ import { html, css, LitElement } from 'lit'
2
2
  import { repeat } from 'lit/directives/repeat.js'
3
- import { property, state } from 'lit/decorators.js';
4
- import Chart, { ChartDataset } from 'chart.js/auto';
5
- import tinycolor from "tinycolor2";
3
+ import { property, state } from 'lit/decorators.js'
4
+ import Chart, { ChartDataset } from 'chart.js/auto'
5
+ import tinycolor from 'tinycolor2'
6
6
  // This does not work. See comments at the end of the file.
7
7
  // import 'chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm';
8
8
  // import 'chartjs-adapter-moment';
9
9
  // import 'chartjs-adapter-date-fns';
10
- import { InputData, Data, Dataseries } from './types.js'
10
+ import { ConfigureTheChart } from './definition-schema.js'
11
+
12
+ type Dataseries = Exclude<ConfigureTheChart['dataseries'], undefined>[number]
13
+ type Data = Exclude<Dataseries['data'], undefined>[number]
11
14
 
12
15
  export class WidgetLinechart extends LitElement {
13
-
14
- @property({type: Object})
15
- inputData = {} as InputData
16
-
17
- @state()
18
- private canvasList: Map<string, {chart?: any, dataSets: Dataseries[]}> = new Map()
19
-
20
- version: string = 'versionplaceholder'
21
-
22
- update(changedProperties: Map<string, any>) {
23
- if (changedProperties.has('inputData')) {
24
- this.transformInputData()
25
- this.applyInputData()
16
+ @property({ type: Object })
17
+ inputData?: ConfigureTheChart
18
+
19
+ @state()
20
+ private canvasList: Map<string, { chart?: any; dataSets: Dataseries[] }> = new Map()
21
+
22
+ version: string = 'versionplaceholder'
23
+
24
+ update(changedProperties: Map<string, any>) {
25
+ if (changedProperties.has('inputData')) {
26
+ this.transformInputData()
27
+ this.applyInputData()
28
+ }
29
+ super.update(changedProperties)
26
30
  }
27
- super.update(changedProperties)
28
- }
29
-
30
- protected firstUpdated(): void {
31
- this.applyInputData()
32
- }
33
-
34
- transformInputData() {
35
-
36
- if (!this?.inputData?.dataseries.length) return
37
-
38
- // reset all existing chart dataseries
39
- this.canvasList.forEach(chartM => chartM.dataSets = [])
40
- this.inputData.dataseries.forEach(ds => {
41
- ds.chartName = ds.chartName ?? ''
42
- if (ds.borderDash && typeof ds.borderDash === 'string') {
43
- ds.borderDash = JSON.parse(ds.borderDash)
44
- } else {
45
- ds.borderDash = undefined
46
- }
47
-
48
- // pivot data
49
- const distincts = [...new Set(ds.data.map((d: Data) => d.pivot))]
50
- const derivedBgColors = tinycolor(ds.backgroundColor).monochromatic(distincts.length).map((c: any) => c.toHexString())
51
- const derivedBdColors = tinycolor(ds.borderColor).monochromatic(distincts.length).map((c: any) => c.toHexString())
52
-
53
- if (distincts.length > 1) {
54
- distincts.forEach((piv, i) => {
55
- const pds: any = {
56
- label: ds.label + ' ' + piv,
57
- type: ds.type,
58
- showLine: true,
59
- radius: ds.radius,
60
- pointStyle: ds.pointStyle,
61
- backgroundColor: derivedBgColors[i],
62
- borderColor: derivedBdColors[i],
63
- borderWidth: ds.borderWidth,
64
- borderDash: ds.borderDash,
65
- fill: ds.fill,
66
- data: ds.data.filter(d => d.pivot === piv)
67
- }
68
- // If the chartName ends with :pivot: then create a seperate chart for each pivoted dataseries
69
- const chartName = ds.chartName.endsWith('#pivot#') ? ds.chartName + piv : ds.chartName
70
- if (!this.canvasList.has(chartName)) {
71
- // initialize new charts
72
- this.canvasList.set(chartName, {chart: undefined, dataSets: [] as Dataseries[]})
73
- }
74
- this.canvasList.get(chartName)?.dataSets.push(pds)
75
- })
76
- } else {
77
- if (!this.canvasList.has(ds.chartName)) {
78
- // initialize new charts
79
- this.canvasList.set(ds.chartName, {chart: undefined, dataSets: [] as Dataseries[]})
80
- }
81
- this.canvasList.get(ds.chartName)?.dataSets.push(ds)
82
- }
83
- })
84
- // prevent duplicate transformation
85
- this.inputData.dataseries = []
86
- // console.log('new linechart datasets', this.canvasList)
87
- }
88
-
89
-
90
- applyInputData() {
91
- this.setupCharts()
92
-
93
- this.canvasList.forEach(({chart, dataSets}) => {
94
- if (chart) {
95
- chart.data.datasets = dataSets
96
- chart.options.scales.x.type = this.xAxisType()
97
- chart.options.scales.x.title.display = !!this.inputData?.settings?.xAxisLabel
98
- chart.options.scales.x.title.text = this.inputData?.settings?.xAxisLabel
99
- chart.options.scales.y.title.display = !!this.inputData?.settings?.yAxisLabel
100
- chart.options.scales.y.title.text = this.inputData?.settings?.yAxisLabel
101
- chart?.update('resize')
102
- }
103
- })
104
- }
105
-
106
- xAxisType() {
107
- if (this.inputData?.settings?.timeseries) return 'time'
108
- const onePoint = this.inputData.dataseries?.[0].data?.[0]
109
- // @ts-ignore
110
- if (onePoint && !isNaN(onePoint.x)) return 'linear'
111
- return 'category'
112
- }
113
-
114
- setupCharts() {
115
-
116
- this.canvasList.forEach((chartM, chartName) => {
117
- if (!chartM.dataSets.length) this.canvasList.delete(chartName)
118
- if (chartM.chart) return
119
- const canvas = this.shadowRoot?.querySelector(`[name="${chartName}"]`) as HTMLCanvasElement
120
- if (!canvas) return
121
- // console.log('chartM', canvas, chartM.chart)
122
- chartM.chart = new Chart(
123
- canvas,
124
- {
125
- type: 'line',
126
- data: {
127
- // @ts-ignore
128
- datasets: chartM.dataSets
129
- },
130
- options: {
131
- responsive: true,
132
- maintainAspectRatio: false,
133
- animations: {
134
- "colors": false,
135
- "x": false,
136
- },
137
- transitions: {
138
- "active": {
139
- "animation": {
140
- "duration": 100
141
- }
142
- }
143
- },
144
- scales: {
145
- x: {
146
- type: this.xAxisType(),
147
- title: {
148
- display: !!this.inputData?.settings?.xAxisLabel,
149
- text: this.inputData?.settings?.xAxisLabel
31
+
32
+ protected firstUpdated(): void {
33
+ this.applyInputData()
34
+ }
35
+
36
+ transformInputData() {
37
+ if (!this?.inputData?.dataseries?.length) return
38
+
39
+ // reset all existing chart dataseries
40
+ this.canvasList.forEach((chartM) => (chartM.dataSets = []))
41
+ this.inputData.dataseries.forEach((ds) => {
42
+ ds.chartName = ds.chartName ?? ''
43
+ if (ds.borderDash && typeof ds.borderDash === 'string') {
44
+ ds.borderDash = JSON.parse(ds.borderDash)
45
+ } else {
46
+ ds.borderDash = undefined
47
+ }
48
+
49
+ // pivot data
50
+ const distincts = [...new Set(ds.data?.map((d: Data) => d.pivot))]
51
+ const derivedBgColors = tinycolor(ds.backgroundColor)
52
+ .monochromatic(distincts.length)
53
+ .map((c: any) => c.toHexString())
54
+ const derivedBdColors = tinycolor(ds.borderColor)
55
+ .monochromatic(distincts.length)
56
+ .map((c: any) => c.toHexString())
57
+
58
+ if (distincts.length > 1) {
59
+ distincts.forEach((piv, i) => {
60
+ const pds: any = {
61
+ label: ds.label + ' ' + piv,
62
+ type: ds.type,
63
+ showLine: true,
64
+ radius: ds.radius,
65
+ pointStyle: ds.pointStyle,
66
+ backgroundColor: derivedBgColors[i],
67
+ borderColor: derivedBdColors[i],
68
+ borderWidth: ds.borderWidth,
69
+ borderDash: ds.borderDash,
70
+ fill: ds.fill,
71
+ data: ds.data?.filter((d) => d.pivot === piv)
72
+ }
73
+ // If the chartName ends with :pivot: then create a seperate chart for each pivoted dataseries
74
+ const chartName = ds.chartName?.endsWith('#pivot#')
75
+ ? ds.chartName + piv
76
+ : ds.chartName ?? ''
77
+ if (!this.canvasList.has(chartName)) {
78
+ // initialize new charts
79
+ this.canvasList.set(chartName, { chart: undefined, dataSets: [] as Dataseries[] })
80
+ }
81
+ this.canvasList.get(chartName)?.dataSets.push(pds)
82
+ })
83
+ } else {
84
+ if (!this.canvasList.has(ds.chartName)) {
85
+ // initialize new charts
86
+ this.canvasList.set(ds.chartName, { chart: undefined, dataSets: [] as Dataseries[] })
150
87
  }
151
- },
152
- y: {
153
- title: {
154
- display: !!this.inputData?.settings?.yAxisLabel,
155
- text: this.inputData?.settings?.yAxisLabel
88
+ this.canvasList.get(ds.chartName)?.dataSets.push(ds)
89
+ }
90
+ })
91
+ // prevent duplicate transformation
92
+ this.inputData.dataseries = []
93
+ // console.log('new linechart datasets', this.canvasList)
94
+ }
95
+
96
+ applyInputData() {
97
+ this.setupCharts()
98
+
99
+ this.requestUpdate()
100
+ this.canvasList.forEach(({ chart, dataSets }) => {
101
+ if (chart) {
102
+ chart.data.datasets = dataSets
103
+ chart.options.scales.x.type = this.xAxisType()
104
+ chart.options.scales.x.title.display = !!this.inputData?.settings?.xAxisLabel
105
+ chart.options.scales.x.title.text = this.inputData?.settings?.xAxisLabel
106
+ chart.options.scales.y.title.display = !!this.inputData?.settings?.yAxisLabel
107
+ chart.options.scales.y.title.text = this.inputData?.settings?.yAxisLabel
108
+ chart?.update('resize')
109
+ }
110
+ })
111
+ }
112
+
113
+ xAxisType(): 'linear' | 'logarithmic' | 'category' | 'time' | 'timeseries' | undefined {
114
+ if (this.inputData?.settings?.timeseries) return 'time'
115
+ const onePoint = this.inputData?.dataseries?.[0].data?.[0]
116
+ if (!isNaN(Number(onePoint?.x))) return 'linear'
117
+ return 'category'
118
+ }
119
+
120
+ setupCharts() {
121
+ this.canvasList.forEach((chartM, chartName) => {
122
+ if (!chartM.dataSets.length) this.canvasList.delete(chartName)
123
+ if (chartM.chart) return
124
+ const canvas = this.shadowRoot?.querySelector(`[name="${chartName}"]`) as HTMLCanvasElement
125
+ if (!canvas) return
126
+ // console.log('chartM', canvas, chartM.chart)
127
+ chartM.chart = new Chart(canvas, {
128
+ type: 'line',
129
+ data: {
130
+ // @ts-ignore
131
+ datasets: chartM.dataSets
132
+ },
133
+ options: {
134
+ responsive: true,
135
+ maintainAspectRatio: false,
136
+ animations: {
137
+ colors: false,
138
+ x: false
139
+ },
140
+ transitions: {
141
+ active: {
142
+ animation: {
143
+ duration: 100
144
+ }
145
+ }
146
+ },
147
+ scales: {
148
+ x: {
149
+ type: this.xAxisType(),
150
+ title: {
151
+ display: !!this.inputData?.settings?.xAxisLabel,
152
+ text: this.inputData?.settings?.xAxisLabel
153
+ }
154
+ },
155
+ y: {
156
+ title: {
157
+ display: !!this.inputData?.settings?.yAxisLabel,
158
+ text: this.inputData?.settings?.yAxisLabel
159
+ }
160
+ }
161
+ }
156
162
  }
157
- }
158
- },
159
- },
160
- }
161
- )
162
- })
163
- }
164
-
165
- static styles = css`
166
- :host {
167
- display: block;
168
- color: var(--re-text-color, #000);
169
-
170
- font-family: sans-serif;
171
- padding: 16px;
172
- box-sizing: border-box;
173
- margin: auto;
174
- }
175
-
176
- .paging:not([active]) { display: none !important; }
177
-
178
- .columnLayout {
179
- flex-direction: column;
180
- }
181
-
182
- .wrapper {
183
- display: flex;
184
- flex-direction: column;
185
- height: 100%;
186
- width: 100%;
187
- }
188
-
189
- .chart-container {
190
- display: flex;
191
- flex: 1;
192
- overflow: hidden;
193
- position: relative;
163
+ })
164
+ })
194
165
  }
195
166
 
196
- .sizer {
197
- flex: 1;
198
- overflow: hidden;
199
- position: relative;
200
- }
201
-
202
- header {
203
- display: flex;
204
- flex-direction: column;
205
- margin: 0 0 16px 0;
206
- }
207
- h3 {
208
- margin: 0;
209
- max-width: 300px;
210
- overflow: hidden;
211
- text-overflow: ellipsis;
212
- white-space: nowrap;
213
- }
214
- p {
215
- margin: 10px 0 0 0;
216
- max-width: 300px;
217
- font-size: 14px;
218
- line-height: 17px;
219
- }
220
- `;
221
-
222
- render() {
223
- return html`
224
- <div class="wrapper">
225
- <header>
226
- <h3 class="paging" ?active=${this.inputData?.settings?.title}>${this.inputData?.settings?.title}</h3>
227
- <p class="paging" ?active=${this.inputData?.settings?.subTitle}>${this.inputData?.settings?.subTitle}</p>
228
- </header>
229
-
230
- <div class="chart-container ${this?.inputData?.settings?.columnLayout ? 'columnLayout': ''}">
231
- ${repeat(this.canvasList, ([chartName, chartM]) => chartName, ([chartName]) => html`
232
- <div class="sizer">
233
- <canvas name="${chartName}"></canvas>
167
+ static styles = css`
168
+ :host {
169
+ display: block;
170
+ color: var(--re-text-color, #000);
171
+
172
+ font-family: sans-serif;
173
+ padding: 16px;
174
+ box-sizing: border-box;
175
+ margin: auto;
176
+ }
177
+
178
+ .paging:not([active]) {
179
+ display: none !important;
180
+ }
181
+
182
+ .columnLayout {
183
+ flex-direction: column;
184
+ }
185
+
186
+ .wrapper {
187
+ display: flex;
188
+ flex-direction: column;
189
+ height: 100%;
190
+ width: 100%;
191
+ }
192
+
193
+ .chart-container {
194
+ display: flex;
195
+ flex: 1;
196
+ overflow: hidden;
197
+ position: relative;
198
+ }
199
+
200
+ .sizer {
201
+ flex: 1;
202
+ overflow: hidden;
203
+ position: relative;
204
+ }
205
+
206
+ header {
207
+ display: flex;
208
+ flex-direction: column;
209
+ margin: 0 0 16px 0;
210
+ }
211
+ h3 {
212
+ margin: 0;
213
+ max-width: 300px;
214
+ overflow: hidden;
215
+ text-overflow: ellipsis;
216
+ white-space: nowrap;
217
+ }
218
+ p {
219
+ margin: 10px 0 0 0;
220
+ max-width: 300px;
221
+ font-size: 14px;
222
+ line-height: 17px;
223
+ }
224
+ `
225
+
226
+ render() {
227
+ return html`
228
+ <div class="wrapper">
229
+ <header>
230
+ <h3 class="paging" ?active=${this.inputData?.settings?.title}>
231
+ ${this.inputData?.settings?.title}
232
+ </h3>
233
+ <p class="paging" ?active=${this.inputData?.settings?.subTitle}>
234
+ ${this.inputData?.settings?.subTitle}
235
+ </p>
236
+ </header>
237
+
238
+ <div class="chart-container ${this?.inputData?.settings?.columnLayout ? 'columnLayout' : ''}">
239
+ ${repeat(
240
+ [...this.canvasList.entries()].sort(),
241
+ ([chartName, chartM]) => chartName,
242
+ ([chartName]) => html`
243
+ <div class="sizer">
244
+ <canvas name="${chartName}"></canvas>
245
+ </div>
246
+ `
247
+ )}
248
+ </div>
234
249
  </div>
235
- `)}
236
- </div>
237
- </div>
238
- `;
239
- }
250
+ `
251
+ }
240
252
  }
241
253
  window.customElements.define('widget-linechart-versionplaceholder', WidgetLinechart)
242
254
 
@@ -247,125 +259,193 @@ window.customElements.define('widget-linechart-versionplaceholder', WidgetLinech
247
259
  // So the current solution is to execute the source code here in-line. (moving this to a local file and importing that does not work!)
248
260
  // This is the source code of https://github.com/chartjs/chartjs-adapter-date-fns/blob/master/src/index.js
249
261
 
250
- import {_adapters} from 'chart.js';
262
+ import { _adapters } from 'chart.js'
251
263
 
252
264
  import {
253
- parse, parseISO, toDate, isValid, format,
254
- startOfSecond, startOfMinute, startOfHour, startOfDay,
255
- startOfWeek, startOfMonth, startOfQuarter, startOfYear,
256
- addMilliseconds, addSeconds, addMinutes, addHours,
257
- addDays, addWeeks, addMonths, addQuarters, addYears,
258
- differenceInMilliseconds, differenceInSeconds, differenceInMinutes,
259
- differenceInHours, differenceInDays, differenceInWeeks,
260
- differenceInMonths, differenceInQuarters, differenceInYears,
261
- endOfSecond, endOfMinute, endOfHour, endOfDay,
262
- endOfWeek, endOfMonth, endOfQuarter, endOfYear
263
- } from 'date-fns';
265
+ parse,
266
+ parseISO,
267
+ toDate,
268
+ isValid,
269
+ format,
270
+ startOfSecond,
271
+ startOfMinute,
272
+ startOfHour,
273
+ startOfDay,
274
+ startOfWeek,
275
+ startOfMonth,
276
+ startOfQuarter,
277
+ startOfYear,
278
+ addMilliseconds,
279
+ addSeconds,
280
+ addMinutes,
281
+ addHours,
282
+ addDays,
283
+ addWeeks,
284
+ addMonths,
285
+ addQuarters,
286
+ addYears,
287
+ differenceInMilliseconds,
288
+ differenceInSeconds,
289
+ differenceInMinutes,
290
+ differenceInHours,
291
+ differenceInDays,
292
+ differenceInWeeks,
293
+ differenceInMonths,
294
+ differenceInQuarters,
295
+ differenceInYears,
296
+ endOfSecond,
297
+ endOfMinute,
298
+ endOfHour,
299
+ endOfDay,
300
+ endOfWeek,
301
+ endOfMonth,
302
+ endOfQuarter,
303
+ endOfYear
304
+ } from 'date-fns'
264
305
 
265
306
  const FORMATS = {
266
- datetime: 'MMM d, yyyy, h:mm:ss aaaa',
267
- millisecond: 'h:mm:ss.SSS aaaa',
268
- second: 'h:mm:ss aaaa',
269
- minute: 'h:mm aaaa',
270
- hour: 'ha',
271
- day: 'MMM d',
272
- week: 'PP',
273
- month: 'MMM yyyy',
274
- quarter: 'qqq - yyyy',
275
- year: 'yyyy'
276
- };
307
+ datetime: 'MMM d, yyyy, h:mm:ss aaaa',
308
+ millisecond: 'h:mm:ss.SSS aaaa',
309
+ second: 'h:mm:ss aaaa',
310
+ minute: 'h:mm aaaa',
311
+ hour: 'ha',
312
+ day: 'MMM d',
313
+ week: 'PP',
314
+ month: 'MMM yyyy',
315
+ quarter: 'qqq - yyyy',
316
+ year: 'yyyy'
317
+ }
277
318
 
278
319
  _adapters._date.override({
279
- _id: 'date-fns', // DEBUG
280
- formats: function() {
281
- return FORMATS;
282
- },
283
-
284
- parse: function(value, fmt) {
285
- if (value === null || typeof value === 'undefined') {
286
- return null;
287
- }
288
- const type = typeof value;
289
- if (type === 'number' || value instanceof Date) {
290
- // @ts-ignore
291
- value = toDate(value);
292
- } else if (type === 'string') {
293
- if (typeof fmt === 'string') {
294
- // @ts-ignore
295
- value = parse(value, fmt, new Date(), this.options);
296
- } else {
297
- // @ts-ignore
298
- value = parseISO(value, this.options);
299
- }
300
- }
301
- // @ts-ignore
302
- return isValid(value) ? value.getTime() : null;
303
- },
304
-
305
- format: function(time, fmt) {
306
- return format(time, fmt, this.options);
307
- },
308
-
309
- // @ts-ignore
310
- add: function(time, amount, unit) {
311
- switch (unit) {
312
- case 'millisecond': return addMilliseconds(time, amount);
313
- case 'second': return addSeconds(time, amount);
314
- case 'minute': return addMinutes(time, amount);
315
- case 'hour': return addHours(time, amount);
316
- case 'day': return addDays(time, amount);
317
- case 'week': return addWeeks(time, amount);
318
- case 'month': return addMonths(time, amount);
319
- case 'quarter': return addQuarters(time, amount);
320
- case 'year': return addYears(time, amount);
321
- default: return time;
322
- }
323
- },
324
-
325
- diff: function(max, min, unit) {
326
- switch (unit) {
327
- case 'millisecond': return differenceInMilliseconds(max, min);
328
- case 'second': return differenceInSeconds(max, min);
329
- case 'minute': return differenceInMinutes(max, min);
330
- case 'hour': return differenceInHours(max, min);
331
- case 'day': return differenceInDays(max, min);
332
- case 'week': return differenceInWeeks(max, min);
333
- case 'month': return differenceInMonths(max, min);
334
- case 'quarter': return differenceInQuarters(max, min);
335
- case 'year': return differenceInYears(max, min);
336
- default: return 0;
337
- }
338
- },
339
-
340
- // @ts-ignore
341
- startOf: function(time, unit, weekday) {
342
- switch (unit) {
343
- case 'second': return startOfSecond(time);
344
- case 'minute': return startOfMinute(time);
345
- case 'hour': return startOfHour(time);
346
- case 'day': return startOfDay(time);
347
- case 'week': return startOfWeek(time);
348
- // @ts-ignore
349
- case 'isoWeek': return startOfWeek(time, {weekStartsOn: +weekday});
350
- case 'month': return startOfMonth(time);
351
- case 'quarter': return startOfQuarter(time);
352
- case 'year': return startOfYear(time);
353
- default: return time;
354
- }
355
- },
356
-
357
- // @ts-ignore
358
- endOf: function(time, unit) {
359
- switch (unit) {
360
- case 'second': return endOfSecond(time);
361
- case 'minute': return endOfMinute(time);
362
- case 'hour': return endOfHour(time);
363
- case 'day': return endOfDay(time);
364
- case 'week': return endOfWeek(time);
365
- case 'month': return endOfMonth(time);
366
- case 'quarter': return endOfQuarter(time);
367
- case 'year': return endOfYear(time);
368
- default: return time;
320
+ _id: 'date-fns', // DEBUG
321
+ formats: function () {
322
+ return FORMATS
323
+ },
324
+
325
+ parse: function (value, fmt) {
326
+ if (value === null || typeof value === 'undefined') {
327
+ return null
328
+ }
329
+ const type = typeof value
330
+ if (type === 'number' || value instanceof Date) {
331
+ // @ts-ignore
332
+ value = toDate(value)
333
+ } else if (type === 'string') {
334
+ if (typeof fmt === 'string') {
335
+ // @ts-ignore
336
+ value = parse(value, fmt, new Date(), this.options)
337
+ } else {
338
+ // @ts-ignore
339
+ value = parseISO(value, this.options)
340
+ }
341
+ }
342
+ // @ts-ignore
343
+ return isValid(value) ? value.getTime() : null
344
+ },
345
+
346
+ format: function (time, fmt) {
347
+ return format(time, fmt, this.options)
348
+ },
349
+
350
+ // @ts-ignore
351
+ add: function (time, amount, unit) {
352
+ switch (unit) {
353
+ case 'millisecond':
354
+ return addMilliseconds(time, amount)
355
+ case 'second':
356
+ return addSeconds(time, amount)
357
+ case 'minute':
358
+ return addMinutes(time, amount)
359
+ case 'hour':
360
+ return addHours(time, amount)
361
+ case 'day':
362
+ return addDays(time, amount)
363
+ case 'week':
364
+ return addWeeks(time, amount)
365
+ case 'month':
366
+ return addMonths(time, amount)
367
+ case 'quarter':
368
+ return addQuarters(time, amount)
369
+ case 'year':
370
+ return addYears(time, amount)
371
+ default:
372
+ return time
373
+ }
374
+ },
375
+
376
+ diff: function (max, min, unit) {
377
+ switch (unit) {
378
+ case 'millisecond':
379
+ return differenceInMilliseconds(max, min)
380
+ case 'second':
381
+ return differenceInSeconds(max, min)
382
+ case 'minute':
383
+ return differenceInMinutes(max, min)
384
+ case 'hour':
385
+ return differenceInHours(max, min)
386
+ case 'day':
387
+ return differenceInDays(max, min)
388
+ case 'week':
389
+ return differenceInWeeks(max, min)
390
+ case 'month':
391
+ return differenceInMonths(max, min)
392
+ case 'quarter':
393
+ return differenceInQuarters(max, min)
394
+ case 'year':
395
+ return differenceInYears(max, min)
396
+ default:
397
+ return 0
398
+ }
399
+ },
400
+
401
+ // @ts-ignore
402
+ startOf: function (time, unit, weekday) {
403
+ switch (unit) {
404
+ case 'second':
405
+ return startOfSecond(time)
406
+ case 'minute':
407
+ return startOfMinute(time)
408
+ case 'hour':
409
+ return startOfHour(time)
410
+ case 'day':
411
+ return startOfDay(time)
412
+ case 'week':
413
+ return startOfWeek(time)
414
+ case 'isoWeek':
415
+ // @ts-ignore
416
+ return startOfWeek(time, { weekStartsOn: +weekday })
417
+ case 'month':
418
+ return startOfMonth(time)
419
+ case 'quarter':
420
+ return startOfQuarter(time)
421
+ case 'year':
422
+ return startOfYear(time)
423
+ default:
424
+ return time
425
+ }
426
+ },
427
+
428
+ // @ts-ignore
429
+ endOf: function (time, unit) {
430
+ switch (unit) {
431
+ case 'second':
432
+ return endOfSecond(time)
433
+ case 'minute':
434
+ return endOfMinute(time)
435
+ case 'hour':
436
+ return endOfHour(time)
437
+ case 'day':
438
+ return endOfDay(time)
439
+ case 'week':
440
+ return endOfWeek(time)
441
+ case 'month':
442
+ return endOfMonth(time)
443
+ case 'quarter':
444
+ return endOfQuarter(time)
445
+ case 'year':
446
+ return endOfYear(time)
447
+ default:
448
+ return time
449
+ }
369
450
  }
370
- }
371
- });
451
+ })