Options
All
  • Public
  • Public/Protected
  • All
Menu

Utils

Index

Type aliases

ArrayElement

ArrayElement<ArrayType>: ArrayType[number]

Turns array elements into a type.

['name', 'email'] becomes 'name' | 'email'

Type parameters

  • ArrayType: readonly unknown[]

DeepPartial

DeepPartial<T>: {[ P in keyof T]: DeepPartial<T[P]> }

Works the same as Partial<T> except it applies Partial to sub elements.

Type parameters

  • T

Variables

Const HOUR_IN_MINUTES

HOUR_IN_MINUTES: 60 = 60

Const HOUR_IN_MS

HOUR_IN_MS: number = ...

Const HOUR_IN_SECONDS

HOUR_IN_SECONDS: number = ...

Const MINUTE_IN_MS

MINUTE_IN_MS: number = ...

Const MINUTE_IN_SECONDS

MINUTE_IN_SECONDS: 60 = 60

Const SECOND_IN_MS

SECOND_IN_MS: 1000 = 1000

Functions

Const asyncForEach

  • asyncForEach<T>(array: T[], itterator: (value: T, index: number, array: T[]) => Promise<void>, options?: DeepPartial<AsyncForEachOptions>): Promise<void>
  • Runs the supplied itterator for all elements in the array asyncronously.

    Type parameters

    • T

    Parameters

    • array: T[]

      The array to itterate through.

    • itterator: (value: T, index: number, array: T[]) => Promise<void>

      The async function to run for each element.

        • (value: T, index: number, array: T[]): Promise<void>
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns Promise<void>

    • Optional options: DeepPartial<AsyncForEachOptions>

    Returns Promise<void>

Const asyncMap

  • asyncMap<T, K>(list: T[], executor: (element: T, index: number, array: T[]) => Promise<K>): Promise<K[]>
  • Runs Array.map asyncronously.

    Type parameters

    • T

    • K

    Parameters

    • list: T[]

      The array to map

    • executor: (element: T, index: number, array: T[]) => Promise<K>

      The function to run for each

        • (element: T, index: number, array: T[]): Promise<K>
        • Parameters

          • element: T
          • index: number
          • array: T[]

          Returns Promise<K>

    Returns Promise<K[]>

Const asyncTimes

  • asyncTimes<T>(number: number, cb: (i: number) => Promise<T>): Promise<T[]>
  • Repeat the given function number times asyncronously

    Type parameters

    • T

    Parameters

    • number: number

      The number of times to itterate

    • cb: (i: number) => Promise<T>

      The function to run

        • (i: number): Promise<T>
        • Parameters

          • i: number

          Returns Promise<T>

    Returns Promise<T[]>

Const cacheFor

  • cacheFor<T>(__namedParameters: CacheForOptions, generator: () => Promise<T>): Promise<T>
  • A crude caching system that will cache values for the given time.

    Type parameters

    • T

    Parameters

    • __namedParameters: CacheForOptions
    • generator: () => Promise<T>

      The promise function that returns the cached value.

        • (): Promise<T>
        • Returns Promise<T>

    Returns Promise<T>

Const cacheForSync

  • cacheForSync<T>(__namedParameters: CacheForOptions, generator: () => T): T
  • A crude caching system that will cache values for the given time.

    Type parameters

    • T

    Parameters

    • __namedParameters: CacheForOptions
    • generator: () => T

      The function that returns the cached value.

        • (): T
        • Returns T

    Returns T

Const cacheKey

  • cacheKey<T>(key: string, generator: () => T): T
  • Cache the given value in the supplied key if the key doesn't already exist.

    Type parameters

    • T

    Parameters

    • key: string

      The key to store.

    • generator: () => T
        • (): T
        • Returns T

    Returns T

Const cacheKeyExists

  • cacheKeyExists(key: string): boolean
  • Does the supplied key exist in the cache?

    Parameters

    • key: string

      Key to check.

    Returns boolean

Const createMap

  • createMap<T>(maxX: number, maxY: number, initialValue: T, minX?: number, minY?: number): Map<T>
  • Create a co-ordinate grid

    Type parameters

    • T

    Parameters

    • maxX: number

      The maximum X value.

    • maxY: number

      The maximum Y value.

    • initialValue: T

      THe default value for all cells.

    • minX: number = 0

      (Optional) The minimum X value, defaults to 0

    • minY: number = 0

      (Optional) The minimum Y value, defaults to 0

    Returns Map<T>

Const deepIncludesArray

  • deepIncludesArray<T>(array: T[], compare: T): boolean
  • Check if an array exists inside an array

    Type parameters

    • T: any[]

    Parameters

    • array: T[]

      The arraay of arrays to check

    • compare: T

      The array to check for existance of

    Returns boolean

    true id the exact array matches, false if not.

