@peterbenoit/utility-helpers
v1.0.0
Published
A comprehensive collection of JavaScript utility functions for image processing, array manipulation, string operations, and more.
Downloads
34
Maintainers
Readme
UtilityHelpers
A comprehensive collection of JavaScript utility functions for image processing, array manipulation, string operations, object utilities, date helpers, validation, DOM utilities, and more.
Author: Peter Benoit License: MIT GitHub: peterbenoit/UtilityHelpers
CDN Usage
jsDelivr (recommended)
<!-- Pinned to a specific version -->
<script src="https://cdn.jsdelivr.net/npm/@peterbenoit/[email protected]/utilityHelpers.js"></script>
<!-- Always latest -->
<script src="https://cdn.jsdelivr.net/npm/@peterbenoit/utility-helpers/utilityHelpers.js"></script>unpkg
<!-- Pinned to a specific version -->
<script src="https://unpkg.com/@peterbenoit/[email protected]/utilityHelpers.js"></script>
<!-- Always latest -->
<script src="https://unpkg.com/@peterbenoit/utility-helpers/utilityHelpers.js"></script>After including the script, UtilityHelpers is available as a global class:
const uuid = UtilityHelpers.generateUUID();
const chunks = UtilityHelpers.chunk([1, 2, 3, 4], 2);
const hex = UtilityHelpers.rgbToHex(255, 0, 0);npm / Node.js
npm install @peterbenoit/utility-helpersconst UtilityHelpers = require('@peterbenoit/utility-helpers');
const uuid = UtilityHelpers.generateUUID();Documentation
Full API documentation with live examples is available at the project homepage.
Methods
Image Utilities
getImageDetails(imageSrc, calculateAverageColor?)— width, height, size, format, orientationgetImageOrientation(image)—'landscape'|'portrait'|'square'getImageFormat(mimeType)— human-readable format name from MIME typegetAverageImageColor(imageSrc)— average rgb color of an imagegetImageBase64(imageSrc, options?)— base64-encoded data URLgetLuminance(color)— relative luminance valuegetContrastRatio(color1, color2)— WCAG contrast ratioformatBytes(bytes, decimals?)— human-readable file size
Array Utilities
chunk(array, size)— split into chunksshuffleArray(array)— random shuffle (non-mutating)flattenArray(arr)— deep flattensumArray(arr)— sum of valuescountUniqueElements(arr)— count of distinct valuesfindDuplicates(arr)— array of duplicate valuescountOccurrences(arr, val)— count of a specific valuegetRandomElement(arr)— random elementgetMedian(arr)— median valuerange(start, end, step?)— numeric range arraygroupBy(array, key)— group by key or function
String Utilities
capitalizeWords(str)— title casepluralize(word, count)— smart pluralizationescapeHtml(str)/unescapeHtml(str)— HTML entity handlingrandomString(length)— random alphanumeric stringnumberToWords(num)— number to English words (supports negatives)escapeRegex(str)— escape special regex characterscamelCase(str)/kebabCase(str)/snakeCase(str)— case conversiontruncate(str, length?)— truncate with ellipsisstripHtml(str)— remove HTML tags
Object Utilities
pick(object, keys)— select keysomit(object, keys)— exclude keysisEqual(a, b)— deep equality checkdeepMerge(target, source)— deep immutable mergegetType(value)—'array'|'object'| typeofisObjectEmpty(obj)— empty object check
Date & Time
randomDateBetweenTwo(start, end)— random date in rangetimeStampID()— unique timestamp-based ID
General Utilities
generateUUID()— UUID v4rgbToHex(r, g, b)— RGB to hexlimitDecimalPlaces(num, places)— precision controlgenerateRandomColor()— random hex colorgetQueryParams(url)— parse URL query stringgetURLParts(url)— parse URL componentsshortenURL(url)/expandURL(encoded)— base64-url encode/decodelevenshteinDistance(str1, str2)— edit distanceclamp(value, min, max)— clamp numberparseJSON(str, fallback?)— safe JSON parseformatNumber(num, locale?)— locale-formatted numberformatCurrency(amount, currency?, locale?)— locale-formatted currencygetFileExtension(filename)— file extension
Performance & Optimization
debounce(fn, delay)— delay execution until quietthrottle(fn, limit)— limit execution ratememoize(fn)— cache resultswait(ms)/sleep(ms)— async delayretry(fn, attempts?, delay?)— retry with exponential backoffonce(fn)— execute only oncepipe(...fns)/compose(...fns)— function composition
Validation & Sanitization
isValidEmail(email)— email format checkisValidURL(url)— URL format checkvalidateAndSanitizeInput(inputElement, type, options?)— form input validation
DOM & UI
copyToClipboard(text)— clipboard writesmoothScrollTo(selector)— smooth scrolltoggleClassAfterDelay(element, className, delay)— delayed class toggledetectUserIdleTime(callback, idleTime?)— idle detection (returns cancel fn)getFormData(formElement)— form data as objectdownloadFile(content, filename, mimeType?)— trigger downloadtoggleFullscreen(element?)— fullscreen togglelazyLoadImages(selector?)— intersection observer lazy loadgetCSSVariable(name, element?)/setCSSVariable(name, value, element?)— CSS custom propertiessaveScrollPosition()/restoreScrollPosition()— scroll position persistencetextToSpeech(text, options?)/getVoices()— speech synthesisdebouncedInput(inputElement, callback, delay?)— debounced input handlerrunInParallel(tasks)—Promise.allwrapper
Release Steps
# 1. Bump version
npm version patch # or minor / major
# 2. Verify what will be published
npm run pack:dry
# 3. Publish to npm
npm publish --access public
# 4. Push tag to GitHub
git push && git push --tagsLicense
MIT © Peter Benoit
