Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DataHelper

BaseDataHelper provides concrete implementation for DataHelper.

Do not use BaseDataHelper. Use DataHelper instead.

Hierarchy

Index

Constructors

Properties

initialization: Promise<void>

A Promise which resolves when this Component has been successfully initialized.

Methods

  • init(): Promise<void>
  • Initializes the object. This method is invoked at the end of the constructor after the object is initialized with the given configuration.

    Returns Promise<void>

  • arrayFill(startIndex: any, num: any, mixedVal: any): {}
  • arrayMultiSort(arr: any[]): boolean
  • findValuesByPrefix(object: any, prefix: any): {}
  • getColumn(array: any[], name: string | number): {}
  • Returns the values of a specified column in an array. The input array should be multidimensional or an array of objects.

    For example,

    let array = [
    {id: '123', data: 'abc'},
    {id: '345', data: 'def'},
    ]
    let result = DataHelper.getColumn(array, 'id');
    // the result is: ['123', '345']

    // using callback
    result = DataHelper.getColumn(array, (element) => {
    return element['id'];
    });

    Parameters

    • array: any[]
    • name: string | number

    Returns {}

    the list of column values

    • [key: string]: any
  • getValue(object: any, key: any, defaultValue?: any): any
  • Retrieves the value of an array element or object property with the given key or property name. If the key does not exist in the array, the default value will be returned instead. Not used when getting value from an object.

    The key may be specified in a dot format to retrieve the value of a sub-array or the property of an embedded object. In particular, if the key is x.y.z, then the returned value would be array['x']['y']['z'] or array.x.y.z (if array is an object). If array['x'] or array.x is neither an array nor an object, the default value will be returned. Note that if the array already has an element x.y.z, then its value will be returned instead of going through the sub-arrays. So it is better to be done specifying an array of key names like ['x', 'y', 'z'].

    Below are some usage examples,

    // working with array
    let username = DataHelper.getValue(POST, 'username');
    // working with object
    username = DataHelper.getValue(user, 'username');
    // working with callback function
    let fullName = DataHelper.getValue(user, (user, defaultValue) => {
    return user.firstName . ' ' . user.lastName;
    });
    // using dot format to retrieve the property of embedded object
    let street = DataHelper.getValue(users, 'address.street');
    // using an array of keys to retrieve the value
    let value = DataHelper.getValue(versions, ['1.0', 'date']);

    Parameters

    • object: any

      array or object to extract value from

    • key: any

      key name of the array element, an array of keys or property name of the object, or a callback function returning the value. The callback function signature should be: function(array, defaultValue).

    • defaultValue: any = null

    Returns any

    the value of the element if found, default value otherwise

  • multiSort(array: any[], key: string | number | (string | number)[], direction?: string | {}, sortFlag?: string | {}): void
  • Sorts an array of objects or arrays (with the same structure) by one or several keys.

    throws

    InvalidArgumentException if the direction or sortFlag parameters do not have correct number of elements as that of key.

    Parameters

    • array: any[]

      the array to be sorted. The array will be modified after calling this method.

    • key: string | number | (string | number)[]

      the key(s) to be sorted by. This refers to a key name of the sub-array elements, a property name of the objects, or a callback function returning the values for comparison purpose. The callback function signature should be: function(item). To sort by multiple keys, provide an array of keys here.

    • direction: string | {} = 'SORT_ASC'

      the sorting direction. It can be either SORT_ASC or 'SORT_DESC'. When sorting by multiple keys with different sorting directions, use an array of sorting directions.

    • sortFlag: string | {} = 'SORT_REGULAR'

      the JS sort flag. Valid values include 'SORT_REGULAR', 'SORT_NUMERIC', 'SORT_STRING', 'SORT_LOCALE_STRING', 'SORT_NATURAL' and 'SORT_FLAG_CASE'. for more details. When sorting by multiple keys with different sort flags, use an array of sort flags.

    Returns void

  • parseQueryParams(params: {}): {}
  • Parameters

    • params: {}
      • [key: string]: string | string[] | undefined

    Returns {}

    • [key: string]: any
  • parseUrl(queryString: string): {}
  • range(low: any, high: any, step?: number): any[]
  • remove(object: {}, key: string, defaultValue?: any): any
  • Removes an item from an array and returns the value. If the key does not exist in the array, the default value will be returned instead.

    Usage examples,

    // let array = {type: 'A', options: [1, 2]};
    // working with array
    let type = DataHelper.remove(array, 'type');
    // array content
    // array = {options: [1, 2]];

    Parameters

    • object: {}
      • [key: string]: any
    • key: string

      key name of the array element

    • defaultValue: any = null

    Returns any

    the value of the element if found, default value otherwise

  • replaceAsync(string: string, regex: any, asyncFn: CallableFunction): Promise<string>
  • resolveDotStructuredObject(object: any, attribute: any): any
  • serializeLinks<T>(links: T): T
  • strcmp(str1: any, str2: any): 0 | 1 | -1
  • substrCompare(mainStr: any, str: any, offset: any, length: any, caseInsensitivity?: boolean): false | 0 | 1 | -1
  • Parameters

    • mainStr: any
    • str: any
    • offset: any
    • length: any
    • caseInsensitivity: boolean = false

    Returns false | 0 | 1 | -1

Generated using TypeDoc