-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
84 lines (80 loc) · 2.79 KB
/
helpers.ts
File metadata and controls
84 lines (80 loc) · 2.79 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
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @fileoverview Environment variable type conversion helpers.
*
* Thin wrappers over the unified implementations in `@socketsecurity/lib/env`
* that preserve the narrower `string | undefined` input signature and the
* original strict-no-trim / float / whitespace-preserving defaults. Prefer
* the root `env` module for new code — it supports both modes via options.
*/
import {
envAsBoolean as envAsBooleanRoot,
envAsNumber as envAsNumberRoot,
envAsString as envAsStringRoot,
} from '../env'
/**
* Convert an environment variable string to a boolean.
* Strict matching — does NOT trim whitespace (' true ' is false).
*
* @param value - The environment variable value to convert
* @returns `true` if value is exactly '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(' true ') // false (no trim)
* envAsBoolean(undefined) // false
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function envAsBoolean(value: string | undefined): boolean {
return envAsBooleanRoot(value, { trim: false })
}
/**
* Convert an environment variable string to a number.
* Uses `Number()` (decimals, hex, octal, binary, Infinity preserved); returns
* 0 only for undefined/empty/NaN. For int-only parsing use `envAsNumber` in
* `@socketsecurity/lib/env` with default `mode: 'int'`.
*
* @param value - The environment variable value to convert
* @returns The parsed number, or `0` if the value is undefined or NaN
*
* @example
* ```typescript
* import { envAsNumber } from '@socketsecurity/lib/env/helpers'
*
* envAsNumber('3000') // 3000
* envAsNumber('3.14') // 3.14
* envAsNumber('Infinity') // Infinity
* envAsNumber(undefined) // 0
* envAsNumber('abc') // 0
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function envAsNumber(value: string | undefined): number {
return envAsNumberRoot(value, { mode: 'float', allowInfinity: true })
}
/**
* Convert an environment variable value to a string, preserving whitespace.
* For trimmed-string behavior use `envAsString` in `@socketsecurity/lib/env`
* (default `trim: true`); this helper passes `trim: false`.
*
* @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(' x ') // ' x ' (whitespace preserved)
* envAsString(undefined) // ''
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function envAsString(value: string | undefined): string {
return envAsStringRoot(value, { trim: false })
}