@tim-code/my-util 0.0.18 → 0.0.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
package/src/time.js CHANGED
@@ -95,7 +95,7 @@ export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
95
95
 
96
96
  /**
97
97
  * Add an amount of time to a time string.
98
- * @param {string} timeString HH:mm:ss or HH:mm; parsable numbers separated by :
98
+ * @param {string} timeString HH:mm:ss or HH:mm
99
99
  * @param {Object} $1
100
100
  * @param {number} $1.hours Hours to add to time string
101
101
  * @param {number} $1.minutes Minutes to add to time string
@@ -119,9 +119,35 @@ export function addTime(timeString, { minutes = 0, hours = 0 } = {}) {
119
119
  return newTime
120
120
  }
121
121
 
122
+ const MINUTES_IN_DAY = 24 * 60
123
+ /**
124
+ * Get all minutes between two times. This does not work across day i.e 23:59:00 to 00:00:00.
125
+ * @param {string} start HH:mm:ss or HH:mm
126
+ * @param {string} end HH:mm:ss or HH:mm
127
+ * @returns {Array} times in HH:mm:ss
128
+ */
129
+ export function getMinuteRange(start, end) {
130
+ // coerce start and end to seconds
131
+ start = addTime(start)
132
+ end = addTime(end)
133
+ if (!(start <= end)) {
134
+ throw new Error("start time is not less than end time")
135
+ }
136
+ const times = []
137
+ let current = start
138
+ while (current <= end) {
139
+ times.push(current)
140
+ current = addTime(current, { minutes: 1 })
141
+ if (times.length >= MINUTES_IN_DAY) {
142
+ break
143
+ }
144
+ }
145
+ return times
146
+ }
147
+
122
148
  /**
123
149
  * Adds a number of days to a date string.
124
- * @param {string} dateString
150
+ * @param {string} dateString YYYY-MM-DD
125
151
  * @param {number} days
126
152
  * @returns {string}
127
153
  */
@@ -134,8 +160,8 @@ export function addDays(dateString, days = 0) {
134
160
 
135
161
  /**
136
162
  * Get all dates between two dates, with limit.
137
- * @param {string} start
138
- * @param {string} end
163
+ * @param {string} start YYYY-MM-DD
164
+ * @param {string} end YYYY-MM-DD
139
165
  * @returns {Array}
140
166
  */
141
167
  export function getDateRange(start, end, { limit = 1000 } = {}) {
package/src/time.test.js CHANGED
@@ -4,10 +4,12 @@ import {
4
4
  addTime,
5
5
  getDateRange,
6
6
  getEasternTime,
7
+ getMinuteRange,
7
8
  getTime,
8
9
  isDate,
9
10
  isTime,
10
11
  isUnixTimestamp,
12
+ today,
11
13
  } from "./time.js"
12
14
 
13
15
  // getEasternTime changed: now accepts a "timezone" param and no longer returns "minute" or "datetime"
@@ -177,6 +179,14 @@ describe("getTime", () => {
177
179
  })
178
180
  })
179
181
 
182
+ describe("today", () => {
183
+ test("returns today's date in YYYY-MM-DD format", () => {
184
+ const expected = getTime().date
185
+ expect(today()).toBe(expected)
186
+ expect(/^\d{4}-\d{2}-\d{2}$/u.test(today())).toBe(true)
187
+ })
188
+ })
189
+
180
190
  describe("isDate", () => {
181
191
  test("returns true for valid YYYY-MM-DD dates", () => {
182
192
  expect(isDate("2024-06-01")).toBe(true)
@@ -316,6 +326,49 @@ describe("addTime", () => {
316
326
  })
317
327
  })
318
328
 
329
+ describe("getMinuteRange", () => {
330
+ test("returns all minutes between start and end inclusive", () => {
331
+ expect(getMinuteRange("12:00:00", "12:03:00")).toEqual([
332
+ "12:00:00",
333
+ "12:01:00",
334
+ "12:02:00",
335
+ "12:03:00",
336
+ ])
337
+ })
338
+
339
+ test("works with times that cross the hour", () => {
340
+ expect(getMinuteRange("12:58:00", "13:01:00")).toEqual([
341
+ "12:58:00",
342
+ "12:59:00",
343
+ "13:00:00",
344
+ "13:01:00",
345
+ ])
346
+ })
347
+
348
+ test("works with times with nonzero seconds", () => {
349
+ expect(getMinuteRange("12:00:30", "12:02:30")).toEqual([
350
+ "12:00:30",
351
+ "12:01:30",
352
+ "12:02:30",
353
+ ])
354
+ })
355
+
356
+ test("returns single element if start equals end", () => {
357
+ expect(getMinuteRange("12:34:56", "12:34:56")).toEqual(["12:34:56"])
358
+ })
359
+
360
+ test("throws if start is not less than end", () => {
361
+ expect(() => getMinuteRange("12:04:00", "12:03:00")).toThrow(
362
+ /start time is not less than end time/u
363
+ )
364
+ expect(() => getMinuteRange("12:00:00", "12:00:00")).not.toThrow()
365
+ })
366
+
367
+ test("handles input with no seconds", () => {
368
+ expect(getMinuteRange("12:00", "12:02")).toEqual(["12:00:00", "12:01:00", "12:02:00"])
369
+ })
370
+ })
371
+
319
372
  describe("addDays", () => {
320
373
  test("adds days within the same month", () => {
321
374
  expect(addDays("2024-06-01", 5)).toBe("2024-06-06")