A small, useful utility collection for JavaScript/ESM projects — includes string helpers, DOM utilities, and a simple localStorage-based caching system.
npm install @apsonex/js-utilsOr using Yarn:
yarn add @apsonex/js-utilsimport {
JsCache,
str,
bodyScrollEnable,
bodyScrollDisable,
isIframe,
loadScript,
loadStyle,
} from '@apsonex/js-utils';Powerful string helper chainable class.
str("hello world").kebab().toString(); // "hello-world"
str("some/filename.txt").afterLast("/").toString(); // "filename.txt"
str("html content").minifyHtml(); // removes whitespace, commentsChainable methods:
after,afterLast,before,beforeLastkebab,camel,snake,slug,plural,singularreplaceFirst,replaceLast,replaceArraylimit,words,start,finishcontains,containsAll,is,startsWith,endsWithtitle,minifyHtml,explode
Simple localStorage cache with TTL (time to live) support.
const cache = new JsCache().init({ prefix: 'my_app:' });
cache.put('user', { name: 'John' }, '10m');
cache.remember('settings', '1hr', () => fetchSettings());Supports TTL formats:
60s,10m,1hr,1d,1mo,1yr, or numeric seconds
Methods:
put(key, value, ttl)remember(key, ttl, fallback)get(key)has(key)forget(key)
Lightweight, useful browser DOM helpers.
bodyScrollDisable(); // disables body scroll
bodyScrollEnable(); // enables it back
isIframe(); // true if running inside iframeDynamically load external scripts or stylesheets.
await loadScript('https://example.com/script.js', {
type: 'module',
onLoad: () => console.log('loaded'),
onError: (err) => console.error(err),
});
await loadStyle('https://example.com/style.css', {
media: 'all',
});They support:
- Auto-skip if already loaded
onLoad,onErrorcallbacks- Custom attributes like
crossorigin,media,title
npm run buildOutputs ES module and UMD builds in /dist.
MIT License © Apsonex Inc.