Skip to main content
@spa-tools/utilities

addForwardSlash()

The addForwardSlash() function simply adds a forward slash to a string (if not already present) at the start, end, or both.

Usage

import { addForwardSlash } from '@spa-tools/utilities';

const str1 = '/path/to/something';
const path1 = addForwardSlash(str1);
// --> '/path/to/something/'

const str2 = 'path/to/something';
const path2 = addForwardSlash(str2, 'start');
// --> '/path/to/something'

const str3 = 'path/to/something';
const path3 = addForwardSlash(str3, 'end');
// --> 'path/to/something/'

const str4 = 'path/to/something';
const path4 = addForwardSlash(str4, 'both');
// --> '/path/to/something/'

Arguments

NameTypeRequired?DefaultDescription
strstringyes-The value to add forward slash to
placement'start' | 'end' | 'both'no'end'The placement for the forward slash

Returns

Returns a string.