@zohodesk/utils
Version:
Unified Component Library - Utils
290 lines (257 loc) • 8.29 kB
JavaScript
/**
* check given string is empty.
* @param {string} string - The string to be check.
*/
export function isEmpty(string) {
if (!string) {
return true;
}
return string.length === 0;
}
/**
* check given string is not empty.
* @param {string} string - Th e string to be check.
*/
export function isNotEmpty(string) {
return !isEmpty(string);
}
/**
* check given string is blank.
* @param {string} string - The string to be check.
*/
export function isBlank(string) {
if (!string) {
return true;
}
return !new RegExp('\\S', 'g').test(string);
}
/**
* check given string is not blank.
* @param {string} string - The string to be check.
*/
export function isNotBlank(string) {
return !isBlank(string);
}
/**
* find index of given char in the string.
* @param {string} string - The string to be modify.
*/
export function indexOf(string, chars, pos) {
if (string == null || typeof string === undefined) {
return -1;
}
if (pos) {
return str.indexOf(chars, typeof pos === undefined ? 0 : pos);
} else {
return str.indexOf(chars);
}
}
/**
* Capitalize the first letter of a string.
*
* @param {string} string - The input string to modify.
* @returns {string} The input string with the first letter capitalized.
*/
export function capitalizeFirstLetter(string) {
return !isStringEmpty(string) ? string.charAt(0).toUpperCase() + string.slice(1) : string;
}
/**
* Encode a string for HTML to prevent cross-site scripting (XSS) attacks.
*
* @param {string} str - The input string to encode for HTML.
* @returns {string} The encoded string safe for inclusion in HTML content.
*/
export function encodeForHtml(str) {
if (/<|>|&|"|'|\\/g.test(str) === true) {
str = str.replace(/&/g, '&');
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
str = str.replace(/"/g, '"');
str = str.replace(/\\/g, '\');
str = str.replace(/'/g, ''');
}
return str;
}
/**
* Decode HTML entities in a string.
*
* @param {string} str - The input string containing HTML-encoded content.
* @returns {string} The decoded string.
*/
export function decodeContent(str) {
if (/&|<|>|"|\|'|&lt;|&gt;/g.test(str) == true) {
str = str.replace(/&/g, '&');
str = str.replace(/<|&lt;/g, '<');
str = str.replace(/>|&gt;/g, '>');
str = str.replace(/"/g, '"');
str = str.replace(/\/g, '\\');
str = str.replace(/'/g, "'");
}
return str;
}
/**
* Validatate email.
*
* @param {string} email - The input string to validate.
*/
export function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
/**
* extract domain from the email.
*
* @param {string} email - The input email to extract domain.
*/
export function extractDomainFromEmail(email) {
const atIndex = email.lastIndexOf('@');
return atIndex !== -1 ? email.slice(atIndex + 1) : '';
}
/**
* Reverse a given string.
*
* @param {string} string - The input string to reverse.
* @returns {string} The reversed string.
*/
export function reverseString(string) {
return !isStringEmpty(string) ? string.split('').reverse().join('') : string;
}
/**
* Truncate a given string and append an ellipsis if it exceeds a specified length.
* @param {string} string - The string to truncate.
* @param {number} maxLength - The maximum length of the truncated string.
* @returns {string} The truncated string.
*/
export function truncate(string) {
let maxLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
if (!isStringEmpty() && string.length > maxLength) {
return string.slice(0, maxLength) + '...';
}
return string;
}
/**
* Convert a string to PascalCase.
* Split the string into words, capitalize the first letter of each word, and join them
*
* @param {string} string - The input string to convert.
* @returns {string} The PascalCase version of the input string.
*/
export function toPascalCase(string) {
if (typeof string === 'string' && !isStringEmpty(string)) {
const pascalCaseStr = string.split(/[\s_-]+/).map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join('');
return pascalCaseStr;
}
}
/**
* Convert a string to kebab-case.
* Replace spaces and underscores with hyphens, and convert to lowercase
*
* @param {string} string - The input string to convert.
* @returns {string} The kebab-case version of the input string.
*/
export function toKebabCase(string) {
if (typeof string === 'string' && !isStringEmpty(string)) {
const kebabCaseStr = string.replace(/[\s_]+/g, '-').toLowerCase();
return kebabCaseStr;
}
}
/**
* Convert a string to snake_case.
* Replace spaces and hyphens with underscores, and convert to lowercase
*
* @param {string} string - The input string to convert.
* @returns {string} The snake_case version of the input string.
*/
export function toSnakeCase(string) {
if (typeof string === 'string' && !isStringEmpty(string)) {
const snakeCaseStr = string.replace(/[\s-]+/g, '_').toLowerCase();
return snakeCaseStr;
}
}
/**
* Join an array of strings into a single string using a specified delimiter.
*
* @param {string[]} array - The array of strings to join.
* @param {string} delimiter - The delimiter used to separate the strings in the output.
* @returns {string} The concatenated string with the specified delimiter.
*/
export function joinArray(array) {
let delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ', ';
if (Array.isArray(array)) {
return array.join(delimiter);
}
}
/**
* Remove whitespace characters from a given string.
*
* @param {string} inputString - The input string from which to remove whitespace.
* @returns {string} The input string with whitespace characters removed.
*/
export function removeWhitespace(inputString) {
if (!isStringEmpty(inputString) && typeof inputString == 'string') {
return inputString.replace(/\s+/g, '');
}
return inputString;
}
/**
* Check if a given string is a palindrome.
*
* @param {string} string - The input string to check for palindromic property.
* @returns {boolean} True if the input string is a palindrome, false otherwise.
*/
export function isPalindrome(string) {
if (!isStringEmpty(string) && typeof string == 'string') {
const cleanString = string.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
const reversedString = cleanString.split('').reverse().join('');
return cleanString === reversedString;
}
return string;
}
/**
* Check if a string starts with a vowel (a, e, i, o, or u).
*
* @param {string} string - The input string to check.
* @returns {boolean} True if the string starts with a vowel, false otherwise.
*/
export function isStartsWithVowel(string) {
return /^[aeiouAEIOU]/.test(string);
}
;
/**
* Count the number of occurrences of a substring in a string.
*
* @param {string} string - The input string to search in.
* @param {string} substring - The substring to count occurrences of.
* @returns {number} The number of occurrences of the substring in the input string.
*/
export function countOccurrences(string, substring) {
if (typeof string == 'string') {
const regex = new RegExp(substring, 'g');
const matches = string.match(regex);
return matches ? matches.length : 0;
}
return string;
}
/**
* Replace all occurrences of a substring with another substring in a string.
*
* @param {string} string - The input string in which to perform replacements.
* @param {string} target - The substring to find and replace.
* @param {string} replacement - The substring to replace occurrences of the target with.
* @returns {string} The input string with all occurrences of the target replaced.
*/
export function replaceAllOccurrences(string, target, replacement) {
if (!isStringEmpty(string) && typeof string == 'string') {
return string.split(target).join(replacement);
}
return string;
}
/**
* Encode a string to Base64.
*
* @param {string} inputString - The input string to encode.
* @returns {string} The Base64-encoded string.
*/
export function encodeToBase64(string) {
return btoa(string);
}