@akram6t/string-utils-js
v2.1.5
Published
A lightweight collection of essential string utilities for Node.js and browser environments.
Downloads
4
Readme
String Utils JS
A lightweight collection of essential string utilities for Node.js and browser environments.
Installation
Install using npm:
npm install @akram6t/string-utils-jsOr using yarn:
yarn add @akram6t/string-utils-jsUsage
Here are the most commonly used utility functions,
1. String Manipulation
console.log(capitalize('hello world')); // "Hello world"
console.log(capitalizeWords('hello world')); // "Hello World"
console.log(truncate('This is a long string', 10)); // "This is a..."
console.log(camelCase('hello world')); // "helloWorld"
console.log(reverseString('hello')); // "olleh"
console.log(kebabCase('Hello World')); // "hello-world"
console.log(maskEmail('[email protected]')); // "u***@example.com"
console.log(removeWhitespace(' hello world ')); // "helloworld"
console.log(wordCount('Four words here')); // 4
console.log(isPalindrome('racecar')); // true2. Array Operations
console.log(removeDuplicates([1, 2, 2, 3])); // [1, 2, 3]
console.log(flattenArray([1, [2, [3]]])); // [1, 2, 3]
console.log(chunkArray([1, 2, 3, 4], 2)); // [[1, 2], [3, 4]]
console.log(shuffleArray([1, 2, 3, 4])); // [3, 1, 4, 2] (randomized)
console.log(intersection([1, 2, 3], [2, 3, 4])); // [2, 3]
console.log(sortByKey([{id: 2}, {id: 1}], 'id')); // [{id: 1}, {id: 2}]
console.log(groupBy(['apple', 'banana', 'avocado'], 'length')); // {5: ['apple'], 6: ['banana', 'avocado']}
console.log(countOccurrences(['a', 'b', 'a'])); // {a: 2, b: 1}
console.log(findMax([1, 5, 3])); // 5
console.log(findMin([1, 5, 3])); // 13. Number/Date Formatting
console.log(formatNumber(1234567)); // "1,234,567"
console.log(roundTo(3.14159, 2)); // 3.14
console.log(calculatePercentage(50, 200)); // 25
console.log(formatDate(new Date(), 'YYYY-MM-DD')); // "2023-05-15"
console.log(timeAgo(new Date(2023, 0, 1))); // "5 months ago"
console.log(daysBetween(new Date(2023, 0, 1), new Date(2023, 0, 10))); // 9
console.log(addDays(new Date(), 7)); // Date object (7 days later)
console.log(isLeapYear(2024)); // true
console.log(calculateAge('1990-01-01')); // 33
console.log(secondsToHMS(3661)); // "1:01:01"4. Object Manipulation
console.log(deepClone({a: 1, b: {c: 2}})); // {a: 1, b: {c: 2}} (new copy)
console.log(mergeObjects({a: 1}, {b: 2})); // {a: 1, b: 2}
console.log(pick({a: 1, b: 2, c: 3}, ['a', 'b'])); // {a: 1, b: 2}
console.log(omit({a: 1, b: 2, c: 3}, ['a'])); // {b: 2, c: 3}
console.log(isEmptyObject({})); // true
console.log(objectToArray({a: 1, b: 2})); // [['a', 1], ['b', 2]]
console.log(filterObject({a: 1, b: 2}, val => val > 1)); // {b: 2}
console.log(mapObject({a: 1, b: 2}, val => val * 2)); // {a: 2, b: 4}
console.log(invertObject({a: 1, b: 2})); // {1: 'a', 2: 'b'}
console.log(deepEqual({a: 1}, {a: 1})); // true5. Validation & Checks
console.log(isEmail('[email protected]')); // true
console.log(isPhone('+1234567890')); // true
console.log(isURL('https://example.com')); // true
console.log(isCreditCard('4111111111111111')); // true
console.log(isStrongPassword('Password123!')); // true
console.log(isHexColor('#ff0000')); // true
console.log(isUUID('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11')); // true
console.log(isAlphaNumeric('abc123')); // true
console.log(isDateString('2023-05-15')); // true
console.log(isEmptyString(' ')); // true6. Browser/DOM Utilities
console.log(getQueryParam('https://example.com?page=1', 'page')); // "1"
console.log(setQueryParam('https://example.com', 'page', '1')); // "https://example.com?page=1"
console.log(getCookie('session_id')); // "abc123"
console.log(detectDevice()); // "mobile" | "desktop" | "tablet"
console.log(isTouchDevice()); // true/false
console.log(serializeForm(document.getElementById('form'))); // "name=John&age=30"
console.log(deserialize('name=John&age=30')); // {name: "John", age: "30"}
console.log(copyToClipboard('text')); // true (if successful)
console.log(scrollToTop()); // Smooth scroll to top
console.log(toggleFullscreen()); // Toggles fullscreen mode7. Math/Financial
console.log(randomInt(1, 10)); // 7 (random between 1-10)
console.log(calculateTip(50, 15)); // 7.5 (15% tip)
console.log(compoundInterest(1000, 0.05, 5)); // 1276.28
console.log(calculateVAT(100, 0.2)); // 20
console.log(calculateLoanPayment(10000, 0.05, 5)); // 188.71
console.log(calculateBMI(70, 1.75)); // 22.86
console.log(calculateDistance(40.7128, -74.0060, 34.0522, -118.2437)); // 3935.75 (km)
console.log(factorial(5)); // 120
console.log(fibonacci(10)); // 55
console.log(clamp(15, 0, 10)); // 10 (forces value within range)8. File/Data Utilities
console.log(formatBytes(1024)); // "1 KB"
console.log(getFileExtension('image.jpg')); // "jpg"
console.log(csvToJSON('name,age\nJohn,30')); // [{name: "John", age: "30"}]
console.log(jsonToCSV([{name: "John", age: 30}])); // "name,age\nJohn,30"
console.log(base64Encode('hello')); // "aGVsbG8="
console.log(base64Decode('aGVsbG8=')); // "hello"
console.log(generateHash('password')); // "5f4dcc3b5aa765d61d8327deb882cf99"
console.log(generateUUID()); // "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
console.log(compressString('aaaabbbcc')); // "a4b3c2"
console.log(isImageFile('photo.png')); // true9. Async/Performance
console.log(sleep(1000)); // Promise (resolves after 1 second)
console.log(debounce(() => console.log('Resized'), 300)); // Function (delayed)
console.log(throttle(() => console.log('Scrolling'), 300)); // Function (rate-limited)
console.log(retry(fetchData, 3)); // Retries function 3 times
console.log(memoize(fibonacci)); // Cached function
console.log(parallelLimit([fetch1, fetch2], 2)); // Runs 2 promises at once
console.log(timeoutPromise(fetchData, 5000)); // Rejects after 5s
console.log(poll(checkStatus, 1000, 10)); // Checks every 1s (max 10 tries)
console.log(sequence([fetch1, fetch2])); // Runs promises in orderThese cover 90% of real-world use cases. Let me know if you'd like to focus on a specific category!
