UNPKG

turbocommons-ts

Version:

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

90 lines (89 loc) 3.89 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 */ /** * Common operations and tools related with numbers */ export declare class NumericUtils { /** * Defines the error message for an exception when a non-numeric value is detected. */ static readonly NON_NUMERIC_ERROR = "value is not numeric"; /** * Checks if the given value is numeric. * * @param value A value to check. * @param decimalDivider The decimal divider to use. Possible values are '.' and ','. If not provided, it will be auto-detected. * * @return true if the given value is numeric, false otherwise. */ static isNumeric(value: any, decimalDivider?: string): boolean; /** * Tells if the given value is a numeric integer or not * * @param value A value to check * * @return true if the given value is a numeric integer or represents a a numeric integer value, false otherwise */ static isInteger(value: any): boolean; /** * Strictly check that the provided value is numeric or throw an exception * * @param value A value to check * @param valueName The name of the value to be shown at the beginning of the exception message * @param errorMessage The rest of the exception message * * @throws Error If the check fails * * @return void */ static forceNumeric(value: any, valueName?: string, errorMessage?: string): void; /** * Strictly check that the provided value is a positive integer or throw an exception * * @param value A value to check * @param valueName The name of the value to be shown at the beginning of the exception message * @param errorMessage The rest of the exception message * * @throws Error If the check fails * * @return void */ static forcePositiveInteger(value: any, valueName?: string, errorMessage?: string): void; /** * Get the number represented by the given value * * @param value A value to convert to a number * @param decimalDivider The decimal divider to use. Possible values are '.' and ','. If not provided, it will be auto-detected. * * @return The numeric type representation from the given value. For example, a string '0001' will return 1 */ static getNumeric(value: any, decimalDivider?: string): number; /** * Generate a random integer between the specified range (both extremes are included). * * @param min lowest possible value (negative values are allowed) * @param max highest possible value (negative values are allowed) * * @throws Exception if max is equal or less than min. * * @return A random integer value between min and max */ static generateRandomInteger(min: number, max: number): number; /** * Format a given value to a numeric string. If the conversion is not possible, an exception will be thrown * * @param value A value to format * @param decimalDivider The decimal divider to use. possible values are '.' and ','. It will be auto detected If set to empty string * * @return The formatted numeric string. * * @throws Error If the value is not numeric or if the decimal divider is invalid. */ private static _formatNumericString; }