string-transformation

2.1.0 • Public • Published

String Transformation

A JavaScript module for performing various string transformation tasks. To install this module run command

npm i string-transformation

Current Features Badge Badge

Import Module

const stringUtil = require('string-transformation');
stringUtil.stringLength(str);

OR

const { stringLength } = require('string-transformation');

Functions

stringLength(str)

Calculates the length of the given string.

Input:

stringLength('Hello, World!');

Output:

13

convertToUpperCase(str)

Convert string to UpperCase.

Input:

convertToUpperCase('hello');

Output:

HELLO

convertToLowerCase(str)

Convert string to LowerCase.

Input:

convertToLowerCase('WORLD');

Output:

world

capitalizeFirstLetter(str)

Capitalize first character of string.

Input:

capitalizeFirstLetter('hello world!');

Output:

Hello world!

convertToTitleCase(str)

Capitalize first character of every word in the string.

Input:

convertToTitleCase('this is a test sentence');

Output:

This Is A Test Sentence

getSubstring(str, start, end)

Get a substring from the string.

Input:

getSubstring('abcdef', 1, 4);

Output:

bcd

trimWhitespace(str)

Trim white space from both end of the string.

Input:

trimWhitespace('   Hello, World!   ');

Output:

Hello, World!

trimLeft(str)

Trim leading white space from string.

Input:

trimLeft('   Leading whitespace');

Output:

Leading whitespace

trimRight(str)

Trim trailing white space from string.

Input:

trimRight('Trailing whitespace   ');

Output:

Trailing whitespace

replaceCharacter(str, oldChar, newChar)

Replace a character in the string.

Input:

replaceCharacter('banana', 'a', 'o');

Output:

bonono

removeCharacter(str, charToRemove)

Remove a character from the string.

Input:

removeCharacter('banana', 'a');

Output:

bnn

splitString(str, delimiter)

Split string and gives an array.

Input:

splitString('apple,orange,banana', ',');

Output:

[ 'apple', 'orange', 'banana' ]

joinStrings(array, delimiter)

Joins an array of string and provides a string.

Input:

joinStrings(['apple', 'orange', 'banana'], ', ');

Output:

apple, orange, banana

firstOccurrence(str, substring)

Get the first occorrence of a substring in a string.

Input:

firstOccurrence('programming', 'g');

Output:

3

findAllOccurrences(str, substring)

Get all the occorrence of a substring in a string as a array.

Input:

findAllOccurrences('banana', 'a');

Output:

[1, 3, 5]

replaceSubstring(str, oldSubstring, newSubstring)

Replace a substring from the string with a new string.

Input:

replaceSubstring('hello world', 'world', 'there');

Output:

hello there

replaceWhitespaceWith(str, newCharacter)

Remove white space from string and replace it with new character.

Input:

replaceWhitespaceWith('Hello    World!', '-');

Output:

Hello----World!

padLeft(str, padChar, totalLength)

Make the string longer by adding padding character to the left of the string if the string is shorter than the total required length of the string.

Input:

padLeft('42', '0', 5);

Output:

00042

padRight(str, padChar, totalLength)

Make the string longer by adding padding character to the right of the string if the string is shorter than the total required length of the string.

Input:

padRight('42', '0', 5);

Output:

42000

reverseString(str)

Reverse the string.

Input:

reverseString('world');

Output:

dlrow

isPalindrome(str)

Check if the provided string is a palindrome.

Input:

isPalindrome('racecar');

Output:

true

isNumeric(str)

Check if the provided string contains only numeric values (numbers).

Input:

isNumeric('42.5');

Output:

true

encodeBase64(str)

Encode the provided string to base64.

Input:

encodeBase64('Hello, World!');

Output:

SGVsbG8sIFdvcmxkIQ==

decodeBase64(str)

Decode base64 code to string.

Input:

decodeBase64('SGVsbG8sIFdvcmxkIQ==');

Output:

Hello, World!

matchRegex(str, regex)

Returns an array of all occurrences of a given regular expression pattern in a string.

Input:

matchRegex('abc123def456', /\d+/g);

Output:

["123", "456"]

replaceRegex(str, regex, replacement)

Replaces occurrences of a given regular expression pattern with a specified replacement in a string.

Input:

replaceRegex('abc123def456', /\d+/g, 'X');

Output:

abcXdefX

encodeURIComponent(str)

Encodes a string for safe use in a URL.

Input:

encodeURIComponent('Hello, World!');

Output:

Hello%2C%20World!

decodeURIComponent(str)

Decodes a URL-encoded string back to its original form.

Input:

decodeURIComponent('Hello%2C%20World%21');

Output:

Hello, World!

getPathFromURL(url)

Extracts the path portion from a given URL.

Input:

getPathFromURL('https://example.com/path/file.html');

Output:

/path/file.html

compareStrings(str1, str2)

Compares two strings lexicographically and returns a comparison result.

Input:

compareStrings('apple', 'banana');

Output:

-1

equalsIgnoreCase(str1, str2)

Checks if two strings are equal, ignoring their case.

Input:

equalsIgnoreCase('hello', 'Hello');

Output:

true

formatString(format, ...values)

Formats a string using placeholders and corresponding values.

Input:

formatString('Hello, {0}!', 'World');

Output:

Hello, World!

Input:

formatString('Hello {0}, Hello {1}!', 'there', 'World');

Output:

Hello there, Hello World!

getUnicodeCodePointAt(str, index)

Retrieves the Unicode code point at a specific position in a string.

Input:

getUnicodeCodePointAt('A', 0);

Output:

65

convertToUnicode(str)

Converts a string to a sequence of Unicode code points.

Input:

convertToUnicode('Hello');

Output:

U+48 U+65 U+6c U+6c U+6f

truncateText(str, maxLength, ellipsis)

Truncates a string to a specified length with an optional ellipsis.

Input:

truncateText('Lorem ipsum dolor sit amet', 15);

Output:

Lorem ipsum dol...

normalizeWhitespace(str)

Replaces consecutive whitespace characters with a single space.

Input:

normalizeWhitespace('  Hello    World!   ');

Output:

 Hello World! 

collapseWhitespace(str)

Removes all whitespace characters from a string.

Input:

collapseWhitespace('Hello   World!');

Output:

HelloWorld! 

getCharacterType(char)

Determines the type of a character (uppercase, lowercase, digit, etc.).

Input:

getCharacterType('A');

Output:

uppercase 

getCharacterCode(char)

Retrieves the Unicode code point of a character.

Input:

getCharacterCode('A');

Output:

65 

padWithContent(str, padContent, totalLength)

Pads a string with a specified content to achieve a desired length.

Input:

padWithContent('Hello', '*', 10);

Output:

**Hello***

convertToCamelCase(str)

Converts a string to camel case format.

Input:

convertToCamelCase('hello-world');

Output:

helloWorld

convertToSnakeCase(str)

Converts a string to snake case format.

Input:

convertToSnakeCase('helloWorld');

Output:

hello_world

wrapText(text, maxWidth)

Wraps a long text string into multiple lines with a specified maximum width.

Input:

wrapText('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 20);

Output:

Lorem ipsum dolor sit
amet, consectetur
adipiscing elit.

uuid()

Provide uuid for (Universally Unique IDentifier).

Input:

uuid();

Output:

28d921a6-fc82-2dcf79-8244-c7112d52af

uuidNumber()

Provide uuid which only consist of numbers.

Input:

uuidNumber();

Output:

500879049-52937-13546-19988-1519210004

uuidDateTime()

Provide uuid based on Date and Time.

Input:

uuidDateTime();

Output:

1ee41006916c750-faba78b560-1ee4-9234-0123456789ab

Package Sidebar

Install

npm i string-transformation

Weekly Downloads

2

Version

2.1.0

License

MIT

Unpacked Size

25.9 kB

Total Files

5

Last publish

Collaborators

  • soumodeep-ganguly