@tailgatefantasysports/seasonservice 0.5.1 → 0.6.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.
- package/SeasonService.js +24 -11
- package/__tests__/SeasonService.test.js +48 -10
- package/package.json +1 -1
package/SeasonService.js
CHANGED
|
@@ -71,23 +71,36 @@ module.exports = class SeasonService {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
* Get the
|
|
75
|
-
* @summary
|
|
76
|
-
*
|
|
74
|
+
* Get the last season that finished, including all of its types.
|
|
75
|
+
* @summary Walks backwards from `date` past every frame of the season `date` falls in, then
|
|
76
|
+
* collects all frames of the season before it, keyed by type.
|
|
77
|
+
*
|
|
78
|
+
* A season `date` sits inside has not finished, so it is never the answer. Skipping the
|
|
79
|
+
* whole season rather than just the current frame matters mid-season: from a regular-season
|
|
80
|
+
* date, stepping back one frame lands on that same season's preseason, which would report a
|
|
81
|
+
* partial in-progress season as the previous one.
|
|
82
|
+
*
|
|
77
83
|
* @param {object} date - date to begin search
|
|
78
|
-
* @returns {object}
|
|
84
|
+
* @returns {object|null} frames keyed by type, or null when no earlier season exists
|
|
79
85
|
*/
|
|
80
86
|
getPreviousFullSeason(date) {
|
|
81
|
-
|
|
87
|
+
const current = this.getSeason(date);
|
|
88
|
+
let frame = current || this._getSeasonDirection(date, -1);
|
|
82
89
|
if (!frame)
|
|
83
|
-
return null
|
|
90
|
+
return null;
|
|
91
|
+
|
|
92
|
+
if (current) {
|
|
93
|
+
while (frame && frame.season === current.season)
|
|
94
|
+
frame = this._getSeasonDirection(new Date(frame.start - 1), -1);
|
|
95
|
+
if (!frame)
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
84
98
|
|
|
85
99
|
const full = { };
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
newFrame = this.getPreviousSeason(new Date(newFrame.start - 1))
|
|
100
|
+
const season = frame.season;
|
|
101
|
+
while (frame && frame.season === season) {
|
|
102
|
+
full[frame.type] = frame;
|
|
103
|
+
frame = this._getSeasonDirection(new Date(frame.start - 1), -1);
|
|
91
104
|
}
|
|
92
105
|
|
|
93
106
|
return full;
|
|
@@ -127,21 +127,28 @@ describe('GetPreviousFullSeason', () => {
|
|
|
127
127
|
end: new Date('2022-04-01')
|
|
128
128
|
}
|
|
129
129
|
}],
|
|
130
|
+
// Inside season 3, so season 3 is the *current* season and cannot be the previous one.
|
|
131
|
+
// The answer is season 2, whole. This previously returned season 3's preseason and
|
|
132
|
+
// regular frames — a partial, in-progress season reported as a finished one.
|
|
130
133
|
['Exists - start inside frame', new Date('2022-02-02'), {
|
|
131
|
-
[types.preseason]: {
|
|
132
|
-
season: "3",
|
|
133
|
-
type: types.preseason,
|
|
134
|
-
start: new Date('2022-01-01'),
|
|
135
|
-
end: new Date('2022-02-01')
|
|
136
|
-
},
|
|
137
134
|
[types.regular]: {
|
|
138
|
-
season: "
|
|
135
|
+
season: "2",
|
|
139
136
|
type: types.regular,
|
|
140
|
-
start: new Date('
|
|
141
|
-
end: new Date('
|
|
137
|
+
start: new Date('2021-03-01'),
|
|
138
|
+
end: new Date('2021-04-01')
|
|
142
139
|
},
|
|
143
140
|
}],
|
|
144
|
-
['Doesn\'t exist', new Date('2019-01-01'), null]
|
|
141
|
+
['Doesn\'t exist', new Date('2019-01-01'), null],
|
|
142
|
+
// Mid-season, one frame back is the *same* season's preseason. Skipping only the
|
|
143
|
+
// current frame rather than the whole season would report season 3 as previous.
|
|
144
|
+
['Exists - mid regular season', new Date('2022-02-20'), {
|
|
145
|
+
[types.regular]: {
|
|
146
|
+
season: "2",
|
|
147
|
+
type: types.regular,
|
|
148
|
+
start: new Date('2021-03-01'),
|
|
149
|
+
end: new Date('2021-04-01')
|
|
150
|
+
},
|
|
151
|
+
}]
|
|
145
152
|
])('%s', (_tName, inp, expected) => {
|
|
146
153
|
// setup
|
|
147
154
|
// execute
|
|
@@ -150,4 +157,35 @@ describe('GetPreviousFullSeason', () => {
|
|
|
150
157
|
// assert
|
|
151
158
|
expect(season).toStrictEqual(expected);
|
|
152
159
|
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe('GetPreviousFullSeason — real calendar', () => {
|
|
163
|
+
// The configured singleton, not the bare class the synthetic fixtures above use.
|
|
164
|
+
const { SeasonService: configured } = require('../index');
|
|
165
|
+
|
|
166
|
+
// The case this method exists for: during the 2026 preseason the app asks what last season
|
|
167
|
+
// was, to show finished-season stats. It used to answer "2026 preseason" — the frame the
|
|
168
|
+
// date sits in — so the app showed preseason stats beside a regular-season fixture.
|
|
169
|
+
test.each([
|
|
170
|
+
['from the 2026 preseason', new Date('2026-08-23T00:00:00Z')],
|
|
171
|
+
['from mid 2026 regular season', new Date('2026-11-01T00:00:00Z')],
|
|
172
|
+
['from the 2026 off-season', new Date('2026-05-01T00:00:00Z')]
|
|
173
|
+
])('%s answers 2025, whole', (_tName, date) => {
|
|
174
|
+
const full = configured.getPreviousFullSeason(date);
|
|
175
|
+
expect(full).not.toBeNull();
|
|
176
|
+
|
|
177
|
+
const frames = Object.values(full);
|
|
178
|
+
expect(frames.every(f => f.season === '2025')).toBe(true);
|
|
179
|
+
expect(frames.map(f => f.type).sort()).toEqual(
|
|
180
|
+
[types.preseason, types.regular, types.postseason].sort());
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('never reports the season the date sits inside', () => {
|
|
184
|
+
const inPreseason = new Date('2026-08-23T00:00:00Z');
|
|
185
|
+
const current = configured.getSeason(inPreseason);
|
|
186
|
+
const full = configured.getPreviousFullSeason(inPreseason);
|
|
187
|
+
|
|
188
|
+
expect(current.season).toBe('2026');
|
|
189
|
+
expect(Object.values(full).some(f => f.season === current.season)).toBe(false);
|
|
190
|
+
});
|
|
153
191
|
})
|