1.8.3 • Published 2 months ago

light-string-utils v1.8.3

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

Light String Utils

npm npm npm

A light weight utility collection on string. Without any external dependencies.

The unit test cases are generated by our app UnitRunner.app

Happy coding!

Found any bugs/features?

Please feel free to open an issue. I shall be happy to help soon. Click here

Install

$ npm install light-string-utils --save

Usage

// CommonJS
const ltStringUtils = require("light-string-utils"); // OR:
const { length } = require("light-string-utils");

//: START GENERATED CODE

abbreviate

Abbreviates a string by returning the first character of each word.

Parameters

NameTypeDefaultDescription
str{string}noneThe input string to be abbreviated.

Returns

{string} - The abbreviated string.

Examples

abbreviate("Hello World"); // Returns "HW"
abbreviate(" "); // Returns ""
abbreviate("test"); // Returns "t"

capitalize

Capitalizes the first letter of a string and converts the rest to lowercase.

Parameters

NameTypeDefaultDescription
str{string}noneThe input string to be capitalized.

Returns

{string} - The capitalized string.

Examples

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

Center aligns the given string within a new string of the specified length, padded with the specified character.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to center align.
totalStringLength{number}noneThe total length of the resulting string.
paddingCharacter{string}" "The character to use for padding. Defaults to a space.

Returns

{string} - A new string of the specified length, with the original string centered and padded.

Examples

center("Hello World", 20); // Returns "    Hello World     "
center("Hello World", 20, "*"); // Returns "****Hello World*****"

deCapitalize

De-capitalizes the first letter of a string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to de

Returns

{string} - A new string with the first letter de

Examples

deCapitalize("Hello World"); // Returns "hello World"
deCapitalize(" "); // Returns ""
deCapitalize("test"); // Returns "test"

endsWith

Checks if a string ends with the specified substring.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to center align.
compareSubString{string}noneThe substring to compare.

Returns

{boolean} - A boolean indicating if the string ends with the specified substring.

Examples

endsWith("Hello World", "World"); // Returns true
endsWith("Hello World", "Worlds"); // Returns false
endsWith("Hello World", " "); // Returns false
endsWith("Hello World", "Hello World"); // Returns true

equalsIgnoreCase

Checks if two strings are equal ignoring the case.

Parameters

NameTypeDefaultDescription
str1{string}noneThe first string to compare.
str2{string}noneThe second string to compare.

Returns

{boolean} - A boolean indicating if the strings are equal ignoring the case.

Examples

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

escapeHTML

Escapes HTML characters.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to escape.

Returns

{string} - The escaped string.

Examples

escapeHTML("<p>hello world</p>"); // returns "&lt;p&gt;hello world&lt;/p&gt;"
escapeHTML("hello world"); // returns "hello world"
escapeHTML("hello & world"); // returns "hello &amp; world"
escapeHTML("hello < world"); // returns "hello &lt; world"

first

Returns the first character of a string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to get the first character from.
characterCount{number}1The number of characters to return.

Returns

{string} - The first character of the string.

Examples

first("Hello World"); // Returns "H"
first(" "); // Returns ""
first("test"); // Returns "t"
first("Hello World", 2); // Returns "He"

isAlNum

Checks if the string contains only alphanumeric characters.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to check.

Returns

{boolean} - A boolean indicating if the string contains only alphanumeric characters.

Examples

isAlNum("Hello World"); // Returns false
isAlNum("HelloWorld"); // Returns true
isAlNum("HelloWorld123"); // Returns true
isAlNum("Hello World123"); // Returns false

isAlpha

Checks if the string contains only alphanumeric characters.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to check.

Returns

{boolean} - A boolean indicating if the string contains only alphanumeric characters.

Examples

isAlpha("Hello World"); // Returns false
isAlpha("HelloWorld"); // Returns true
isAlpha("HelloWorld123"); // Returns false
isAlpha("Hello World123"); // Returns false

last

Returns the last character of a string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to get the last character from.
characterCount{number}1The number of characters to return.

