Skip to main content

String Utilities

A comprehensive collection of string manipulation, validation, and formatting utilities used throughout the WorkPayCore frontend application.

Regular Expressions

EMAIL_REG_EXP

export const EMAIL_REG_EXP =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

Regular expression pattern for validating email addresses.

WEB_URL_REG_EXP

export const WEB_URL_REG_EXP =
/^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$/;

Regular expression pattern for validating web URLs.

FLOATING_POINT_NUMBER_REG_EXP

export const FLOATING_POINT_NUMBER_REG_EXP = /^[0-9]\d*(\.\d+)?$/;

Regular expression pattern for validating floating point numbers.


Validation Functions

isEmail(val)

Validates if a string is a valid email address.

Parameters:

  • val (string): The email string to validate

Returns:

  • string | undefined: Returns "Email is invalid" if invalid, undefined if valid

Example:

const result = isEmail('test@example.com');
// Returns: undefined (valid)

const result = isEmail('invalid-email');
// Returns: "Email is invalid"

isPassword(val)

Validates if a string meets basic password requirements.

Parameters:

  • val (string): The password string to validate

Returns:

  • string | undefined: Returns "Password is invalid" if invalid, undefined if valid

Example:

const result = isPassword('MyPassword123!');
// Returns: undefined (valid)

isPasswordValid(val)

Provides detailed password validation with specific criteria checking.

Parameters:

  • val (string): The password string to validate

Returns:

  • object: Object with boolean properties for each validation criteria:
    • nine: Has at least 9 characters
    • uppercase: Contains uppercase letter
    • lowercase: Contains lowercase letter
    • symbol: Contains special symbol
    • number: Contains number

Example:

const result = isPasswordValid('MyPass123!');
// Returns: { nine: true, uppercase: true, lowercase: true, symbol: true, number: true }

const result = isPasswordValid('weak');
// Returns: { nine: false, uppercase: false, lowercase: true, symbol: false, number: false }

validateFullname(value)

Validates if a string is a valid full name (at least two words).

Parameters:

  • value (string): The full name to validate

Returns:

  • string | undefined: Returns "Please enter your Fullname" if invalid, undefined if valid

Example:

const result = validateFullname('John Doe');
// Returns: undefined (valid)

const result = validateFullname('John');
// Returns: "Please enter your Fullname"

Formatting Functions

numberFormat(num)

Formats a number with thousand separators and two decimal places.

Parameters:

  • num (number): The number to format

Returns:

  • string: Formatted number string or "-" if null

Example:

const result = numberFormat(1234.5);
// Returns: "1,234.00"

const result = numberFormat(null);
// Returns: "-"

toUppercase(word)

Converts a string to uppercase.

Parameters:

  • word (string): The string to convert

Returns:

  • string: Uppercase string

Example:

const result = toUppercase('hello world');
// Returns: "HELLO WORLD"

toLowercase(word)

Converts a string to lowercase and replaces underscores/hyphens with spaces.

Parameters:

  • word (string): The string to convert

Returns:

  • string: Formatted lowercase string

Example:

const result = toLowercase('HELLO_WORLD');
// Returns: "hello world"

const result = toLowercase('HELLO-WORLD');
// Returns: "hello world"

strictCurrencyFormatter(num, decimals?)

Formats a number as currency with specified decimal places.

Parameters:

  • num (number | string): The number to format
  • decimals (number, optional): Number of decimal places (default: 2)

Returns:

  • string: Formatted currency string

Example:

const result = strictCurrencyFormatter(1234.567);
// Returns: "1,234.57"

const result = strictCurrencyFormatter(1234.567, 3);
// Returns: "1,234.567"

mobileNumberFormatter(num, country)

Formats a mobile number in international format.

Parameters:

  • num (string): The phone number to format
  • country (string): Country code (default: 'KE')

Returns:

  • string: Formatted international phone number

Example:

const result = mobileNumberFormatter('0712345678', 'KE');
// Returns: "+254 712 345 678"

