Skip to content

Commit

Permalink
refactor: replace time formatters with re-exports from @softwareventu…
Browse files Browse the repository at this point in the history
…res/format-time
  • Loading branch information
djcsdy committed May 16, 2024
1 parent 46f7e41 commit 577476d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 52 deletions.
10 changes: 1 addition & 9 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import test from "ava";
import {timestamp} from "@softwareventures/timestamp";
import {iso8601, secondsMs} from "./index";

test("secondsMs", t => {
t.is(secondsMs({seconds: 0.001}), "00.001");
t.is(secondsMs({seconds: 1}), "01.000");
t.is(secondsMs({seconds: 1.0012}), "01.001");
t.is(secondsMs({seconds: 1.0018}), "01.001");
t.is(secondsMs({seconds: 22.0018}), "22.001");
});
import {iso8601} from "./index";

test("iso8601", t => {
t.is(
Expand Down
52 changes: 14 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {concatMap} from "@softwareventures/array";
import type {Timestamp} from "@softwareventures/timestamp";
import {normalize} from "@softwareventures/timestamp";
import * as formatDate from "@softwareventures/format-date";
import * as formatTime from "@softwareventures/format-time";
import {JsDate} from "./js-date";

/** A function that formats a {@link Timestamp} or part of a {@link Timestamp}
Expand Down Expand Up @@ -102,85 +103,60 @@ export const dayOfWeek = formatDate.dayOfWeek;

/** Formats the hours portion of the specified {@link Timestamp} as a 24-hour
* numeric string. */
export function hours(timestamp: {readonly hours: number}): string {
return String(timestamp.hours);
}
export const hours = formatTime.hours;

/** Formats the hours portion of the specified {@link Timestamp} as a 2-digit
* 24-hour numeric string. */
export function hours2(timestamp: {readonly hours: number}): string {
return String(timestamp.hours).padStart(2, "0");
}
export const hours2 = formatTime.hours2;

/** Formats the hours portion of the specified {@link Timestamp} as a 12-hour
* numeric string. */
export function hours12(timestamp: {readonly hours: number}): string {
return String((12 + (timestamp.hours % 12)) % 12);
}
export const hours12 = formatTime.hours12;

/** Formats the hours portion of the specified {@link Timestamp} as a 2-digit
* 12-hour numeric string. */
export function hours122(timestamp: {readonly hours: number}): string {
return String((12 + (timestamp.hours % 12)) % 12).padStart(2, "0");
}
export const hours122 = formatTime.hours122;

export type AmPm = "AM" | "PM";
export {AmPm} from "@softwareventures/format-time";

/** Returns `"AM"` or `"PM"` depending on the hour of the specified
* {@link Timestamp}. */
export function amPm(timestamp: {readonly hours: number}): AmPm {
return timestamp.hours < 12 ? "AM" : "PM";
}
export const amPm = formatTime.amPm;

/** Formats the minutes portion of the specified {@link Timestamp} as a
* numeric string. */
export function minutes(timestamp: {readonly minutes: number}): string {
return String(timestamp.minutes);
}
export const minutes = formatTime.minutes;

/** Formats the minutes portion of the specified {@link Timestamp} as a
* 2-digit numeric string. */
export function minutes2(timestamp: {readonly minutes: number}): string {
return String(timestamp.minutes).padStart(2, "0");
}
export const minutes2 = formatTime.minutes2;

/** Formats the seconds portion of the specified {@link Timestamp} as a
* numeric string.
*
* Note that fractional seconds will not be rounded, so this might produce
* a result similar to `"2.234"` */
export function seconds(timestamp: {readonly seconds: number}): string {
return String(timestamp.seconds);
}
export const seconds = formatTime.seconds;

/** Formats the seconds portion of the specified {@link Timestamp} as a
* numeric string. If necessary, adds a leading zero to the whole part of the
* seconds to ensure the whole part is at least two digits.
*
* Note that fractional seconds will not be rounded, so this might produce
* a result similar to `"02.234"`. */
export function seconds2(timestamp: {readonly seconds: number}): string {
return String(timestamp.seconds).replace(/^\d+/u, s => s.padStart(2, "0"));
}
export const seconds2 = formatTime.seconds2;

/** Rounds the seconds portion of the specified {@link Timestamp} down and
* formats the result as a numeric string. */
export function floorSeconds(timestamp: {readonly seconds: number}): string {
return String(Math.floor(timestamp.seconds));
}
export const floorSeconds = formatTime.floorSeconds;

/** Rounds the seconds portion of the specified {@link Timestamp} down and
* formats the result as a 2-digit numeric string. */
export function floorSeconds2(timestamp: {readonly seconds: number}): string {
return String(Math.floor(timestamp.seconds)).padStart(2, "0");
}
export const floorSeconds2 = formatTime.floorSeconds2;

/** Rounds the seconds portion of the specified {@link Timestamp} down to the
* next lower millisecond, and formats the result as a 2.3-digit string. */
export function secondsMs(timestamp: {readonly seconds: number}): string {
const s = String(Math.floor(timestamp.seconds * 1000)).padStart(5, "0");
return `${s.substr(0, 2)}.${s.substr(2)}`;
}
export const secondsMs = formatTime.secondsMs;

/** Formats the specified {@link Timestamp} as IS0 8601 extended, rounded down
* to the next lower second e.g. `"2021-05-01T11:57:23Z"`. */
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@softwareventures/array": "^3.10.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
"@softwareventures/format-date": "^1.0.0",
"@softwareventures/format-time": "^1.0.0",
"@softwareventures/timestamp": "^1.0.0 || ^2.0.0",
"tslib": "^2.6.2"
},
Expand Down
23 changes: 18 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,12 @@
tslib "^2.6.2"

"@softwareventures/format-time@^1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@softwareventures/format-time/-/format-time-1.0.1.tgz#3bd0a483eedbe06159a21ea5f8d15080aaca41cf"
integrity sha512-IRD8Uyv2lYce/tcFY5mUah6jsG14YfkHYfD6ROEvwk++gaLuQlHaZoEYzgUFWp/tnhDe1b8DpU6dUhRYSZ2JYA==
version "1.0.0"
resolved "https://registry.yarnpkg.com/@softwareventures/format-time/-/format-time-1.0.0.tgz#5fa0431320a9baba2fdf014263dee712968ed125"
integrity sha512-2jeoP5imm+jlMBS51b5Lyu7Ca9YBeEoqYd+xJlkoJEdg5gkKvV3+Cg8XX1FXRkrvhl5GBGpK59L0VN5pvBqr5w==
dependencies:
"@softwareventures/array" "^7.0.0"
"@softwareventures/time" "^2.0.0 || ^3.0.0"
"@softwareventures/time" "^2.0.0"
tslib "^2.5.0"

"@softwareventures/format-timestamp@^1.0.0":
Expand Down Expand Up @@ -743,7 +743,7 @@
dependencies:
tslib "2.6.2"

"@softwareventures/time@^1.0.0 || ^2.0.0 || ^3.0.0", "@softwareventures/time@^2.0.0 || ^3.0.0":
"@softwareventures/time@^1.0.0 || ^2.0.0 || ^3.0.0":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@softwareventures/time/-/time-3.1.1.tgz#38134c2ae4bd9b124aa7cae6800e92fbad3c57af"
integrity sha512-ZMi4/upMM7HavvNeOzbROAS6yicNFsqqdRRth79AA2hR8RdSu0StTRM8y2FOyC2d9SPzzqS8B1f7JOSnvrJgRw==
Expand All @@ -757,6 +757,19 @@
tslib "^2.1.0"
unknown "^0.2.5"

"@softwareventures/time@^2.0.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@softwareventures/time/-/time-2.1.0.tgz#53ee6d27b33242a2f5a6dfbeb1bc892fe404930a"
integrity sha512-jL/9cUZTIcEu2dC0ZKslxYU5QMipnRdd0i0olIDDYpVPB4T0vSfRvfpmErx6HOR4fnzk4B+9/kWPS1MRAMrRPw==
dependencies:
"@softwareventures/nullable" "^3.2.0"
"@softwareventures/ordered" "^1.0.0 || ^2.0.0"
"@types/is-integer" "1.0.2"
is-integer "^1.0.7"
is-integer-in-range "^3.0.1 || ^4.0.0"
tslib "^1.9.3 || ^2.0.0 || ^2.1.0"
unknown "^0.2.5"

"@softwareventures/timestamp@^1.0.0 || ^2.0.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@softwareventures/timestamp/-/timestamp-2.2.0.tgz#84dbc08f1bd12c95e45ce056223460c2c5116289"
Expand Down

0 comments on commit 577476d

Please sign in to comment.