iol-front-end-utils
v1.3.0-beta
Published
IOL Inc. / Systems Dev / Front-End utility functions
Downloads
28
Readme
Constants
Functions
calcHeight1610 ⇒
Calculates the height of a 16:10 rectangle from the width.
Kind: global constant
Returns: The height of the rectangle.
| Param | Description | | --- | --- | | width | The width of the rectangle. |
formatCurrencyPHP : Intl.NumberFormat
A utility for formatting numbers as currency in Philippine Peso (PHP) using the Filipino locale.
Kind: global constant
Note: This utility uses the "fil-PH" locale and is set to display numbers in decimal style with a minimum of 2 fraction digits.
Example
const amount = 1234.56;
console.log(formatCurrencyPHP.format(amount)); // "1,234.56"
createIndexMap(array, idKey) ⇒ Record.<any, any>
Creates an index map for an array of objects based on a specified key.
Kind: global function
Returns: Record.<any, any> - An index map where the keys are the values from the idKey of the objects and the values are their indices in the array.
| Param | Type | Description | | --- | --- | --- | | array | Array.<ArrayOfObjects> | The array of objects to create the index map from. | | idKey | string | The key in the objects to use for indexing. |
Example
const arr = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
const indexMap = createIndexMap(arr, 'id');
console.log(indexMap); // {1: 0, 2: 1}
findIndexById(indexMap, id) ⇒ number
Retrieves the index of an object in an array using its ID from a pre-constructed index map.
Kind: global function
Returns: number - The index of the object in the original array. Returns undefined
if the ID is not found in the index map.
| Param | Type | Description | | --- | --- | --- | | indexMap | Record.<any, any> | The index map where the keys are object IDs and the values are their indices in the original array. | | id | string | The ID of the object whose index needs to be retrieved. |
Example
const indexMap = {1: 0, 2: 1};
const index = findIndexById(indexMap, '1');
console.log(index); // 0
IS_ARRAY_EQUAL(arr1, arr2) ⇒ boolean
Checks if two arrays are equal by comparing their elements.
Kind: global function
Returns: boolean - Returns true
if the arrays are equal, otherwise false
.
Note: This function uses xorWith
and isEqual
for comparison and isEmpty
to check the result. Ensure these utilities are imported and available in the scope.
| Param | Type | Description | | --- | --- | --- | | arr1 | Array.<any> | The first array to compare. | | arr2 | Array.<any> | The second array to compare. |
Example
const array1 = [1, 2, 3];
const array2 = [1, 2, 3];
const areEqual = IS_ARRAY_EQUAL(array1, array2);
console.log(areEqual); // true
IS_VALID_ARRAY(array) ⇒ boolean
Checks if a value is a an array and if it contains an element.
Kind: global function
Returns: boolean - Returns true
if the value provided is an array and if it contains an element
| Param | Type | Description | | --- | --- | --- | | array | Array.<any> | value to check if array or not. |
IS_VALUE_ARRAY(array) ⇒ boolean
Checks if a value is an array or not.
Kind: global function
Returns: boolean - Returns true
if the value provided is an array
| Param | Type | Description | | --- | --- | --- | | array | Array.<any> | value to check if array or not. |
IS_FILE_LARGER_100MB(fileSize) ⇒ boolean
Checks if file size is greater than 100MB
Kind: global function
Returns: boolean - Returns true
if the value is greater than 100MB.
| Param | Type | Description | | --- | --- | --- | | fileSize | Array.<any> | file size of the file for checking |
IS_FILE_LARGER_5MB(fileSize) ⇒ boolean
Checks if file size is greater than 5MB
Kind: global function
Returns: boolean - Returns true
if the value is greater than 5MB.
| Param | Type | Description | | --- | --- | --- | | fileSize | Array.<any> | file size of the file for checking |
IS_FILE_LARGER_3MB(fileSize) ⇒ boolean
Checks if file size is greater than 3MB
Kind: global function
Returns: boolean - Returns true
if the value is greater than 3MB.
| Param | Type | Description | | --- | --- | --- | | fileSize | Array.<any> | file size of the file for checking |
hs_decodeIdToken(params) ⇒ Object
HEY SUCCESS Decodes an ID token and extracts user information and roles.
Kind: global function
Returns: Object - An object containing user details and roles.
Note: This function uses the jwt-decode
library to decode the ID token.
| Param | Type | Description | | --- | --- | --- | | params | Object | The parameters for decoding. | | params.IdToken | string | The ID token to decode. | | params.ROLE_ID | Object | An object containing role identifiers. |
Properties
| Name | Type | Description | | --- | --- | --- | | name | string | The user's name. | | fname | string | The user's first name. | | lname | string | The user's last name. | | email | string | The user's email address. | | email_verified | boolean | Indicates whether the user's email is verified. | | username | string | The user's username. | | roles | Array.<string> | The roles assigned to the user. | | isUserFreelancer | boolean | Indicates if the user is a freelancer. | | isUserHSAdmin | boolean | Indicates if the user is an HS admin. | | isUserFacilitator | boolean | Indicates if the user is a facilitator. | | isUserBusiness | boolean | Indicates if the user is a business admin. |
Example
const tokenDetails = hs_decodeIdToken({ IdToken: 'yourTokenHere', ROLE_ID: { freelancer: 'freelancerRoleID', hs_admin: 'hsAdminRoleID', facilitator: 'facilitatorRoleID', business_admin: 'businessAdminRoleID' } });
console.log(tokenDetails);
convertToBase64(text) ⇒ string
Converts a given text to a Base64 data URI for PNG/JPEG images.
Kind: global function
Returns: string - A Base64 data URI formatted for PNG/JPEG images.
Note: This function assumes the provided text is a valid Base64 encoded PNG or JPEG image.
| Param | Type | Description | | --- | --- | --- | | text | string | The text to be converted to a Base64 data URI. |
Example
const base64Data = convertToBase64('yourBase64EncodedImageHere');
console.log(base64Data); // "data:image/png/jpeg;base64, yourBase64EncodedImageHere"
customCapitalize(params) ⇒ string
Capitalizes the first letter of a string or each substring separated by a specified character.
Kind: global function
Returns: string - The capitalized string or capitalized substrings joined by the specified character.
Note: This function assumes the capitalize
function is available in the scope to capitalize individual strings.
| Param | Type | Description | | --- | --- | --- | | params | Object | The parameters for capitalization. | | params.string | string | The string to be capitalized. | | params.character | string | The character used to split the string. |
Example
const capitalizedString = customCapitalize({ string: 'hello-world', character: '-' });
console.log(capitalizedString); // "Hello-World"
limitWords(text, wordLimit) ⇒
Limits the given text to a specified number of words.
Kind: global function
Returns: The limited text.
| Param | Description | | --- | --- | | text | The original text. | | wordLimit | The maximum number of words. |
getWordLength([text]) ⇒ number
Calculates the number of words in a given text.
Kind: global function
Returns: number - The number of words in the text.
Note: This function splits the text based on whitespace to determine word count.
| Param | Type | Default | Description | | --- | --- | --- | --- | | [text] | string | """" | The text whose word count needs to be determined. Defaults to an empty string. |
Example
const wordCount = getWordLength("Hello, how are you?");
console.log(wordCount); // 4
stringRemoveSpaceLowercase(string_param) ⇒ string
Removes all spaces from a string and converts it to lowercase.
Kind: global function
Returns: string - The modified string without spaces and in lowercase.
Note: This function uses regular expressions to remove spaces from the string.
| Param | Type | Description | | --- | --- | --- | | string_param | string | The string from which spaces need to be removed and then converted to lowercase. |
Example
const modifiedString = stringRemoveSpaceLowercase("Hello World");
console.log(modifiedString); // "helloworld"