scdate 0.2.4 → 0.2.5

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,3 +1,6 @@
1
+ /**
2
+ * SDate represents a date in the ISO-8601 format (YYYY-MM-DD)
3
+ */
1
4
  export declare class SDate {
2
5
  readonly date: string;
3
6
  constructor(isoValue: string);
@@ -1,4 +1,7 @@
1
1
  import { validateISODate } from './date.js';
2
+ /**
3
+ * SDate represents a date in the ISO-8601 format (YYYY-MM-DD)
4
+ */
2
5
  export class SDate {
3
6
  date;
4
7
  constructor(isoValue) {
@@ -1,3 +1,6 @@
1
+ /**
2
+ * STime represents a time in the ISO-8601 format (HH:MM)
3
+ */
1
4
  export declare class STime {
2
5
  readonly time: string;
3
6
  constructor(isoValue: string);
@@ -1,4 +1,7 @@
1
1
  import { validateISOTime } from './time.js';
2
+ /**
3
+ * STime represents a time in the ISO-8601 format (HH:MM)
4
+ */
2
5
  export class STime {
3
6
  time;
4
7
  constructor(isoValue) {
@@ -1,3 +1,6 @@
1
+ /**
2
+ * STimestamp represents a timestamp in the ISO-8601 format (YYYY-MM-DDTHH:MM)
3
+ */
1
4
  export declare class STimestamp {
2
5
  readonly timestamp: string;
3
6
  constructor(timestamp: string);
@@ -1,4 +1,7 @@
1
1
  import { validateISOTimestamp } from './timestamp.js';
2
+ /**
3
+ * STimestamp represents a timestamp in the ISO-8601 format (YYYY-MM-DDTHH:MM)
4
+ */
2
5
  export class STimestamp {
3
6
  timestamp;
4
7
  constructor(timestamp) {
@@ -1,3 +1,10 @@
1
+ /**
2
+ * SWeekdays represents a string of weekdays in the format 'SMTWTFS' where each
3
+ * position is represented by a flag indicating if the weekday (starting on
4
+ * Sunday and ending on Saturday using the first letter of the english word for
5
+ * the week day) is included or excluded. If excluded, the position is filled
6
+ * with a '-' character.
7
+ */
1
8
  export declare class SWeekdays {
2
9
  readonly weekdays: string;
3
10
  constructor(weekdays: string);
@@ -1,4 +1,11 @@
1
1
  import { validateWeekdays } from './weekdays.js';
2
+ /**
3
+ * SWeekdays represents a string of weekdays in the format 'SMTWTFS' where each
4
+ * position is represented by a flag indicating if the weekday (starting on
5
+ * Sunday and ending on Saturday using the first letter of the english word for
6
+ * the week day) is included or excluded. If excluded, the position is filled
7
+ * with a '-' character.
8
+ */
2
9
  export class SWeekdays {
3
10
  weekdays;
4
11
  constructor(weekdays) {
@@ -25,6 +25,7 @@ export const validateISODate = (isoDate) => {
25
25
  const date = Number(getISODateFromISODate(isoDate));
26
26
  const nativeDate = new Date();
27
27
  nativeDate.setUTCFullYear(year, month, date);
28
+ // This will result in an error if for example the date uses 32 as the date
28
29
  if (nativeDate.getUTCFullYear() !== year ||
29
30
  nativeDate.getUTCMonth() !== month ||
30
31
  nativeDate.getUTCDate() !== date) {
@@ -19,6 +19,8 @@ export const validateISOTime = (isoTime) => {
19
19
  const nativeDate = new Date();
20
20
  nativeDate.setUTCHours(hours);
21
21
  nativeDate.setUTCMinutes(minutes);
22
+ // The following will result in an error if for example the date uses a time
23
+ // outside of 0:00 and 23:59.
22
24
  if (nativeDate.getUTCMinutes() !== minutes ||
23
25
  nativeDate.getUTCHours() !== hours) {
24
26
  throw new Error(`Invalid ISO time value. Expected from 00:00 to 23:59. Current value: '${isoTime}'.`);
@@ -1,2 +1,6 @@
1
1
  export declare const hasFlag: (flags: number, checkFlag: number) => boolean;
2
+ /**
3
+ * Retrieves the value from the array or string at the defined index or throws
4
+ * if the value is undefined.
5
+ */
2
6
  export declare const getAtIndex: <T extends string | unknown[]>(arrayOrString: T, index: number) => T[number];
@@ -1,4 +1,8 @@
1
1
  export const hasFlag = (flags, checkFlag) => (flags & checkFlag) === checkFlag;
2
+ /**
3
+ * Retrieves the value from the array or string at the defined index or throws
4
+ * if the value is undefined.
5
+ */
2
6
  export const getAtIndex = (arrayOrString, index) => {
3
7
  const value = arrayOrString[index];
4
8
  if (value === undefined) {
@@ -2,4 +2,8 @@ import { SDate } from './SDate.js';
2
2
  import { STimestamp } from './STimestamp.js';
3
3
  export declare const getMillisecondsInUTCFromTimestamp: (timestamp: STimestamp, timeZone: string) => number;
4
4
  export declare const getMillisecondsInUTCFromDate: (date: SDate, timeZone: string) => number;
5
+ /**
6
+ * @param timeZone For a list of valid time zones run
7
+ * `Intl.supportedValuesOf('timeZone')` on your environment.
8
+ */
5
9
  export declare const getTimeZonedDate: (millisecondsInUTC: number, timeZone: string) => Date;
@@ -18,6 +18,10 @@ export const getMillisecondsInUTCFromDate = (date, timeZone) => {
18
18
  const timeZoneOffset = getValidatedTimeZoneOffset(timeZone, utcDate);
19
19
  return utcDate.getTime() - timeZoneOffset;
20
20
  };
21
+ /**
22
+ * @param timeZone For a list of valid time zones run
23
+ * `Intl.supportedValuesOf('timeZone')` on your environment.
24
+ */
21
25
  export const getTimeZonedDate = (millisecondsInUTC, timeZone) => {
22
26
  if (isNaN(millisecondsInUTC.valueOf())) {
23
27
  throw new Error(`Invalid date. Date: '${String(millisecondsInUTC.valueOf())}'`);
package/dist/sDate.d.ts CHANGED
@@ -4,30 +4,310 @@ export interface SDateShortStringOptions {
4
4
  includeWeekday: boolean;
5
5
  onTodayText: () => string;
6
6
  }
7
+ /**
8
+ * --- Factory ---
9
+ */
10
+ /**
11
+ * Returns a new SDate instance.
12
+ *
13
+ * @param date An instance of SDate or a string in the YYYY-MM-DD format.
14
+ */
7
15
  export declare const sDate: (date: string | SDate) => SDate;
16
+ /**
17
+ * --- Factory helpers ---
18
+ */
19
+ /**
20
+ * Returns a new SDate instance with the current date in the given time zone.
21
+ *
22
+ * @param timeZone The time zone to get the current date for. See
23
+ * `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
24
+ */
8
25
  export declare const getDateToday: (timeZone: string) => SDate;
26
+ /**
27
+ * Returns a new SDate instance set to the next date after the provided date
28
+ * that match the given weekday.
29
+ *
30
+ * @param date The date to start from (not included). It can be an SDate or a
31
+ * string in the YYYY-MM-DD format.
32
+ * @param weekday The weekday to find the next date for.
33
+ */
9
34
  export declare const getNextDateByWeekday: (date: string | SDate, weekday: Weekday) => SDate;
35
+ /**
36
+ * Returns a new SDate instance set to date that is before the provided date and
37
+ * matches the given weekday.
38
+ *
39
+ * @param date The date to start from (not included).
40
+ * @param weekday The weekday to find the previous date for. It can be an SDate
41
+ * or a string in the YYYY-MM-DD format.
42
+ */
10
43
  export declare const getPreviousDateByWeekday: (date: string | SDate, weekday: Weekday) => SDate;
44
+ /**
45
+ * Returns a new SDate instance set to the first day of the month for the
46
+ * provided date.
47
+ *
48
+ * @param date The date to get the first day of the month for. It can be an
49
+ * SDate or a string in the YYYY-MM-DD format.
50
+ */
11
51
  export declare const getDateForFirstDayOfMonth: (date: string | SDate) => SDate;
52
+ /**
53
+ * Returns a new SDate instance set to the last day of the month for the
54
+ * provided date.
55
+ *
56
+ * @param date The date to get the last day of the month for. It can be an SDate
57
+ * or a string in the YYYY-MM-DD format.
58
+ */
12
59
  export declare const getDateForLastDayOfMonth: (date: string | SDate) => SDate;
60
+ /**
61
+ * --- Getters ---
62
+ */
63
+ /**
64
+ * Returns the year from the given date.
65
+ *
66
+ * @param date The date to get the year from. It can be an SDate or a string in
67
+ * the YYYY-MM-DD format.
68
+ */
13
69
  export declare const getYearFromDate: (date: string | SDate) => number;
70
+ /**
71
+ * Returns the month from the given date. Returns a 0-index value (i.e. Janary
72
+ * is 0 and December is 11) to match the result from native Date object.
73
+ *
74
+ * @param date The date to get the month from. It can be an SDate or a string in
75
+ * the YYYY-MM-DD format.
76
+ */
14
77
  export declare const getMonthFromDate: (date: string | SDate) => number;
78
+ /**
79
+ * Returns the day of the month from the given date.
80
+ *
81
+ * @param date The date to get the day from. It can be an SDate or a string in
82
+ * the YYYY-MM-DD format.
83
+ */
15
84
  export declare const getDateFromDate: (date: string | SDate) => number;
85
+ /**
86
+ * Returns the day of the week from the given date (Sunday to Saturday / 0 to
87
+ * 6).
88
+ *
89
+ * @param date The date to get the weekday from. It can be an SDate or a string
90
+ * in the YYYY-MM-DD format.
91
+ */
16
92
  export declare const getWeekdayFromDate: (date: string | SDate) => number;
93
+ /**
94
+ * Returns a native Date adjusted so that the local time of that date matches
95
+ * the local time at the specified time zone.
96
+ *
97
+ * @param date The date to get the time zoned date from. It can be an SDate or a
98
+ * string in the YYYY-MM-DD format.
99
+ * @param timeZone The time zone to adjust the date to. See
100
+ * `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
101
+ */
17
102
  export declare const getTimeZonedDateFromDate: (date: string | SDate, timeZone: string) => Date;
103
+ /**
104
+ * Returns the number of days between the first date to the second date. The
105
+ * value is positive if the first date is before the second date, and negative
106
+ * if the first date is after the second date. This accounts for calendar days
107
+ * and not full 24-hour periods which could be different due to daylight saving
108
+ * adjustments.
109
+ *
110
+ * @param date1 The first date to get the days between. It can be an SDate or a
111
+ * string in the YYYY-MM-DD format.
112
+ * @param date2 The second date to get the days between. It can be an SDate or a
113
+ * string in the YYYY-MM-DD format.
114
+ */
18
115
  export declare const getDaysBetweenDates: (date1: string | SDate, date2: string | SDate) => number;
116
+ /**
117
+ * Returns a string representation that includes all of the date components of
118
+ * the given date formatted according to the given locale.
119
+ *
120
+ * @param date The date to get the full string representation for. It can be an
121
+ * SDate or a string in the YYYY-MM-DD format.
122
+ * @param locale The locale to use for the string representation.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * getFullDateString('2021-02-05', 'es')
127
+ * //=> 'viernes, 5 de febrero de 2021'
128
+ * ```
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * getFullDateString('2021-02-05', 'en')
133
+ * //=> 'Friday, February 5, 2021'
134
+ * ```
135
+ */
19
136
  export declare const getFullDateString: (date: string | SDate, locale: Intl.LocalesArgument) => string;
137
+ /**
138
+ * Get the short string representation of the given date in the given locale.
139
+ *
140
+ * @param date The date to get the short string representation for. It can be an
141
+ * SDate or a string in the YYYY-MM-DD format.
142
+ * @param timeZone The time zone used to determine if in the current year. See
143
+ * `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
144
+ * @param locale The locale to use for the string representation.
145
+ * @param options The options to customize the short string representation.
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * getShortDateString('2021-02-05', TestLocalTimeZone, 'en', {
150
+ * onTodayText,
151
+ * includeWeekday: false,
152
+ * }),
153
+ * //=> 'Feb 5' (year is not shown when in the current year)
154
+ * ```
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * getShortDateString('2021-02-05', TestLocalTimeZone, 'es', {
159
+ * onTodayText,
160
+ * includeWeekday: true,
161
+ * })
162
+ * //=> 'vie, 5 feb 21' (year when not in current year)
163
+ * ```
164
+ */
20
165
  export declare const getShortDateString: (date: string | SDate, timeZone: string, locale: Intl.LocalesArgument, options: SDateShortStringOptions) => string;
166
+ /**
167
+ * --- Operations ---
168
+ */
169
+ /**
170
+ * Returns a new SDates instance with the date resulting from adding the given
171
+ * number of days to the given date. Because it adds calendar days rather than
172
+ * 24-hour days, this operation is not affected by time zones.
173
+ *
174
+ * @param date The date to add days to. It can be an SDate or a string in the
175
+ * YYYY-MM-DD format.
176
+ * @param days The number of days to add to the date.
177
+ */
21
178
  export declare const addDaysToDate: (date: string | SDate, days: number) => SDate;
179
+ /**
180
+ * Returns a new SDate instance with the date resulting from adding the given
181
+ * number of months to the given date. Because it just adds to the month
182
+ * component of the date, this operation is not affected by time zones.
183
+ *
184
+ * @param date The date to add months to. It can be an SDate or a string in the
185
+ * YYYY-MM-DD format.
186
+ * @param months The number of months to add to the date.
187
+ */
22
188
  export declare const addMonthsToDate: (date: string | SDate, months: number) => SDate;
189
+ /**
190
+ * Returns a new SDate instance with the date resulting from adding the given
191
+ * number of years to the given date. Because this only adds to the year
192
+ * component of the date, this method is not affected by leap years.
193
+ *
194
+ * @param date The date to add years to. It can be an SDate or a string in the
195
+ * YYYY-MM-DD format.
196
+ * @param years The number of years to add to the date.
197
+ */
23
198
  export declare const addYearsToDate: (date: string | SDate, years: number) => SDate;
199
+ /**
200
+ * --- Comparisons ---
201
+ */
202
+ /**
203
+ * Returns true when the two given dates represent the same day and false
204
+ * otherwise.
205
+ *
206
+ * @param date1 The first date to compare. It can be an SDate or a string in the
207
+ * YYYY-MM-DD format.
208
+ * @param date2 The second date to compare. It can be an SDate or a string in
209
+ * the YYYY-MM-DD format.
210
+ */
24
211
  export declare const isSameDate: (date1: string | SDate, date2: string | SDate) => boolean;
212
+ /**
213
+ * Returns true when the first date represents a date before the second date and
214
+ * false otherwise.
215
+ *
216
+ * @param date1 The first date to compare. It can be an SDate or a string in the
217
+ * YYYY-MM-DD format.
218
+ * @param date2 The second date to compare. It can be an SDate or a string in
219
+ * the YYYY-MM-DD format.
220
+ */
25
221
  export declare const isBeforeDate: (date1: string | SDate, date2: string | SDate) => boolean;
222
+ /**
223
+ * Returns true when the first date represents a date that happens on the same
224
+ * date or before the second date and false otherwise.
225
+ *
226
+ * @param date1 The first date to compare. It can be an SDate or a string in the
227
+ * YYYY-MM-DD format.
228
+ * @param date2 The second date to compare. It can be an SDate or a string in the
229
+ * the YYYY-MM-DD format.
230
+ */
26
231
  export declare const isSameDateOrBefore: (date1: string | SDate, date2: string | SDate) => boolean;
232
+ /**
233
+ * Returns true when the first date represents a date that happens after the
234
+ * second date and false otherwise.
235
+ *
236
+ * @param date1 The first date to compare. It can be an SDate or a string in the
237
+ * YYYY-MM-DD format.
238
+ * @param date2 The second date to compare. It can be an SDate or a string in the
239
+ * the YYYY-MM-DD format.
240
+ */
27
241
  export declare const isAfterDate: (date1: string | SDate, date2: string | SDate) => boolean;
242
+ /**
243
+ * Returns true when the first date represents a date that happens on the same
244
+ * date or after the second date and false otherwise.
245
+ *
246
+ * @param date1 The first date to compare. It can be an SDate or a string in the
247
+ * YYYY-MM-DD format.
248
+ * @param date2 The second date to compare. It can be an SDate or a string in the
249
+ * the YYYY-MM-DD format.
250
+ */
28
251
  export declare const isSameDateOrAfter: (date1: string | SDate, date2: string | SDate) => boolean;
252
+ /**
253
+ * Returns true when the date is today and false otherwise.
254
+ *
255
+ * @param date The date to check if it is today. It can be an SDate or a string
256
+ * in the YYYY-MM-DD format.
257
+ * @param timeZone The time zone to check if the date is today. See
258
+ * `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
259
+ */
29
260
  export declare const isDateToday: (date: string | SDate, timeZone: string) => boolean;
261
+ /**
262
+ * Returns true when the month on the first date is the same as the month on the
263
+ * second date. It also checks that the year is the same. Returns false
264
+ * otherwise.
265
+ *
266
+ * @param date1 The first date to compare. It can be an SDate or a string in the
267
+ * YYYY-MM-DD format.
268
+ * @param date2 The second date to compare. It can be an SDate or a string in
269
+ * the YYYY-MM-DD format.
270
+ *
271
+ * @example
272
+ * ```ts
273
+ * areDatesInSameMonth('2021-02-05', '2021-02-15')
274
+ * //=> true
275
+ * ```
276
+ *
277
+ * @example
278
+ * ```ts
279
+ * areDatesInSameMonth('2022-02-05', '2023-02-15')
280
+ * //=> false (different years)
281
+ * ```
282
+ */
30
283
  export declare const areDatesInSameMonth: (date1: string | SDate, date2: string | SDate) => boolean;
284
+ /**
285
+ * Returns true when the date represents a date in the current month and year.
286
+ * Returns false otherwise.
287
+ *
288
+ * @param date The date to check if it is in the current month. It can be an
289
+ * SDate or a string in the YYYY-MM-DD format.
290
+ * @param timeZone The time zone to check if the date is in the current month.
291
+ * See `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
292
+ */
31
293
  export declare const isDateInCurrentMonth: (date: string | SDate, timeZone: string) => boolean;
294
+ /**
295
+ * Returns true when the year of the first date is the same as the year on the
296
+ * second date. Returns false otherwise.
297
+ *
298
+ * @param date1 The first date to compare. It can be an SDate or a string in the
299
+ * YYYY-MM-DD format.
300
+ * @param date2 The second date to compare. It can be an SDate or a string in
301
+ * the YYYY-MM-DD format.
302
+ */
32
303
  export declare const areDatesInSameYear: (date1: string | SDate, date2: string | SDate) => boolean;
304
+ /**
305
+ * Returns true when the year component of the date matches the current year in
306
+ * the given time zone. Returns false otherwise.
307
+ *
308
+ * @param date The date to check if it is in the current year. It can be an
309
+ * SDate or a string in the YYYY-MM-DD format.
310
+ * @param timeZone The time zone to check if the date is in the current year.
311
+ * See `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
312
+ */
33
313
  export declare const isDateInCurrentYear: (date: string | SDate, timeZone: string) => boolean;