UNPKG

@oxog/string

Version:

Comprehensive string manipulation utilities with zero dependencies

86 lines (85 loc) 2.75 kB
export class StringCoreImpl { constructor() { this.plugins = new Map(); this.extensions = new Map(); } use(plugin) { if (this.plugins.has(plugin.name)) { throw new Error(`Plugin ${plugin.name} is already installed`); } // Install plugin first, only add to registry if successful plugin.install(this); this.plugins.set(plugin.name, plugin); } extend(name, fn) { if (this.extensions.has(name)) { throw new Error(`Extension ${name} already exists`); } this.extensions.set(name, fn); } getExtension(name) { return this.extensions.get(name); } getPlugin(name) { return this.plugins.get(name); } listPlugins() { return Array.from(this.plugins.keys()); } listExtensions() { return Array.from(this.extensions.keys()); } } export function createPlugin(name, version, installer) { return { name, version, install: installer }; } // Example plugin implementations export const localePlugin = createPlugin('locale', '1.0.0', (core) => { core.extend('toLocaleLowerCase', (str, locale) => { return str.toLocaleLowerCase(locale); }); core.extend('toLocaleUpperCase', (str, locale) => { return str.toLocaleUpperCase(locale); }); core.extend('localeCompare', (str1, str2, locale) => { return str1.localeCompare(str2, locale); }); }); export const colorPlugin = createPlugin('color', '1.0.0', (core) => { core.extend('colorize', (str, color) => { const colors = { red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m', magenta: '\x1b[35m', cyan: '\x1b[36m', white: '\x1b[37m', reset: '\x1b[0m' }; return `${colors[color] || ''}${str}${colors['reset']}`; }); core.extend('stripColors', (str) => { return str.replace(/\x1b\[[0-9;]*m/g, ''); }); }); export const mathPlugin = createPlugin('math', '1.0.0', (core) => { core.extend('extractNumbers', (str) => { var _a; return ((_a = str.match(/-?\d+(?:\.\d+)?/g)) === null || _a === void 0 ? void 0 : _a.map(Number)) || []; }); core.extend('sumNumbers', (str) => { var _a; const numbers = ((_a = str.match(/-?\d+(?:\.\d+)?/g)) === null || _a === void 0 ? void 0 : _a.map(Number)) || []; return numbers.reduce((sum, num) => sum + num, 0); }); core.extend('replaceNumbers', (str, replacer) => { return str.replace(/-?\d+(?:\.\d+)?/g, (match) => { return replacer(parseFloat(match)); }); }); });