This documentation provides a detailed description of the functions available in the Utility Library, which is designed to facilitate common programming tasks such as data type conversions, formatting, and validation. Below you will find the method signatures, parameter descriptions, return types, and example usage for each function.
- convertToBool
- convertToNumber
- currencyToDecimal
- dateFormat
- decimalToCurrency
- emptyOrValue
- formatPhoneNumber
- makeArray
- getDynamicId
- getGoogleMapsAddress
- includes
- isEmpty
- isNumber
- instanceOf
- openGoogleMapsAddress
- toCurrency
- toDollarString
- typeOf
- validateEmail
- validatePhone
Converts various data types (string, boolean, integer, or number) to a boolean value based on predefined rules.
function convertToBool(val)
val
(String|Boolean|Int|Number
) - The value to be converted to boolean.
Boolean
- The boolean result of the conversion.
console.log(convertToBool('true')); // true
console.log(convertToBool('0')); // false
console.log(convertToBool(1)); // true
console.log(convertToBool('')); // false
Attempts to convert a given variable (string or number) to a number. If conversion is not possible, returns the original variable.
function convertToNumber(input)
input
(string|number
) - The input variable to convert.
string|number
- The converted number or the original variable, if conversion is not possible.
console.log(convertToNumber("123.45")); // 123.45 (number)
console.log(convertToNumber("abc")); // "abc" (original string)
Converts a string representing a dollar amount to a decimal number by removing any non-numeric characters.
function currencyToDecimal(amount)
amount
(String|Number
) - The dollar amount to convert.
number
- The numeric value of the dollar amount.
console.log(currencyToDecimal('$123.45')); // 123.45
Formats a date string or object into the standard US date format, optionally including time.
function dateFormat(dateTime, wTime)
dateTime
(String
) - The date-time string or object to format.wTime
(Boolean
) - If true, includes the time in the output.
string
- The formatted date.
console.log(dateFormat('2201-01-01 16:15PM', true)); // 01/01/2201 @ 4:15 PM
Formats a numeric value into a string representing a dollar amount, ensuring two decimal places.
function decimalToCurrency(amount)
amount
(String|Number
) - The numeric amount to format.
string
- The formatted dollar amount.
console.log(decimalToCurrency(2123.46)); // 2,123.46
Checks if a value is "empty" (e.g., an empty string, array, object, or null). Returns the value if not empty, or a default value otherwise.
function emptyOrValue(value, _default)
value
(String|Number
) - The value to check._default
(String|Number
) - The default value to return if the original value is considered empty. Default isnull
.
mixed
- Either the original value if not empty, or the default value.
console.log(emptyOrValue('', '
default')); // 'default'
console.log(emptyOrValue('test')); // 'test'
Formats a given phone number string according to a specified template.
function formatPhoneNumber(phoneNumber, template)
phoneNumber
(string
) - The phone number to format.template
(string
) - The template to use for formatting, where0
represents a digit placeholder.
string
- The formatted phone number.
console.log(formatPhoneNumber('1234567890', '(000) 000-0000')); // (123) 456-7890
Ensures that the provided item is an array. If the item is not an array, it is wrapped into an array.
const makeArray = (item) => (Array.isArray(item) ? item : [item]);
item
(String|Array
) - The item to ensure is an array.
array
- The item as an array.
console.log(makeArray('test')); // ['test']
Generates a unique identifier based on the current timestamp and a random number.
function getDynamicId()
- None
string
- The generated unique identifier.
console.log(getDynamicId()); // kn__1615961863463__105
Generates a Google Maps URL for a given address, which can be a string or an object containing address components.
function getGoogleMapsAddress(address)
address
(String|Object
) - The address information.
string
- The Google Maps URL.
console.log(getGoogleMapsAddress('New York')); // 'https://maps.google.it/maps?q=New+York'
Checks if a value is present in a collection (array, string, or object).
function includes(collection, value, fromIndex)
collection
(collection
) - The collection to search within.value
(value
) - The value to search for.fromIndex
(fromIndex
) - The index to start searching from. Default is 0.
boolean
- True if the value is found in the collection, false otherwise.
console.log(includes([1, 2, 3], 2)); // true
Determines if the provided value is empty. Different types are checked differently, e.g., strings and arrays must be empty, objects must have no keys, etc.
function isEmpty(value)
value
(string|array|object|map|set|number|boolean
) - The value to check.
boolean
- True if the value is considered empty, false otherwise.
console.log(isEmpty([])); // true
console.log(isEmpty({})); // true
Checks if a value is a number or can be converted to one. If not, returns null.
function isNumber(value)
value
(String|Number
) - The value to check.
null|int
- The numeric value if the input is a number or can be converted to one, otherwise null.
console.log(isNumber("123")); // 123
console.log(isNumber("abc")); // null
Determines if the provided input is an instance of a specified type or matches a provided type description.
function instanceOf(input, test)
input
(any
) - The variable to check.test
(string|instance
) - The type or instance to compare against.
string|boolean
- The type of the variable if no test is provided, or a boolean indicating if the input matches the test.
console.log(instanceOf(new Date(), 'date')); // true
console.log(instanceOf(123, 'number')); // true
Opens a Google Maps page for a given address using the system's default web browser.
function
openGoogleMapsAddress(object)
object
(String|Object
) - The address information, either as a plain string or as an object.
void
- Opens a web page.
openGoogleMapsAddress('New York'); // Opens Google Maps for 'New York'
Alias for decimalToCurrency
. Formats a numeric value into a string representing a dollar amount.
function toCurrency(amount)
amount
(String|Number
) - The amount to format.
number
- The formatted dollar amount.
console.log(toCurrency(123.45)); // 123.45
Converts large numeric values into a shorter string representation using "K" for thousands and "M" for millions.
function toDollarString(amount)
amount
(String|Number
) - The amount to convert.
number
- The converted string representation.
console.log(toDollarString(2500000)); // 2.5M
Determines the type of a given input and optionally checks if it matches a specified type.
function typeOf(input, test)
input
(any
) - The variable to check.test
(string
) - An optional type to check against.
string|boolean
- The type of the variable, or a boolean indicating if the input matches the specified type.
console.log(typeOf('hello')); // 'string'
console.log(typeOf('hello', 'string')); // true
Validates an email address based on a specific regular expression pattern.
function validateEmail(email)
email
(String
) - The email address to validate.
Boolean
- True if the email is valid, false otherwise.
console.log(validateEmail('test@test.com')); // true
console.log(validateEmail('test@test')); // false
Validates a phone number based on a specific format.
function validatePhone(phone)
phone
(String
) - The phone number to validate.
void|Toast
- Returns a toast message indicating validation success or failure.
console.log(validatePhone('1234567890')); // true
console.log(validatePhone('123 456 7890')); // false
This comprehensive documentation should assist developers in effectively utilizing the Utility Library to enhance their projects with reliable, commonly needed functionalities.