UNPKG

cerceis-lib

Version:

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

104 lines (103 loc) 3.43 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/string/index.ts var string_exports = {}; __export(string_exports, { FromString: () => FromString }); module.exports = __toCommonJS(string_exports); 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; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FromString }); //# sourceMappingURL=index.js.map