UNPKG

string-tool

Version:

Useful functions for Strings

66 lines (65 loc) 1.54 kB
"use strict"; exports.__esModule = true; /** * Capitalizes the first letter of the text */ function capitalize(text) { return text.charAt(0).toUpperCase() + text.slice(1); } exports.capitalize = capitalize; /** * Finds the index of the nth occurence of `search` */ function nthIndexOf(text, n, search) { var index; for (; n > 0; --n) { index = text.indexOf(search, index); if (index === -1) { return; } if (n > 1) { index += search.length; } } return index; } exports.nthIndexOf = nthIndexOf; /** * Removes the first occurence of `search` */ function cutFirst(search, text) { var index = text.indexOf(search); if (index === -1) { return text; } return text.slice(0, index) + text.slice(index + search.length); } exports.cutFirst = cutFirst; /** * Removes everything after (and including) `search` * * Returns the unmodified text if `search` is not found * * Useful for extracting the URL protocol: * cutBefore(':', 'http://www.example.org/'); */ function cutBefore(search, text) { var index = text.indexOf(search); if (index < 0) { return text; } return text.slice(0, index); } exports.cutBefore = cutBefore; /** * Removes everything after (and including) the index. * * Returns the unmodified text if the index is negative. */ function cutBeforeIndex(index, text) { if (index < 0) { return text; } return text.slice(0, index); } exports.cutBeforeIndex = cutBeforeIndex;