New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@mrtujiawei/utils

Package Overview
Dependencies
Maintainers
1
Versions
600
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mrtujiawei/utils

把自己写的工具函数打包发布到npm上

  • 1.9.16
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
225
decreased by-34.02%
Maintainers
1
Weekly downloads
 
Created
Source

@mrtujiawei/utils

npm version install size npm downloads js delivr downloads issues forks PRs Welcome stars license

Installing

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>

Usage

note: Brower usage

const { Stack } = utils.Stack;

note: CommonJS usage

const { Stack } = require('@mrtujiawei/utils');

or

const utils = require('@mrtujiawei/utils');
const Stack = utils.Stack;

note: ESModule usage

import { Stack } from '@mrtujiawei/utils';

or

import utils from '@mrtujiawei/utils';
const Stack = utils.Stack;

Example

使用示例

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

Queue

队列

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();

Heap

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

BinarySearchTree

二叉搜索树

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(); // []

Lock

异步流程加锁

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

TaskQueue

任务队列,主要是用来执行单任务

ResponsibilityChain

职责链

DateTimeTool

日期时间处理类
解析时间太复杂,没做

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()); // 获取当周的日期范围

CountDown

倒计时

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();

Pagination

分页

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); // 跳到第二页

Logger

日志记录

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);

Events

事件发射

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');
// 没有输出

Random

随机

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(); // ********-**************-************

PriorityQueue

优先队列

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

Trie

前缀树

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

utils

reverseRange

翻转数组中的某一段

import { reverseRange } from '@mrtujiawei/utils';

const arr = [1, 2, 3];
reverseRange(arr, 1, 3); // [1, 3, 2]
swap

交换数组中的两个元素

import { swap } from '@mrtujiawei/utils';

const arr = [1, 2, 3];
swap(arr, 1, 2);  // [1, 3, 2];
sleep

延时一段时间

// 延时 1s
await sleep(1);
debounce

防抖

import { debounce } from '@mrtujiawei/utils';

const listener = debounce((event) => {
  console.log(event);
}, 500);

addEventListener('scroll', listener, {
  passive: true,
});
throttle

节流

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);
isInteger

是否是整数, 实际值

import { isInteger } from '@mrtujiawei/utils';

isInteger(0);     // true
isInteger('1');   // true
isInteger(1.1);   // false
isInteger('a');   // false
isNaturalNumber

是否是自然数

import { isNaturalNumber } from '@mrtujiawei/utils';

isNaturalNumber('123');   // true
isNaturalNumber(-1);      // false
isNaturalNumber('a');     // false
isPromise

判断是否是promise

import { isPromise } from '@mrtujiawei/utils';
const promise = new Promise(() => {});
const number = 0;

isPromise(promise);   // true
isPromise(number);    // false
retry

重试

import { retry } from '@mrtujiawei/utils';

// 如果回调执行失败,会重复执行直到成功或者执行次数超过10次
const listener = retry((data) => {
  console.log(data);
}, 9);
listener(1);
reentrant

重入

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#
findFirstIndex

二分查找第一个满足条件的下标

import { findFirstIndex, } from '@mrtujiawei/utils';

findFirstIndex([1, 2, 3, 4, 5]);
objectToString

对象转字符串,不是json

import { objectToString } from '@mrtujiawei/utils';

objectToString('asf');    // "asf"
objectToString([1, 2]);   // [1, 2, length: 2]
isSame

判断两个值是否相同,两个值都是 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

Keywords

FAQs

Package last updated on 14 Feb 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc