UNPKG

@c_s_v_s_subrahmanyam/string-utils

Version:

A comprehensive utility library for advanced string operations.

72 lines (52 loc) 2.96 kB
// Update 1 - core functionality const stringUtils = require('./src/index'); console.log(stringUtils.isEmail('test@example.com')); // true console.log(stringUtils.reverse('hello')); // olleh console.log(stringUtils.passwordStrength('P@ssw0rd123')); // Strong console.log(stringUtils.getLength('hello world')); // 11 console.log(stringUtils.startsWith('hello', 'he')); // true //Update 2 // Test Unicode Operations console.log(stringUtils.toUnicode('hello')); // \u0068\u0065\u006c\u006c\u006f console.log(stringUtils.fromUnicode('\\u0068\\u0065\\u006c\\u006c\\u006f')); // hello // Test Encoding and Decoding console.log(stringUtils.encodeBase64('hello')); // aGVsbG8= console.log(stringUtils.decodeBase64('aGVsbG8=')); // hello // Test Regex Utilities console.log(stringUtils.extractEmails('Contact us at support@example.com.')); // [ 'support@example.com' ] console.log(stringUtils.extractURLs('Visit https://example.com now!')); // [ 'https://example.com' ] // Test Repetition and Captcha console.log(stringUtils.repeatString('ha', 3)); // hahaha console.log(stringUtils.generateCaptcha(8)); // Random 8-character captcha console.log(stringUtils.sortCharacters('dcba')); // abcd console.log(stringUtils.sortWords('banana apple cherry')); // apple banana cherry console.log(stringUtils.uniqueCharacters('aabbcc')); // abc // Test Partitioning console.log(stringUtils.partition('hello world', ' ')); // ['hello', ' ', 'world'] console.log(stringUtils.rpartition('hello world hello', 'hello')); // ['hello world ', 'hello', ''] // Test Prefix/Suffix Operations console.log(stringUtils.removePrefix('unhappy', 'un')); // happy console.log(stringUtils.removeSuffix('baking', 'ing')); // bak // Test Case Folding console.log(stringUtils.caseFold('Straße')); // straße console.log(stringUtils.normalize('\u1E9B\u0323', 'NFC')); // ẛ̣ // Update 3 console.log(stringUtils.translate('hello', { h: 'H', e: '3', l: '1', o: '0' })); // H3110 // Test Expand Tabs console.log(stringUtils.expandTabs('Hello\tWorld', 8)); // Hello World // Test Character Iteration console.log(stringUtils.iterateCharacters('abc')); // [ 'a', 'b', 'c' ] // Test Indexing and Slicing console.log(stringUtils.sliceString('hello', 1, 4)); // ell console.log(stringUtils.getIndex('hello', 2)); // l console.log(stringUtils.splitAt('hello', 2)); // [ 'he', 'llo' ] // Test Multibyte Encoding const bytes = stringUtils.encodeToBytes('hello'); console.log(bytes); // Uint8Array([...]) console.log(stringUtils.decodeFromBytes(bytes)); // hello // Test Comparison Utilities console.log(stringUtils.equalsIgnoreCase('Hello', 'hello')); // true console.log(stringUtils.compareLexicographically('abc', 'abd')); // -1 console.log(stringUtils.startsWith('hello', 'he')); // true console.log(stringUtils.endsWith('hello', 'lo')); // true console.log(stringUtils.containsSubstring('hello world', 'world'));