UNPKG

cerceis-lib

Version:

Contains list of quality of life functions that is written in TypeScript and es6

79 lines 2.42 kB
// src/string/index.ts var FromString = { /** * Copy string to clipboard. * Only works in browser environments — silently does nothing in Node.js. * @param textToCopy Text to copy. */ copyToClipboard(textToCopy) { if (typeof navigator === "undefined") return; navigator.clipboard.writeText(textToCopy); }, /** * Replace the first N characters with the given string. * Does not mutate — returns a new string. * @param n First N characters to replace (starts from 1) * @param w Replacement string (must have length === n) * @param str Input string */ replaceFirst(n, w, str) { try { if (n !== w.length) throw "Length of replacement and target slot must be equal."; return `${w}${str.slice(n)}`; } catch (err) { console.log(`FromString.replaceFirst: ${err}`); return ""; } }, /** * Replace the last N characters with the given string. * Does not mutate — returns a new string. * @param n Last N characters to replace (starts from 1) * @param w Replacement string (must have length === n) * @param str Input string */ replaceLast(n, w, str) { try { if (n !== w.length) throw "Length of replacement and target slot must be equal."; return `${str.slice(0, str.length - n)}${w}`; } catch (err) { console.log(`FromString.replaceLast: ${err}`); return ""; } }, /** * Parse an HTTP cookie string into a key/value object. * @param cookie Raw cookie string */ parseCookies(cookie) { const list = {}; cookie.split(";").forEach((part) => { const parts = part.split("="); const key = parts.shift()?.trim(); if (key) list[key] = decodeURI(parts.join("=")); }); return list; }, /** * Remove all whitespace, symbols, and special characters. * Leaves only 0–9 and a–Z. * @param str Input string */ deepClean(str) { return str.trim().replace(/[^0-9a-zA-Z]/g, ""); }, /** * Count occurrences of a word within a string. * @param str Input string. * @param wordToCount Word to count. * @param isolated Only count when the word appears as a standalone word (space-bounded). */ count(str, wordToCount, isolated = false) { if (isolated) return str.split(` ${wordToCount} `).length - 1; return str.split(wordToCount).length - 1; } }; export { FromString }; //# sourceMappingURL=index.mjs.map