@tim-code/my-util 0.8.0 → 0.8.2

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.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/array.js CHANGED
@@ -194,6 +194,12 @@ function naturalCompare(a, b) {
194
194
  b = b.slice(1, b.length)
195
195
  return b.localeCompare(a, undefined, { numeric: true })
196
196
  }
197
+ if (a[0] === "+" && !Number.isNaN(Number(a))) {
198
+ a = a.slice(1, a.length)
199
+ }
200
+ if (b[0] === "+" && !Number.isNaN(Number(b))) {
201
+ b = b.slice(1, b.length)
202
+ }
197
203
  return a.localeCompare(b, undefined, { numeric: true })
198
204
  }
199
205
 
package/src/array.test.js CHANGED
@@ -542,6 +542,18 @@ describe("naturalAsc", () => {
542
542
  arr.sort(naturalAsc())
543
543
  expect(arr).toEqual(["-10", "-2", "-1"])
544
544
  })
545
+
546
+ it("can sort negative to positive", () => {
547
+ const arr = ["-2", "+5", "0"]
548
+ arr.sort(naturalAsc())
549
+ expect(arr).toEqual(["-2", "0", "+5"])
550
+ })
551
+
552
+ it("handles leading plus signs on numeric-like strings", () => {
553
+ const arr = ["a", "+10", "+2", "+1"]
554
+ arr.sort(naturalAsc())
555
+ expect(arr).toEqual(["+1", "+2", "+10", "a"])
556
+ })
545
557
  })
546
558
 
547
559
  describe("naturalDesc", () => {
@@ -586,6 +598,18 @@ describe("naturalDesc", () => {
586
598
  arr.sort(naturalDesc())
587
599
  expect(arr).toEqual(["-1", "-2", "-10"])
588
600
  })
601
+
602
+ it("can sort positive to negative", () => {
603
+ const arr = ["+5", "-2", "0"]
604
+ arr.sort(naturalDesc())
605
+ expect(arr).toEqual(["+5", "0", "-2"])
606
+ })
607
+
608
+ it("handles leading plus signs on numeric-like strings (descending)", () => {
609
+ const arr = ["+1", "+2", "+10", "a"]
610
+ arr.sort(naturalDesc())
611
+ expect(arr).toEqual(["a", "+10", "+2", "+1"])
612
+ })
589
613
  })
590
614
 
591
615
  describe("multilevel", () => {
package/src/time.js CHANGED
@@ -1,9 +1,5 @@
1
1
  import { mod } from "./math.js"
2
2
 
3
- // `floorMinute` at first glance seems ugly and something like `floor: "minute"` seems better.
4
- // However, practically and realistically, it doesn't seem like we need other values for floor besides "second" and "minute".
5
- // So, then it seems more problematic to rely on an exact string to be passed... probably would need to throw on some other value.
6
- // We don't really ever want to disable flooring because we want timestamp, date, and time returned to represent the same thing.
7
3
  /**
8
4
  * Gets various ways of representing the current time in EDT. Floors to nearest second by default.
9
5
  * @param {Object} $1
@@ -91,7 +87,7 @@ export function getDayIndexInWeek(string = today()) {
91
87
 
92
88
  /**
93
89
  * Get the minute from a time string.
94
- * @param {string} time HH:mm or HH:mm::ss
90
+ * @param {string} time HH:mm or HH:mm:ss
95
91
  * @returns {number} Between 0 and 59 inclusive
96
92
  */
97
93
  export function getMinute(time) {
@@ -264,3 +260,23 @@ export function getStartOfWeek(dateString) {
264
260
  const start = addDays(dateString, { days: -index })
265
261
  return start
266
262
  }
263
+
264
+ /**
265
+ * Returns number of seconds from any combination of weeks, days, hours, and minutes.
266
+ * @param {Object} $1
267
+ * @param {number=} $1.weeks Default is 0
268
+ * @param {number=} $1.days Default is 0
269
+ * @param {number=} $1.hours Default is 0
270
+ * @param {number=} $1.minutes Default is 0
271
+ * @returns {number}
272
+ */
273
+ export function convertToSeconds({ weeks = 0, days = 0, hours = 0, minutes = 0 }) {
274
+ if (!(weeks >= 0) || !(days >= 0) || !(hours >= 0) || !(minutes >= 0)) {
275
+ throw new Error("weeks, days, hours, and minutes must be nonnegative")
276
+ }
277
+ days += weeks * 7
278
+ hours += days * 24
279
+ minutes += hours * 60
280
+ const seconds = minutes * 60
281
+ return seconds
282
+ }
package/src/time.test.js CHANGED
@@ -2,6 +2,7 @@ import { describe, expect, test } from "@jest/globals"
2
2
  import {
3
3
  addDays,
4
4
  addTime,
5
+ convertToSeconds,
5
6
  getDateRange,
6
7
  getDayIndexInWeek,
7
8
  getEasternTime,
@@ -21,7 +22,7 @@ import {
21
22
  // Exported functions:
22
23
  // getEasternTime, getTime, getUnixTimestamp, today, getDayIndexInWeek, getMinute,
23
24
  // isDateString, isTimeString, isDateTimeString, isUTCString, isUnixTimestamp,
24
- // addTime, getTimeRange, addDays, getDateRange, getStartOfWeek
25
+ // addTime, getTimeRange, addDays, getDateRange, getStartOfWeek, convertToSeconds
25
26
 
26
27
  describe("getEasternTime", () => {
27
28
  test("returns correct structure and types", () => {
@@ -586,3 +587,32 @@ describe("getStartOfWeek", () => {
586
587
  expect(getStartOfWeek("2023-01-02")).toBe("2023-01-01")
587
588
  })
588
589
  })
590
+
591
+ describe("convertToSeconds", () => {
592
+ test("returns 0 for empty input object", () => {
593
+ expect(convertToSeconds({})).toBe(0)
594
+ })
595
+
596
+ test("converts single units correctly", () => {
597
+ expect(convertToSeconds({ minutes: 5 })).toBe(5 * 60)
598
+ expect(convertToSeconds({ hours: 2 })).toBe(2 * 3600)
599
+ expect(convertToSeconds({ days: 1 })).toBe(24 * 3600)
600
+ expect(convertToSeconds({ weeks: 1 })).toBe(7 * 24 * 3600)
601
+ })
602
+
603
+ test("converts combined weeks, days, hours, and minutes", () => {
604
+ // 1w + 2d + 3h + 4m = 788640 seconds
605
+ expect(convertToSeconds({ weeks: 1, days: 2, hours: 3, minutes: 4 })).toBe(788640)
606
+ })
607
+
608
+ test("supports fractional values (minutes: 0.5 => 30 seconds)", () => {
609
+ expect(convertToSeconds({ minutes: 0.5 })).toBe(30)
610
+ })
611
+
612
+ test("throws if any value is negative", () => {
613
+ const cases = [{ weeks: -1 }, { days: -1 }, { hours: -1 }, { minutes: -1 }]
614
+ for (const opts of cases) {
615
+ expect(() => convertToSeconds(opts)).toThrow("must be nonnegative")
616
+ }
617
+ })
618
+ })