Const defaults

  • defaults<T>(supplied: undefined | DeepPartial<T>, defaultValues: T): T
  • Deeply applies defaults to an object.

    Type parameters

    • T: {}

    Parameters

    • supplied: undefined | DeepPartial<T>

      The supplied options, a DeepPartial of defaultValues.

    • defaultValues: T

      The default values to fallback on. Should represent a full copy of the options object.

    Returns T

Const diffArray

  • diffArray<T>(original: T[], compare: T[]): { additional: T[]; missing: T[] }
  • Returns an object with 2 parameters. additional which is elements in compare that are not in original, and missing which are elements in original that are not in compare

    Type parameters

    • T

    Parameters

    • original: T[]

      The base array to compare against

    • compare: T[]

      The array to compare to

    Returns { additional: T[]; missing: T[] }

    • additional: T[]
    • missing: T[]

Const diffObject

  • diffObject<T, K>(original: T, compare: K): DiffObjectResult<T, K>
  • Type parameters

    • T: {}

    • K: {}

    Parameters

    • original: T
    • compare: K

    Returns DiffObjectResult<T, K>

Const emToNumber

  • emToNumber(unit: string): number
  • Convert em number to a plain number

    Parameters

    • unit: string

    Returns number

Const expireKey

  • expireKey(key: string): void
  • Delete the supplied key from the cache.

    Parameters

    • key: string

      Key to delete.

    Returns void

Const groupedBy

  • Takes an array of objects and returns an object with a property for each value in the key containing an array of all the objects that have that value.

    Type parameters

    • T: {}

    • K: string | number | symbol

    Parameters

    • key: K

      The key to group by.

    • array: T[]

      The array of objects.

    Returns GroupedArray<T>

Const hoursInMinutes

  • hoursInMinutes(value: number): number

Const hoursInMs

  • hoursInMs(value: number): number

Const hoursInSeconds

  • hoursInSeconds(value: number): number

Const indexedBy

  • Returns an object indexed by the given field from the array of objects.

    Type parameters

    • T: {}

    • K: string | number | symbol

    Parameters

    • key: K

      The key to index by.

    • array: T[]

      The array of Objects.

    • options: Partial<IndexedByOptions> = {}

      See IndexedByOptions

    Returns IndexedArray<T>

Const keys

  • keys<T>(object: T): keyof T[]
  • Returns an array of keys that matches the type keyof T

    Type parameters

    • T: {}

    Parameters

    • object: T

      Any object.

    Returns keyof T[]

Const mapProperty

  • mapProperty<T, K>(array: T[], property: K): T[K][]
  • Takes an array of objects and returns an array of values from the given property

    Type parameters

    • T

    • K: string | number | symbol

    Parameters

    • array: T[]

      The array of objects

    • property: K

      The property to extract

    Returns T[K][]

Const minutesAsHours

  • minutesAsHours(value: number): number

Const minutesInMs

  • minutesInMs(value: number): number

Const minutesInSeconds

  • minutesInSeconds(value: number): number

Const msAsHour

  • msAsHour(value: number): number

Const msAsMinutes

  • msAsMinutes(value: number): number

Const msAsSeconds

  • msAsSeconds(value: number): number

Const nl2br

  • nl2br(string: string): string
  • Adds
    for new lines in a string

    Parameters

    • string: string

      The multi line string to add
      tags to

    Returns string

Const numberToEm

  • numberToEm(value: number): string

Const numberToPx

  • numberToPx(value: number): string

Const numberToRem

  • numberToRem(value: number): string
  • Conver a number to rem

    Parameters

    • value: number

    Returns string

Const numberToVh

  • numberToVh(value: number): string

Const numberToVw

  • numberToVw(value: number): string

Const omit

  • omit<T, K, P>(object: T, fields: K): Omit<T, P>
  • Pragmatic version of Typescripts Omit<Object, Fields>

    Type parameters

    • T: {}

    • K: keyof T[]

    • P: string | number | symbol

    Parameters

    • object: T

      The object to omit fields from.

    • fields: K

      The fields to omit.

    Returns Omit<T, P>

    An object without the supplied fields.

Const parameterize

  • parameterize(slug: string): string
  • Takes a string and makes it safe for url slugs.

    For Example: My Article's title becomes my-articles-title

    Parameters

    • slug: string

      The string to parameterize

    Returns string

