0.0.5 • Published 7 months ago
ustd v0.0.5
Javascript utility library all in one.
ustd/node
the exports point ustd/node
, when directly excuted, serves as a node register:
node -r ustd/node esnext.ts
or you can embbed the register to your project, for example in some.cjs
:
const { register } = require('ustd/node');
const disposable = register({});
require('./esnext.ts');
ustd/node/register
ustd/glob
import { str, reg, alt } from 'ustd/glob';
const IPV4 = reg(/\d+\.\d+\.\d+.\d+/); // match string like "127.0.0.1"
const IPV6 = reg(/\w+\.\w+\.\w+.\w+/); // match string like @todo
const PORT = reg(/\d+/).map(([str]) => parseInt(str)); // match string like "1080" and map to number 1080
const address = str`${alt(IPV4, IPV6)}:${PORT}`; // match string like "127.0.0.1:1080" or "@todo:1080"
const result = address.match('127.0.0.1:1080');
assert(result.expect(), ['127.0.0.1', 1080]);
Glob match result is will typed, greate for Typescript user:
Check test file glob.test.ts
for more useage examples.
ustd/mime
Query common mime type name by file extension name.
ustd/date
Deal with date.
import { fromNow } from 'ustd/date';
ustd/ansi
Deal with ansi charator.
import { ansi, ANSI } from 'ustd/ansi';
console.log(ansi`${ANSI.fgRed} I am a red color message`());
console.log(ansi`${ANSI.fgBlue} I am a ${(s1) => s1} ${(_, s2) => s2} message`('blue', 'color'));
ustd/console
print colorful log messages.
import { Print } from 'ustd/console';
ustd/json
Enhanced version of JSON.stringify that supports:
Uint8Array
URL
Date
Error
import { encode, decode } from 'ustd/json';
const buffer = encode({
name: 'The json encode test',
birthday: new Date('2023-08-20'),
homepage: new URL('https://sherluok.com'),
avatar: new Uint8Array([12, 25, 58, 64]),
});
fetch('https://your/api', {
method: 'POST',
body: buffer,
}).then(async (res) => {
const encodedResponseData = await res.arrayBuffer();
const data = decode(encodedResponseData);
return data.someProperty instanceof Uint8Array;
});
ustd/rpc
import {} from 'ustd/rpc';
ustd/react
import {} from 'ustd/react';
ustd/buffer
import { formatByteLength } from 'ustd/buffer';
ustd/iterable
import { fromAsyncIterable } from 'ustd/iterable';
ustd/semver
import {} from 'ustd/semver';
ustd/node/net
import { accessPort, queryPort, killPort } from 'ustd/node/net';
const isPortFree = await accessPort(3000);
if (!isPortFree) {
console.log(queryPort(3000));
killPort(3000);
}
ustd/node/stream
import { fromReadable } from 'ustd/node/stream';
const response = fromReadable(fs.createReadStream('./package.json'));
await response.arrayBuffer();
await response.text();
await response.json();
ustd/dom/css
import { cx } from 'ustd/dom/css';
const className = cx('base', 0, 1, true, false, ['a', 'b'], {
'button-primary': true,
'button-active': false,
});
console.assert(className === 'base a b button-primary');
ustd/reactivex
import { Observable, Subject, map, skip, take, switchMap } from 'ustd/reactivex';
import { fromEvent } from 'ustd/dom/reactivex';
const mouseDown$ = fromEvent(window, 'mousedown');
const mouseMove$ = fromEvent(window, 'mousemove');
const mouseUp$ = fromEvent(window, 'mouseup');
mouseDown$.pipe(switchMap(() => mouseMove$.pipe(takeUntil(mouseUp$)))).subscribe((e) => {
console.log('dragging:', e);
});