Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/planjs/utils
đ§ ćźçšć·„ć
·éć
https://github.com/planjs/utils
js-utils promisekit ts-utils typescript web-kit
Last synced: 23 days ago
JSON representation
đ§ ćźçšć·„ć ·éć
- Host: GitHub
- URL: https://github.com/planjs/utils
- Owner: planjs
- License: mit
- Created: 2020-09-13T05:34:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-30T05:05:40.000Z (3 months ago)
- Last Synced: 2024-12-01T01:29:10.882Z (about 1 month ago)
- Topics: js-utils, promisekit, ts-utils, typescript, web-kit
- Language: TypeScript
- Homepage: https://planjs.github.io/utils/
- Size: 2.67 MB
- Stars: 16
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# utils
![npm](https://img.shields.io/npm/v/@planjs/utils?label=@planjs/utils) ![npm](https://packagephobia.now.sh/badge?p=@planjs/utils) [![action](https://github.com/planjs/utils/actions/workflows/npm-publish.yml/badge.svg?branch=master)](https://github.com/planjs/utils/actions/workflows/npm-publish.yml)
[![NPM](https://nodei.co/npm/@planjs/utils.png?downloads=true&downloadRank=true&stars=true)](https://npmjs.org/package/@planjs/utils)> đ§ contactInfoDefault [doc](https://planjs.github.io/utils/)
## Installation
```bash
npm i @planjs/utils
```## Usage
```javascript
import {
delay,
retry,
pTry,
sequenceChain,
nextTick,
range,
REG_TEL_PHONE,
eventCenter,
TimeoutMap,
} from '@planjs/utils';// promise utils
async function main() {
await delay(100);
function fetchData() {
return Promise.reject(new Error('testing'));
}
await retry(fetchData, { maxAttempts: 3, delayMs: 500 })();
}const pool = asyncPool({ maxConcurrency: 10 });
const randArr = range(1, 10);
nextTick(async () => {
const [err, res] = await pTry(main());
if (!err) {
console.log(res);
}
let start = Date.now();
await Promise.all(randArr.map((v) => pool.executor(delay(v))));
// The timer has an accuracy problem
if (Date.now() - start + 1 >= Math.max(...randArr)) {
console.log('concurrency');
}
const count = (a = 0) => ++a;
const val = await sequenceChain([...range(0, 10).map((v) => [count])])();
console.log(val); // 11
});// commonly used regex
REG_TEL_PHONE.test('132xxxxxxx'); // false// event center
eventCenter.on('log', console.log);
eventCenter.trigger('log', 'planjs');// ext obj
const timeoutMap = new TimeoutMap({
timeout: 200,
passiveDeletion: true,
onTimeout(k, v) {
console.log(`expired key [${k}], val [${v}]`);
},
});
timeoutMap.set('name', 'fupengl');
delay(201).then(() => timeoutMap.get('name')); // null
```