UNPKG

cerceis-lib

Version:

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

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