@@ -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- }
0 commit comments