![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@mrtujiawei/utils
Advanced tools
Using npm:
$ npm install @mrtujiawei/utils
Using yarn:
$ yarn add @mrtujiawei/utils
Using unpkg CDN:
<script src="https://unpkg.com/@mrtujiawei/utils/dist/utils.js"></script>
const { Stack } = utils.Stack;
const { Stack } = require('@mrtujiawei/utils');
or
const utils = require('@mrtujiawei/utils');
const Stack = utils.Stack;
import { Stack } from '@mrtujiawei/utils';
or
import utils from '@mrtujiawei/utils';
const Stack = utils.Stack;
使用示例
堆栈
import { Stack } from '@mrtujiawei/utils';
const stack = new Stack();
stack.size; // 0
stack.isEmpty(); // true
stack.push(1); // [1]
stack.push(2, 3); // [1, 2, 3]
stack.size; // 3
stack.isEmpty(); // false
stack.pop(); // 3
stack.peak(); // 2
stack.pop(); // 2
stack.peak(); // 1
stack.pop(); // 1
stack.peak(); // throw Error: StackEmptyError
stack.pop(); // throw Error: StackEmptyError
队列
import { Queue } from '@mrtujiawei/utils';
const queue = new Queue();
queue.size; // 0
queue.isEmpty(); // true
queue.enqueue(1); // [1]
queue.enqueue(2); // [1, 2]
queue.dequeue(); // 1
queue.enqueue(3); // [2, 3]
queue.size; // 2
queue.isEmpty(); // false
queue.dequeue(); // 2
queue.dequeue(); // 3
queue.dequeue(); // throw Error: QueueEmptyError
双向链表
import { LinkList } from '@mrtujiawei/utils';
const list = new LinkList();
堆
import { Heap } from '@mrtujiawei/utils';
// 小顶堆
const heap = new Heap((a, b) => a - b);
heap.insert(2);
heap.insert(3);
heap.insert(1);
heap.remove(); // 1
heap.remove(); // 2
heap.insert(0);
heap.remove(); // 0
heap.remove(); // 3
二叉搜索树
import { BinarySearchTree } from '@mrtujiawei/utils';
const bTree = new BinarySearchTree((a, b) => a - b, []);
bTree.append(1);
bTree.append(2);
bTree.append(3);
bTree.append(4);
// 执行4次
// 1, 2, 3, 4
bTree.inorderTraversal((value) => {
console.log(value);
});
bTree.getMin(); // 1
bTree.getMax(); // 4
bTree.toArray(); // [1, 2, 3, 4]
bTree.clear(); // []
异步流程加锁
import { Lock, sleep } from '@/mrtujiawei/utils';
const lock = new Lock(1);
/**
* 异步任务只有等上次任务结束后才会开始执行下一个异步任务
*/
const run = async (value, timeout) => {
try {
await lock.lock();
// 异步任务
await sleep(timeout);
console.log(value);
} finally {
lock.unlock();
}
};
run(0, 1000);
run(1, 100);
run(2, 0);
output: 0 1 2
任务队列,主要是用来执行单任务
职责链
日期时间处理类
解析时间太复杂,没做
import { DateTimeTool } from '@/mrtujiawei/utils';
DateTimeTool.timeFormat(new Date(), ':'); // hh:mm:ss
DateTimeTool.dateFormat(new Date(), '-'); // yyyy-mm-dd
DateTimeTool.dateTimeFormat(new Date()); // yyyy-mm-dd hh:mm:ss
DateTimeTool.getNthDayBefore(2); // 获取n天以前时间和当前日期时间
DateTimeTool.getNthHourBefore(2); // 获取n小时之前到当前时间
DateTimeTool.getNthMonthBefore(1); // 获取n月以前时间到当前月时间
DateTimeTool.toDayBegin(new Date()); // 设置到当前天的开始 00:00:00.000
DateTimeTool.toDayEnd(new Date()); // 设置到当前天的结束 23:59:59.999
DateTimeTool.isLeapYear(new Date()); // 是否是闰年
DateTimeTool.diffTimestamp(new Date(), new Date()); // 时间戳差值
DateTimeTool.diffSeconds(new Date(), new Date()); // 秒差值
DateTimeTool.diffMinutes(new Date(), new Date()); // 分钟差值
DateTimeTool.diffHours(new Date(), new Date()); // 小时差值
DateTimeTool.diffDays(new Date(), new Date()); // 天差值
DateTimeTool.addDays(new Date(), 10); // 日期往后加10天
DateTimeTool.timestampToTime(123); // hh:mm:ss
DateTimeTool.timestampToDate(123); // yyyy-mm-dd
DateTimeTool.timestampToDateTime(123); // yyyy-mm-dd hh:mm:ss
DateTimeTool.getCurrentWeek(new Date()); // 获取当周的日期范围
倒计时
import { CountDown } from '@mrtujiawei/utils';
const countDown = new CountDown('默认信息');
const callback = countDown.subscribe((data) => {
// 结束倒计时
if (data.done) {
data.message; // 默认信息
} else {
data.message; // 倒计时数字 60, 59...
}
});
countDown.start({
start: 60,
end: 0,
timeout: 1,
});
// 取消其中的一个订阅
countDown.unsubscribe(callback);
// 清空所有订阅函数
countDown.clear();
分页
import { Pagination } from '@mrtujiawei/utils';
const tableData = {
pageNum: 1,
pageSize: 10,
tableData: [],
total: 0,
};
const pagination = new Pagination(tableData);
pagination.subscribe((tableData) => {
console.log(tableData);
});
pagination.setPageSize(20);
const key = 'key';
pagination.setOrder(key); // 设置排序
pagination.sort(); // 重新排序
pagination.to(2); // 跳到第二页
日志记录
import { Logger } from '@mrtujiawei/utils';
const logger = Logger.getLogger();
const callback = logger.subscribe((message) => {
console.log(message);
});
logger.setLevel(Logger.LOG_LEVEL.ALL);
logger.trace('info');
logger.info('info');
logger.debug('debug');
logger.warn('warn');
logger.error('error');
logger.fatal('fatal');
logger.unsubscribe(callback);
事件发射
const events = new Events();
const listener = events.on('start', (...data) => {
console.log(data);
});
events.once('start', (...data) => {
console.log(data);
});
events.emit('start', 1, 2, 3);
// 1 2 3
// 1 2 3
events.off('start', listener);
events.emit('start');
// 没有输出
随机
import { Random } from '@mrtujiawei/utils';
Random.getRandomNumber(100, 1000); // [100, 1000)
Random.getRandomBoolean(); // true | false
Random.getRandomUppercaseLetter(); // [A-Z]
Random.getRandomUppercaseString(n); // [A-Z]{n}
Random.getRandomLowercaseLetter(); // [a-z]
Random.getRandomLowercaseString(n); // [a-z]{n}
Random.getRandomAlphabetString(n); // [a-zA-Z]{n}
Random.getRandomString(n); // [a-zA-Z0-9]{n}
Random.getRandomID(); // ********-**************-************
优先队列
import { PriorityQueue } from '@mrtujiawei/utils';
// 数字越小优先级越高
const pq = new PriorityQueue((a, b) => a - b);
pq.isEmpty(); // true
pq.enqueue(5);
pq.isEmpty(); // false
pq.enqueue(3);
pq.enqueue(1);
pq.enqueue(2);
pq.peak(); // 1
pq.dequeue(); // 1
pq.dequeue(); // 2
前缀树
const trie = new Trie();
trie.insert('Hello');
trie.insert('World');
trie.search('Hello'); // true
trie.search('He'); // false
trie.startsWith('W'); // true
trie.startsWith('w'); // false
翻转数组中的某一段
import { reverseRange } from '@mrtujiawei/utils';
const arr = [1, 2, 3];
reverseRange(arr, 1, 3); // [1, 3, 2]
交换数组中的两个元素
import { swap } from '@mrtujiawei/utils';
const arr = [1, 2, 3];
swap(arr, 1, 2); // [1, 3, 2];
延时一段时间
// 延时 1s
await sleep(1);
防抖
import { debounce } from '@mrtujiawei/utils';
const listener = debounce((event) => {
console.log(event);
}, 500);
addEventListener('scroll', listener, {
passive: true,
});
节流
import { throttle } from '@mrtujiawei/utils';
const options = {
// 100ms以内只触发一次
timeout: 100,
// 第一次是否直接触发
// false 100ms以后才会触发
leading: true,
};
const listener = throttle((data) => {
console.log(data);
}, { timeout: 100, leading: true });
addEventListener('scroll', listener);
是否是整数, 实际值
import { isInteger } from '@mrtujiawei/utils';
isInteger(0); // true
isInteger('1'); // true
isInteger(1.1); // false
isInteger('a'); // false
是否是自然数
import { isNaturalNumber } from '@mrtujiawei/utils';
isNaturalNumber('123'); // true
isNaturalNumber(-1); // false
isNaturalNumber('a'); // false
判断是否是promise
import { isPromise } from '@mrtujiawei/utils';
const promise = new Promise(() => {});
const number = 0;
isPromise(promise); // true
isPromise(number); // false
重试
import { retry } from '@mrtujiawei/utils';
// 如果回调执行失败,会重复执行直到成功或者执行次数超过10次
const listener = retry((data) => {
console.log(data);
}, 9);
listener(1);
重入
import { reentrant, sleep, } from '@mrtujiawei/utils';
const func = reentrant(async (value) => {
await sleep(1);
return `#{value}#`;
});
const run = (data) => {
const result = await func(data);
console.log(result);
};
// 无输出
run(100);
run(200); // #200#
二分查找第一个满足条件的下标
import { findFirstIndex, } from '@mrtujiawei/utils';
findFirstIndex([1, 2, 3, 4, 5]);
对象转字符串,不是json
import { objectToString } from '@mrtujiawei/utils';
objectToString('asf'); // "asf"
objectToString([1, 2]); // [1, 2, length: 2]
判断两个值是否相同,两个值都是 NaN 也是 true
type isSame = (value1: unknown, value2: unknown) => boolean;
import { isSame } from '@mrtujiawei/utils';
isSame(NaN, NaN); // true
isSame(null, null); // true
isSame('123', 123); // false
isSame(undefined, undefined); // true
FAQs
The npm package @mrtujiawei/utils receives a total of 184 weekly downloads. As such, @mrtujiawei/utils popularity was classified as not popular.
We found that @mrtujiawei/utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.