Skip to content

Commit 577476d

Browse files
committed
refactor: replace time formatters with re-exports from @softwareventures/format-time
1 parent 46f7e41 commit 577476d

File tree

4 files changed

+34
-52
lines changed

4 files changed

+34
-52
lines changed

index.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import test from "ava";
22
import {timestamp} from "@softwareventures/timestamp";
3-
import {iso8601, secondsMs} from "./index";
4-
5-
test("secondsMs", t => {
6-
t.is(secondsMs({seconds: 0.001}), "00.001");
7-
t.is(secondsMs({seconds: 1}), "01.000");
8-
t.is(secondsMs({seconds: 1.0012}), "01.001");
9-
t.is(secondsMs({seconds: 1.0018}), "01.001");
10-
t.is(secondsMs({seconds: 22.0018}), "22.001");
11-
});
3+
import {iso8601} from "./index";
124

135
test("iso8601", t => {
146
t.is(

index.ts

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {concatMap} from "@softwareventures/array";
22
import type {Timestamp} from "@softwareventures/timestamp";
33
import {normalize} from "@softwareventures/timestamp";
44
import * as formatDate from "@softwareventures/format-date";
5+
import * as formatTime from "@softwareventures/format-time";
56
import {JsDate} from "./js-date";
67

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

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

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

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

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

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

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

135126
/** Formats the minutes portion of the specified {@link Timestamp} as a
136127
* numeric string. */
137-
export function minutes(timestamp: {readonly minutes: number}): string {
138-
return String(timestamp.minutes);
139-
}
128+
export const minutes = formatTime.minutes;
140129

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

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

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

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

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

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

185161
/** Formats the specified {@link Timestamp} as IS0 8601 extended, rounded down
186162
* to the next lower second e.g. `"2021-05-01T11:57:23Z"`. */

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"@softwareventures/array": "^3.10.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
3131
"@softwareventures/format-date": "^1.0.0",
32+
"@softwareventures/format-time": "^1.0.0",
3233
"@softwareventures/timestamp": "^1.0.0 || ^2.0.0",
3334
"tslib": "^2.6.2"
3435
},

yarn.lock

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,12 @@
696696
tslib "^2.6.2"
697697

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

707707
"@softwareventures/format-timestamp@^1.0.0":
@@ -743,7 +743,7 @@
743743
dependencies:
744744
tslib "2.6.2"
745745

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

760+
"@softwareventures/time@^2.0.0":
761+
version "2.1.0"
762+
resolved "https://registry.yarnpkg.com/@softwareventures/time/-/time-2.1.0.tgz#53ee6d27b33242a2f5a6dfbeb1bc892fe404930a"
763+
integrity sha512-jL/9cUZTIcEu2dC0ZKslxYU5QMipnRdd0i0olIDDYpVPB4T0vSfRvfpmErx6HOR4fnzk4B+9/kWPS1MRAMrRPw==
764+
dependencies:
765+
"@softwareventures/nullable" "^3.2.0"
766+
"@softwareventures/ordered" "^1.0.0 || ^2.0.0"
767+
"@types/is-integer" "1.0.2"
768+
is-integer "^1.0.7"
769+
is-integer-in-range "^3.0.1 || ^4.0.0"
770+
tslib "^1.9.3 || ^2.0.0 || ^2.1.0"
771+
unknown "^0.2.5"
772+
760773
"@softwareventures/timestamp@^1.0.0 || ^2.0.0":
761774
version "2.2.0"
762775
resolved "https://registry.yarnpkg.com/@softwareventures/timestamp/-/timestamp-2.2.0.tgz#84dbc08f1bd12c95e45ce056223460c2c5116289"

0 commit comments

Comments
 (0)