String Manipulation Functions

hyphenateString(str)

Converts a string to hyphenated format (replaces spaces with hyphens).

Parameters:

  • str (string): The string to hyphenate

Returns:

  • string: Hyphenated string

Example:

const result = hyphenateString('hello world');
// Returns: "hello-world"

removeUnderscoreFromString(str)

Removes underscores from a string and replaces them with spaces.

Parameters:

  • str (string): The string to process

Returns:

  • string: String with underscores replaced by spaces

Example:

const result = removeUnderscoreFromString('hello_world');
// Returns: "hello world"

formatLabel(str)

Formats a string label by capitalizing words and handling special cases.

Parameters:

  • str (string): The string to format

Returns:

  • string: Formatted label string

Example:

const result = formatLabel('user_name');
// Returns: "User Name"

Utility Functions

isNumeric(value)

Checks if a value is numeric.

Parameters:

  • value (any): The value to check

Returns:

  • boolean: True if numeric, false otherwise

Example:

const result = isNumeric('123');
// Returns: true

const result = isNumeric('abc');
// Returns: false

findWordInString(word, str)

Finds if a word exists in a string using word boundaries.

Parameters:

  • word (string): The word to find
  • str (string): The string to search in

Returns:

  • boolean: True if word is found, false otherwise

Example:

const result = findWordInString('hello', 'hello world');
// Returns: true

const result = findWordInString('hello', 'helloworld');
// Returns: false

toFixedNumber(int, places)

Formats a number to a fixed number of decimal places.

Parameters:

  • int (number): The number to format
  • places (number): Number of decimal places (default: 0)

Returns:

  • string: Formatted number string

Example:

const result = toFixedNumber(3.14159, 2);
// Returns: "3.14"

convertYESNO(param)

Converts a boolean or number to "Yes" or "No" string.

Parameters:

  • param (boolean | number): The value to convert

Returns:

  • string: "Yes", "No", or empty string

Example:

const result = convertYESNO(true);
// Returns: "Yes"

const result = convertYESNO(false);
// Returns: "No"

convertToBoolean(param)

Converts various types to boolean.

Parameters:

  • param (string | number | boolean): The value to convert

Returns:

  • boolean: Converted boolean value

Example:

const result = convertToBoolean('1');
// Returns: true

const result = convertToBoolean('0');
// Returns: false

Date Utility Functions

dateToYyyMmDdFormat(date)

Converts a Date object to YYYY-MM-DD format string.

Parameters:

  • date (Date): The date to format

Returns:

  • string: Date in YYYY-MM-DD format

Example:

const result = dateToYyyMmDdFormat(new Date('2023-12-25'));
// Returns: "2023-12-25"

getMonthFromString(mon)

Converts a month string/number to month name.

Parameters:

  • mon (string | number): Month identifier

Returns:

  • string: Month name

Example:

const result = getMonthFromString('01');
// Returns: "January"

Input Validation Functions

onNumberKeyDownValidation(e)

Prevents invalid characters in number inputs.

Parameters:

  • e (KeyboardEvent): The keyboard event

Returns:

  • void: Prevents default behavior for invalid keys

Example:

<input onKeyDown={onNumberKeyDownValidation} />

onDecimalKeyDownValidation(e)

Prevents invalid characters in decimal number inputs.

Parameters:

  • e (KeyboardEvent): The keyboard event

Returns:

  • void: Prevents default behavior for invalid keys

onAlphanumericValidation(e)

Validates alphanumeric input.

Parameters:

  • e (KeyboardEvent): The keyboard event

Returns:

  • boolean: True if valid, prevents default if invalid

Serialization Functions

wpSerialize(data)

Serializes data for WorkPay-specific storage.

Parameters:

  • data (any): The data to serialize

Returns:

  • string: Serialized data string

wpDeserialize(data)

Deserializes WorkPay-specific data.

Parameters:

  • data (string): The serialized data string

Returns:

  • any: Deserialized data