Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @kcutils/helper

Index

Type aliases

ConvertOption: { lang: DatetimeLang; type: DatetimeType }

Type declaration

  • lang: DatetimeLang
  • type: DatetimeType
DatetimeKey: "day" | "month"
Json<T>: Partial<Record<JsonKey, PossibleValue<T>>>

Type parameters

  • T = JsonValue

JsonSortableData<T>: { data: T; index: number }

Type parameters

  • T

Type declaration

  • data: T
  • index: number
MaskOption: { back: number; enabled: boolean; front: number; mask: string }

Type declaration

  • back: number
  • enabled: boolean
  • front: number
  • mask: string
NestedJson<T>: Partial<Record<JsonKey, PossibleValues<PossibleValue<T>>>>

Type parameters

  • T = JsonValue

NewDateOption: { date: number; hour: number; millisecond: number; minute: number; month: number; second: number; year: number }

Type declaration

  • date: number
  • hour: number
  • millisecond: number
  • minute: number
  • month: number
  • second: number
  • year: number
Null: undefined | null
Nullable<T>: Optional<T>

Type parameters

  • T

Optional<T>: T | undefined | null

Type parameters

  • T

PossibleValue<T>: T | T[]

Type parameters

  • T = JsonValue

PossibleValues<T>: T | Record<JsonKey, T>

Type parameters

SortableJson: {}

Type declaration

TimestampType: "second" | "millisecond"
WithNull<T>: T | null

Type parameters

  • T

WithUndefined<T>: T | undefined

Type parameters

  • T

YearType: "thai" | "global"

