UNPKG

@sapphire/utilities

Version:
32 lines (29 loc) 1.08 kB
'use strict'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/lib/cutText.ts var WordSeparatorCharacter = /[\p{Separator}\p{Punctuation}\p{Control}]/u; function cutText(str, length) { if (str.length <= length) return str; const codepoints = [...str]; if (codepoints.length <= length) return str; let lastSeparator = length; for (let i = 0; i < length; ++i) { if (WordSeparatorCharacter.test(codepoints[i])) { lastSeparator = i; } } const lastCharacterIndex = lastSeparator === length ? length - 1 : lastSeparator; return codepoints.slice(0, lastCharacterIndex).concat("\u2026").join(""); } __name(cutText, "cutText"); /** * Split a text by its latest space character in a range from the character 0 to the selected one. * @param str The text to split. * @param length The length of the desired string. * @copyright 2019 Aura Román * @license Apache-2.0 */ exports.cutText = cutText; //# sourceMappingURL=cutText.cjs.map //# sourceMappingURL=cutText.cjs.map