typescript | javascript utils
Function for formatting a number.
Based by Intl.NumberFormat.
Expect settings object.
const enFormat = number.formatFactory({
lang: 'en-US',
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
console.log(enFormat('123, 123')) // returns 123.23
Function for wait time in Promise.
Expect milliseconds.
async function myBeautyLoadingAnimation () {
let loading = '.';
await async.wait(500);
loading = '..';
await async.wait(500);
loading = '...';
}
Function for debounce.
Expect object argument:
- callback - function, which be called with debounce;
- timeout - number of milliseconds after which function will be called, updates after interact with function;
- initialInvoke - call callback if timer is not started;
const search = async.debounce({
callback: (event: Event) => {
searchFetch(`https://api.enpoint/search?query=${event.target.value}`);
},
timeout: 1000
});
searchInput.oninput = search;
Function for throttling.
Expect object argument:
- callback - function, which be called with throttle;
- timeout - number of milliseconds in which only one time callback will be called;
const myMouseWatcher = async.throttle({
callback: (event: Event) => {
sendMouseStats(Event);
},
timeout: 300
});
document.body.onmousemove = myMouseWatcher;
Function for interval with additional functionality.
Expect arguments:
- callback - function, which be called with interval;
- timeout - number of milliseconds;
- settings object:
- times (number | false | undefined) - number of times, after which the interval will be clear;
- initialInvoke (boolean) - run callback after interval initialized;
setCustomInterval(() => {
console.log("Initial");
}, 1000, {
initialInvoke: true,
times: 4
});
Function for fast date formatting;
Expect:
- timestamp;
- options:
- time - show time;
- locales - Intl.LocalesArgument
date.convert(new Date()) // return formatted date // 01/01/2024;
Function for date validation.
Expect date
date.isValid(new Date());
Function which provides today date info object.
date.getThisDate(); // return { day, month, year, numberInWeek }
Function which return time between two dates in millisecond.
date.getDiff(new Date('2023-01-03 00:00:00'), new Date('2023-01-02 00:00:00')); // return 86400000 one day
Function with typecast to not nullable. Equivalent PHP empty.
helpers.isEmpty(''); // returns true
helpers.isEmpty([]); // returns true
helpers.isEmpty(undefined); // returns true
helpers.isEmpty(null); // returns true
helpers.isEmpty({}); // returns true
helpers.isEmpty(false); // returns false
helpers.isEmpty('true'); // returns false
helpers.isEmpty(123); // returns false
helpers.isEmpty([1]); // returns false
helpers.isEmpty({ example: 'test' }); // returns false
Typecast value to number or NaN.
helpers.toNumber('123,123'); // returns 123.123;
helpers.toNumber(true); // returns 1;
helpers.toNumber('aaaa'); // returns NaN;
Typecast value to boolean;
helpers.toBoolean(1); // returns true;
helpers.toBoolean(0); // returns false;
helpers.toBoolean('true'); // returns true;
helpers.toBoolean('1'); // returns true;
helpers.toBoolean('asdsda'); // returns true;
helpers.toBoolean('false'); // returns false;
helpers.toBoolean('0'); // returns false;
helpers.toBoolean(''); // returns false;
Function that tries to do callback and return null if error trowed.
Function that tries to do async callback and return null if error trowed.
Polyfill of Array.prototype.at
import { array } from '@ankasru/utils-ts'
array.at([1,2,3], -1) // 3;
Function returns user OS.
Functions check user OS.
If clipboard is supported, write to clipboard.
await system.writeToClipboard('text');
If clipboard is supported, read from clipboard.
await system.readFromClipboard(); // text
Check is clipboard is supported;
await system.isClipboardSupported(); // returns boolean;
If cookies are supported, returns CookiesObject
.
const cookies = system.parseCookies();
cookies.getCookie('cookie name');
cookies.removeCookie('cookie name');
cookies.setCookie({
name: 'cookie name',
value: 'value',
expAt: '2099-01-01 00:00:00', // optional
path: '/' // optional, default '/'
});
cookies.parseCookies(); // update cookies object
Function for checking window in ssr.
canUseDom();
Function generate random string.
text.generateHash(10); // returns a random string with length 10. Default is 6 characters.
Returns true if the URL contains .local
or test
.