Skip to content

Commit 2bb0072

Browse files
committed
refc: extract some functions into a utilities.ts file
1 parent a137b20 commit 2bb0072

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

source/ratelimit-header-parser.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
RateLimitInfo,
88
ParserOptions,
99
} from './types'
10+
import { secondsToDate, toInt } from './utilities.js'
1011

1112
/**
1213
* Parses the passed response/headers object and returns rate limit information.
@@ -136,17 +137,6 @@ export function parseCombinedRateLimitHeader(header: string): RateLimitInfo {
136137
}
137138
}
138139

139-
function secondsToDate(seconds: number): Date {
140-
const d = new Date()
141-
d.setSeconds(d.getSeconds() + seconds)
142-
return d
143-
}
144-
145-
function toInt(input: string | number | undefined): number {
146-
if (typeof input === 'number') return input
147-
return Number.parseInt(input ?? '', 10)
148-
}
149-
150140
function getHeader(headers: HeadersObject, name: string): string | undefined {
151141
if ('get' in headers && typeof headers.get === 'function') {
152142
return headers.get(name) ?? undefined // Returns null if missing, but everything else is undefined for missing values

source/utilities.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// source/utilities.ts
2+
// The utility functions for the library
3+
4+
/**
5+
* Adds the given number of seconds to the current time and returns a `Date`.
6+
*
7+
* @param seconds {number} - The number of seconds to add to the current time.
8+
*
9+
* @return {Date} - The date, with a timestamp n seconds from now.
10+
*/
11+
export const secondsToDate = (seconds: number): Date => {
12+
const date = new Date()
13+
date.setSeconds(date.getSeconds() + seconds)
14+
return date
15+
}
16+
17+
/**
18+
* Converts a string/number to a number.
19+
*
20+
* @param input {string | number | undefined} - The input to convert to a number.
21+
*
22+
* @return {number} - The parsed integer.
23+
* @throws {Error} - Thrown if the string does not contain a valid number.
24+
*/
25+
export const toInt = (input: string | number | undefined): number => {
26+
if (typeof input === 'number') return input
27+
return Number.parseInt(input ?? '', 10)
28+
}

0 commit comments

Comments
 (0)