1.4.0 • Published 6 days ago

@iol-inc/fe-utils v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

Constants

Functions

FILE_SIZE_100MB : number

Represents a file size of 100MB in bytes.

Kind: global constant

FILE_SIZE_5MB : number

Represents a file size of 5MB in bytes.

Kind: global constant

FILE_SIZE_3MB : number

Represents a file size of 3MB in bytes.

Kind: global constant

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.

ParamTypeDescription
arrayArray.<ArrayOfObjects>The array of objects to create the index map from.
idKeystringThe 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.

ParamTypeDescription
indexMapRecord.<any, any>The index map where the keys are object IDs and the values are their indices in the original array.
idstringThe 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.

ParamTypeDescription
arr1Array.<any>The first array to compare.
arr2Array.<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

ParamTypeDescription
arrayArray.<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

ParamTypeDescription
arrayArray.<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.

ParamTypeDescription
fileSizeArray.<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.

ParamTypeDescription
fileSizeArray.<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.

ParamTypeDescription
fileSizeArray.<any>file size of the file for checking

calcHeight1610(width) ⇒

Calculates the height of a 16:10 rectangle from the width.

Kind: global function
Returns: The height of the rectangle.

ParamDescription
widthThe width of the rectangle.

roundUpTwoDecimals(value) ⇒ number

Rounds up a number to two decimal places.

Kind: global function
Returns: number - The number rounded up to two decimal places.

ParamTypeDescription
valuenumberThe number to be rounded up.

decode_id_token(params) ⇒ Object

Decodes an ID token using the jwt-decode library and extracts detailed user information and roles.

Kind: global function
Returns: Object - An object containing detailed user information including names, email, roles, and status flags indicating the user's specific roles.

ParamTypeDescription
paramsObjectThe parameters for decoding.
params.IdTokenstringThe JWT ID token to decode.
params.ROLE_IDObjectAn object containing role identifiers for comparison.

Properties

NameTypeDescription
tenantIDstringThe tenant ID associated with the user.
branchIDstringThe branch ID associated with the user.
suffixstringA suffix associated with the user, if any.
namestringThe full name of the user.
fnamestringThe user's first name.
lnamestringThe user's last name.
emailstringThe user's email address.
email_verifiedbooleanIndicates whether the user's email address is verified.
phone_numberstringThe user's phone number.
phone_number_verifiedbooleanIndicates whether the user's phone number is verified.
usernamestringThe username associated with the user.
rolesArray.<string>An array of roles assigned to the user.
isUserFreelancerbooleanIndicates if the user is identified as a freelancer.
isUserHSAdminbooleanIndicates if the user is an HS (Health and Safety) admin.
isUserFacilitatorbooleanIndicates if the user is a facilitator.
isUserBusinessbooleanIndicates if the user is a business admin.
isUserCashierbooleanIndicates if the user is a cashier.
isUserOwnerbooleanIndicates if the user is an owner of the business.
isUserManagerbooleanIndicates if the user is a manager.

Example

const tokenDetails = decode_id_token({
  IdToken: 'yourTokenHere',
  ROLE_ID: {
    freelancer: 'freelancerRoleID',
    hs_admin: 'hsAdminRoleID',
    facilitator: 'facilitatorRoleID',
    business_admin: 'businessAdminRoleID',
    cashier: 'cashierRoleID',
    owner: 'ownerRoleID',
    manager: 'managerRoleID'
  }
});
console.log(tokenDetails);

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.

ParamTypeDescription
paramsObjectThe parameters for decoding.
params.IdTokenstringThe ID token to decode.
params.ROLE_IDObjectAn object containing role identifiers.

Properties

NameTypeDescription
namestringThe user's name.
fnamestringThe user's first name.
lnamestringThe user's last name.
emailstringThe user's email address.
email_verifiedbooleanIndicates whether the user's email is verified.
usernamestringThe user's username.
rolesArray.<string>The roles assigned to the user.
isUserFreelancerbooleanIndicates if the user is a freelancer.
isUserHSAdminbooleanIndicates if the user is an HS admin.
isUserFacilitatorbooleanIndicates if the user is a facilitator.
isUserBusinessbooleanIndicates if the user is a business admin.
isUserCashierbooleanIndicates if the user is a a cashier.
isUserOwnerbooleanIndicates if the user is a owner.
isUserManagerbooleanIndicates if the user is a manager.

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.

ParamTypeDescription
textstringThe 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.

ParamTypeDescription
paramsObjectThe parameters for capitalization.
params.stringstringThe string to be capitalized.
params.characterstringThe 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.

ParamDescription
textThe original text.
wordLimitThe 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.

ParamTypeDefaultDescription
textstring"\"\""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.

ParamTypeDescription
string_paramstringThe string from which spaces need to be removed and then converted to lowercase.

Example

const modifiedString = stringRemoveSpaceLowercase("Hello World");
console.log(modifiedString); // "helloworld"
1.4.0

6 days ago

1.3.1

2 months ago

1.3.0

2 months ago

1.2.2

3 months ago

1.2.1

3 months ago

1.2.0

7 months ago

1.1.0

7 months ago

1.0.1

7 months ago

1.0.0

8 months ago