Install common-utils-pkg
Install the
common-utils-pkg library using npm or Yarn.# Using NPM
npm i --save common-utils-pkg
# Using Yarn
yarn add common-utils-pkgwebsite·Indexed 30 days ago
https://common-utils-pkg.js.orgA package of commonly used JavaScript utilities providing functions for color manipulation (invertColor, colorBrightness, randomPastelColor), string formatting (slugify, toSentenceCase, toTitleCase), storage management (storageSet, storageGet, storageRemove, storageClear), and general helpers like guid, safeJsonParse, and browserDetect.
common-utils-pkg library using npm or Yarn.# Using NPM
npm i --save common-utils-pkg
# Using Yarn
yarn add common-utils-pkgarrayToString and stringToArray have been removed. Developers should use the native JavaScript methods Array.prototype.join and String.prototype.split respectively.window['common-utils-pkg'] global object.<script src="https://unpkg.com/common-utils-pkg"></script>
<script>
// Access specific methods
const randomPastelColor = window['common-utils-pkg'].randomPastelColor;
randomPastelColor();
// Access all methods
const UTILS = window['common-utils-pkg'];
UTILS.randomPastelColor();
</script>// Import specific methods
import { randomPastelColor } from 'common-utils-pkg';
randomPastelColor();
// Import all methods
import * as UTILS from 'common-utils-pkg';
UTILS.randomPastelColor();browserDetect() method (introduced in v3.1.0) is used to detect the browser environment.readableColor utility (introduced in v4.3.0) provides the appropriate light or dark mode color to ensure text readability against a given background.storageSet function allows you to save data to the browser's local storage. It requires a unique key to identify the entry and the data value to be stored.storageSet('key', data);invertColor function takes a HEX color string and returns its inverted HEX color equivalent. It supports both full 6-digit HEX codes and shorthand 3-digit HEX codes.// Invert a full 6-digit HEX color
invertColor('#E3CCA8');
// => '#1C3357'
// Invert a shorthand 3-digit HEX color
invertColor('ccc');
// => '#333333'slugify function converts a provided string into a slug suitable for URL usage. It handles case normalization (converting to lowercase), removes special characters, and replaces spaces/punctuation with hyphens. It also supports normalizing special characters (e.g., 'ï' to 'i').slugify('The quick brown fox');
// => 'the-quick-brown-fox'
slugify('A. more-Complicated string!..');
// => 'a-more-complicated-string'
slugify('wïth speciäl chærs');
// => 'with-special-chars'readableBytes function accepts an optional options object as the second parameter. This allows developers to specify the minimum unit and the number of decimal places for the formatted output.readableBytes(number, { /* options for decimal and minimum unit */ });readableBytes function converts a number of bytes into a human-readable string (e.g., '1.21 kB'). It accepts the number of bytes and an optional options object to control precision and the minimum unit displayed.readableBytes(1234);
// => "1.205078125 kB"
readableBytes(1234, { decimals: 2 });
// => "1.21 kB"
readableBytes(500, { minUnit: 'kB' });
// => "0.48828125 kB"
readableBytes(500, { decimals: 2, minUnit: 'kB' });
// => "0.49 kB"colorBrightness function brightens or darkens a HEX color based on a specified percentage, behaving similarly to SASS lighten and darken functions. The returned value is always a string starting with the # symbol.// Brighten a color by 50%
colorBrightness('#3CB878', 50);
// => '#6EEAAA'
// Darken a color by 50%
colorBrightness('#3CB878', -50);
// => '#A8646'