UNPKG

turbocommons-ts

Version:

General purpose library that implements frequently used and generic software development tasks

178 lines (177 loc) 6.7 kB
/** * TurboCommons is a general purpose and cross-language library that implements frequently used and generic software development tasks. * * Website : -> https://turboframework.org/en/libs/turbocommons * License : -> Licensed under the Apache License, Version 2.0. You may not use this file except in compliance with the License. * License Url : -> http://www.apache.org/licenses/LICENSE-2.0 * CopyRight : -> Copyright 2015 Edertone Advanded Solutions (08211 Castellar del Vallès, Barcelona). http://www.edertone.com */ /** * HashMapObject abstraction */ export declare class HashMapObject { /** * Sort mode that compares values as strings (alphabetically) */ static readonly SORT_METHOD_STRING = "SORT_METHOD_STRING"; /** * Sort mode that compares values as numbers (Avoid using it with non numeric values) */ static readonly SORT_METHOD_NUMERIC = "SORT_METHOD_NUMERIC"; /** * Defines that elements will be sorted upward */ static readonly SORT_ORDER_ASCENDING = "SORT_ORDER_ASCENDING"; /** * Defines that elements will be sorted downward */ static readonly SORT_ORDER_DESCENDING = "SORT_ORDER_DESCENDING"; /** * Javascript objects specification does not guarantee object keys order. So we must keep * a sepparate array with the currently sorted hashmap keys to be sure that sorting is guaranteed */ protected _keys: string[]; /** * Structure that contains the HashMapObject data * Note that javascript objects do not guarantee key order, so we must also keep a * sepparate array with the sorted list of keys */ protected _data: { [key: string]: any; }; /** * Stores the number of elements inside the HashMapObject */ protected _length: number; /** * An Object that defines a sorted collection of key/value pairs and all their related operations. * * @param data A value that will be used to initialize the HashMapObject. It can be an object instance * (where each key/value will be directly assigned to the HashMap), or a plain array in which case the keys will be * created from each element numeric index */ constructor(data?: any); /** * Define a key / value pair and add it to the collection. * If the key already exists, value will be replaced. * * @param key A string that labels the provided value * @param value A value to be stored with the provided key * * @return The value after being stored to the collection */ set(key: string, value: any): any; /** * Get the number of key/value pairs that are currently stored on this HashMapObject instance * * @return integer The number of items inside the collection */ length(): number; /** * Get the value that is associated to a key from an existing key/value pair * * @param key The key we are looking for * * @throws error If key does not exist or is invalid * @return The value that is associated to the provided key */ get(key: string): any; /** * Get the value that is located at a certain position at the ordered list of key/pair values * * @param index The position we are looking for * * @throws Error If index does not exist or is invalid * @return The value that is located at the specified position */ getAt(index: number): any; /** * Get a list with all the keys from the HashMapObject with the same order as they are stored. * * @return List of strings containing all the HashMapObject sorted keys. */ getKeys(): string[]; /** * Get a list with all the values from the HashMapObject with the same order as they are stored. * * @return List of elements containing all the HashMapObject sorted values */ getValues(): any[]; /** * Tells if the provided value matches a key that's stored inside the HashMapObject * * @param A value to find on the currently stored keys. * * @return True if the provided value is a valid HashMap key, false in any other case */ isKey(key: any): boolean; /** * Delete a key/value pair from the HashMapObject, given it's key. * * @param key The key for the key/value pair we want to delete * * @throws Error * @return The value from the key/value pair that's been deleted. */ remove(key: any): any; /** * Change the name for an existing key * * @param key The name we want to change * @param newKey The new name that will replace the previous one * * @throws Error * @return True if rename was successful */ rename(key: any, newKey: any): boolean; /** * Exchange the positions for two key/value pairs on the HashMapObject sorted elements list * * @param key1 The first key to exchange * @param key2 The second key to exchange * * @return True if the two key/value pairs positions were correctly exchanged * @throws Error If any of the two provided keys does not exist or is invalid */ swap(key1: string, key2: string): boolean; /** * Sort the key/value pairs inside the HashMapObject by their key values. * * @param method Defines sort mode: HashMapObject.SORT_STRING or HashMapObject.SORT_NUMERIC * @param order Defines the order for the sorted elements: HashMapObject.SORT_ORDER_ASCENDING (default) or HashMapObject.SORT_ORDER_DESCENDING * * @throws Error * @return True if sort was successful false on failure */ sortByKey(method?: string, order?: string): boolean; /** * Remove and get the first element value from the HashMapObject sorted list * * @throws Error If the HashMapObject is empty * @return The value on the first element of the list */ shift(): any; /** * Remove and get the last element value from the HashMapObject sorted list * * @throws Error If the HashMapObject is empty * @return The value on the last element of the list */ pop(): any; /** * Reverse the order of the HashMapObject elements * * @return void */ reverse(): boolean; /** * Checks that specified key value has a valid format (Non empty string) * * @param key The key value to test * * @throws Error * * @return void */ private _validateKeyFormat; }