flowerbi 1.22.0 → 2.0.0

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,211 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- var fillDates_1 = require("./fillDates");
18
- var moment_1 = __importDefault(require("moment"));
19
- test("detects days", function () {
20
- expect(fillDates_1.detectDateType([
21
- moment_1.default("2021-04-01"),
22
- moment_1.default("2020-11-01"),
23
- moment_1.default("2020-11-02"),
24
- moment_1.default("2022-07-01"),
25
- ])).toBe(fillDates_1.dateTypes.days);
26
- });
27
- test("detects months", function () {
28
- expect(fillDates_1.detectDateType([
29
- moment_1.default("2021-04-01"),
30
- moment_1.default("2020-11-01"),
31
- moment_1.default("2022-07-01"),
32
- ])).toBe(fillDates_1.dateTypes.months);
33
- });
34
- test("detects quarters", function () {
35
- expect(fillDates_1.detectDateType([
36
- moment_1.default("2021-04-01"),
37
- moment_1.default("2020-10-01"),
38
- moment_1.default("2022-07-01"),
39
- ])).toBe(fillDates_1.dateTypes.quarters);
40
- });
41
- test("detects years", function () {
42
- expect(fillDates_1.detectDateType([
43
- moment_1.default("2021-01-01"),
44
- moment_1.default("2020-01-01"),
45
- moment_1.default("2022-01-01"),
46
- ])).toBe(fillDates_1.dateTypes.years);
47
- });
48
- test("handles empty list", function () {
49
- expect(fillDates_1.fillDates({
50
- records: [],
51
- getDate: function (x) { return 0; },
52
- fill: function () { return undefined; }
53
- })).toStrictEqual([]);
54
- });
55
- test("leaves complete months alone", function () {
56
- expect(fillDates_1.fillDates({
57
- records: [
58
- { date: "2020-04-01", totalSales: 10 },
59
- { date: "2020-05-01", totalSales: 4 },
60
- { date: "2020-06-01", totalSales: 9 },
61
- { date: "2020-07-01", totalSales: 3 },
62
- ],
63
- getDate: function (rec) { return rec.date; },
64
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); },
65
- min: "2020-04-12",
66
- max: "2020-07-06"
67
- })).toStrictEqual([
68
- { label: "Apr 2020", date: "2020-04-01", totalSales: 10 },
69
- { label: "May 2020", date: "2020-05-01", totalSales: 4 },
70
- { label: "Jun 2020", date: "2020-06-01", totalSales: 9 },
71
- { label: "Jul 2020", date: "2020-07-01", totalSales: 3 },
72
- ]);
73
- });
74
- test("fills in missing months", function () {
75
- expect(fillDates_1.fillDates({
76
- records: [
77
- { date: "2020-04-01", totalSales: 10 },
78
- { date: "2020-06-01", totalSales: 4 },
79
- { date: "2020-07-01", totalSales: 9 },
80
- { date: "2020-11-01", totalSales: 3 },
81
- ],
82
- getDate: function (rec) { return rec.date; },
83
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); }
84
- })).toStrictEqual([
85
- { label: "Apr 2020", date: "2020-04-01", totalSales: 10 },
86
- { label: "May 2020", totalSales: 0 },
87
- { label: "Jun 2020", date: "2020-06-01", totalSales: 4 },
88
- { label: "Jul 2020", date: "2020-07-01", totalSales: 9 },
89
- { label: "Aug 2020", totalSales: 0 },
90
- { label: "Sep 2020", totalSales: 0 },
91
- { label: "Oct 2020", totalSales: 0 },
92
- { label: "Nov 2020", date: "2020-11-01", totalSales: 3 },
93
- ]);
94
- });
95
- test("fills in missing months and min/max range", function () {
96
- expect(fillDates_1.fillDates({
97
- records: [
98
- { date: "2020-04-01", totalSales: 10 },
99
- { date: "2020-06-01", totalSales: 4 },
100
- { date: "2020-07-01", totalSales: 9 },
101
- { date: "2020-11-01", totalSales: 3 },
102
- ],
103
- getDate: function (rec) { return rec.date; },
104
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); },
105
- min: "2020-02-15",
106
- max: "2020-12-02"
107
- })).toStrictEqual([
108
- { label: "Feb 2020", totalSales: 0 },
109
- { label: "Mar 2020", totalSales: 0 },
110
- { label: "Apr 2020", date: "2020-04-01", totalSales: 10 },
111
- { label: "May 2020", totalSales: 0 },
112
- { label: "Jun 2020", date: "2020-06-01", totalSales: 4 },
113
- { label: "Jul 2020", date: "2020-07-01", totalSales: 9 },
114
- { label: "Aug 2020", totalSales: 0 },
115
- { label: "Sep 2020", totalSales: 0 },
116
- { label: "Oct 2020", totalSales: 0 },
117
- { label: "Nov 2020", date: "2020-11-01", totalSales: 3 },
118
- { label: "Dec 2020", totalSales: 0 },
119
- ]);
120
- });
121
- test("leaves complete quarters alone", function () {
122
- expect(fillDates_1.fillDates({
123
- records: [
124
- { date: "2020-07-01", totalSales: 10 },
125
- { date: "2020-10-01", totalSales: 4 },
126
- { date: "2021-01-01", totalSales: 9 },
127
- { date: "2021-04-01", totalSales: 3 },
128
- ],
129
- getDate: function (rec) { return rec.date; },
130
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); },
131
- min: "2020-07-12",
132
- max: "2020-04-06"
133
- })).toStrictEqual([
134
- { label: "Jul-Sep 2020", date: "2020-07-01", totalSales: 10 },
135
- { label: "Oct-Dec 2020", date: "2020-10-01", totalSales: 4 },
136
- { label: "Jan-Mar 2021", date: "2021-01-01", totalSales: 9 },
137
- { label: "Apr-Jun 2021", date: "2021-04-01", totalSales: 3 },
138
- ]);
139
- });
140
- test("fills in missing quarters", function () {
141
- expect(fillDates_1.fillDates({
142
- records: [
143
- { date: "2020-04-01", totalSales: 10 },
144
- { date: "2020-10-01", totalSales: 4 },
145
- { date: "2021-01-01", totalSales: 9 },
146
- { date: "2021-10-01", totalSales: 3 },
147
- ],
148
- type: fillDates_1.dateTypes.quarters,
149
- getDate: function (rec) { return rec.date; },
150
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); }
151
- })).toStrictEqual([
152
- { label: "Apr-Jun 2020", date: "2020-04-01", totalSales: 10 },
153
- { label: "Jul-Sep 2020", totalSales: 0 },
154
- { label: "Oct-Dec 2020", date: "2020-10-01", totalSales: 4 },
155
- { label: "Jan-Mar 2021", date: "2021-01-01", totalSales: 9 },
156
- { label: "Apr-Jun 2021", totalSales: 0 },
157
- { label: "Jul-Sep 2021", totalSales: 0 },
158
- { label: "Oct-Dec 2021", date: "2021-10-01", totalSales: 3 },
159
- ]);
160
- });
161
- test("fills in missing quarters and min/max range", function () {
162
- expect(fillDates_1.fillDates({
163
- records: [
164
- { date: "2020-04-01", totalSales: 10 },
165
- { date: "2020-10-01", totalSales: 4 },
166
- { date: "2021-01-01", totalSales: 9 },
167
- { date: "2021-10-01", totalSales: 3 },
168
- ],
169
- type: fillDates_1.dateTypes.quarters,
170
- getDate: function (rec) { return rec.date; },
171
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); },
172
- min: "2019-10-10",
173
- max: "2022-01-06"
174
- })).toStrictEqual([
175
- { label: "Oct-Dec 2019", totalSales: 0 },
176
- { label: "Jan-Mar 2020", totalSales: 0 },
177
- { label: "Apr-Jun 2020", date: "2020-04-01", totalSales: 10 },
178
- { label: "Jul-Sep 2020", totalSales: 0 },
179
- { label: "Oct-Dec 2020", date: "2020-10-01", totalSales: 4 },
180
- { label: "Jan-Mar 2021", date: "2021-01-01", totalSales: 9 },
181
- { label: "Apr-Jun 2021", totalSales: 0 },
182
- { label: "Jul-Sep 2021", totalSales: 0 },
183
- { label: "Oct-Dec 2021", date: "2021-10-01", totalSales: 3 },
184
- { label: "Jan-Mar 2022", totalSales: 0 },
185
- ]);
186
- });
187
- test("explicit type overrides detection", function () {
188
- expect(fillDates_1.fillDates({
189
- records: [
190
- { date: "2020-07-01", totalSales: 10 },
191
- { date: "2020-10-01", totalSales: 4 },
192
- { date: "2021-01-01", totalSales: 9 },
193
- { date: "2021-04-01", totalSales: 3 },
194
- ],
195
- getDate: function (rec) { return rec.date; },
196
- fill: function (label, rec) { return (__assign({ label: label, totalSales: 0 }, rec)); },
197
- type: fillDates_1.dateTypes.months
198
- })).toStrictEqual([
199
- { label: "Jul 2020", date: "2020-07-01", totalSales: 10 },
200
- { label: "Aug 2020", totalSales: 0 },
201
- { label: "Sep 2020", totalSales: 0 },
202
- { label: "Oct 2020", date: "2020-10-01", totalSales: 4 },
203
- { label: "Nov 2020", totalSales: 0 },
204
- { label: "Dec 2020", totalSales: 0 },
205
- { label: "Jan 2021", date: "2021-01-01", totalSales: 9 },
206
- { label: "Feb 2021", totalSales: 0 },
207
- { label: "Mar 2021", totalSales: 0 },
208
- { label: "Apr 2021", date: "2021-04-01", totalSales: 3 },
209
- ]);
210
- });
211
- //# sourceMappingURL=fillDates.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fillDates.test.js","sourceRoot":"","sources":["../src/fillDates.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAmE;AACnE,kDAA4B;AAE5B,IAAI,CAAC,cAAc,EAAE;IACjB,MAAM,CAAC,0BAAc,CAAC;QAClB,gBAAM,CAAC,YAAY,CAAC;QACpB,gBAAM,CAAC,YAAY,CAAC;QACpB,gBAAM,CAAC,YAAY,CAAC;QACpB,gBAAM,CAAC,YAAY,CAAC;KACvB,CAAC,CAAC,CAAC,IAAI,CAAC,qBAAS,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,gBAAgB,EAAE;IACnB,MAAM,CAAC,0BAAc,CAAC;QAClB,gBAAM,CAAC,YAAY,CAAC;QACpB,gBAAM,CAAC,YAAY,CAAC;QACpB,gBAAM,CAAC,YAAY,CAAC;KACvB,CAAC,CAAC,CAAC,IAAI,CAAC,qBAAS,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,kBAAkB,EAAE;IACrB,MAAM,CA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;QACpC,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC,EAAE;QACxD,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,EAAE;QACpC,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,EAAE;QACpC,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC,EAAE;QACxD,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,EAAE;QACpC,EAAE,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,EAAE;QACpC,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC,EAAE;KAC3D,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
@@ -1,234 +0,0 @@
1
- import { fillDates, dateTypes, detectDateType } from "./fillDates";
2
- import moment from "moment";
3
-
4
- test("detects days", () => {
5
- expect(detectDateType([
6
- moment("2021-04-01"),
7
- moment("2020-11-01"),
8
- moment("2020-11-02"),
9
- moment("2022-07-01"),
10
- ])).toBe(dateTypes.days);
11
- });
12
-
13
- test("detects months", () => {
14
- expect(detectDateType([
15
- moment("2021-04-01"),
16
- moment("2020-11-01"),
17
- moment("2022-07-01"),
18
- ])).toBe(dateTypes.months);
19
- });
20
-
21
- test("detects quarters", () => {
22
- expect(detectDateType([
23
- moment("2021-04-01"),
24
- moment("2020-10-01"),
25
- moment("2022-07-01"),
26
- ])).toBe(dateTypes.quarters);
27
- });
28
-
29
- test("detects years", () => {
30
- expect(detectDateType([
31
- moment("2021-01-01"),
32
- moment("2020-01-01"),
33
- moment("2022-01-01"),
34
- ])).toBe(dateTypes.years);
35
- });
36
-
37
- test("handles empty list", () => {
38
- expect(fillDates({
39
- records: [],
40
- getDate: x => 0,
41
- fill: () => undefined
42
- })).toStrictEqual([]);
43
- });
44
-
45
- test("leaves complete months alone", () => {
46
- expect(fillDates({
47
- records: [
48
- { date: "2020-04-01", totalSales: 10 },
49
- { date: "2020-05-01", totalSales: 4 },
50
- { date: "2020-06-01", totalSales: 9 },
51
- { date: "2020-07-01", totalSales: 3 },
52
- ],
53
- getDate: rec => rec.date,
54
- fill: (label, rec) => ({
55
- label,
56
- totalSales: 0,
57
- ...rec
58
- }),
59
- min: "2020-04-12",
60
- max: "2020-07-06"
61
- })).toStrictEqual([
62
- { label: "Apr 2020", date: "2020-04-01", totalSales: 10 },
63
- { label: "May 2020", date: "2020-05-01", totalSales: 4 },
64
- { label: "Jun 2020", date: "2020-06-01", totalSales: 9 },
65
- { label: "Jul 2020", date: "2020-07-01", totalSales: 3 },
66
- ]);
67
- });
68
-
69
- test("fills in missing months", () => {
70
- expect(fillDates({
71
- records: [
72
- { date: "2020-04-01", totalSales: 10 },
73
- { date: "2020-06-01", totalSales: 4 },
74
- { date: "2020-07-01", totalSales: 9 },
75
- { date: "2020-11-01", totalSales: 3 },
76
- ],
77
- getDate: rec => rec.date,
78
- fill: (label, rec) => ({
79
- label,
80
- totalSales: 0,
81
- ...rec
82
- })
83
- })).toStrictEqual([
84
- { label: "Apr 2020", date: "2020-04-01", totalSales: 10 },
85
- { label: "May 2020", totalSales: 0 },
86
- { label: "Jun 2020", date: "2020-06-01", totalSales: 4 },
87
- { label: "Jul 2020", date: "2020-07-01", totalSales: 9 },
88
- { label: "Aug 2020", totalSales: 0 },
89
- { label: "Sep 2020", totalSales: 0 },
90
- { label: "Oct 2020", totalSales: 0 },
91
- { label: "Nov 2020", date: "2020-11-01", totalSales: 3 },
92
- ]);
93
- });
94
-
95
- test("fills in missing months and min/max range", () => {
96
- expect(fillDates({
97
- records: [
98
- { date: "2020-04-01", totalSales: 10 },
99
- { date: "2020-06-01", totalSales: 4 },
100
- { date: "2020-07-01", totalSales: 9 },
101
- { date: "2020-11-01", totalSales: 3 },
102
- ],
103
- getDate: rec => rec.date,
104
- fill: (label, rec) => ({
105
- label,
106
- totalSales: 0,
107
- ...rec
108
- }),
109
- min: "2020-02-15",
110
- max: "2020-12-02"
111
- })).toStrictEqual([
112
- { label: "Feb 2020", totalSales: 0 },
113
- { label: "Mar 2020", totalSales: 0 },
114
- { label: "Apr 2020", date: "2020-04-01", totalSales: 10 },
115
- { label: "May 2020", totalSales: 0 },
116
- { label: "Jun 2020", date: "2020-06-01", totalSales: 4 },
117
- { label: "Jul 2020", date: "2020-07-01", totalSales: 9 },
118
- { label: "Aug 2020", totalSales: 0 },
119
- { label: "Sep 2020", totalSales: 0 },
120
- { label: "Oct 2020", totalSales: 0 },
121
- { label: "Nov 2020", date: "2020-11-01", totalSales: 3 },
122
- { label: "Dec 2020", totalSales: 0 },
123
- ]);
124
- });
125
-
126
- test("leaves complete quarters alone", () => {
127
- expect(fillDates({
128
- records: [
129
- { date: "2020-07-01", totalSales: 10 },
130
- { date: "2020-10-01", totalSales: 4 },
131
- { date: "2021-01-01", totalSales: 9 },
132
- { date: "2021-04-01", totalSales: 3 },
133
- ],
134
- getDate: rec => rec.date,
135
- fill: (label, rec) => ({
136
- label,
137
- totalSales: 0,
138
- ...rec
139
- }),
140
- min: "2020-07-12",
141
- max: "2020-04-06"
142
- })).toStrictEqual([
143
- { label: "Jul-Sep 2020", date: "2020-07-01", totalSales: 10 },
144
- { label: "Oct-Dec 2020", date: "2020-10-01", totalSales: 4 },
145
- { label: "Jan-Mar 2021", date: "2021-01-01", totalSales: 9 },
146
- { label: "Apr-Jun 2021", date: "2021-04-01", totalSales: 3 },
147
- ]);
148
- });
149
-
150
- test("fills in missing quarters", () => {
151
- expect(fillDates({
152
- records: [
153
- { date: "2020-04-01", totalSales: 10 },
154
- { date: "2020-10-01", totalSales: 4 },
155
- { date: "2021-01-01", totalSales: 9 },
156
- { date: "2021-10-01", totalSales: 3 },
157
- ],
158
- type: dateTypes.quarters,
159
- getDate: rec => rec.date,
160
- fill: (label, rec) => ({
161
- label,
162
- totalSales: 0,
163
- ...rec
164
- })
165
- })).toStrictEqual([
166
- { label: "Apr-Jun 2020", date: "2020-04-01", totalSales: 10 },
167
- { label: "Jul-Sep 2020", totalSales: 0 },
168
- { label: "Oct-Dec 2020", date: "2020-10-01", totalSales: 4 },
169
- { label: "Jan-Mar 2021", date: "2021-01-01", totalSales: 9 },
170
- { label: "Apr-Jun 2021", totalSales: 0 },
171
- { label: "Jul-Sep 2021", totalSales: 0 },
172
- { label: "Oct-Dec 2021", date: "2021-10-01", totalSales: 3 },
173
- ]);
174
- });
175
-
176
- test("fills in missing quarters and min/max range", () => {
177
- expect(fillDates({
178
- records: [
179
- { date: "2020-04-01", totalSales: 10 },
180
- { date: "2020-10-01", totalSales: 4 },
181
- { date: "2021-01-01", totalSales: 9 },
182
- { date: "2021-10-01", totalSales: 3 },
183
- ],
184
- type: dateTypes.quarters,
185
- getDate: rec => rec.date,
186
- fill: (label, rec) => ({
187
- label,
188
- totalSales: 0,
189
- ...rec
190
- }),
191
- min: "2019-10-10",
192
- max: "2022-01-06"
193
- })).toStrictEqual([
194
- { label: "Oct-Dec 2019", totalSales: 0 },
195
- { label: "Jan-Mar 2020", totalSales: 0 },
196
- { label: "Apr-Jun 2020", date: "2020-04-01", totalSales: 10 },
197
- { label: "Jul-Sep 2020", totalSales: 0 },
198
- { label: "Oct-Dec 2020", date: "2020-10-01", totalSales: 4 },
199
- { label: "Jan-Mar 2021", date: "2021-01-01", totalSales: 9 },
200
- { label: "Apr-Jun 2021", totalSales: 0 },
201
- { label: "Jul-Sep 2021", totalSales: 0 },
202
- { label: "Oct-Dec 2021", date: "2021-10-01", totalSales: 3 },
203
- { label: "Jan-Mar 2022", totalSales: 0 },
204
- ]);
205
- });
206
-
207
- test("explicit type overrides detection", () => {
208
- expect(fillDates({
209
- records: [
210
- { date: "2020-07-01", totalSales: 10 },
211
- { date: "2020-10-01", totalSales: 4 },
212
- { date: "2021-01-01", totalSales: 9 },
213
- { date: "2021-04-01", totalSales: 3 },
214
- ],
215
- getDate: rec => rec.date,
216
- fill: (label, rec) => ({
217
- label,
218
- totalSales: 0,
219
- ...rec
220
- }),
221
- type: dateTypes.months
222
- })).toStrictEqual([
223
- { label: "Jul 2020", date: "2020-07-01", totalSales: 10 },
224
- { label: "Aug 2020", totalSales: 0 },
225
- { label: "Sep 2020", totalSales: 0 },
226
- { label: "Oct 2020", date: "2020-10-01", totalSales: 4 },
227
- { label: "Nov 2020", totalSales: 0 },
228
- { label: "Dec 2020", totalSales: 0 },
229
- { label: "Jan 2021", date: "2021-01-01", totalSales: 9 },
230
- { label: "Feb 2021", totalSales: 0 },
231
- { label: "Mar 2021", totalSales: 0 },
232
- { label: "Apr 2021", date: "2021-04-01", totalSales: 3 },
233
- ]);
234
- });
package/src/fillDates.ts DELETED
@@ -1,217 +0,0 @@
1
- import moment, { Moment } from "moment";
2
-
3
- export type FillDate = Date | string | number | Moment;
4
-
5
- function parseDate(val: FillDate) {
6
- // Ensure a numeric year is not interpreted as offset from 1970!
7
- if (typeof val === "number") {
8
- val = "" + val;
9
- }
10
-
11
- return moment(val);
12
- }
13
-
14
- /**
15
- * Three operations on dates used by {@link fillDates}.
16
- */
17
- export type FillDateType = {
18
- /** Round the given date down to the nearest whole unit (e.g. start of month, quarter, year) */
19
- round(d: Moment): Moment;
20
- /** Format the given date to a string */
21
- format(d: Moment): string;
22
- /** Increment the date by the unit. The given date will already be rounded down. */
23
- increment(d: Moment): Moment;
24
- }
25
-
26
- const days: FillDateType = {
27
- round(d) {
28
- return d.clone().startOf("day");
29
- },
30
- format(d) {
31
- return d.format("ll");
32
- },
33
- increment(d) {
34
- return d.clone().add(1, "day");
35
- }
36
- };
37
-
38
- const months: FillDateType = {
39
- round(d) {
40
- return d.clone().startOf("month");
41
- },
42
- format(d) {
43
- return d.format("MMM YYYY");
44
- },
45
- increment(d) {
46
- return d.clone().add(1, "month");
47
- }
48
- }
49
-
50
- const quarters: FillDateType = {
51
- round(d) {
52
- return d.clone().startOf("quarter");
53
- },
54
- format(d) {
55
- const monthFirst = d.format("MMM");
56
- const monthLast = d.clone().add(2, "months").format("MMM");
57
- const year = d.format("YYYY");
58
- return `${monthFirst}-${monthLast} ${year}`;
59
- },
60
- increment(d) {
61
- return d.clone().add(3, "months");
62
- }
63
- }
64
-
65
- const years: FillDateType = {
66
- round(d) {
67
- return d.clone().startOf("year");
68
- },
69
- format(d) {
70
- return d.format("YYYY");
71
- },
72
- increment(d) {
73
- return d.clone().add(1, "year");
74
- }
75
- }
76
-
77
- /**
78
- * Standard built-in date types. To customise, implement the {@link FillDateType} interface.
79
- */
80
- export const dateTypes = {
81
- days, months, quarters, years
82
- } as const;
83
-
84
- /**
85
- * Examines a set of dates and chooses the most specific type that includes them all. If
86
- * all fall on Jan, 1 then `years` is chosen, and so on.
87
- */
88
- export function detectDateType(dates: Moment[]): FillDateType {
89
- return !dates.every(x => x.date() === 1) ? dateTypes.days :
90
- !dates.every(x => x.month() % 3 === 0) ? dateTypes.months :
91
- !dates.every(x => x.month() === 0) ? dateTypes.quarters :
92
- dateTypes.years;
93
- }
94
-
95
- /** Options for {@link fillDates} function. */
96
- export interface FillDatesOptions<T, R> {
97
- /** The records to base the filled list on. */
98
- records: T[];
99
- /** The operations to use for rounding, incrementing and formatting dates. */
100
- type?: FillDateType;
101
- /** Extracts a date value from a record in the input list. */
102
- getDate(record: T): FillDate;
103
- /**
104
- * Generate a record for a date, from the formatted {@link dateText} and
105
- * the input record for that date, if any.
106
- */
107
- fill(dateText: string, record: T | undefined): R;
108
- /**
109
- * The minimum date to generate. It will be rounded down by the {@link type}
110
- * so doesn't need to be on an exact boundary.
111
- */
112
- min?: FillDate;
113
- /**
114
- * The maximum date to generate. It will be rounded down by the {@link type}
115
- * so doesn't need to be on an exact boundary.
116
- */
117
- max?: FillDate;
118
- }
119
-
120
- /**
121
- * When querying for a time series chart, e.g. x-axis is _Month_ and y-axis is
122
- * _Total Sales_, there may be months where nothing was sold so they are
123
- * missing from the list of records.
124
- *
125
- * To render a proper time-series, we need these gaps to be filled in with
126
- * runs of fake records that give zero amounts for those months. e.g.
127
- *
128
- * ```ts
129
- * const filled = fillDates({
130
- * records: [
131
- * { date: '2020-04-01', totalSales: 10 },
132
- * { date: '2020-06-01', totalSales: 4 },
133
- * { date: '2020-07-01', totalSales: 9 },
134
- * ],
135
- * type: dateTypes.months,
136
- * getDate: rec => rec.date,
137
- * fill: (label, rec) => ({
138
- * label,
139
- * totalSales: 0,
140
- * ...rec
141
- * })
142
- * });
143
- * ```
144
- *
145
- * In the above example we add a `label` property to all the records, and
146
- * for the records that fill the gaps we set the `totalSales` property to 0.
147
- * For the real records, `...rec` will copy the real value of `totalSales`.
148
- *
149
- * To do this, we need to know:
150
- *
151
- * - how to round a date to the start of a unit (year, month, quarter),
152
- * - how to increment a date by that unit,
153
- * - how to format a date to a string for display.
154
- *
155
- * These operations are encapsulated by the {@link FillDateType} interface.
156
- * Several built-in types are provided in {@link dateTypes}, but you can
157
- * implement your own.
158
- *
159
- * Optionally you can also pass `min` and `max` dates, which will cause
160
- * extra records to be added at the start and end of the range if necessary.
161
- *
162
- * If you don't pass a `type`, a suitable type will be detected based on
163
- * how the input record dates fall on unit boundaries.
164
- */
165
- export function fillDates<T, R>(
166
- { records, getDate, fill, min, max, type }: FillDatesOptions<T, R>
167
- ) {
168
- records = [...records];
169
- records.sort((x, y) => parseDate(getDate(x)).diff(parseDate(getDate(y))));
170
-
171
- type = type ?? detectDateType(records.map(d => parseDate(getDate(d))));
172
-
173
- const results: R[] = [];
174
- let latest: Moment | undefined = undefined;
175
- const lower = min ? type.round(parseDate(min)) : undefined;
176
-
177
- for (const record of records) {
178
- const current = parseDate(getDate(record));
179
-
180
- for (;;) {
181
- latest = latest ? type.increment(latest) : lower;
182
- if (!latest || latest >= current) {
183
- break;
184
- }
185
-
186
- results.push(fill(type.format(latest), undefined));
187
- }
188
-
189
- results.push(fill(type.format(current), record));
190
- latest = current;
191
- }
192
-
193
- if (latest && max) {
194
- const upper = type.round(parseDate(max))
195
- for (;;) {
196
- latest = type.increment(latest);
197
- if (latest > upper) {
198
- break;
199
- }
200
-
201
- results.push(fill(type.format(latest), undefined));
202
- }
203
- }
204
-
205
- return results;
206
- }
207
-
208
- /** @deprecated */
209
- export function smartDates<T, R>(
210
- records: T[],
211
- min: FillDate | undefined,
212
- max: FillDate | undefined,
213
- getDate: (record: T) => FillDate,
214
- fill: (dateText: string, record: T | undefined) => R
215
- ) {
216
- return fillDates({ records, min, max, getDate, fill });
217
- }