Skip to content
On this page

Helpers

Universal

notEmpty()

Checks if an object is not any null-ish value.

ts
notEmpty = (item: unknown): boolean

Array

range()

Creates an Array between start and stop given stepsize step with an optional callback function to manipulate the returned items.

ts
range = <T = number>(
    start: number,
    stop: number,
    step = 1,
    callback?: (current: T) => T
): T[]

Usage

ts
range(0, 10).forEach(...)

for(const item of range(0, 10, 2, (item) => item * 2)) {}

Number

randomInt()

Generates a pseudo-random integer.

ts
randomInt = (min: number, max: number): number

Usage

ts
const random = randomInt(0, 250)

Object

clone()

Clones a given object

ts
clone = <T extends object>(obj: T): T

flattenObject

Flattens an Object using dot-notation.

ts
flattenObject = <T extends Record<string, any>>(obj: T)

Usage

ts
const flattened = flattenObject({
    test: "foo",
    schning: {
        spire: "yay!"
    }
})

/**
 * flattened = {
 *   "test": "foo",
 *   "schning.spire": "yay!"
 * }
 */

String

repeat()

Repeats chars given x-amount of times.

ts
repeat = (chars: string, amount: number): string

limitText()

Limits a text to x-characters.

ts
limitText = (text: string, limit: number = 20): string

removeHtmlTags()

Removes all HTML Tags from a string.

ts
removeHtmlTags = (htmlString: string): string

unsafeRandomString()

Generates an unsafe random string that can be optionally prefixed.

ts
unsafeRandomString = (prefix?: string, id?: string): string