JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q37964F
  • License MIT

A lightweight and powerful collection of string utility functions for Node.js - trimming, casing, formatting, and more.

Package Exports

  • @js-utility/string

Readme

String Utils JS

A comprehensive collection of utility functions for string manipulation, including trimming, casing, formatting, validation, transformation, HTML escaping, random string generation, and more. These utilities are designed to simplify common string operations and are used across the System Designer Core module.

Features

  • Trimming & Whitespace: Trim, remove, or replace whitespace.
  • Casing: Convert to upper, lower, camel, kebab, snake, and title case.
  • Formatting: Truncate, pad, repeat, join, and replace substrings.
  • Validation: Check for empty strings, prefixes, and suffixes.
  • Transformation: Reverse, capitalize, remove duplicates, and split strings.
  • HTML Utilities: Escape and unescape HTML characters, strip HTML tags.
  • Random String Generation: Generate random strings with customizable character sets.
  • Slugify: Convert strings into URL-friendly slugs.
  • Pluralization: Pluralize and singularize English nouns.

Installation

npm install @js-utility/string

API & Use Cases

Trimming & Whitespace

trim(str)

Removes whitespace from both ends.

import { trim } from '@js-utility/string';
trim('   Hello World!   '); // 'Hello World!'

removeWhitespace(str)

Removes all whitespace.

import { removeWhitespace } from '@js-utility/string';
removeWhitespace('  a b  c d '); // 'abcd'

replaceMultipleSpaces(str)

Replaces multiple spaces with a single space.

import { replaceMultipleSpaces } from '@js-utility/string';
replaceMultipleSpaces('a   b    c'); // 'a b c'

Casing

upper(str)

Converts to uppercase.

import { upper } from '@js-utility/string';
upper('hello'); // 'HELLO'

lower(str)

Converts to lowercase.

import { lower } from '@js-utility/string';
lower('HELLO'); // 'hello'

capitalize(str)

Capitalizes the first letter.

import { capitalize } from '@js-utility/string';
capitalize('hello world'); // 'Hello world'

capitalizeEachWord(str)

Capitalizes the first letter of each word.

import { capitalizeEachWord } from '@js-utility/string';
capitalizeEachWord('hello world'); // 'Hello World'

camelCase(str)

Converts to camelCase.

import { camelCase } from '@js-utility/string';
camelCase('hello world example'); // 'helloWorldExample'

kebabCase(str)

Converts to kebab-case.

import { kebabCase } from '@js-utility/string';
kebabCase('Hello World Example'); // 'hello-world-example'

snakeCase(str)

Converts to snake_case.

import { snakeCase } from '@js-utility/string';
snakeCase('Hello World Example'); // 'hello_world_example'

titleCase(str)

Converts to Title Case.

import { titleCase } from '@js-utility/string';
titleCase('hello world example'); // 'Hello World Example'

Formatting

truncate(str, maxLength)

Truncates a string and adds ellipsis if needed.

import { truncate } from '@js-utility/string';
truncate('Hello World', 5); // 'He...'

padLeft(str, targetLength, padChar?)

Pads string on the left.

import { padLeft } from '@js-utility/string';
padLeft('42', 5, '0'); // '00042'

padRight(str, targetLength, padChar?)

Pads string on the right.

import { padRight } from '@js-utility/string';
padRight('42', 5, '0'); // '42000'

padBoth(str, targetLength, padChar?)

Pads string on both sides.

import { padBoth } from '@js-utility/string';
padBoth('42', 6, '*'); // '**42**'

repeat(str, count)

Repeats a string.

import { repeat } from '@js-utility/string';
repeat('ab', 3); // 'ababab'

replaceAll(str, search, replacement)

Replaces all occurrences of a substring.

import { replaceAll } from '@js-utility/string';
replaceAll('foo bar foo', 'foo', 'baz'); // 'baz bar baz'

joinStrings(separator, ...parts)

Joins multiple strings with a separator.

import { joinStrings } from '@js-utility/string';
joinStrings('-', 'a', 'b', 'c'); // 'a-b-c'

Validation

isEmptyStr(str)

Checks if a string is empty or whitespace.

import { isEmptyStr } from '@js-utility/string';
isEmptyStr('   '); // true

startsWith(str, prefix)

Checks if string starts with prefix.

import { startsWith } from '@js-utility/string';
startsWith('Hello', 'He'); // true

endsWith(str, suffix)

Checks if string ends with suffix.

import { endsWith } from '@js-utility/string';
endsWith('Hello', 'lo'); // true

Transformation

reverse(str)

Reverses a string.

import { reverse } from '@js-utility/string';
reverse('abc'); // 'cba'

split(str, delimiter)

Splits a string by delimiter.

import { split } from '@js-utility/string';
split('a,b,c', ','); // ['a', 'b', 'c']

charAt(str, index)

Gets character at index.

import { charAt } from '@js-utility/string';
charAt('hello', 1); // 'e'

toCharArray(str)

Converts string to array of characters.

import { toCharArray } from '@js-utility/string';
toCharArray('abc'); // ['a', 'b', 'c']

removeDuplicateChars(str)

Removes duplicate characters.

import { removeDuplicateChars } from '@js-utility/string';
removeDuplicateChars('aabbcc'); // 'abc'

removeDuplicateWords(str)

Removes duplicate words.

import { removeDuplicateWords } from '@js-utility/string';
removeDuplicateWords('foo bar foo baz'); // 'foo bar baz'

removeConsecutiveDuplicates(str)

Removes consecutive duplicate characters.

import { removeConsecutiveDuplicates } from '@js-utility/string';
removeConsecutiveDuplicates('aaabbbcc'); // 'abc'

removeConsecutiveDuplicateWords(str)

Removes consecutive duplicate words.

import { removeConsecutiveDuplicateWords } from '@js-utility/string';
removeConsecutiveDuplicateWords('foo foo bar bar bar baz'); // 'foo bar baz'

HTML Utilities

escapeHtml(str)

Escapes special HTML characters.

import { escapeHtml } from '@js-utility/string';
escapeHtml('<div>"Hello"</div>'); // '&lt;div&gt;&quot;Hello&quot;&lt;/div&gt;'

unescapeHtml(str)

Unescapes HTML entities.

import { unescapeHtml } from '@js-utility/string';
unescapeHtml('&lt;div&gt;Hello&lt;/div&gt;'); // '<div>Hello</div>'

stripHtmlTags(str)

Removes HTML tags.

import { stripHtmlTags } from '@js-utility/string';
stripHtmlTags('<b>Hello</b> World'); // 'Hello World'

Random String Generation

random(length?, type?)

Generates a random string.
Types: "upper", "lower", "alpha", "number", "alphanumeric", "special", "mix" (default).

import { random } from '@js-utility/string';
random(8, 'alpha'); // e.g. 'aBcDeFgH'
random(10, 'number'); // e.g. '4839201745'

Slugify

slugify(str)

Converts to a URL-friendly slug.

import { slugify } from '@js-utility/string';
slugify('Hello World!'); // 'hello-world'

Pluralization

pluralize(str)

Converts a singular noun to plural.

import { pluralize } from '@js-utility/string';
pluralize('child'); // 'children'

singularize(str)

Converts a plural noun to singular.

import { singularize } from '@js-utility/string';
singularize('children'); // 'child'

Support & Feedback

For support or feedback, feel free to reach out via support email. We value your input and are here to assist with any questions or suggestions you may have.

License

MIT