@aneuhold/core-ts-lib 1.1.9 → 1.1.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.
package/lib/index.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import datesAreOnSameDay from './helperFunctions/dateFunctions';
2
2
  import sleep from './helperFunctions/sleep';
3
+ import ArrayService from './services/ArrayService';
4
+ import DateService from './services/DateService/DateService';
3
5
  import DependencyService from './services/DependencyService';
4
6
  import FileSystemService from './services/FileSystemService/FileSystemService';
5
7
  import StringService from './services/StringService';
6
8
  import ErrorUtils from './utils/ErrorUtils';
7
9
  import Logger from './utils/Logger';
8
10
  import TestUtils from './utils/TestUtils';
9
- export { datesAreOnSameDay, DependencyService, FileSystemService, Logger, sleep, StringService, TestUtils, ErrorUtils };
11
+ export { datesAreOnSameDay, DependencyService, FileSystemService, Logger, sleep, StringService, TestUtils, ErrorUtils, DateService, ArrayService };
10
12
  export type {};
11
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,iCAAiC,CAAC;AAChE,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAC5C,OAAO,iBAAiB,MAAM,8BAA8B,CAAC;AAC7D,OAAO,iBAAiB,MAAM,gDAAgD,CAAC;AAC/E,OAAO,aAAa,MAAM,0BAA0B,CAAC;AACrD,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,MAAM,MAAM,gBAAgB,CAAC;AACpC,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAG1C,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,MAAM,EACN,KAAK,EACL,aAAa,EACb,SAAS,EACT,UAAU,EACX,CAAC;AAGF,YAAY,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,iCAAiC,CAAC;AAChE,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAC5C,OAAO,YAAY,MAAM,yBAAyB,CAAC;AACnD,OAAO,WAAW,MAAM,oCAAoC,CAAC;AAC7D,OAAO,iBAAiB,MAAM,8BAA8B,CAAC;AAC7D,OAAO,iBAAiB,MAAM,gDAAgD,CAAC;AAC/E,OAAO,aAAa,MAAM,0BAA0B,CAAC;AACrD,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,MAAM,MAAM,gBAAgB,CAAC;AACpC,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAG1C,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,MAAM,EACN,KAAK,EACL,aAAa,EACb,SAAS,EACT,UAAU,EACV,WAAW,EACX,YAAY,EACb,CAAC;AAGF,YAAY,EAAE,CAAC"}
package/lib/index.js CHANGED
@@ -3,11 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ErrorUtils = exports.TestUtils = exports.StringService = exports.sleep = exports.Logger = exports.FileSystemService = exports.DependencyService = exports.datesAreOnSameDay = void 0;
6
+ exports.ArrayService = exports.DateService = exports.ErrorUtils = exports.TestUtils = exports.StringService = exports.sleep = exports.Logger = exports.FileSystemService = exports.DependencyService = exports.datesAreOnSameDay = void 0;
7
7
  const dateFunctions_1 = __importDefault(require("./helperFunctions/dateFunctions"));
8
8
  exports.datesAreOnSameDay = dateFunctions_1.default;
9
9
  const sleep_1 = __importDefault(require("./helperFunctions/sleep"));
10
10
  exports.sleep = sleep_1.default;
11
+ const ArrayService_1 = __importDefault(require("./services/ArrayService"));
12
+ exports.ArrayService = ArrayService_1.default;
13
+ const DateService_1 = __importDefault(require("./services/DateService/DateService"));
14
+ exports.DateService = DateService_1.default;
11
15
  const DependencyService_1 = __importDefault(require("./services/DependencyService"));
12
16
  exports.DependencyService = DependencyService_1.default;
13
17
  const FileSystemService_1 = __importDefault(require("./services/FileSystemService/FileSystemService"));
