Slugify & Deslugify

Quick helpers to create a slugified or deslugified string

Updated:
Utility FunctionString Manipulation
function
function capitalizeFirstLetter(str: string): string
capitalizeFirstLetter
(
str: string
str
: string) {
if (
str: string
str
.
String.length: number

Returns the length of a String object.

length
== 0) return "";
if (
str: string
str
.
String.length: number

Returns the length of a String object.

length
== 1) return
str: string
str
[0].
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
();
return
str: string
str
[0].
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
() +
str: string
str
.
String.substring(start: number, end?: number): string

Returns the substring at the specified location within a String object.

@paramstart The zero-based index number indicating the beginning of the substring.

@paramend Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end. If end is omitted, the characters from start through the end of the original string are returned.

substring
(1);
}
function
function deSlugifyStr(str: string): string
deSlugifyStr
(
str: string
str
: string) {
// if string doesn't contain a '-' or '_' just return back the capitalized word
if (!
str: string
str
.
String.includes(searchString: string, position?: number): boolean

Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false.

@paramsearchString search string

@paramposition If position is undefined, 0 is assumed, so as to search all of the String.

includes
("-") && !
str: string
str
.
String.includes(searchString: string, position?: number): boolean

Returns true if searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false.

@paramsearchString search string

@paramposition If position is undefined, 0 is assumed, so as to search all of the String.

includes
("_")) return
function capitalizeFirstLetter(str: string): string
capitalizeFirstLetter
(
str: string
str
);
// replaces '-' or '_ with ' '
const
const StrWithDashesAndHypensReplacedWithSpaces: string
StrWithDashesAndHypensReplacedWithSpaces
=
str: string
str
.
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/-/g, " ").
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/_/g, " ");
let
let deSlugifiedStr: string
deSlugifiedStr
= "";
const StrWithDashesAndHypensReplacedWithSpaces: string
StrWithDashesAndHypensReplacedWithSpaces
.
String.split(separator: string | RegExp, limit?: number): string[] (+1 overload)

Split a string into substrings using the specified separator and return them as an array.

@paramseparator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.

@paramlimit A value used to limit the number of elements returned in the array.

split
(" ").
Array<string>.forEach(callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any): void

Performs the specified action for each element in an array.

@paramcallbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

forEach
((
char: string
char
) => {
let deSlugifiedStr: string
deSlugifiedStr
+= !!
char: string
char
?
function capitalizeFirstLetter(str: string): string
capitalizeFirstLetter
(
char: string
char
) : " ";
});
return
let deSlugifiedStr: string
deSlugifiedStr
;
}
function
function slugifyString(str: string): string
slugifyString
(
str: string
str
: string) {
if (typeof
str: string
str
!== "string")
throw new
var TypeError: TypeErrorConstructor
new (message?: string, options?: ErrorOptions) => TypeError (+3 overloads)
TypeError
(
`Invalid value passed to slugifyString!: Expected string but received ${typeof
str: never
str
}`,
);
return (
str: string
str
.
String.trim(): string

Removes the leading and trailing white space and line terminator characters from a string.

trim
()
.
String.toLowerCase(): string

Converts all the alphabetic characters in a string to lowercase.

toLowerCase
()
//Remove all characters except alphanumeric characters and spaces
.
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/[^\w\s]/gi, "")
//replace all spaces with -
.
String.replace(searchValue: {
[Symbol.replace](string: string, replaceValue: string): string;
}, replaceValue: string): string (+3 overloads)

Passes a string and {@linkcode replaceValue } to the [Symbol.replace] method on {@linkcode searchValue } . This method is expected to implement its own replacement algorithm.

@paramsearchValue An object that supports searching for and replacing matches within a string.

@paramreplaceValue The replacement text.

replace
(/[^a-z0-9]/gi, "-")
);
}