Skip to content

Commit 05b9059

Browse files
committed
Add conversion documentation for env.ts
1 parent d4a0354 commit 05b9059

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Conversion for `env.ts`
2+
3+
## `export const envInt = (name: string): number | undefined`
4+
5+
This is redundant and will not be converted. Simply use `ProcessInfo` and wrap it into an `Int` initializer. A fallback can be made by using `??`.
6+
7+
```swift
8+
let port = Int(ProcessInfo.processInfo.environment["PORT"] ?? "") ?? 8080
9+
```
10+
11+
## `export const envStr = (name: string): string | undefined`
12+
13+
This is redundant and will not be converted. Simply use `ProcessInfo` and wrap it into a `String` initializer. A fallback can be made by using `??`.
14+
15+
```swift
16+
let nameValue = String(ProcessInfo.processInfo.environment["NAME"] ?? "")
17+
```
18+
19+
## `export const envBool = (name: string): boolean | undefined`
20+
21+
This is redunant and will not be converted. Simply use `ProcessInfo` for this. Creating a method for this is trivial:
22+
23+
```swift
24+
func envBool(_ name: String) -> Bool? {
25+
guard let value = ProcessInfo.processInfo.environment[name]?.lowercased() else { return nil }
26+
switch value {
27+
case "true", "1": return true
28+
case "false", "0": return false
29+
default: return nil
30+
}
31+
}
32+
```
33+
34+
## `export const envList = (name: string): string[]`
35+
36+
This is redundant and will not be converted. Simply use `ProcessInfo` for this. This can be done with just one line:
37+
38+
```swift
39+
let arrayItems = ProcessInfo.processInfo.environment["ITEMS"]?.split(separator: ",").map(String.init) ?? []
40+
```

0 commit comments

Comments
 (0)