UNPKG

quarkle

Version:

quarkle is the JavaScript util library providing support of all data types and data structures.

99 lines (98 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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;