UNPKG

petcarescript

Version:

PetCareScript - A modern, expressive programming language designed for humans

308 lines (258 loc) 10.1 kB
/** * PetCareScript Standard Library - String * String manipulation functions - VERSÃO COMPLETA E CORRIGIDA */ const StringLib = { // Basic manipulation upper: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.toUpperCase(); }, lower: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.toLowerCase(); }, capitalize: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); }, // Case transformations camelCase: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.replace(/[-_\s]+(.)?/g, (match, chr) => { return chr ? chr.toUpperCase() : ''; }); }, kebabCase: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); }, snakeCase: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase(); }, pascalCase: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.replace(/[-_\s]+(.)?/g, (match, chr) => { return chr ? chr.toUpperCase() : ''; }).replace(/^(.)/, (match, chr) => chr.toUpperCase()); }, // Search and verification contains: (str, substring) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.includes(substring); }, startsWith: (str, prefix) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.startsWith(prefix); }, endsWith: (str, suffix) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.endsWith(suffix); }, indexOf: (str, substring) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.indexOf(substring); }, lastIndexOf: (str, substring) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.lastIndexOf(substring); }, // Cleaning and formatting trim: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.trim(); }, trimStart: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.trimStart(); }, trimEnd: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.trimEnd(); }, // Replacement replace: (str, search, replace) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.replace(search, replace); }, replaceAll: (str, search, replace) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.replaceAll(search, replace); }, // Split and join split: (str, separator = '') => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.split(separator); }, // Repetition and padding repeat: (str, times) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); if (typeof times !== 'number') throw new Error('Second argument must be a number'); return str.repeat(times); }, padStart: (str, length, padding = ' ') => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.padStart(length, padding); }, padEnd: (str, length, padding = ' ') => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.padEnd(length, padding); }, // Extraction slice: (str, start, end = str.length) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.slice(start, end); }, substring: (str, start, end = str.length) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.substring(start, end); }, charAt: (str, index) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.charAt(index); }, charCodeAt: (str, index) => { if (typeof str !== 'string') throw new Error('First argument must be a string'); return str.charCodeAt(index); }, // Utility functions length: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.length; }, // Checks - CORRIGIDO isEmpty para lidar com null isEmpty: (str) => { if (str === null || str === undefined) return true; if (typeof str !== 'string') return false; return str.length === 0; }, isWhitespace: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return /^\s*$/.test(str); }, isNumeric: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return !isNaN(str) && !isNaN(parseFloat(str)); }, isAlpha: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return /^[a-zA-Z]+$/.test(str); }, isAlphaNumeric: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return /^[a-zA-Z0-9]+$/.test(str); }, isEmail: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(str); }, isUrl: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); try { new URL(str); return true; } catch { return false; } }, // Special formatting reverse: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.split('').reverse().join(''); }, shuffle: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); const array = str.split(''); for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array.join(''); }, truncate: (str, length, suffix = '...') => { if (typeof str !== 'string') throw new Error('First argument must be a string'); if (str.length <= length) return str; return str.slice(0, length) + suffix; }, wordCount: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.trim().split(/\s+/).filter(word => word.length > 0).length; }, lineCount: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.split('\n').length; }, // Escape and unescape escapeHTML: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); const map = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }; return str.replace(/[&<>"']/g, (m) => map[m]); }, unescapeHTML: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); const map = { '&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"', '&#39;': "'" }; return str.replace(/&(amp|lt|gt|quot|#39);/g, (m) => map[m]); }, escapeRegex: (str) => { if (typeof str !== 'string') throw new Error('Argument must be a string'); return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }, // Simple template formatting format: (template, ...args) => { if (typeof template !== 'string') throw new Error('First argument must be a string'); return template.replace(/{(\d+)}/g, (match, number) => { return typeof args[number] !== 'undefined' ? args[number] : match; }); }, // String comparison compare: (str1, str2) => { if (typeof str1 !== 'string' || typeof str2 !== 'string') { throw new Error('Both arguments must be strings'); } if (str1 < str2) return -1; if (str1 > str2) return 1; return 0; }, // Distance/similarity functions levenshteinDistance: (str1, str2) => { if (typeof str1 !== 'string' || typeof str2 !== 'string') { throw new Error('Both arguments must be strings'); } const matrix = []; for (let i = 0; i <= str2.length; i++) { matrix[i] = [i]; } for (let j = 0; j <= str1.length; j++) { matrix[0][j] = j; } for (let i = 1; i <= str2.length; i++) { for (let j = 1; j <= str1.length; j++) { if (str2.charAt(i - 1) === str1.charAt(j - 1)) { matrix[i][j] = matrix[i - 1][j - 1]; } else { matrix[i][j] = Math.min( matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1 ); } } } return matrix[str2.length][str1.length]; } }; module.exports = StringLib;