Skip to content

Commit bc801b4

Browse files
committed
🎨 Organize functions
1 parent c813222 commit bc801b4

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

‎src/env.ts‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ export function envBool(key: string, fallback?: boolean): boolean {
1616
return toBool(key, value)
1717
}
1818

19+
/**
20+
* Obtains an environment variable as a Date.
21+
*
22+
* @example
23+
*
24+
* export const START_DATE = envDate("START_DATE")
25+
*/
26+
export function envDate(key: string, fallback?: Date): Date {
27+
const str = process.env[key]?.trim() || fallback?.toISOString().trim()
28+
if (str === undefined) throw new Error(`$${key} is missing`)
29+
30+
const date = new Date(str)
31+
if (isNaN(date.getTime()))
32+
throw new Error(`$${key} is not a valid Date: ${str}`)
33+
34+
return date
35+
}
36+
1937
/**
2038
* Obtains an environment variable as a floating-point number.
2139
*
@@ -112,35 +130,35 @@ export function envUuid(key: string, fallback?: UUID): UUID {
112130
}
113131

114132
/**
115-
* Obtains an environment variable as a Date.
133+
* Obtains an optional environment variable as a boolean.
116134
*
117135
* @example
118136
*
119-
* export const START_DATE = envDate("START_DATE")
137+
* export const CI = maybeEnvBool("CI")
120138
*/
121-
export function envDate(key: string, fallback?: Date): Date {
122-
const str = process.env[key]?.trim() || fallback?.toISOString().trim()
123-
if (str === undefined) throw new Error(`$${key} is missing`)
124-
125-
const date = new Date(str)
126-
if (isNaN(date.getTime()))
127-
throw new Error(`$${key} is not a valid Date: ${str}`)
139+
export function maybeEnvBool(key: string): boolean | undefined {
140+
const str = process.env[key]?.trim()
141+
if (!str) return undefined
128142

129-
return date
143+
return toBool(key, str)
130144
}
131145

132146
/**
133-
* Obtains an optional environment variable as a boolean.
147+
* Obtains an optional environment variable as a Date.
134148
*
135149
* @example
136150
*
137-
* export const CI = maybeEnvBool("CI")
151+
* export const END_DATE = maybeEnvDate("END_DATE")
138152
*/
139-
export function maybeEnvBool(key: string): boolean | undefined {
153+
export function maybeEnvDate(key: string): Date | undefined {
140154
const str = process.env[key]?.trim()
141155
if (!str) return undefined
142156

143-
return toBool(key, str)
157+
const date = new Date(str)
158+
if (isNaN(date.getTime()))
159+
throw new Error(`$${key} is not a valid Date: ${str}`)
160+
161+
return date
144162
}
145163

146164
/**
@@ -236,21 +254,3 @@ export function maybeEnvUuid(key: string): UUID | undefined {
236254
if (!isUuid(str)) throw new Error(`$${key} is not a UUID: ${str}`)
237255
return str
238256
}
239-
240-
/**
241-
* Obtains an optional environment variable as a Date.
242-
*
243-
* @example
244-
*
245-
* export const END_DATE = maybeEnvDate("END_DATE")
246-
*/
247-
export function maybeEnvDate(key: string): Date | undefined {
248-
const str = process.env[key]?.trim()
249-
if (!str) return undefined
250-
251-
const date = new Date(str)
252-
if (isNaN(date.getTime()))
253-
throw new Error(`$${key} is not a valid Date: ${str}`)
254-
255-
return date
256-
}

‎src/load_env.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { config } from "dotenv"
33
import { join } from "path"
44
import type { LoadedEnv } from "./loaded_env.ts"
55

6+
export interface LoadEnvOptions extends Omit<DotenvConfigOptions, "path"> {
7+
/** Where to find `.env` files. */
8+
readonly path?: string | undefined
9+
}
10+
611
/** Loads environment variables from the `.env` files. `NODE_ENV` has to be set
712
* in the environment and will not be picked up from the filesystem.
813
*
@@ -43,8 +48,3 @@ export function loadEnv(options?: LoadEnvOptions): LoadedEnv {
4348
function prepend(file: string, path: string | undefined): string {
4449
return path ? join(path, file) : file
4550
}
46-
47-
export interface LoadEnvOptions extends Omit<DotenvConfigOptions, "path"> {
48-
/** Where to find `.env` files. */
49-
readonly path?: string | undefined
50-
}

0 commit comments

Comments
 (0)