Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @marcj/estdlib

estdlib.ts

Build Status npm version

Installation

npm install @marcj/estdlib

Usage example

import {each} from "@marcj/estdlib";

for (const item of each({a: 1, b: 2, c: 3})) {
    console.log(item)
}

Index

Variables

Const cacheEnumKeys

cacheEnumKeys: Map<Object, string[]> = new Map<Object, string[]>()

Const cacheEnumLabels

cacheEnumLabels: Map<Object, string[]> = new Map<Object, string[]>()

Functions

appendObject

  • appendObject(origin: object, extend: object, prependKeyName?: string): void
  • Parameters

    • origin: object
      • [k: string]: any
    • extend: object
      • [k: string]: any
    • Default value prependKeyName: string = ""

    Returns void

applyDefaults

  • applyDefaults<T>(classType: ClassType<T>, target: object): T
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [k: string]: any

    Returns T

arrayClear

  • arrayClear<T>(array: T[]): number
  • Clears the array so its empty. Returns the amount of removed items.

    Type parameters

    • T

    Parameters

    • array: T[]

    Returns number

arrayHasItem

  • arrayHasItem<T>(array: T[], item: T): boolean
  • Type parameters

    • T

    Parameters

    • array: T[]
    • item: T

    Returns boolean

arrayRemoveItem

  • arrayRemoveItem<T>(array: T[], item: T): boolean
  • Removes on particular item by reference of an array.

    Type parameters

    • T

    Parameters

    • array: T[]
    • item: T

    Returns boolean

average

  • average(array: number[]): number
  • Returns the average of a number array.

    Parameters

    • array: number[]

    Returns number

copy

  • copy<T>(v: T[]): T[]
  • Creates a shallow copy of given array.

    Type parameters

    • T

    Parameters

    • v: T[]

    Returns T[]

createStack

  • createStack(removeCallee?: boolean): string
  • beta

    Parameters

    • Default value removeCallee: boolean = true

    Returns string