Functions

  • applyMixins<T>(derivedCtor: T, constructors: Constructor[]): void
  • checkEnv(...d: string[]): boolean
  • Checking ENV in env variable must equal one of input value

    Parameters

    • Rest ...d: string[]

      checking value

    Returns boolean

    true if ENV has input value

  • convertYear(year: string, type?: YearType): number
  • convert year from type to global year number

    Thai year example:

    • 54 => 2011
    • 61 => 2018
    • 2544 => 2001
    • 2565 => 2022
    • 5 => 1962
    • 612 => 2037
    • Y65 => 2022

    Global year example

    • 12 => 2012
    • 25 => 2525
    • 2044 => 2044
    • 2001 => 2001
    • 5 => 2005
    • 612 => 2612
    • Y18 => 2018

    Parameters

    • year: string

      year string

    • type: YearType = "thai"

      year type

    Returns number

  • deepMergeObject<T, U>(_jsonA?: T, _jsonB?: U, size?: number): T & U
  • deepEquals object data with specify keys

    example

    equals({a: 1}, {a: 2}, ["a"]) - check only keys 'a' equals({a: 2, b: 3}, {a: 2, b: 3}) - check all keys ('a' and 'b') equals({a: {aa: 1, ab: 2}}, {a: {aa: 2, bb: 3, cc: 1}, ["a.ab"]}) - check only 'b' key inside a object

    Type parameters

    • T: Partial<Record<string, unknown>>

    Parameters

    • o1: Optional<T>

      base object

    • o2: Optional<T>

      checking object with o1

    • keys: string[] = []

      only specify key to check, empty means check all. The keys can cover nested object by . (see on example)

    Returns boolean

    true if two object is identical

  • flatmap<T>(...arr: (T | T[])[]): T[]
  • flat input to single array with values

    Type parameters

    • T

    Parameters

    • Rest ...arr: (T | T[])[]

      input array / multiple array

    Returns T[]

    flat array without nested array

  • forceObject<T>(obj: Optional<T>, def?: {}): T
  • format(format: string, ...args: unknown[]): string
  • fromTimestamp(timestamp: string | number): undefined | Date
  • getObject<R, T>(obj: T, key: string, all?: boolean): undefined | R | T
  • Type parameters

    Parameters

    • obj: T

      input object to query

    • key: string

      object query statment (format as a.b.c)

    • all: boolean = false

      if enable this, getObject will return full object instead of undefined

    Returns undefined | R | T

  • isBoolean(t: unknown): t is boolean
  • isCI(): boolean
  • isDevelopment(): boolean
  • return true when match following condition

    1. is undefined or null
    2. is empty string
    3. is empty object ({})
    4. is empty array ([])

    Type parameters

    • T = unknown

    Parameters

    Returns t is Null

  • isNumber(t: unknown, ignoreSpecial?: boolean): t is number
  • isProduction(): boolean
  • isStaging(): boolean
  • isString(t: unknown): t is string
  • isTesting(): boolean
  • mergeObject<T>(base: Partial<T>, ...obj: Optional<Partial<T>>[]): T
  • newDate(opt?: Partial<NewDateOption>, type?: DatetimeTimezone): Date
  • padEnd(str: string, length: number, fill?: string): string
  • Add on the end if not enough or remove character if exceed

    Parameters

    • str: string

      input string

    • length: number

      string limitation

    • fill: string = " "

      character for fill if input is not long enough

    Returns string

    string with exactly length size

  • padStart(str: string, size: number, fill?: string): string
  • Add on the start if not enough or remove character if exceed

    Parameters

    • str: string

      input string

    • size: number
    • fill: string = " "

      character for fill if input is not long enough

    Returns string

    string with exactly length size

  • read(name: string, def: string): string
  • Read environment with default value if value not exist

    Parameters

    • name: string

      environment name

    • def: string

      default value if environment is not exist

    Returns string

    output

  • replace(format: string, argument: Record<string, unknown>): string
  • replace all {} with data from argument

    example

    replace("{key} {value}", {key: "hello", value: "world"}) => hello world

    Parameters

    • format: string

      format string

    • argument: Record<string, unknown>

      argument object

    Returns string

    format with object replacement

  • stringify<T>(obj: T, replacer?: Replacer, spaces?: string | number, cycleReplacer?: Replacer): string
  • same with {@link JSON.stringify()} this function copy from https://github.com/moll/json-stringify-safe

    Type parameters

    • T: Record<string, any>

    Parameters

    • obj: T

      similar object you would pass to JSON.stringify

    • Optional replacer: Replacer

      same with replacer in JSON.stringify

    • Optional spaces: string | number

      same with spaces in JSON.stringify

    • Optional cycleReplacer: Replacer

      same with replacer in JSON.stringify but for cycle object

    Returns string

    text represent input object

  • timestamp(input: string | number | Date, type?: TimestampType): number
  • toArray<T>(t: T | T[]): T[]
  • convert input to array size 1 if not, or return input if already be array type

    Type parameters

    • T

    Parameters

    • t: T | T[]

      input value

    Returns T[]

    array

  • toBoolean<T>(input: T): undefined | boolean
  • toNumber<T>(input: T, converter?: (s: string) => number): undefined | number
  • Cast input to number, This supported by list below

    1. undefined | null => return undefined
    2. boolean => return 1 if true; otherwise, return 0
    3. string => return by converter function

    Type parameters

    • T = unknown

    Parameters

    • input: T

      input data on any type

    • converter: (s: string) => number = parseFloat

      parseFloat or parseInt, applied only when input is string. (default is parseFloat())

        • (s: string): number
        • Parameters

          • s: string

          Returns number

    Returns undefined | number

  • toObjectArray(json: Partial<SortableJson>): (undefined | string)[]
  • toOptional<T>(data: undefined | null | T, extra?: Checking<T, boolean>): Optional<T>
  • toString<T>(input: T): undefined | string
  • Cast input to string. This supporting by listed below

    1. undefined | null => return undefined
    2. boolean | string => return as string
    3. number => return as fix digit not more than 6
    4. array => return as array syntax without space
    5. object => return by object.stringify()

    Type parameters

    • T = unknown

    Parameters

    • input: T

      input data on any type

    Returns undefined | string

Generated using TypeDoc