1.8.3 • Published 4 months ago

essential-common-utils v1.8.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Essential common utilities

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.

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 removes double spaces.

  • 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('Hello­World');
    // 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 world"

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.

  • 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

Usage

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 ');

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.


License

This package is licensed under the MIT License.

1.8.2

4 months ago

1.8.1

4 months ago

1.8.0

4 months ago

1.8.3

4 months ago

1.7.0

5 months ago

1.6.0

7 months ago

1.5.0

7 months ago

1.4.5

7 months ago

1.4.4

7 months ago

1.4.3

7 months ago

1.4.2

8 months ago

1.4.1

9 months ago

1.4.0

10 months ago

1.3.1

10 months ago

1.3.0

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.9

11 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago