A collection of essesntial utility functions for various common operations in JavaScript and TypeScript projects. The essential-common-utils
package provides a set of reusable functions for string manipulation, number formatting, URL extraction, and more.
Generates a random index for an array.
-
Parameters:
arrayLength
(optional, default:5
) - Returns: A random index.
-
Example:
const randomIndex = generateRandomArrayIndex(10);
Trims a string and removes all spaces.
-
Parameters:
input
(string) - Returns: A string without spaces.
-
Example:
const result = trimAndRemoveSpaces(' Hello World '); // Output: "HelloWorld"
Extracts URLs from a given text.
-
Parameters:
text
(string) - Returns: An array of URLs found in the text.
-
Example:
const urls = extractUrlsFromText('Check out https://example.com'); // Output: ["https://example.com"]
Trims a string and removes double spaces.
-
Parameters:
input
(string) - Returns: A string without double spaces.
-
Example:
const result = trimAndRemoveDoubleSpaces('Hello World'); // Output: "Hello World"
Trims a string and removes a specific Unicode character.
-
Parameters:
input
(string) - Returns: A string without the Unicode character.
-
Example:
const result = trimAndRemoveUnicodeCharacter('HelloWorld'); // Output: "HelloWorld"
Normalizes whitespace in a string.
-
Parameters:
input
(string) - Returns: A string with normalized whitespace.
-
Example:
const result = normalizeWhitespace("Hello World"); // Output: "Hello World"
Splits a text by a delimiter and returns a specific part.
-
Parameters:
-
text
(string): Text to split. -
delimiter
(string): Delimiter for splitting. -
index
(number): Index of the desired part.
-
- Returns: The specified part of the split text.
-
Example:
const part = splitTextAndGetPart('apple,orange,banana', ',', 1); // Output: "orange"
Normalizes whitespace and removes soft hyphen characters.
-
Parameters:
text
(string) - Returns: A string with normalized whitespace and no soft hyphens.
-
Example:
const result = normalizeWhitespacesAndRemoveSoftHyphen('Hello World'); // Output: "Hello World"
Capitalizes the first word and makes the rest lowercase.
-
Parameters:
text
(string) - Returns: A string with the first word capitalized.
-
Example:
const result = capitalizeFirstWord('hello world'); // Output: "Hello world"
Converts a number to a European format string.
-
Parameters:
value
(number) - Returns: A string in the European format.
-
Example:
const formatted = convertToEuropeFormat(123456.789); // Output: "123.456,79"
Converts a string to camelCase.
-
Parameters:
text
(string) - Returns: A camelCase string.
-
Example:
const result = getCamelCaseText('Hello World'); // Output: "helloWorld"
Converts a string to PascalCase.
-
Parameters:
text
(string) - Returns: A PascalCase string.
-
Example:
const result = getPascalCaseText('hello world'); // Output: "HelloWorld"
Calculates the execution time in seconds.
-
Parameters:
startTime
(number) - Returns: The execution time in seconds (string).
-
Example:
const start = Date.now(); // some process const time = getExecutionTime(start); // Output: "0.12"
Extracts text content, removing numbers and currency symbols.
-
Parameters:
input
(string) - Returns: A string without numbers and currency symbols.
-
Example:
const result = extractTextOnly('Price: $123.45'); // Output: "Price"
Trims a number to two decimal places.
-
Parameters:
num
(number) - Returns: A number with two decimal places.
-
Example:
const trimmed = trimToTwoDecimalPlaces(123.456); // Output: 123.46
Generates multiple unique indexes for an array.
-
Parameters:
-
arrayLength
(number) -
count
(number)
-
- Returns: An array of unique indexes.
-
Example:
const indexes = getMultipleUniqueIndexes(10, 5); // Output: [1, 3, 5, 7, 9]
Returns a random string from the provided list.
-
Parameters:
items
(string[]): An array of items to choose from. - Returns: A random item from the array.
-
Example:
const randomQuote = getRandomString(['apple', 'banana', 'cherry']); // Output: "banana" (or any other item from the array)
Parses a price string with locale formatting, handling different formats like European and default parsing.
-
Parameters:
priceText
(string) - Returns: A number representing the parsed price.
-
Example:
const price = parsePricesWithLocaleFormatting('$1,234.56'); // Output: 1234.56
A collection of text helper functions for handling HTML entities and text normalization.
Decodes HTML entities using a simple replacement approach.
-
Parameters:
text
(string) - The text that might contain HTML entities - Returns: The decoded text
-
Example:
const decoded = textHelper.decodeHtmlEntities('<div>Hello World</div>'); // Output: "<div>Hello World</div>"
Normalizes text by decoding HTML entities and standardizing special characters.
-
Parameters:
text
(string) - The text to normalize - Returns: The normalized text
-
Example:
const normalized = textHelper.normalizeText('"Smart quotes" and 'apostrophes''); // Output: '"Smart quotes" and \'apostrophes\''
Compares two strings after normalization.
-
Parameters:
-
actual
(string) - The actual text from the page -
expected
(string) - The expected text from test data
-
- Returns: boolean indicating if texts match after normalization
-
Example:
const matches = textHelper.compareTexts('"Hello"', '"Hello"'); // Output: true
To use these utilities, import them from the Common.ts
file in your project. For example:
import {generateRandomArrayIndex, trimAndRemoveSpaces} from './src/Common';
const index = generateRandomArrayIndex(10);
const text = trimAndRemoveSpaces(' Hello World ');
Contributions are welcome! If you'd like to report a bug, suggest a feature, or contribute to the codebase, feel free to open an issue or submit a pull request.
This package is licensed under the MIT License.