npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ankasru/utils-ts

v1.1.16

Published

utils for ts and js

Downloads

44

Readme

utils-ts

typescript | javascript utils

Utils types

number.formatFactory

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

async.wait

Function for wait time in Promise.

Expect milliseconds.


async function myBeautyLoadingAnimation () {
    let loading = '.';
    await async.wait(500);
    loading = '..';
    await async.wait(500);
    loading = '...';
}

async.debounce

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;

async.throttle

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;

async.setCustomInterval

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
});

date.convert

Function for fast date formatting;

Expect:

date.convert(new Date()) // return formatted date // 01/01/2024;

date.isValid

Function for date validation.

Expect date

date.isValid(new Date());

date.getThisDate

Function which provides today date info object.

date.getThisDate(); // return { day, month, year, numberInWeek }

date.getDiff

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

helpers.isEmpty

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

helpers.toNumber

Typecast value to number or NaN.

helpers.toNumber('123,123'); // returns 123.123;
helpers.toNumber(true); // returns 1;
helpers.toNumber('aaaa'); // returns NaN;

helpers.toBoolean

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;

helpers.tryOrNull

Function that tries to do callback and return null if error trowed.

helpers.tryOrNullAsync

Function that tries to do async callback and return null if error trowed.

array.at

Polyfill of Array.prototype.at

import { array } from '@ankasru/utils-ts'

array.at([1,2,3], -1) // 3;

system.getOs

Function returns user OS.

system.(isMacOs|isWidows|isIos|isAndroid|isLinux)

Functions check user OS.

system.writeToClipboard

If clipboard is supported, write to clipboard.

await system.writeToClipboard('text');

system.readFromClipboard

If clipboard is supported, read from clipboard.

await system.readFromClipboard(); // text

system.isClipboardSupported

Check is clipboard is supported;

await system.isClipboardSupported(); // returns boolean;

system.parseCookies

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

system.canUseDom

Function for checking window in ssr.

canUseDom();

text.generateHash

Function generate random string.

text.generateHash(10); // returns a random string with length 10. Default is 6 characters.

url.isLocal

Returns true if the URL contains .local or test.