4.4.0 • Published 3 days ago

web-utility v4.4.0

Weekly downloads
42
License
LGPL-3.0
Repository
github
Last release
3 days ago

Web utility

Web front-end toolkit based on TypeScript

NPM Dependency CI & CD

Open in Visual Studio Code

NPM

Installation

npm install web-utility

index.html

<head>
    <script src="https://polyfill.web-cell.dev/feature/Regenerator.js"></script>
    <script src="https://polyfill.web-cell.dev/feature/TextEncoder.js"></script>
    <script src="https://polyfill.web-cell.dev/feature/URL.js"></script>
    <script src="https://polyfill.web-cell.dev/feature/ScrollBehavior.js"></script>
    <script src="https://polyfill.web-cell.dev/feature/IntersectionObserver.js"></script>
</head>

tsconfig.json

{
    "compilerOptions": {
        "module": "ES2021",
        "moduleResolution": "Node",
        "downlevelIteration": true,
        "lib": ["ES2021", "DOM", "DOM.Iterable"]
    }
}

Usage

API document

CSS Animation

  1. Watch Swipe

  2. Simple Hover

  3. Switch with await

  4. Toggle with Inline Styles

  5. Work with Existed Classes

Function Cache

import { cache } from 'web-utility';

const getToken = cache(async (cleaner, code) => {
    const { access_token, expires_in } = await (
        await fetch(`https://example.com/access_token?code=${code}`)
    ).json();

    setTimeout(cleaner, expires_in * 1000);

    return access_token;
}, 'Get Token');

Promise.all([getToken('xxx'), getToken('yyy')]).then(([first, second]) =>
    console.assert(
        first === second,
        'Getting token for many times should return the same before deadline'
    )
);

DOM operation

import { parseDOM, walkDOM, stringifyDOM } from 'web-utility';

const [root] = parseDOM('<a>Hello, <b>Web</b>!</a>');

var count = 0;

for (const { nodeName, nodeType, dataset } of walkDOM(root)) {
    console.log(nodeName);

    if (nodeType === Node.ELEMENT_NODE) dataset.id = ++count;
}

console.log(stringifyDOM(root)); // '<a data-id="1">Hello, <b data-id="2">Web</b>!</a>'

jQuery-like DOM event delegation

import { delegate } from 'web-utility';

document.addEventListener(
    'click',
    delegate('a[href]', (event, link) => {
        event.preventDefault();

        console.log(link.href);
    })
);

Message Channel

index.ts

import { createMessageServer } from 'web-utility';

createMessageServer({
    preset: () => ({ test: 1 })
});

iframe.ts

import { createMessageClient } from 'web-utility';

const request = createMessageClient(globalThis.parent);

(async () => {
    console.log(await request('preset')); // { test: 1 }
})();

Service Worker updating

import { serviceWorkerUpdate } from 'web-utility';

const { serviceWorker } = window.navigator;

serviceWorker
    ?.register('/sw.js')
    .then(serviceWorkerUpdate)
    .then(worker => {
        if (window.confirm('New version of this Web App detected, update now?'))
            // Trigger the message callback listened in the Service Worker
            // generated by Workbox CLI
            worker.postMessage({ type: 'SKIP_WAITING' });
    });

serviceWorker?.addEventListener('controllerchange', () =>
    window.location.reload()
);

Internationalization

Migrate to MobX i18n since v4.

Test scripts

If you are looking for a simple alternative of Mocha or Jest, just use these Test Utility methods with ts-node:

npx ts-node index.spec.ts

index.spec.ts

import { describe, it } from 'web-utility';

class App {
    name = 'test';

    static create() {
        return new App();
    }
}

describe('My module', async () => {
    const app = await it('should create an App object', async expect => {
        const app = App.create();

        expect(app instanceof App);

        return app;
    });

    await it('should init an App name', expect => {
        expect(app.name === 'test');
    });
});
4.4.0

3 days ago

4.3.0

3 months ago

4.0.1

10 months ago

4.1.3

8 months ago

4.1.0

9 months ago

3.9.9

2 years ago

3.9.8

2 years ago

3.9.7

2 years ago

4.0.0

1 year ago

3.9.3

2 years ago

3.9.6

2 years ago

3.9.5

2 years ago

3.9.4

2 years ago

3.7.5

2 years ago

3.9.2

2 years ago

3.7.4

2 years ago

3.9.1

2 years ago

3.7.3

2 years ago

3.9.0

2 years ago

3.7.2

2 years ago

3.7.8

2 years ago

3.7.7

2 years ago

3.7.6

2 years ago

3.8.0

2 years ago

3.8.2

2 years ago

3.7.1

2 years ago

3.6.5

2 years ago

3.6.4

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

3.6.3

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.0

2 years ago

3.2.0

2 years ago

3.0.1

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.0.0

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.2.0-rc.0

2 years ago

3.1.0-rc.0

2 years ago

2.9.2

2 years ago

2.9.4

2 years ago

2.9.3

2 years ago

2.9.6

2 years ago

2.9.5

2 years ago

2.9.0

2 years ago

2.9.1

2 years ago

2.8.0

3 years ago

2.6.3

3 years ago

2.6.1

3 years ago

2.6.2

3 years ago

2.6.0

3 years ago

2.5.4

3 years ago

2.5.3

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.5.0

3 years ago

2.4.4

3 years ago

2.4.3

3 years ago

2.4.2

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.9.0

3 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.2

4 years ago

1.5.0

4 years ago

0.13.1

4 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.12.1

4 years ago

0.11.2

4 years ago

0.11.1

4 years ago

0.11.0

4 years ago