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 🙏

© 2026 – Pkg Stats / Ryan Hefner

kong-util

v0.8.14

Published

my own library for js

Downloads

147

Readme

kong-util

Here are some codes I usually use. Some runs in any environment that supports JavaScript (ES6 and later); but some is usable only in browsers.

See also demo, documentation, and changelog.

Start

in browsers

traditional way

<script src="https://cdn.jsdelivr.net/npm/kong-util/dist/all.js"></script>
<script>
    // ... other codes
</script>

module way

<script type="module">
    import kongUtil from "https://cdn.jsdelivr.net/npm/kong-util/mod/all.mjs";
    // ... other codes
</script>

in Node.js

install

npm install --save kong-util

use

// ES module
import kongUtil from "kong-util";

// both in ES module and CommonJS
import("kong-util").then(kongUtil => {
    // codes here
});

Usage

basic

kongUtil.wait(100).then(() => console.log("xxx"));

The above code would call console.log after 100 microseconds without blocking codes after it.

You can also call kongUtil.use, which makes methods global to call.

kongUtil.use('wait'); // make `wait` a global function
wait(100).then(() => console.log("xxx")); // same as above

To globalize more than two methods, either the following ways work:

kongUtil.use('wait', 'waitFor');
kongUtil.use(['wait', 'waitFor']);

Or, simply kongUtil.use() (without any argument) would lead all its methods global to use.

kongUtil.use();
wait(100).then(() => console.log("xxx"));
$("body").append("some text"); // `$` works as `document.querySelector` if assigned one string.

Warning: Some function names may conflict with other library, such as $ in jQuery.

categories

Functions are categorized into different files. You can also use only one category without importing others.

<script src="https://cdn.jsdelivr.net/npm/kong-util/dist/async.js"></script>
<script>
    kongUtilAsync.wait(100).then(() => console.log("xxx"));

    // Also, `use` method makes one, some, or all methods global.
    kongUtilAsync.use('wait', 'waitFor');
    wait(200).then(() => console.log("yyy"));
</script>

extend native classes

Assume we have an array arr = [1, 2, 3, 4, 5];

If you wanna randomize the order of the array in place, kongUtilArray.shuffle(arr) would work.

However, what about we use the function as a method of Array class? Here it goes:

Array.prototype.shuffle = kongUtilArray.shuffle; // or `kongUtil.shuffle`

arr.shuffle();

Or, call extendArrayPrototype to extend more functions.

kongUtilArray.extendArrayPrototype();

arr.shuffle();

Warning: This may cause problems if some day JavaScript has native shuffle method in Array class.

functions you may be interested in

For all functions, see documentation.

utilArray

Go sequentially through the elements one by one:

// old way
await fetch(url1);
await fetch(url2);

// new way
await kongUtil.mapAsync(fetch, [url1, url2]);

// after prototype extended
await [url1, url2].mapAsync(fetch);

utilAsync

Add time limit to an async function:

waitFor(fetch(url3), 1000)
.then(
    resp => console.log("success"),
    err => console.error('timeout or error')
);

// another way
const fetchAutoReject = addTimeLimit(fetch, 1000);
fetchAutoReject(url3)
.then(
    resp => console.log("success"),
    err => console.error('timeout or error')
);

utilDom

// creates an HTMLElement by a string
parseHTML("<EM>hi!</em>");

// creates an HTMLElement by JsonML
createElementFromJsonML(
    ["ul",
        ["li", "first"],
        ["li",
            ["em", "second"]
        ]
    ]
);

// sets attributes of an Element
setAttributes('#my-button', {
    type: "button",
    style: "border: 1px solid red",
    onclick: () => console.log("zzz")
});

setText('h1', 'new text here');

// after prototype extended
$('#my-button').set({
    type: "button",
    style: "border: 1px solid red",
    onclick: () => console.log("zzz")
});

$('h1').setText('new text here');

utilEvent

function foo1() {console.log('aaa');}

listen('#my-button', 'click', foo1);
unlisten('#my-button', 'click', foo1);

listenMulti('button', 'click', foo1); // applies to all <button>s

// after prototype extended
$('#my-button').listen('click', foo1); // alias to `addEventListener`
$('#my-button').unlisten('click', foo1); // alias to `removeEventListener`

utilImage

// resize the chosen file and then show it
resizeImage(
    $('[type=file]').files[0],
    {scale: .5, returnType: 'dataURL'}
)
.then(url => {
    $('img').src = url;
});

utilObject

Sometimes you may want to treat objects as arrays:

// returns [4, 9]
emulateArray("map", x => x*x, {a: 2, b: 3});

// returns {a: 4, b: 9}
objectMap(x => x*x, {a: 2, b: 3});

utilString

  • camelize
  • kebabize
  • parseChineseNumber
  • compareVersionNumbers
  • toCSV
  • parseCSV
  • base64ToBlob
  • dateFormat: simulates PHP's DateTime::format
  • numberFormat: shortcut to Intl.NumberFormat.prototype.format

utilWeb

const params = {x: 3, y: 4};

// old way
fetch(url4, {method: 'POST', body: new URLSearchParams(params)})
.then(res => {
    if (response.ok) return response;
    throw new ReferenceError(response.statusText);
})
.then(res => res.json())
.then(obj => { /* ... */ });

// new way
fetchJSON(url4, {method: 'POST', body: params})
.then(obj => { /* ... */ });