Capitalizes the first letter of a string.
| Name | Type | Description |
|---|---|---|
str |
string |
The string to capitalize. |
string — Returns the string with the first letter capitalized, or an empty string if the input is empty or not a string.
import { capitalize } from "js-utility-method";
capitalize("hello"); // => "Hello"
capitalize("world"); // => "World"
capitalize(""); // => ""
capitalize("a"); // => "A"
capitalize("Hello"); // => "Hello"
Converts a string to camelCase format.
| Name | Type | Description |
|---|---|---|
str |
string |
The string to convert to camelCase. |
string — Returns the string in camelCase format, or an empty string if the input is empty or not a string.
import { camelCase } from "js-utility-method";
camelCase("hello world"); // => "helloWorld"
camelCase("foo-bar-baz"); // => "fooBarBaz"
camelCase("hello_world"); // => "helloWorld"
camelCase("Hello World"); // => "helloWorld"
camelCase("PascalCase"); // => "pascalCase"
camelCase(""); // => ""
Removes all spaces from a string.
| Name | Type | Description |
|---|---|---|
str |
string |
The string to remove spaces from. |
string — Returns the string with all spaces removed.
import { removeSpaces } from "js-utility-method";
removeSpaces("hello world"); // => "helloworld"
removeSpaces(" foo bar "); // => "foobar"
removeSpaces("no spaces here"); // => "nospaceshere"
Converts a string to a URL-friendly slug.
| Name | Type | Description |
|---|---|---|
str |
string |
The string to convert to a slug. |
string — Returns a URL-friendly slug.
import { slugify } from "js-utility-method";
slugify("Hello World"); // => "hello-world"
slugify("JavaScript is Awesome"); // => "javascript-is-awesome"
slugify("foo_bar"); // => "foo-bar"
slugify(" Multiple Spaces "); // => "multiple-spaces"
Truncates a string to a specified length with an optional suffix.
| Name | Type | Description |
|---|---|---|
str |
string |
The string to truncate. |
length |
number |
Maximum length of the string. |
suffix |
string |
(optional) Suffix to append. Default: "..." |
string — Returns the truncated string with suffix if applicable.
import { truncate } from "js-utility-method";
truncate("Hello World", 5); // => "Hello..."
truncate("JavaScript", 10); // => "JavaScript"
truncate("Long text here", 8, "..."); // => "Long tex..."
truncate("Short", 10); // => "Short"