A light weight utility collection on string. Without any external dependencies.
The unit test cases are generated by our app UnitRunner.app
Happy coding!
Please feel free to open an issue. I shall be happy to help soon. Click here
$ npm install light-string-utils --save
// CommonJS
const ltStringUtils = require("light-string-utils"); // OR:
const { length } = require("light-string-utils");
[//]: START GENERATED CODE
abbreviate()
capitalize()
center()
deCapitalize()
endsWith()
equalsIgnoreCase()
escapeHTML()
first()
isAlNum()
isAlpha()
last()
length()
limit()
occurance()
pad()
repeat()
replaceBy()
sentenceCase()
slugify()
subString()
titleCase()
toArray()
worldCount()
Abbreviates a string by returning the first character of each word.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The input string to be abbreviated. |
{string} - The abbreviated string.
abbreviate("Hello World"); // Returns "HW"
abbreviate(" "); // Returns ""
abbreviate("test"); // Returns "t"
Capitalizes the first letter of a string and converts the rest to lowercase.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The input string to be capitalized. |
{string} - The capitalized string.
capitalize("hello world"); // returns "Hello world"
capitalize("hElLo wOrLD"); // returns "Hello world"
capitalize(" A "); // returns "A"
capitalize(""); // returns ""
capitalize(null); // throws Error "Invalid string served"
capitalize(123); // throws Error "Invalid string served"
Center aligns the given string within a new string of the specified length, padded with the specified character.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to center align. |
totalStringLength | {number} | none | The total length of the resulting string. |
paddingCharacter | {string} | " " | The character to use for padding. Defaults to a space. |
{string} - A new string of the specified length, with the original string centered and padded.
center("Hello World", 20); // Returns " Hello World "
center("Hello World", 20, "*"); // Returns "****Hello World*****"
De-capitalizes the first letter of a string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to de |
{string} - A new string with the first letter de
deCapitalize("Hello World"); // Returns "hello World"
deCapitalize(" "); // Returns ""
deCapitalize("test"); // Returns "test"
Checks if a string ends with the specified substring.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to center align. |
compareSubString | {string} | none | The substring to compare. |
{boolean} - A boolean indicating if the string ends with the specified substring.
endsWith("Hello World", "World"); // Returns true
endsWith("Hello World", "Worlds"); // Returns false
endsWith("Hello World", " "); // Returns false
endsWith("Hello World", "Hello World"); // Returns true
Checks if two strings are equal ignoring the case.
Name | Type | Default | Description |
---|---|---|---|
str1 | {string} | none | The first string to compare. |
str2 | {string} | none | The second string to compare. |
{boolean} - A boolean indicating if the strings are equal ignoring the case.
equalsIgnoreCase("Hello World", "hello world"); // Returns true
equalsIgnoreCase("Hello World", "hello worlds"); // Returns false
equalsIgnoreCase("Hello World", " "); // Returns false
equalsIgnoreCase("Hello World", "Hello World"); // Returns true
Escapes HTML characters.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to escape. |
{string} - The escaped string.
escapeHTML("<p>hello world</p>"); // returns "<p>hello world</p>"
escapeHTML("hello world"); // returns "hello world"
escapeHTML("hello & world"); // returns "hello & world"
escapeHTML("hello < world"); // returns "hello < world"
Returns the first character of a string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to get the first character from. |
characterCount | {number} | 1 | The number of characters to return. |
{string} - The first character of the string.
first("Hello World"); // Returns "H"
first(" "); // Returns ""
first("test"); // Returns "t"
first("Hello World", 2); // Returns "He"
Checks if the string contains only alphanumeric characters.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to check. |
{boolean} - A boolean indicating if the string contains only alphanumeric characters.
isAlNum("Hello World"); // Returns false
isAlNum("HelloWorld"); // Returns true
isAlNum("HelloWorld123"); // Returns true
isAlNum("Hello World123"); // Returns false
Checks if the string contains only alphanumeric characters.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to check. |
{boolean} - A boolean indicating if the string contains only alphanumeric characters.
isAlpha("Hello World"); // Returns false
isAlpha("HelloWorld"); // Returns true
isAlpha("HelloWorld123"); // Returns false
isAlpha("Hello World123"); // Returns false
Returns the last character of a string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to get the last character from. |
characterCount | {number} | 1 | The number of characters to return. |
{string} - The last character of the string.
last("Hello World"); // Returns "d"
last(" "); // Returns ""
last("test"); // Returns "t"
last("Hello World", 2); // Returns "ld"
Returns the length of a string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to get the length of a string. |
{number} - The length of the string.
length("Hello World"); // Returns 11
length(" "); // Returns 1
length("test"); // Returns 4
Limits the length of a string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to limit. |
strLimit | {number} | none | The limit of the string. |
padSide | {string} | "right" | The side to pad the string. |
padString | {string} | "..." | The string to pad the string with. |
{string} - The limited string.
limit("Hello World", 5); // Returns "Hello..."
limit("Hello World", 4, "left"); // Returns "...Hello "
limit("Hello World", 4, "left", "!!!"); // Returns "!!!Hello"
Returns the indices of the finding string in the input string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The input string (target string). |
findStr | {string} | none | The finding string. |
caseSensitive | {string} | false | Should it be case sensitive or in |
{number[]} - The starting indices of the occurances.
occurance("Hello World!!, Hello all", "Hellow"); // Returns []
occurance("You are great as usual", " "); // Returns [3, 7, 13, 16]
occurance("Contributing to the open source encourages mutual side benefits"); // Returns [3, 8, 13, 16, 45, 61]
Pads a string with another string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to pad. |
side | {string} | none | The side to pad the string (left |
padWith | {string} | none | The string to pad the string with. |
{string} - The padded string.
pad("Hello World", "left", "!!!"); // Returns "!!!Hello World"
pad("Hello World", "right", "!!!"); // Returns "Hello World!!!"
pad("Hello World", "both", "!!!"); // Returns "!!!Hello World!!!"
pad("Hello World", "both"); // Returns "Hello World"
repeats a string by defined times.
Name | Type | Default | Description |
---|---|---|---|
item | {item} | none | The input string/number to be repeated. |
times | {number} | 1 | The number of times to repeat the string. |
delimiter | {string} | "" | The delimiter to be used between the repeated strings. |
{string} - The repeated string.
repeat("san"); // Returns "san"
repeat("san", 4); // Returns "sansansansan"
repeat("test ", 2); // Returns "test test"
Replaces all the occurrences of a string with another string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to replace. |
searchReplaceObj | {string} | none | The object containing the search and replace strings. |
{string} - The replaced string.
replaceBy("{{{Hello}}} {{{World}}}", { "Hello": "Hi", "World": "Earth" }); // Returns "Hi Earth"
replaceBy("{{{Hello}}} World", { "Hello": "Hi" }); // Returns "Hi World"
replaceBy("{{{Hello}}} {{{World}}}", { "Hello": "Hi", "World": "Earth", "Earth": "World" }); // Returns "Hi World"
Converts a string to sentence case.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to convert. |
{string} - The converted string.
sentenceCase("hello world"); // Returns "Hello world"
sentenceCase("hello world."); // Returns "Hello world."
Converts a string to a slug.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to convert. |
{string} - The converted string.
slugify("Hello World"); // Returns "hello-world"
slugify("Hello World!"); // Returns "hello-world"
slugify("Hello World!@#$%^&*()"); // Returns "hello-world"
Returns a substring of a string.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to get the substring from. |
start | {number} | 0 | The index to start the substring from. |
countFromStart | {number} | undefined | The number of characters to return. |
{string} - The substring of the string.
subString("Hello World"); // Returns "Hello World"
subString(" "); // Returns " "
subString("Hello World", 2); // Returns "llo World"
subString("Hello World", 2, 3); // Returns "llo"
Converts a string to title case.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to convert. |
{string} - The converted string.
titleCase("hello world"); // Returns "Hello World"
titleCase("hello world!@#$%^&*()"); // Returns "Hello World!@#$%^&*()"
Converts a string to an array.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to convert. |
explodeBy | {string} | "" | The separator to use. |
{string[]} - The converted string.
toArray("Hello World", " "); // Returns ["Hello", "World"]
toArray("test"); // Returns ["t", "e", "s", "t"]
Counts the number of words seperated by spaces or the given seperator.
Name | Type | Default | Description |
---|---|---|---|
str | {string} | none | The string to convert. |
explodeBy | {string} | "" | The separator to use. |
{number} - The word count.
worldCount("Hello World", " "); // Returns 2
wc("test me!"); // Returns 2
[//]: END GENERATED CODE
$ npm test
This software is released under the MIT License.