@@ -0,0 +1,4 @@
1
+ export default class ArrayService {
2
+ static arraysHaveSamePrimitiveValues(array1: Array<unknown>, array2: Array<unknown>): boolean;
3
+ }
4
+ //# sourceMappingURL=ArrayService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ArrayService.d.ts","sourceRoot":"","sources":["../../src/services/ArrayService.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,MAAM,CAAC,6BAA6B,CAClC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,EACtB,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,GACrB,OAAO;CAaX"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ArrayService {
4
+ static arraysHaveSamePrimitiveValues(array1, array2) {
5
+ if (array1.length !== array2.length) {
6
+ return false;
7
+ }
8
+ const array1Copy = [...array1];
9
+ const array2Copy = [...array2];
10
+ array1Copy.sort();
11
+ array2Copy.sort();
12
+ return array1Copy.every((value, index) => value === array2Copy[index]);
13
+ }
14
+ }
15
+ exports.default = ArrayService;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * A class that supports converting dates across various personal projects.
3
+ *
4
+ * All of the things in this class should be able to be used in Node and the
5
+ * browser.
6
+ */
7
+ export default class DateService {
8
+ private static dateFormatter;
9
+ private static dateTimeFormatter;
10
+ /**
11
+ * Gets the date in date format if it is set to midnight, otherwise gets the
12
+ * date in date time format.
13
+ */
14
+ static getAutoDateString(date: Date): string;
15
+ /**
16
+ * Determines if a date has a user-specified time component to it, meaning
17
+ * that it isn't midnight.
18
+ */
19
+ static dateHasTime(date: Date): boolean;
20
+ static getDateString(date: Date): string;
21
+ static getDateTimeString(date: Date): string;
22
+ /**
23
+ * Determines if two dates are equal. This is strict, down to the ms.
24
+ */
25
+ static datesAreEqual(date1: Date | undefined | null, date2: Date | undefined | null): boolean;
26
+ /**
27
+ * Gets the last day of the month for the provided date. This will retain the
28
+ * time that was provided in the date.
29
+ */
30
+ static getLastDayOfGivenMonth(date: Date): Date;
31
+ /**
32
+ * Gets the week of the month for the provided date. This starts at 1.
33
+ */
34
+ static getWeekOfMonth(date: Date): number;
35
+ static getLastWeekOfMonth(date: Date): number;
36
+ static getDaysUntilWeekDay(date: Date, day: number): number;
37
+ /**
38
+ * Gets the date of the provided weekday in the provided week of the month.
39
+ * Retains the time that was provided in the date.
40
+ *
41
+ * For example, the 2nd Monday of the month, or the last Saturday of the
42
+ * month.
43
+ */
44
+ static getWeekDayOfXWeekOfMonth(date: Date, day: number, week: number | 'last'): Date | null;
45
+ /**
46
+ * Adds the provided number of days to the provided date. If the days are
47
+ * over or under the number of days in the month, then the month will be
48
+ * adjusted accordingly.
49
+ */
50
+ static addDays(date: Date, days: number): Date;
51
+ static addMonths(date: Date, months: number): Date;
52
+ static addWeeks(date: Date, weeks: number): Date;
53
+ static addYears(date: Date, years: number): Date;
54
+ /**
55
+ * For personal projects, midnight can mean exactly midnight, or 11:59pm.
56
+ * It acts as a border between two dates.
57
+ */
58
+ static dateIsMidnight(date: Date): boolean;
59
+ }
60
+ //# sourceMappingURL=DateService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DateService.d.ts","sourceRoot":"","sources":["../../../src/services/DateService/DateService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAC9B,OAAO,CAAC,MAAM,CAAC,aAAa,CAIzB;IAEH,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAM7B;IAEH;;;OAGG;IACH,MAAM,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI;IAOnC;;;OAGG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI;IAI7B,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI;IAI/B,MAAM,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI;IAInC;;OAEG;IACH,MAAM,CAAC,aAAa,CAClB,KAAK,EAAE,IAAI,GAAG,SAAS,GAAG,IAAI,EAC9B,KAAK,EAAE,IAAI,GAAG,SAAS,GAAG,IAAI;IAWhC;;;OAGG;IACH,MAAM,CAAC,sBAAsB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAS/C;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAmBzC,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAK7C,MAAM,CAAC,mBAAmB,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAQ3D;;;;;;OAMG;IACH,MAAM,CAAC,wBAAwB,CAC7B,IAAI,EAAE,IAAI,EACV,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,GACpB,IAAI,GAAG,IAAI;IAiCd;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM;IAMvC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM;IAM3C,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM;IAIzC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM;IAMzC;;;OAGG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI;CAMjC"}
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * A class that supports converting dates across various personal projects.
5
+ *
6
+ * All of the things in this class should be able to be used in Node and the
7
+ * browser.
8
+ */
9
+ class DateService {
10
+ static dateFormatter = new Intl.DateTimeFormat('en-US', {
11
+ year: 'numeric',
12
+ month: 'short',
13
+ day: 'numeric'
14
+ });
15
+ static dateTimeFormatter = new Intl.DateTimeFormat('en-US', {
16
+ year: 'numeric',
17
+ month: 'short',
18
+ day: 'numeric',
19
+ hour: 'numeric',
20
+ minute: 'numeric'
21
+ });
22
+ /**
23
+ * Gets the date in date format if it is set to midnight, otherwise gets the
24
+ * date in date time format.
25
+ */
26
+ static getAutoDateString(date) {
27
+ if (this.dateHasTime(date)) {
28
+ return this.getDateTimeString(date);
29
+ }
30
+ return this.getDateString(date);
31
+ }
32
+ /**
33
+ * Determines if a date has a user-specified time component to it, meaning
34
+ * that it isn't midnight.
35
+ */
36
+ static dateHasTime(date) {
37
+ return !this.dateIsMidnight(date);
38
+ }
39
+ static getDateString(date) {
40
+ return this.dateFormatter.format(date);
41
+ }
42
+ static getDateTimeString(date) {
43
+ return this.dateTimeFormatter.format(date);
44
+ }
45
+ /**
46
+ * Determines if two dates are equal. This is strict, down to the ms.
47
+ */
48
+ static datesAreEqual(date1, date2) {
49
+ if (!date1 && !date2) {
50
+ return true;
51
+ }
52
+ if (!date1 || !date2) {
53
+ return false;
54
+ }
55
+ return date1.getTime() === date2.getTime();
56
+ }
57
+ /**
58
+ * Gets the last day of the month for the provided date. This will retain the
59
+ * time that was provided in the date.
60
+ */
61
+ static getLastDayOfGivenMonth(date) {
62
+ // Setting the day to 0, makes the date move to the last day of the
63
+ // previous month.
64
+ const newDate = new Date(date);
65
+ newDate.setMonth(newDate.getMonth() + 1);
66
+ newDate.setDate(0);
67
+ return newDate;
68
+ }
69
+ /**
70
+ * Gets the week of the month for the provided date. This starts at 1.
71
+ */
72
+ static getWeekOfMonth(date) {
73
+ const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
74
+ const firstDayOfMonthWeekday = firstDayOfMonth.getDay();
75
+ const firstDayOfSecondWeek = new Date(firstDayOfMonth);
76
+ const daysUntilSecondWeek = 7 - firstDayOfMonthWeekday;
77
+ firstDayOfSecondWeek.setDate(firstDayOfSecondWeek.getDate() + daysUntilSecondWeek);
78
+ if (date < firstDayOfSecondWeek) {
79
+ return 1;
80
+ }
81
+ const daysSinceFirstDayOfSecondWeek = date.getDate() - firstDayOfSecondWeek.getDate();
82
+ const weeksSinceFirstDayOfSecondWeek = Math.floor(daysSinceFirstDayOfSecondWeek / 7);
83
+ return weeksSinceFirstDayOfSecondWeek + 2;
84
+ }
85
+ static getLastWeekOfMonth(date) {
86
+ const lastDayOfMonth = this.getLastDayOfGivenMonth(date);
87
+ return this.getWeekOfMonth(lastDayOfMonth);
88
+ }
89
+ static getDaysUntilWeekDay(date, day) {
90
+ const daysUntilWeekDay = day - date.getDay();
91
+ if (daysUntilWeekDay < 0) {
92
+ return daysUntilWeekDay + 7;
93
+ }
94
+ return daysUntilWeekDay;
95
+ }
96
+ /**
97
+ * Gets the date of the provided weekday in the provided week of the month.
98
+ * Retains the time that was provided in the date.
99
+ *
100
+ * For example, the 2nd Monday of the month, or the last Saturday of the
101
+ * month.
102
+ */
103
+ static getWeekDayOfXWeekOfMonth(date, day, week) {
104
+ if ((typeof week === 'number' && (week < 1 || week > 5)) ||
105
+ day < 0 ||
106
+ day > 6) {
107
+ return null;
108
+ }
109
+ if (week === 'last') {
110
+ let resultDate = this.getLastDayOfGivenMonth(date);
111
+ while (resultDate.getDay() !== day) {
112
+ resultDate = this.addDays(resultDate, -1);
113
+ }
114
+ return resultDate;
115
+ }
116
+ const firstDayOfMonth = new Date(date);
117
+ firstDayOfMonth.setDate(1);
118
+ let resultDate = new Date(firstDayOfMonth);
119
+ resultDate.setDate(resultDate.getDate() + this.getDaysUntilWeekDay(firstDayOfMonth, day));
120
+ // The number of times the day has shown up so far.
121
+ let instanceOfDay = 1;
122
+ while (instanceOfDay < week) {
123
+ resultDate = this.addWeeks(resultDate, 1);
124
+ instanceOfDay += 1;
125
+ if (resultDate.getMonth() !== date.getMonth()) {
126
+ return null;
127
+ }
128
+ }
129
+ return resultDate;
130
+ }
131
+ /**
132
+ * Adds the provided number of days to the provided date. If the days are
133
+ * over or under the number of days in the month, then the month will be
134
+ * adjusted accordingly.
135
+ */
136
+ static addDays(date, days) {
137
+ const newDate = new Date(date);
138
+ newDate.setDate(newDate.getDate() + days);
139
+ return newDate;
140
+ }
141
+ static addMonths(date, months) {
142
+ const newDate = new Date(date);
143
+ newDate.setMonth(newDate.getMonth() + months);
144
+ return newDate;
145
+ }
146
+ static addWeeks(date, weeks) {
147
+ return this.addDays(date, weeks * 7);
148
+ }
149
+ static addYears(date, years) {
150
+ const newDate = new Date(date);
151
+ newDate.setFullYear(newDate.getFullYear() + years);
152
+ return newDate;
153
+ }
154
+ /**
155
+ * For personal projects, midnight can mean exactly midnight, or 11:59pm.
156
+ * It acts as a border between two dates.
157
+ */
158
+ static dateIsMidnight(date) {
159
+ return ((date.getHours() === 0 && date.getMinutes() === 0) ||
160
+ (date.getHours() === 23 && date.getMinutes() === 59));
161
+ }
162
+ }
163
+ exports.default = DateService;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=DateService.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DateService.spec.d.ts","sourceRoot":"","sources":["../../../src/services/DateService/DateService.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const DateService_1 = __importDefault(require("./DateService"));
7
+ describe('DateService', () => {
8
+ describe('addWeeks', () => {
9
+ it('should successfully add weeks to a date', () => {
10
+ const result = DateService_1.default.addWeeks(new Date(2024, 0, 1), 1);
11
+ expect(result).toEqual(new Date(2024, 0, 8));
12
+ });
13
+ it('should successfully add weeks to a date that crosses months', () => {
14
+ const result = DateService_1.default.addWeeks(new Date(2024, 0, 23), 2);
15
+ expect(result).toEqual(new Date(2024, 1, 6));
16
+ });
17
+ it('should successfully add weeks to a date that crosses years', () => {
18
+ const result = DateService_1.default.addWeeks(new Date(2024, 11, 25), 1);
19
+ expect(result).toEqual(new Date(2025, 0, 1));
20
+ });
21
+ });
22
+ describe('getWeekOfMonth', () => {
23
+ it('should successfully get the week of month for a date', async () => {
24
+ const result = DateService_1.default.getWeekOfMonth(new Date(2024, 0, 14));
25
+ expect(result).toEqual(3);
26
+ const result2 = DateService_1.default.getWeekOfMonth(new Date(2024, 0, 1));
27
+ expect(result2).toEqual(1);
28
+ const result3 = DateService_1.default.getWeekOfMonth(new Date(2024, 0, 31));
29
+ expect(result3).toEqual(5);
30
+ });
31
+ });
32
+ describe('getWeekDayOfXWeekOfMonth', () => {
33
+ it('should successfully get the 2nd monday of January 2024', () => {
34
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 1, 2);
35
+ expect(result).toEqual(new Date(2024, 0, 8));
36
+ });
37
+ it('should successfully get the 3rd monday of January 2024', () => {
38
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 1, 3);
39
+ expect(result).toEqual(new Date(2024, 0, 15));
40
+ });
41
+ it('should successfully get the 3rd tuesday of January 2024', () => {
42
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 2, 3);
43
+ expect(result).toEqual(new Date(2024, 0, 16));
44
+ });
45
+ it('should successfully get the 1st Saturday of January 2024', () => {
46
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 6, 1);
47
+ expect(result).toEqual(new Date(2024, 0, 6));
48
+ });
49
+ it('should successfully get the 1st sunday of January 2024', () => {
50
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 0, 1);
51
+ expect(result).toEqual(new Date(2024, 0, 7));
52
+ });
53
+ it('should successfully get the last sunday of January 2024', () => {
54
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 0, 'last');
55
+ expect(result).toEqual(new Date(2024, 0, 28));
56
+ });
57
+ it('should return null if the week and day do not exist', () => {
58
+ const result = DateService_1.default.getWeekDayOfXWeekOfMonth(new Date(2024, 0, 1), 0, 5);
59
+ expect(result).toEqual(null);
60
+ });
61
+ });
62
+ });
@@ -0,0 +1,50 @@
1
+ /**
2
+ * A class that supports converting dates across various personal projects.
3
+ *
4
+ * All of the things in this class should be able to be used in Node and the
5
+ * browser.
6
+ */
7
+ export default class DateService {
8
+ private static dateFormatter;
9
+ private static dateTimeFormatter;
10
+ /**
11
+ * Gets the date in date format if it is set to midnight, otherwise gets the
12
+ * date in date time format.
13
+ */
14
+ static getAutoDateString(date: Date): string;
15
+ /**
16
+ * Determines if a date has a user-specified time component to it, meaning
17
+ * that it isn't midnight.
18
+ */
19
+ static dateHasTime(date: Date): boolean;
20
+ static getDateString(date: Date): string;
21
+ static getDateTimeString(date: Date): string;
22
+ /**
23
+ * Determines if two dates are equal. This is strict, down to the ms.
24
+ */
25
+ static datesAreEqual(date1: Date | undefined | null, date2: Date | undefined | null): boolean;
26
+ /**
27
+ * Gets the last day of the month for the provided date. This will return
28
+ * the date at exactly 00:00 on the last day of the month.
29
+ */
30
+ static getLastDayOfGivenMonth(date: Date): Date;
31
+ /**
32
+ * Gets the week of the month for the provided date. This starts at 1.
33
+ */
34
+ static getWeekOfMonth(date: Date): number;
35
+ /**
36
+ * Adds the provided number of days to the provided date. If the days are
37
+ * over or under the number of days in the month, then the month will be
38
+ * adjusted accordingly.
39
+ */
40
+ static addDays(date: Date, days: number): Date;
41
+ static addMonths(date: Date, months: number): Date;
42
+ static addWeeks(date: Date, weeks: number): Date;
43
+ static addYears(date: Date, years: number): Date;
44
+ /**
45
+ * For personal projects, midnight can mean exactly midnight, or 11:59pm.
46
+ * It acts as a border between two dates.
47
+ */
48
+ static dateIsMidnight(date: Date): boolean;
49
+ }
50
+ //# sourceMappingURL=DateService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DateService.d.ts","sourceRoot":"","sources":["../../src/services/DateService.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAC9B,OAAO,CAAC,MAAM,CAAC,aAAa,CAIzB;IAEH,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAM7B;IAEH;;;OAGG;IACH,MAAM,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI;IAOnC;;;OAGG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI;IAI7B,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI;IAI/B,MAAM,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI;IAInC;;OAEG;IACH,MAAM,CAAC,aAAa,CAClB,KAAK,EAAE,IAAI,GAAG,SAAS,GAAG,IAAI,EAC9B,KAAK,EAAE,IAAI,GAAG,SAAS,GAAG,IAAI;IAWhC;;;OAGG;IACH,MAAM,CAAC,sBAAsB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAM/C;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAazC;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM;IAMvC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM;IAM3C,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM;IAIzC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM;IAMzC;;;OAGG;IACH,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI;CAMjC"}
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * A class that supports converting dates across various personal projects.
5
+ *
6
+ * All of the things in this class should be able to be used in Node and the
7
+ * browser.
8
+ */
9
+ class DateService {
10
+ static dateFormatter = new Intl.DateTimeFormat('en-US', {
11
+ year: 'numeric',
12
+ month: 'short',
13
+ day: 'numeric'
14
+ });
15
+ static dateTimeFormatter = new Intl.DateTimeFormat('en-US', {
16
+ year: 'numeric',
17
+ month: 'short',
18
+ day: 'numeric',
19
+ hour: 'numeric',
20
+ minute: 'numeric'
21
+ });
22
+ /**
23
+ * Gets the date in date format if it is set to midnight, otherwise gets the
24
+ * date in date time format.
25
+ */
26
+ static getAutoDateString(date) {
27
+ if (this.dateHasTime(date)) {
28
+ return this.getDateTimeString(date);
29
+ }
30
+ return this.getDateString(date);
31
+ }
32
+ /**
33
+ * Determines if a date has a user-specified time component to it, meaning
34
+ * that it isn't midnight.
35
+ */
36
+ static dateHasTime(date) {
37
+ return !this.dateIsMidnight(date);
38
+ }
39
+ static getDateString(date) {
40
+ return this.dateFormatter.format(date);
41
+ }
42
+ static getDateTimeString(date) {
43
+ return this.dateTimeFormatter.format(date);
44
+ }
45
+ /**
46
+ * Determines if two dates are equal. This is strict, down to the ms.
47
+ */
48
+ static datesAreEqual(date1, date2) {
49
+ if (!date1 && !date2) {
50
+ return true;
51
+ }
52
+ if (!date1 || !date2) {
53
+ return false;
54
+ }
55
+ return date1.getTime() === date2.getTime();
56
+ }
57
+ /**
58
+ * Gets the last day of the month for the provided date. This will return
59
+ * the date at exactly 00:00 on the last day of the month.
60
+ */
61
+ static getLastDayOfGivenMonth(date) {
62
+ // Setting the day to 0, makes the date move to the last day of the
63
+ // previous month.
64
+ return new Date(date.getFullYear(), date.getMonth() + 1, 0);
65
+ }
66
+ /**
67
+ * Gets the week of the month for the provided date. This starts at 1.
68
+ */
69
+ static getWeekOfMonth(date) {
70
+ const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
71
+ const firstDayOfMonthWeekday = firstDayOfMonth.getDay();
72
+ const firstDayOfFirstWeek = new Date(firstDayOfMonth);
73
+ firstDayOfFirstWeek.setDate(firstDayOfFirstWeek.getDate() - firstDayOfMonthWeekday);
74
+ const daysSinceFirstDayOfFirstWeek = date.getDate() - firstDayOfFirstWeek.getDate();
75
+ const daysSinceFirstDayOfMonth = daysSinceFirstDayOfFirstWeek + 1;
76
+ return Math.ceil(daysSinceFirstDayOfMonth / 7);
77
+ }
78
+ /**
79
+ * Adds the provided number of days to the provided date. If the days are
80
+ * over or under the number of days in the month, then the month will be
81
+ * adjusted accordingly.
82
+ */
83
+ static addDays(date, days) {
84
+ const newDate = new Date(date);
85
+ newDate.setDate(newDate.getDate() + days);
86
+ return newDate;
87
+ }
88
+ static addMonths(date, months) {
89
+ const newDate = new Date(date);
90
+ newDate.setMonth(newDate.getMonth() + months);
91
+ return newDate;
92
+ }
93
+ static addWeeks(date, weeks) {
94
+ return this.addDays(date, weeks * 7);
95
+ }
96
+ static addYears(date, years) {
97
+ const newDate = new Date(date);
98
+ newDate.setFullYear(newDate.getFullYear() + years);
99
+ return newDate;
100
+ }
101
+ /**
102
+ * For personal projects, midnight can mean exactly midnight, or 11:59pm.
103
+ * It acts as a border between two dates.
104
+ */
105
+ static dateIsMidnight(date) {
106
+ return ((date.getHours() === 0 && date.getMinutes() === 0) ||
107
+ (date.getHours() === 23 && date.getMinutes() === 59));
108
+ }
109
+ }
110
+ exports.default = DateService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aneuhold/core-ts-lib",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "A core library for all of my TypeScript projects",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",