-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
71 lines (68 loc) · 1.82 KB
/
helpers.ts
File metadata and controls
71 lines (68 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @fileoverview Environment variable type conversion helpers.
*/
/**
* Convert an environment variable string to a boolean.
*
* @param value - The environment variable value to convert
* @returns `true` if value is 'true', '1', or 'yes' (case-insensitive), `false` otherwise
*
* @example
* ```typescript
* import { envAsBoolean } from '@socketsecurity/lib/env/helpers'
*
* envAsBoolean('true') // true
* envAsBoolean('1') // true
* envAsBoolean('yes') // true
* envAsBoolean(undefined) // false
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function envAsBoolean(value: string | undefined): boolean {
if (!value) {
return false
}
const lower = value.toLowerCase()
return lower === 'true' || lower === '1' || lower === 'yes'
}
/**
* Convert an environment variable string to a number.
*
* @param value - The environment variable value to convert
* @returns The parsed number, or `0` if the value is undefined or not a valid number
*
* @example
* ```typescript
* import { envAsNumber } from '@socketsecurity/lib/env/helpers'
*
* envAsNumber('3000') // 3000
* envAsNumber(undefined) // 0
* envAsNumber('abc') // 0
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function envAsNumber(value: string | undefined): number {
if (!value) {
return 0
}
const num = Number(value)
return Number.isNaN(num) ? 0 : num
}
/**
* Convert an environment variable value to a string.
*
* @param value - The environment variable value to convert
* @returns The string value, or an empty string if undefined
*
* @example
* ```typescript
* import { envAsString } from '@socketsecurity/lib/env/helpers'
*
* envAsString('hello') // 'hello'
* envAsString(undefined) // ''
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function envAsString(value: string | undefined): string {
return value || ''
}