Skip to content

Latest commit

 

History

History
449 lines (344 loc) · 10.8 KB

utils.md

File metadata and controls

449 lines (344 loc) · 10.8 KB

Utility Library Documentation

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.

Table of Contents

convertToBool

Description

Converts various data types (string, boolean, integer, or number) to a boolean value based on predefined rules.

Syntax

function convertToBool(val)

Parameters

  • val (String|Boolean|Int|Number) - The value to be converted to boolean.

Return Type

  • Boolean - The boolean result of the conversion.

Example

console.log(convertToBool('true'));  // true
console.log(convertToBool('0'));     // false
console.log(convertToBool(1));       // true
console.log(convertToBool(''));      // false

convertToNumber

Description

Attempts to convert a given variable (string or number) to a number. If conversion is not possible, returns the original variable.

Syntax

function convertToNumber(input)

Parameters

  • input (string|number) - The input variable to convert.

Return Type

  • string|number - The converted number or the original variable, if conversion is not possible.

Example

console.log(convertToNumber("123.45"));  // 123.45 (number)
console.log(convertToNumber("abc"));     // "abc" (original string)

currencyToDecimal

Description

Converts a string representing a dollar amount to a decimal number by removing any non-numeric characters.

Syntax

function currencyToDecimal(amount)

Parameters

  • amount (String|Number) - The dollar amount to convert.

Return Type

  • number - The numeric value of the dollar amount.

Example

console.log(currencyToDecimal('$123.45'));  // 123.45

dateFormat

Description

Formats a date string or object into the standard US date format, optionally including time.

Syntax

function dateFormat(dateTime, wTime)

Parameters

  • dateTime (String) - The date-time string or object to format.
  • wTime (Boolean) - If true, includes the time in the output.

Return Type

  • string - The formatted date.

Example

console.log(dateFormat('2201-01-01 16:15PM', true));  // 01/01/2201 @ 4:15 PM

decimalToCurrency

Description

Formats a numeric value into a string representing a dollar amount, ensuring two decimal places.

Syntax

function decimalToCurrency(amount)

Parameters

  • amount (String|Number) - The numeric amount to format.

Return Type

  • string - The formatted dollar amount.

Example

console.log(decimalToCurrency(2123.46));  // 2,123.46

emptyOrValue

Description

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.

Syntax

function emptyOrValue(value, _default)

Parameters

  • value (String|Number) - The value to check.
  • _default (String|Number) - The default value to return if the original value is considered empty. Default is null.

Return Type

  • mixed - Either the original value if not empty, or the default value.

Example

console.log(emptyOrValue('', '

default'));  // 'default'
console.log(emptyOrValue('test'));         // 'test'

formatPhoneNumber

Description

Formats a given phone number string according to a specified template.

Syntax

function formatPhoneNumber(phoneNumber, template)

Parameters

  • phoneNumber (string) - The phone number to format.
  • template (string) - The template to use for formatting, where 0 represents a digit placeholder.

Return Type

  • string - The formatted phone number.

Example

console.log(formatPhoneNumber('1234567890', '(000) 000-0000'));  // (123) 456-7890

makeArray

Description

Ensures that the provided item is an array. If the item is not an array, it is wrapped into an array.

Syntax

const makeArray = (item) => (Array.isArray(item) ? item : [item]);

Parameters

  • item (String|Array) - The item to ensure is an array.

Return Type

  • array - The item as an array.

Example

console.log(makeArray('test'));  // ['test']

getDynamicId

Description

Generates a unique identifier based on the current timestamp and a random number.

Syntax

function getDynamicId()

Parameters

  • None

Return Type

  • string - The generated unique identifier.

Example

console.log(getDynamicId());  // kn__1615961863463__105

getGoogleMapsAddress

Description

Generates a Google Maps URL for a given address, which can be a string or an object containing address components.

Syntax

function getGoogleMapsAddress(address)

Parameters

  • address (String|Object) - The address information.

Return Type

  • string - The Google Maps URL.

Example

console.log(getGoogleMapsAddress('New York'));  // 'https://maps.google.it/maps?q=New+York'

includes

Description

Checks if a value is present in a collection (array, string, or object).

Syntax

function includes(collection, value, fromIndex)

Parameters

  • 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.

Return Type

  • boolean - True if the value is found in the collection, false otherwise.

Example

console.log(includes([1, 2, 3], 2));  // true

isEmpty

Description

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.

Syntax

function isEmpty(value)

Parameters

  • value (string|array|object|map|set|number|boolean) - The value to check.

Return Type

  • boolean - True if the value is considered empty, false otherwise.

Example

console.log(isEmpty([]));  // true
console.log(isEmpty({}));  // true

isNumber

Description

Checks if a value is a number or can be converted to one. If not, returns null.

Syntax

function isNumber(value)

Parameters

  • value (String|Number) - The value to check.

Return Type

  • null|int - The numeric value if the input is a number or can be converted to one, otherwise null.

Example

console.log(isNumber("123"));  // 123
console.log(isNumber("abc"));  // null

instanceOf

Description

Determines if the provided input is an instance of a specified type or matches a provided type description.

Syntax

function instanceOf(input, test)

Parameters

  • input (any) - The variable to check.
  • test (string|instance) - The type or instance to compare against.

Return Type

  • string|boolean - The type of the variable if no test is provided, or a boolean indicating if the input matches the test.

Example

console.log(instanceOf(new Date(), 'date'));  // true
console.log(instanceOf(123, 'number'));       // true

openGoogleMapsAddress

Description

Opens a Google Maps page for a given address using the system's default web browser.

Syntax

function

 openGoogleMapsAddress(object)

Parameters

  • object (String|Object) - The address information, either as a plain string or as an object.

Return Type

  • void - Opens a web page.

Example

openGoogleMapsAddress('New York');  // Opens Google Maps for 'New York'

toCurrency

Description

Alias for decimalToCurrency. Formats a numeric value into a string representing a dollar amount.

Syntax

function toCurrency(amount)

Parameters

  • amount (String|Number) - The amount to format.

Return Type

  • number - The formatted dollar amount.

Example

console.log(toCurrency(123.45));  // 123.45

toDollarString

Description

Converts large numeric values into a shorter string representation using "K" for thousands and "M" for millions.

Syntax

function toDollarString(amount)

Parameters

  • amount (String|Number) - The amount to convert.

Return Type

  • number - The converted string representation.

Example

console.log(toDollarString(2500000));  // 2.5M

typeOf

Description

Determines the type of a given input and optionally checks if it matches a specified type.

Syntax

function typeOf(input, test)

Parameters

  • input (any) - The variable to check.
  • test (string) - An optional type to check against.

Return Type

  • string|boolean - The type of the variable, or a boolean indicating if the input matches the specified type.

Example

console.log(typeOf('hello'));       // 'string'
console.log(typeOf('hello', 'string'));  // true

validateEmail

Description

Validates an email address based on a specific regular expression pattern.

Syntax

function validateEmail(email)

Parameters

  • email (String) - The email address to validate.

Return Type

  • Boolean - True if the email is valid, false otherwise.

Example

console.log(validateEmail('test@test.com'));  // true
console.log(validateEmail('test@test'));      // false

validatePhone

Description

Validates a phone number based on a specific format.

Syntax

function validatePhone(phone)

Parameters

  • phone (String) - The phone number to validate.

Return Type

  • void|Toast - Returns a toast message indicating validation success or failure.

Example

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.