Returns

{string} - The last character of the string.

Examples

last("Hello World"); // Returns "d"
last(" "); // Returns ""
last("test"); // Returns "t"
last("Hello World", 2); // Returns "ld"

length

Returns the length of a string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to get the length of a string.

Returns

{number} - The length of the string.

Examples

length("Hello World"); // Returns 11
length(" "); // Returns 1
length("test"); // Returns 4

limit

Limits the length of a string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to limit.
strLimit{number}noneThe limit of the string.
padSide{string}"right"The side to pad the string.
padString{string}"..."The string to pad the string with.

Returns

{string} - The limited string.

Examples

limit("Hello World", 5); // Returns "Hello..."
limit("Hello World", 4, "left"); // Returns "...Hello "
limit("Hello World", 4, "left", "!!!"); // Returns "!!!Hello"

occurance

Returns the indices of the finding string in the input string.

Parameters

NameTypeDefaultDescription
str{string}noneThe input string (target string).
findStr{string}noneThe finding string.
caseSensitive{string}falseShould it be case sensitive or in

Returns

{number[]} - The starting indices of the occurances.

Examples

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]

pad

Pads a string with another string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to pad.
side{string}noneThe side to pad the string (leftrightboth).
padWith{string}noneThe string to pad the string with.

Returns

{string} - The padded string.

Examples

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"

repeat

repeats a string by defined times.

Parameters

NameTypeDefaultDescription
item{item}noneThe input string/number to be repeated.
times{number}1The number of times to repeat the string.
delimiter{string}""The delimiter to be used between the repeated strings.

Returns

{string} - The repeated string.

Examples

repeat("san"); // Returns "san"
repeat("san", 4); // Returns "sansansansan"
repeat("test ", 2); // Returns "test test"

replaceBy

Replaces all the occurrences of a string with another string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to replace.
searchReplaceObj{string}noneThe object containing the search and replace strings.

Returns

{string} - The replaced string.

Examples

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"

sentenceCase

Converts a string to sentence case.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to convert.

Returns

{string} - The converted string.

Examples

sentenceCase("hello world"); // Returns "Hello world"
sentenceCase("hello world."); // Returns "Hello world."

slugify

Converts a string to a slug.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to convert.

Returns

{string} - The converted string.

Examples

slugify("Hello World"); // Returns "hello-world"
slugify("Hello World!"); // Returns "hello-world"
slugify("Hello World!@#$%^&*()"); // Returns "hello-world"

subString

Returns a substring of a string.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to get the substring from.
start{number}0The index to start the substring from.
countFromStart{number}undefinedThe number of characters to return.

Returns

{string} - The substring of the string.

Examples

subString("Hello World"); // Returns "Hello World"
subString(" "); // Returns " "
subString("Hello World", 2); // Returns "llo World"
subString("Hello World", 2, 3); // Returns "llo"

titleCase

Converts a string to title case.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to convert.

Returns

{string} - The converted string.

Examples

titleCase("hello world"); // Returns "Hello World"
titleCase("hello world!@#$%^&*()"); // Returns "Hello World!@#$%^&*()"

toArray

Converts a string to an array.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to convert.
explodeBy{string}""The separator to use.

Returns

{string[]} - The converted string.

Examples

toArray("Hello World", " "); // Returns ["Hello", "World"]
toArray("test"); // Returns ["t", "e", "s", "t"]

worldCount

Counts the number of words seperated by spaces or the given seperator.

Parameters

NameTypeDefaultDescription
str{string}noneThe string to convert.
explodeBy{string}""The separator to use.

Returns

{number} - The word count.

Examples

worldCount("Hello World", " "); // Returns 2
wc("test me!"); // Returns 2

//: END GENERATED CODE

Test

$ npm test

License

This software is released under the MIT License.

1.8.3

2 months ago

1.8.2

6 months ago

1.8.1

12 months ago

1.8.0

12 months ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.5

1 year ago

1.6.4

1 year ago

1.6.3

1 year ago

1.6.2

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.9

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago