A collection of essential 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 collapses repeated spaces to a single space.
- 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"
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 (including Unicode letters).
- 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 pageexpected(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 package directly. For example:
import {generateRandomArrayIndex, trimAndRemoveSpaces} from 'essential-common-utils';
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.
Releases are published to npm by CI, not from a developer machine. Nothing needs to be tagged or bumped by hand.
- Land changes on
mainusing Conventional Commits.fix:produces a patch,feat:a minor, and a!orBREAKING CHANGE:footer a major.chore:andci:do not release. - release-please keeps a release pull request open with the version bump and the
CHANGELOG.mdentry. It updates itself as more commits land. - Merge that pull request. release-please tags the version, creates the GitHub release, and the same workflow run publishes to npm via trusted publishing (OIDC) with provenance. No npm token is stored anywhere.
Trusted publishing must be configured once on npmjs.com: package settings, Publishing access, add a GitHub Actions trusted publisher for this repository with workflow release.yml.
Renovate raises dependency and GitHub Actions updates every Monday. Patch and minor updates to devDependencies and to Actions are approved and merged automatically once CI passes. Runtime dependencies and every major update wait for a human review.
This package is licensed under the MIT License.