essential-common-utils
v1.14.2
Published
A collection of essential utilities for various tasks
Readme
Essential common utilities
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.
Utility Functions
1. generateRandomArrayIndex
Generates a random index for an array.
- Parameters:
arrayLength(optional, default:5) - Returns: A random index.
- Example:
const randomIndex = generateRandomArrayIndex(10);
2. trimAndRemoveSpaces
Trims a string and removes all spaces.
- Parameters:
input(string) - Returns: A string without spaces.
- Example:
const result = trimAndRemoveSpaces(' Hello World '); // Output: "HelloWorld"
3. extractUrlsFromText
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"]
4. trimAndRemoveDoubleSpaces
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"
5. trimAndRemoveUnicodeCharacter
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"
6. normalizeWhitespace
Normalizes whitespace in a string.
- Parameters:
input(string) - Returns: A string with normalized whitespace.
- Example:
const result = normalizeWhitespace("Hello World"); // Output: "Hello World"
7. splitTextAndGetPart
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"
8. normalizeWhitespacesAndRemoveSoftHyphen
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"
9. capitalizeFirstWord
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"
10. convertToEuropeFormat
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"
11. getCamelCaseText
Converts a string to camelCase.
- Parameters:
text(string) - Returns: A camelCase string.
- Example:
const result = getCamelCaseText('Hello World'); // Output: "helloWorld"
12. getPascalCaseText
Converts a string to PascalCase (including Unicode letters).
- Parameters:
text(string) - Returns: A PascalCase string.
- Example:
const result = getPascalCaseText('hello world'); // Output: "HelloWorld"
13. getExecutionTime
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"
14. extractTextOnly
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"
15. trimToTwoDecimalPlaces
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
16. getMultipleUniqueIndexes
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]
17. getRandomString
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)
18. parsePricesWithLocaleFormatting
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
19. textHelper
A collection of text helper functions for handling HTML entities and text normalization.
decodeHtmlEntities
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>"
normalizeText
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\''
compareTexts
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
Usage
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 ');Contributing
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.
Releasing
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.
Dependency updates
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.
License
This package is licensed under the MIT License.
