UNPKG

utilict

Version:

The JavaScript utility library for performing operations on all data types and data structures.

132 lines (131 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.areStringsAnagram = exports.sortString = exports.titleCase = exports.capitalize = exports.stringCount = exports.getWords = exports.isStringPalindrome = exports.reverseString = void 0; const utils_1 = require("../../utils/utils"); /** * Reverses the given string. It doesn't perform in place reverse but returns a new reverse string. * @param string * @returns the reversed string. */ const reverseString = (string) => { let reverse = ""; let i = string.length - 1; while (i >= 0) { reverse += string[i]; i--; } return reverse; }; exports.reverseString = reverseString; /** * Checks if the given string is a palindrome or not * @param string * @returns 'true' if string is palindrome, otherwise 'false'. */ const isStringPalindrome = (string) => { let i = 0; let j = string.length - 1; while (i <= j) { if (string[i] !== string[j]) { return false; } i++; j--; } return true; }; exports.isStringPalindrome = isStringPalindrome; /** * Returns a list of words in the given sentence. It will ignore spaces, special characters in the sentence. * @param string * @returns The list of all the words in the given sentence. */ const getWords = (string) => { if (string.length === 0) { return []; } const matches = string.match(/\b\S+\b/g); return matches !== null && matches !== void 0 ? matches : []; }; exports.getWords = getWords; /** * Returns the count of substring occurrence in the given string. This will not include the overlapped substring match. Also, the occurrence match is case-sensitive. * @param string * @param substring * @returns The occurrence count of `substring` in the `string`; */ const stringCount = (string, substring) => { const regExp = new RegExp(`${substring}`, "g"); return (string.match(regExp) || []).length; }; exports.stringCount = stringCount; /** * This makes the first letter of the string in the uppercase and rest of the string in the lowercase. * @param string * @returns The capitalized string. */ const capitalize = (string) => { if (string.length === 0) { return string; } const firstChar = string[0]; return firstChar.toUpperCase() + string.slice(1).toLowerCase(); }; exports.capitalize = capitalize; /** * This makes the first letter of each word in the string or the sentence in the uppercase. * @param string * @returns The string in the title case. */ const titleCase = (string) => { if (string.length === 0) { return ""; } return string.replace(/\b\S+\b/g, (text) => text.charAt(0).toUpperCase() + text.substring(1).toLowerCase()); }; exports.titleCase = titleCase; /** * Sort the given string in ascending and descending order * @param string * @param descending * @returns Sorted string */ const sortString = (string, descending) => { const array = [...string]; (0, utils_1.sort)(array, descending); return array.join(""); }; exports.sortString = sortString; /** * Checks if given two strings are anagrams or not. * @param string1 * @param string2 * @returns 'true' if both strings are anagrams, otherwise, 'false'. */ const areStringsAnagram = (string1, string2) => { if (string1.length !== string2.length) { return false; } const map = {}; for (const char of string1) { if (map[char]) { map[char]++; } else { map[char] = 1; } } for (const char of string2) { if (map[char] !== undefined) { map[char]--; if (map[char] < 0) { return false; } } else { return false; } } return true; }; exports.areStringsAnagram = areStringsAnagram;