endsWith
string
Checks if string ends with the given target string.
Installation
Import
import { endsWith } from '@tulx/utils';Source Code
Implementation
/**
* Checks if string ends with the given target string.
*
* @param string - The string to inspect.
* @param target - The string to search for.
* @param position - The position to search up to.
* @returns Returns true if string ends with target, else false.
*
* @example
* ```ts
* endsWith('abc', 'c'); // true
* endsWith('abc', 'b'); // false
* endsWith('abc', 'b', 2); // true
* ```
*/
export function endsWith(
string: string,
target: string,
position?: number
): boolean {
const { length } = string;
const end = position === undefined ? length : position;
const pos = end > length ? length : end;
return string.substring(pos - target.length, pos) === target;
}
Example
import { endsWith } from '@tulx/utils';
endsWith('abc', 'c'); // true
endsWith('abc', 'b'); // false
endsWith('abc', 'b', 2); // trueRelated Functions
camelCase
Converts string to camel case.
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
escapeRegExp
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.
kebabCase
Converts string to kebab case.