Skip to content

Commit 6e71b87

Browse files
committed
fix: singular for parseExpirationReadable
1 parent 29375e7 commit 6e71b87

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/shared.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ export function parseExpirationReadable(expirationStr: string): string | null {
6767
return null
6868
}
6969

70-
const expirationSeconds = parseFloat(expirationStr)
70+
const num = parseFloat(expirationStr)
7171
const lastChar = expirationStr[expirationStr.length - 1]
72-
if (lastChar === "m") return `${expirationSeconds} minutes`
73-
else if (lastChar === "h") return `${expirationSeconds} hours`
74-
else if (lastChar === "d") return `${expirationSeconds} days`
75-
return `${expirationSeconds} seconds`
72+
if (lastChar === "m") return `${num} minute${num > 1 ? "s" : ""}`
73+
else if (lastChar === "h") return `${num} hour${num > 1 ? "s" : ""}`
74+
else if (lastChar === "d") return `${num} day${num > 1 ? "s" : ""}`
75+
return `${num} second${num > 1 ? "s" : ""}`
7676
}
7777

7878
export type ParsedPath = {

test/parser.spec.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { expect, test } from "vitest"
2-
import { parsePath, ParsedPath, parseFilenameFromContentDisposition, parseExpiration } from "../src/shared"
2+
import {
3+
parsePath,
4+
ParsedPath,
5+
parseFilenameFromContentDisposition,
6+
parseExpiration,
7+
parseExpirationReadable,
8+
} from "../src/shared"
39

410
test("parsePath", () => {
511
const testPairs: [string, ParsedPath][] = [
@@ -43,29 +49,35 @@ test("parseFilenameFromContentDisposition", () => {
4349
})
4450

4551
test("parseExpiration", () => {
46-
const testPairs: [string, number | null][] = [
47-
["100", 100],
48-
["10.1", 10.1],
49-
["10m", 600],
50-
["10.0m", 600],
51-
["10h", 10 * 60 * 60],
52-
["10.0h", 10 * 60 * 60],
53-
["10d", 10 * 24 * 60 * 60],
54-
["10 d", 10 * 24 * 60 * 60],
55-
["10 d", 10 * 24 * 60 * 60],
56-
["10 ", 10],
57-
[" 10 ", 10],
52+
const testPairs: [string, number | null, string | null][] = [
53+
["1", 1, "1 second"],
54+
["1m", 60, "1 minute"],
55+
["0.5d", 12 * 60 * 60, "0.5 day"],
56+
["100", 100, "100 seconds"],
57+
["10.1", 10.1, "10.1 seconds"],
58+
["10m", 600, "10 minutes"],
59+
["10.0m", 600, "10 minutes"],
60+
["10h", 10 * 60 * 60, "10 hours"],
61+
["10.0h", 10 * 60 * 60, "10 hours"],
62+
["10d", 10 * 24 * 60 * 60, "10 days"],
63+
["10 d", 10 * 24 * 60 * 60, "10 days"],
64+
["10 d", 10 * 24 * 60 * 60, "10 days"],
65+
["10 ", 10, "10 seconds"],
66+
[" 10 ", 10, "10 seconds"],
5867

59-
[" 10 g", null],
60-
["10g", null],
61-
["-10", null],
62-
["-10d", null],
63-
["10M", null],
64-
["10Y", null],
65-
["d", null],
68+
[" 10 g", null, null],
69+
["10g", null, null],
70+
["-10", null, null],
71+
["-10d", null, null],
72+
["10M", null, null],
73+
["10Y", null, null],
74+
["d", null, null],
6675
]
67-
for (const [input, output] of testPairs) {
68-
const parsed = parseExpiration(input)
69-
expect(parsed, `checking expiration of ${input}`).toStrictEqual(output)
76+
for (const [input, parsed, readableParsed] of testPairs) {
77+
const expiration = parseExpiration(input)
78+
expect(expiration, `checking expiration of ${input}`).toStrictEqual(parsed)
79+
80+
const readable = parseExpirationReadable(input)
81+
expect(readable, `checking readable expiration of ${input}`).toStrictEqual(readableParsed)
7082
}
7183
})

0 commit comments

Comments
 (0)