logitar-js
Version:
Helper functions distributed by Logitar.
176 lines (175 loc) • 6.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.unaccent = exports.trimStart = exports.trimEnd = exports.trim = exports.slugify = exports.shortify = exports.isNullOrWhiteSpace = exports.isNullOrEmpty = exports.isLetterOrDigit = exports.isLetter = exports.isDigit = exports.isAbsoluteURL = exports.combineURL = exports.cleanTrim = void 0;
/**
* Trims the specified string if it is not empty or white-space, or returns undefined.
* @param s The value to clean-trim.
* @returns The trimmed string if it was not empty nor white-space, or undefined otherwise.
*/
function cleanTrim(s) {
return isNullOrWhiteSpace(s) ? undefined : s === null || s === void 0 ? void 0 : s.trim();
}
exports.cleanTrim = cleanTrim;
/**
* Combines the specified segments into an URL.
* @param segments The segments to combine.
* @returns The created URL.
*/
function combineURL(...segments) {
const url = segments
.map((v) => { var _a; return (_a = v === null || v === void 0 ? void 0 : v.trim().replace(/^\/+|\/+$/g, "")) !== null && _a !== void 0 ? _a : ""; })
.filter((v) => v.length)
.join("/");
return isAbsoluteURL(url) ? url : `/${url}`;
}
exports.combineURL = combineURL;
const absoluteUrlRegex = new RegExp("^(?:[a-z+]+:)?//", "i");
/**
* Returns a value indicating whether or not the specified URL is absolute.
* @param url The URL to check.
* @returns True if the URL is absolute, or false otherwise.
*/
function isAbsoluteURL(url) {
return absoluteUrlRegex.test(url);
}
exports.isAbsoluteURL = isAbsoluteURL;
/**
* Returns a value indicating whether or not the specified character is a digit.
* @param c The character to check.
* @returns True if the character is a digit, or false otherwise.
*/
function isDigit(c) {
return c.trim() !== "" && !isNaN(Number(c));
}
exports.isDigit = isDigit;
/**
* Returns a value indicating whether or not the specified character is a letter.
* @param c The character to check.
* @returns True if the character is a letter, or false otherwise.
*/
function isLetter(c) {
return c.toLowerCase() !== c.toUpperCase();
}
exports.isLetter = isLetter;
/**
* Returns a value indicating whether or not the specified character is a letter or a digit.
* @param c The character to check.
* @returns True if the character is a letter or a digit, or false otherwise.
*/
function isLetterOrDigit(c) {
return isDigit(c) || isLetter(c);
}
exports.isLetterOrDigit = isLetterOrDigit;
/**
* Returns a value indicating whether or not the specified string is undefined or empty.
* @param c The string to check.
* @returns True if the string is undefined or empty, or false otherwise.
*/
function isNullOrEmpty(s) {
return typeof s !== "string" || s.length === 0;
}
exports.isNullOrEmpty = isNullOrEmpty;
/**
* Returns a value indicating whether or not the specified string is undefined, empty or white-space.
* @param c The string to check.
* @returns True if the string is undefined, empty or white-space, or false otherwise.
*/
function isNullOrWhiteSpace(s) {
return isNullOrEmpty(s === null || s === void 0 ? void 0 : s.trim());
}
exports.isNullOrWhiteSpace = isNullOrWhiteSpace;
/**
* Returns a shortened version of the specified string, capped to the specified maximum length.
* @param s The string to shorten.
* @param length The maximum length of the string.
* @returns The original string if its length was below the maximum length, or a string of the specified length, with its last character being a `…`.
*/
function shortify(s, length) {
return s.length > length ? s.substring(0, length - 1) + "…" : s;
}
exports.shortify = shortify;
/**
* Formats the specified string into a slug. Slugs are composed of non-empty words separated by hyphens (`-`).
* @param s The string to slugify.
* @returns The formatted slug string.
*/
function slugify(s) {
if (!s) {
return "";
}
const words = [];
let word = "";
for (let i = 0; i < s.length; i++) {
const c = s[i];
if (isLetterOrDigit(c)) {
word += c;
}
else if (word.length) {
words.push(word);
word = "";
}
}
if (word.length) {
words.push(word);
}
return unaccent(words.join("-").toLowerCase());
}
exports.slugify = slugify;
const reservedChars = new Set(["]", "^", "\\"]);
/**
* Trims the specified string of the specified characters, removing all occurrences of this character at the start and at the end of the string.
* @param s The string to trim.
* @param c The character to remove from the string's start and end.
* @returns The trimmed string.
*/
function trim(s, c) {
c = reservedChars.has(c) ? `\\${c}` : c;
return s.replace(new RegExp(`^[${c}]+|[${c}]+$`, "g"), "");
}
exports.trim = trim;
/**
* Trims the specified string of the specified characters, removing all occurrences of this character at the end of the string.
* @param s The string to trim.
* @param c The character to remove from the end of the string.
* @returns The trimmed string.
*/
function trimEnd(s, c) {
c = reservedChars.has(c) ? `\\${c}` : c;
return s.replace(new RegExp(`[${c}]+$`, "g"), "");
}
exports.trimEnd = trimEnd;
/**
* Trims the specified string of the specified characters, removing all occurrences of this character at the start of the string.
* @param s The string to trim.
* @param c The character to remove from the start of the string.
* @returns The trimmed string.
*/
function trimStart(s, c) {
c = reservedChars.has(c) ? `\\${c}` : c;
return s.replace(new RegExp(`^[${c}]+`, "g"), "");
}
exports.trimStart = trimStart;
const accents = new Map([
["à", "a"],
["â", "a"],
["ç", "c"],
["è", "e"],
["é", "e"],
["ê", "e"],
["ë", "e"],
["î", "i"],
["ï", "i"],
["ô", "o"],
["ù", "u"],
["û", "u"],
["ü", "u"],
]);
/**
* Replaces accent characters in the specified string by their ASCII character representation. For example, `é` becomes `e`.
* @param s The string to remove the accents.
* @returns The string without accents.
*/
function unaccent(s) {
return [...s].map((c) => { var _a, _b; return (c.toUpperCase() === c ? ((_a = accents.get(c)) !== null && _a !== void 0 ? _a : c).toUpperCase() : (_b = accents.get(c)) !== null && _b !== void 0 ? _b : c); }).join("");
}
exports.unaccent = unaccent;