UNPKG

fortify2-js

Version:

MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.

233 lines (230 loc) 6.82 kB
import { DEFAULT_SPLIT_OPTIONS, DEFAULT_SEARCH_OPTIONS } from '../types/index.js'; /** * String Operations Module * Handles string manipulation operations for SecureString */ /** * Handles string manipulation operations */ class StringOperations { /** * Appends a value to a string */ static append(current, value) { return current + value; } /** * Prepends a value to a string */ static prepend(current, value) { return value + current; } /** * Replaces the entire string with a new value */ static replace(_current, newValue) { return newValue; } /** * Extracts a substring */ static substring(current, start, end) { return current.substring(start, end); } /** * Splits a string into parts */ static split(current, separator, options = {}) { const opts = { ...DEFAULT_SPLIT_OPTIONS, ...options }; let parts = current.split(separator, opts.limit > 0 ? opts.limit : undefined); if (opts.removeEmpty) { parts = parts.filter((part) => part.length > 0); } if (opts.trim) { parts = parts.map((part) => part.trim()); } return parts; } /** * Trims whitespace from both ends */ static trim(current) { return current.trim(); } /** * Trims whitespace from the start */ static trimStart(current) { return current.trimStart(); } /** * Trims whitespace from the end */ static trimEnd(current) { return current.trimEnd(); } /** * Converts to uppercase */ static toUpperCase(current) { return current.toUpperCase(); } /** * Converts to lowercase */ static toLowerCase(current) { return current.toLowerCase(); } /** * Converts to title case */ static toTitleCase(current) { return current.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase()); } /** * Reverses the string */ static reverse(current) { return current.split("").reverse().join(""); } /** * Repeats the string n times */ static repeat(current, count) { if (count < 0) { throw new Error("Repeat count must be non-negative"); } return current.repeat(count); } /** * Pads the string to a target length */ static padStart(current, targetLength, padString = " ") { return current.padStart(targetLength, padString); } /** * Pads the string to a target length at the end */ static padEnd(current, targetLength, padString = " ") { return current.padEnd(targetLength, padString); } /** * Checks if string contains a substring */ static includes(current, searchString, options = {}) { const opts = { ...DEFAULT_SEARCH_OPTIONS, ...options }; let haystack = current; let needle = searchString; if (!opts.caseSensitive) { haystack = haystack.toLowerCase(); needle = needle.toLowerCase(); } const startPos = Math.max(0, opts.startPosition); const endPos = opts.endPosition > 0 ? opts.endPosition : haystack.length; const searchArea = haystack.substring(startPos, endPos); if (opts.wholeWord) { const wordRegex = new RegExp(`\\b${this.escapeRegExp(needle)}\\b`, opts.caseSensitive ? "g" : "gi"); return wordRegex.test(searchArea); } return searchArea.includes(needle); } /** * Checks if string starts with a prefix */ static startsWith(current, searchString, options = {}) { const opts = { ...DEFAULT_SEARCH_OPTIONS, ...options }; let haystack = current; let needle = searchString; if (!opts.caseSensitive) { haystack = haystack.toLowerCase(); needle = needle.toLowerCase(); } const startPos = Math.max(0, opts.startPosition); return haystack.startsWith(needle, startPos); } /** * Checks if string ends with a suffix */ static endsWith(current, searchString, options = {}) { const opts = { ...DEFAULT_SEARCH_OPTIONS, ...options }; let haystack = current; let needle = searchString; if (!opts.caseSensitive) { haystack = haystack.toLowerCase(); needle = needle.toLowerCase(); } const length = opts.endPosition > 0 ? opts.endPosition : undefined; return haystack.endsWith(needle, length); } /** * Finds the index of a substring */ static indexOf(current, searchString, options = {}) { const opts = { ...DEFAULT_SEARCH_OPTIONS, ...options }; let haystack = current; let needle = searchString; if (!opts.caseSensitive) { haystack = haystack.toLowerCase(); needle = needle.toLowerCase(); } const startPos = Math.max(0, opts.startPosition); return haystack.indexOf(needle, startPos); } /** * Finds the last index of a substring */ static lastIndexOf(current, searchString, options = {}) { const opts = { ...DEFAULT_SEARCH_OPTIONS, ...options }; let haystack = current; let needle = searchString; if (!opts.caseSensitive) { haystack = haystack.toLowerCase(); needle = needle.toLowerCase(); } const startPos = opts.startPosition > 0 ? opts.startPosition : undefined; return haystack.lastIndexOf(needle, startPos); } /** * Replaces occurrences of a substring */ static replaceAll(current, searchValue, replaceValue) { if (typeof searchValue === "string") { return current.split(searchValue).join(replaceValue); } else { return current.replace(searchValue, replaceValue); } } /** * Normalizes the string */ static normalize(current, form = "NFC") { return current.normalize(form); } /** * Escapes special regex characters */ static escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } /** * Gets character at specific index */ static charAt(current, index) { return current.charAt(index); } /** * Gets character code at specific index */ static charCodeAt(current, index) { return current.charCodeAt(index); } /** * Slices the string */ static slice(current, start, end) { return current.slice(start, end); } } export { StringOperations }; //# sourceMappingURL=string-operations.js.map