each

  • each<T>(object: object | ArrayLike<T>): IterableIterator<T>
  • Iterator for each value of an array or object.

    example
    for (const v of each(['a', 'b']) {
       console.log(v); //a, b
    }
    category

    iterator

    Type parameters

    • T

    Parameters

    • object: object | ArrayLike<T>

    Returns IterableIterator<T>

eachKey

  • eachKey<T>(object: ArrayLike<T>): IterableIterator<number>
  • eachKey<T, K>(object: T): IterableIterator<string>
  • Iterator for each key of an array or object.

    example
    for (const i of eachKey(['a', 'b']) {
       console.log(i); //0, 1
    }
    category

    iterator

    Type parameters

    • T

    Parameters

    • object: ArrayLike<T>

    Returns IterableIterator<number>

  • Type parameters

    • T: object

    • K: keyof T

    Parameters

    • object: T

    Returns IterableIterator<string>

eachPair

  • eachPair<T>(object: ArrayLike<T>): IterableIterator<[number, T]>
  • eachPair<T>(object: object): IterableIterator<[string, T]>
  • Iterator for key value pair of an array or object.

    example
    for (const [i, v] of eachPair(['a', 'b']) {
       console.log(i, v); //0 a, 1 b
    }
    
    for (const [i, v] of eachPair({'foo': 'bar}) {
       console.log(i, v); //foo bar
    }
    category

    iterator

    Type parameters

    • T

    Parameters

    • object: ArrayLike<T>

    Returns IterableIterator<[number, T]>

  • Type parameters

    • T

    Parameters

    • object: object
      • [s: string]: T

    Returns IterableIterator<[string, T]>

empty

  • empty<T>(array: T[] | object): boolean
  • Checks whether given array or object is empty (no keys)

    Type parameters

    • T

    Parameters

    • array: T[] | object

    Returns boolean

first

  • first<T>(v: object | T[]): T | undefined
  • Returns the first value of given array or object.

    Type parameters

    • T

    Parameters

    • v: object | T[]

    Returns T | undefined

firstKey

  • firstKey(v: object | object): string | undefined
  • Returns the first key of a given object.

    Parameters

    • v: object | object

    Returns string | undefined

getClassName

  • getClassName<T>(classTypeOrInstance: ClassType<T> | Object): string
  • Returns the class name either of the class definition or of the class of an instance.

    Note when code is minimized/uglified this output will change. You should disable in your compile the className modification.

    example
    class User {}
    
    expect(getClassName(User)).toBe('User');
    expect(getClassName(new User())).toBe('User');

    Type parameters

    • T

    Parameters

    Returns string

getClassPropertyName

  • getClassPropertyName<T>(classType: ClassType<T> | Object, propertyName: string): string
  • Same as getClassName but appends the propertyName.

    Type parameters

    • T

    Parameters

    • classType: ClassType<T> | Object
    • propertyName: string

    Returns string

getEnumLabel

  • getEnumLabel(enumType: object, id: any): undefined | string
  • Returns the enum label for a given enum value.

    Parameters

    • enumType: object
      • [field: string]: any
    • id: any

    Returns undefined | string

getEnumLabels

  • getEnumLabels(enumDefinition: any): string[]
  • Returns all possible enum labels.

    Parameters

    • enumDefinition: any

    Returns string[]

getEnumValues

  • getEnumValues(enumDefinition: any): any[]
  • Returns all possible enum keys.

    Parameters

    • enumDefinition: any

    Returns any[]

getPathValue

  • getPathValue(bag: object, parameterPath: string, defaultValue?: any): any
  • Parameters

    • bag: object
      • [field: string]: any
    • parameterPath: string
    • Optional defaultValue: any

    Returns any

getValidEnumValue

  • getValidEnumValue(enumDefinition: any, value: any, allowLabelsAsValue?: boolean): any
  • Parameters

    • enumDefinition: any
    • value: any
    • Default value allowLabelsAsValue: boolean = false

    Returns any

humanBytes

  • humanBytes(bytes: number, si?: boolean): string
  • Returns the human readable byte representation.

    Parameters

    • bytes: number
    • Default value si: boolean = false

    Returns string

indexOf

  • indexOf<T>(array: T[], item: T): number
  • Type parameters

    • T

    Parameters

    • array: T[]
    • item: T

    Returns number

initializeProperty

  • initializeProperty(target: any, name: string): object
  • Parameters

    • target: any
    • name: string

    Returns object

    • [k: string]: any

isArray

  • isArray(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

isFunction

  • isFunction(obj: any): boolean
  • Returns true if given obj is a function.

    Parameters

    • obj: any

    Returns boolean

isNull

  • isNull(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

isNumber

  • isNumber(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

isObject

  • isObject(obj: any): boolean
  • Returns true for real objects: object literals ({}) or class instances (new MyClass).

    Parameters

    • obj: any

    Returns boolean

isPlainObject

  • isPlainObject(obj: any): boolean
  • Returns true if the given obj is a plain object, and no class instance.

    isPlainObject({}) === true isPlainObject(new ClassXY) === false

    Parameters

    • obj: any

    Returns boolean

isSet

  • isSet(obj: any): boolean
  • Checks if obj is not null and not undefined.

    Parameters

    • obj: any

    Returns boolean

isString

  • isString(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

isUndefined

  • isUndefined(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

isValidEnumValue

  • isValidEnumValue(enumDefinition: any, value: any, allowLabelsAsValue?: boolean): boolean
  • Checks whether given enum value is valid.

    Parameters

    • enumDefinition: any
    • value: any
    • Default value allowLabelsAsValue: boolean = false

    Returns boolean

last

  • last<T>(v: object | T[]): T | undefined
  • Returns the last value of given array or object.

    Type parameters

    • T

    Parameters

    • v: object | T[]

    Returns T | undefined

lastKey

  • lastKey(v: object | object): string | undefined
  • Returns the last key of a given object.

    Parameters

    • v: object | object

    Returns string | undefined

log

  • log(): (Anonymous function)
  • Logs every call to this method on stdout.

    Returns (Anonymous function)

mergePromiseStack

  • mergePromiseStack<T>(promise: Promise<T>, stack?: undefined | string): Promise<T>
  • Type parameters

    • T

    Parameters

    • promise: Promise<T>
    • Optional stack: undefined | string

    Returns Promise<T>

mergeStack

  • mergeStack(error: Error, stack: string): void
  • beta

    Parameters

    • error: Error
    • stack: string

    Returns void

prependObjectKeys

  • prependObjectKeys(o: object, prependText: string): object
  • Parameters

    • o: object
      • [k: string]: any
    • prependText: string

    Returns object

    • [k: string]: any

setPathValue

  • setPathValue(bag: object, parameterPath: string, value: any): void
  • Parameters

    • bag: object
    • parameterPath: string
    • value: any

    Returns void

singleStack

  • singleStack(): (Anonymous function)
  • Makes sure that this async method is only running once at a time. When this method is running and it is tried to call it another times, that call is dropped and returns simply the result of the previous running call.

    Returns (Anonymous function)

size

  • size<T>(array: T[] | object): number
  • Returns the size of given array or object.

    Type parameters

    • T

    Parameters

    • array: T[] | object

    Returns number

sleep

  • sleep(seconds: number): Promise<void>
  • Parameters

    • seconds: number

    Returns Promise<void>

stack

  • stack(): (Anonymous function)
  • Makes sure that calls to this async method are stacked up and are called one after another and not parallel.

    Returns (Anonymous function)

time

  • time(): number
  • Returns the current time as seconds.

    Returns number

typeOf

  • typeOf(obj: any): string
  • Tries to identify the object by normalised result of Object.toString(obj).

    Parameters

    • obj: any

    Returns string

Generated using TypeDoc