Const pick

  • pick<T, K, P>(object: T, fields: K): Pick<T, P>
  • Pragmatic version of Typescripts `Pick<Object, Fields>

    Type parameters

    • T: {}

    • K: keyof T[]

    • P: string | number | symbol

    Parameters

    • object: T

      The Object to pick fields from.

    • fields: K

      An array of fields to pick,

    Returns Pick<T, P>

    An object with the picked fields.

Const propIs

  • propIs<T, K>(items: T[], prop: K, value: T[K]): T[]
  • Find all entires in an array where prop equals value.

    Type parameters

    • T

    • K: string | number | symbol

    Parameters

    • items: T[]

      The Items to filter

    • prop: K

      The property to filter

    • value: T[K]

      The value to check for

    Returns T[]

Const propIsNot

  • propIsNot<T, K>(items: T[], prop: K, value: T[K]): T[]
  • Find all entires in an array where prop does not equals value.

    Type parameters

    • T

    • K: string | number | symbol

    Parameters

    • items: T[]

      The Items to filter

    • prop: K

      The property to filter

    • value: T[K]

      The value to check for

    Returns T[]

Const pxToNumber

  • pxToNumber(unit: string): number
  • Convert px number to a plain number

    Parameters

    • unit: string

    Returns number

Const rangeAsArray

  • rangeAsArray(range: number[]): [number, number][]
  • Returns an array of ranges

    e.g. [[1,3], [5,6], [10,15], [20,20]]

    Parameters

    • range: number[]

      An array of integers to describe

    Returns [number, number][]

Const rangeAsString

  • rangeAsString(integers: number[]): string[]
  • Returns an array of string describing the range.

    e.g. ['1-3','5,6','10-15','20']

    Parameters

    • integers: number[]

    Returns string[]

Const reduceFalsy

  • reduceFalsy<T>(array: T[], check: (entry: T, i: number) => boolean): boolean
  • Reduce the array into a single boolean value for if the check function returns false.

    once the first true is encountered check is not run again

    Type parameters

    • T

    Parameters

    • array: T[]

      The array to test

    • check: (entry: T, i: number) => boolean

      The function to check the array entries with

        • (entry: T, i: number): boolean
        • Parameters

          • entry: T
          • i: number

          Returns boolean

    Returns boolean

    true if check only ever returned false, otherwise false

Const reduceTruthy

  • reduceTruthy<T>(array: T[], check: (entry: T, i: number) => boolean): boolean
  • Reduce the array into a single boolean value for if the check function returns true.

    once the first false is encountered check is not run again

    Type parameters

    • T

    Parameters

    • array: T[]

      The array to test

    • check: (entry: T, i: number) => boolean

      The function to check the array entries with

        • (entry: T, i: number): boolean
        • Parameters

          • entry: T
          • i: number

          Returns boolean

    Returns boolean

    true if check only ever returned true, otherwise false

Const remToNumber

  • remToNumber(unit: string): number
  • Convert rem number to a plain number

    Parameters

    • unit: string

    Returns number

Const replaceProperty

  • replaceProperty<T, K, P>(object: T, property: K, replacer: (value: T[K]) => P): Omit<T, K> & {[ key in string | number | symbol]: P }
  • Replace a property on an objcet with a different type

    Type parameters

    • T: {}

    • K: string | number | symbol

    • P

    Parameters

    • object: T

      The object

    • property: K

      The property to replace

    • replacer: (value: T[K]) => P

      Function to take the value and return the new value

        • (value: T[K]): P
        • Parameters

          • value: T[K]

          Returns P

    Returns Omit<T, K> & {[ key in string | number | symbol]: P }

    An object with the property replaced

Const resetCache

  • resetCache(): void

Const secondsAsHours

  • secondsAsHours(value: number): number

Const secondsAsMinutes

  • secondsAsMinutes(value: number): number

Const secondsInMs

  • secondsInMs(value: number): number

Const times

  • times<T>(number: number, cb: (i: number) => T): T[]
  • Repeat the given function number times

    Type parameters

    • T

    Parameters

    • number: number

      The number of times to itterate

    • cb: (i: number) => T

      The function to run

        • (i: number): T
        • Parameters

          • i: number

          Returns T

    Returns T[]

Const unique

  • unique<T>(array: T[]): T[]
  • Returns an array of unique values from the given array.

    Type parameters

    • T

    Parameters

    • array: T[]

      Array of values.

    Returns T[]

Const valueOr

  • valueOr<T>(value: undefined | T, or: T): T
  • Returns the supplied value if it is not undefined otherwise it returns the or value.

    Type parameters

    • T

    Parameters

    • value: undefined | T

      The Value that could be undefined

    • or: T

      The fallback value

    Returns T

Const vhToNumber

  • vhToNumber(unit: string): number
  • Convert vh number to a plain number

    Parameters

    • unit: string

    Returns number

Const vwToNumber

  • vwToNumber(unit: string): number
  • Convert vw number to a plain number

    Parameters

    • unit: string

    Returns number

Const waitFor

  • waitFor<T>(promise: Promise<T>): Promise<WaitForResult<T>>
  • Normalises a promise that errors into an awaitable [result, error] array.

    Type parameters

    • T

    Parameters

    • promise: Promise<T>

      The resolveable promise

    Returns Promise<WaitForResult<T>>

Generated using TypeDoc