Skip to content

Commit 6389a5b

Browse files
committed
Add comments to basics functionality
1 parent d317c97 commit 6389a5b

5 files changed

Lines changed: 46 additions & 11 deletions

File tree

basics/src/env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { isEmpty } from './validators'
22

3+
/**
4+
* Reads an environment variable, and logs it.
5+
* If the environment variable cannot be found, the process is exited (die).
6+
* @param envName the name of the environment variable to be read
7+
*/
38
export function readEnvOrDie (envName: string): string {
49
const env = process.env[envName]
510
if (isEmpty(env)) {

basics/src/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import { sleep } from './sleep'
2-
import * as validators from './validators'
3-
import * as stringifiers from './stringifiers'
4-
import { readEnvOrDie } from './env'
5-
6-
export {
7-
sleep,
8-
validators,
9-
readEnvOrDie,
10-
stringifiers
11-
}
1+
export { sleep } from './sleep'
2+
export * as validators from './validators'
3+
export * as stringifiers from './stringifiers'
4+
export { readEnvOrDie } from './env'

basics/src/sleep.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Return promise after given amount of milliseconds. Await the promise to simulate a sleep.
3+
* @param ms time in milliseconds
4+
*/
15
export async function sleep (ms: number): Promise<void> {
26
return await new Promise<void>(resolve => setTimeout(resolve, ms))
37
}

basics/src/stringifiers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11

2+
/**
3+
* Stringifies an array and cuts out text from elements if it is longer than maxLen.
4+
* Can be used for logging to reduce clutter.
5+
* @param arg the array of objects to be logged.
6+
* @param maxLen if length of stringified arg is longer than maxLen, then we cut it short.
7+
*/
28
export function stringifyArray (args: unknown[], maxLen?: number): string {
39
return JSON.stringify(
410
args.map(arg => stringify(arg, maxLen))
511
)
612
}
713

14+
/**
15+
* Stringifies an object and cuts out text if it is longer than maxLen.
16+
* Can be used for logging to reduce clutter.
17+
* @param arg the object to be logged.
18+
* @param maxLen if length of stringified arg is longer than maxLen, then we cut it short.
19+
*/
820
export const stringify = (arg: unknown, maxLen?: number): string => {
921
const json = JSON.stringify(arg)
1022
if (maxLen === undefined || json.length <= maxLen) {

basics/src/validators.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1+
/**
2+
* Type guard checking if x is a number.
3+
* @param x
4+
*/
15
export function isNumber (x: unknown): x is number {
26
return typeof x === 'number'
37
}
48

9+
/**
10+
* Type guard checking if x is a string.
11+
* @param x
12+
*/
513
export function isString (x: unknown): x is string {
614
return typeof x === 'string'
715
}
816

17+
/**
18+
* Type guard checking if value is undefined or empty.
19+
* @param value
20+
*/
921
export function isEmpty (value: string | undefined): value is undefined {
1022
return value === undefined || value === ''
1123
}
1224

25+
/**
26+
* Type guard checking if x is an object.
27+
* @param x
28+
*/
1329
export function isObject (x: unknown): x is object {
1430
return typeof x === 'object'
1531
}
1632

33+
/**
34+
* Type guard checking if object has property p.
35+
* @param object the object to be checked
36+
* @param name name of the property
37+
*/
1738
// Helper function to fix issue that `in` operator as type guard is not widening type with the asserted property key
1839
// See https://github.com/microsoft/TypeScript/issues/21732
1940
export function hasProperty<P extends PropertyKey, O extends object> (object: O, name: P):

0 commit comments

Comments
 (0)