@tailgatefantasysports/seasonservice 0.4.4 → 0.4.8

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 CHANGED
@@ -54,16 +54,35 @@ module.exports = class SeasonService {
54
54
 
55
55
  /**
56
56
  * Gets the next (or current if in a season) season for `date` up to a year ahead
57
- * @param {object} date js date to get next season for
57
+ * @param {object} date - js date to get next season for
58
+ * @returns {object}
58
59
  */
59
60
  getNextSeason(date) {
60
- let ceiling = DateTime.fromJSDate(date).plus({ year: 1 }),
61
+ return this._getSeasonDirection(date, 1);
62
+ }
63
+
64
+ /**
65
+ * Gets the previous season (or current if in a season) for date, up to a year behind
66
+ * @param {object} date - js date to get previous season for
67
+ * @returns {object}
68
+ */
69
+ getPreviousSeason(date) {
70
+ return this._getSeasonDirection(date, -1);
71
+ }
72
+
73
+ /**
74
+ * Walks to a nearby season from date in direction
75
+ * @param {number} direction - direction to get season (1 for next, -1 for previous)
76
+ * @returns {object}
77
+ */
78
+ _getSeasonDirection(date, direction) {
79
+ let ceiling = DateTime.fromJSDate(date).plus({ year: 1 * direction }),
61
80
  check = DateTime.fromJSDate(date),
62
81
  season = this.getSeason(date);
63
82
 
64
- while(!season && ceiling > check) {
83
+ while(!season && (direction > 0 ? ceiling > check : ceiling < check)) {
65
84
  season = this.getSeason(check.toJSDate());
66
- check = check.plus({ week: 1 });
85
+ check = check.plus({ week: 1 * direction });
67
86
  }
68
87
 
69
88
  return season;
@@ -60,35 +60,31 @@ describe('Get Week', () => {
60
60
  })
61
61
  });
62
62
 
63
- describe('Get Next Season', () => {
64
- test('Next Season Exists', () => {
65
- // setup
66
- // execute
67
- let season = this.service.getNextSeason(new Date('2020-12-30'));
68
-
69
- // assert
70
- expect(season.season).toBe('1');
71
- expect(season.type).toBe(0);
72
- expect(season.start).toStrictEqual(new Date('2021-01-01'));
73
- expect(season.end).toStrictEqual(new Date('2021-02-01'));
74
- });
75
-
76
- test('Middle of Season', () => {
63
+ describe('Get Next/Previous Season', () => {
64
+ test.each([
65
+ ['Next Season: Exists', 'getNextSeason', new Date('2020-12-30'), '1', 0, new Date('2021-01-01'), new Date('2021-02-01')],
66
+ ['Next Season: Middle of Season', 'getNextSeason', new Date('2021-01-10'), '1', 0, new Date('2021-01-01'), new Date('2021-02-01')],
67
+ ['Previous Season: Exists', 'getPreviousSeason', new Date('2022-01-10'), '2', 1, new Date('2021-03-01'), new Date('2021-04-01')],
68
+ ['Previous Season: Middle of Season', 'getPreviousSeason', new Date('2021-01-10'), '1', 0, new Date('2021-01-01'), new Date('2021-02-01')]
69
+ ])('%s', (_tName, fn, inp, seasonStr, type, start, end) => {
77
70
  // setup
78
71
  // execute
79
- let season = this.service.getNextSeason(new Date('2021-01-10'))
72
+ let season = this.service[fn](inp);
80
73
 
81
74
  // assert
82
- expect(season.season).toBe('1');
83
- expect(season.type).toBe(0);
84
- expect(season.start).toStrictEqual(new Date('2021-01-01'));
85
- expect(season.end).toStrictEqual(new Date('2021-02-01'));
75
+ expect(season.season).toBe(seasonStr);
76
+ expect(season.type).toBe(type);
77
+ expect(season.start).toStrictEqual(start);
78
+ expect(season.end).toStrictEqual(end);
86
79
  });
87
80
 
88
- test('Nonexistent Next Season', () => {
81
+ test.each([
82
+ ['Next Season: Undefined', 'getNextSeason', new Date('2019-12-30')],
83
+ ['Previous Season: Undefined', 'getPreviousSeason', new Date('2022-04-02')]
84
+ ])('%s', (_tName, fn, inp) => {
89
85
  // setup
90
86
  // execute
91
- let season = this.service.getNextSeason(new Date('2021-06-01'));
87
+ let season = this.service[fn](inp);
92
88
 
93
89
  // assert
94
90
  expect(season).toBeUndefined();
@@ -5,9 +5,9 @@ describe('Service', () => {
5
5
  test.each([
6
6
  ['2020 Regular', new Date('2020-10-11T00:00:00Z'), '2020', 1, new Date('2020-09-10T00:00:00Z'), new Date('2021-01-07T00:00:00Z')],
7
7
  ['2020 Post', new Date('2021-01-20T00:00:00Z'), '2020', 2, new Date('2021-01-07T00:00:00Z'), new Date('2021-02-07T00:00:00Z')],
8
- ['2021 Beta', new Date('2021-06-20T00:00:00Z'), 'BETA', 'beta', new Date('2021-06-16T00:00:00Z'), new Date('2021-09-01T00:00:00Z')],
9
- ['2021 Regular', new Date('2021-09-10T00:00:00Z'), '2021', 1, new Date('2021-09-09T00:00:00Z'), new Date('2022-01-10T00:00:00Z')],
10
- ['2021 Post', new Date('2022-01-20T00:00:00Z'), '2021', 2, new Date('2022-01-15T00:00:00Z'), new Date('2022-02-14T00:00:00Z')]
8
+ ['2021 Beta', new Date('2021-06-20T00:00:00Z'), 'BETA', 'beta', new Date('2021-06-16T00:00:00Z'), new Date('2021-08-05T00:00:00Z')],
9
+ ['2021 Regular', new Date('2021-09-10T00:00:00Z'), '2021', 1, new Date('2021-09-08T00:00:00Z'), new Date('2022-01-12T00:00:00Z')],
10
+ ['2021 Post', new Date('2022-01-20T00:00:00Z'), '2021', 2, new Date('2022-01-12T00:00:00Z'), new Date('2022-02-15T00:00:00Z')]
11
11
  ])('%s', (_tName, date, season, type, start, end) => {
12
12
  let frame = SeasonService.getSeason(date);
13
13
  expect(frame.season).toBe(season);
package/app.config.js CHANGED
@@ -20,7 +20,7 @@ module.exports = {
20
20
  },
21
21
  2021: {
22
22
  [types.preseason]: {
23
- start: new Date('2021-08-11T00:00:00Z'),
23
+ start: new Date('2021-08-04T00:00:00Z'),
24
24
  end: new Date('2021-09-01T00:00:00Z')
25
25
  },
26
26
  [types.regular]: {
@@ -31,6 +31,20 @@ module.exports = {
31
31
  start: new Date('2022-01-12T00:00:00Z'),
32
32
  end: new Date('2022-02-15T00:00:00Z'),
33
33
  }
34
+ },
35
+ 2022: {
36
+ [types.preseason]: {
37
+ start: new Date('2022-08-03T00:00:00Z'),
38
+ end: new Date('2022-08-31T00:00:00Z')
39
+ },
40
+ [types.regular]: {
41
+ start: new Date('2022-09-07T00:00:00Z'),
42
+ end: new Date('2023-01-11T00:00:00Z'),
43
+ },
44
+ [types.postseason]: {
45
+ start: new Date('2023-01-11T00:00:00Z'),
46
+ end: new Date('2023-02-15T00:00:00Z'),
47
+ }
34
48
  }
35
49
  }
36
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tailgatefantasysports/seasonservice",
3
- "version": "0.4.4",
3
+ "version": "0.4.8",
4
4
  "description": "Generic Season Service",
5
5
  "main": "index.js",
6
6
  "scripts": {