@network-toolkit/shared

v0.0.1

Published

network-toolkit-shared

Readme

@network-toolkit/shared

工具包

安装

with pnpm

pnpm add @network-toolkit/shared

with yarn

yarn add @network-toolkit/shared

with npm

npm install @network-toolkit/shared

apis

getQueryString

获取 url 中的参数

| 字段名 | 类型 | 描述 | 必传 | | ------ | ------------- | -------------- | ---- | | query | string | 要获取的参数名 | 是 | | href | string |null | 地址 | 否 |

类型

declare const getQueryString: (query: string, href?: string) => string | null;

示例

import { getQueryString } from '@network-toolkit/shared';
const url = 'http://example.com/?test=123&foo=bar';
const query = getQueryString('test');
console.log(query);
// 123

debounce

防抖

| 字段名 | 类型 | 描述 | 必传 | | --------- | ----------------------- | -------------- | ---- | | func | (...args: any[]) => any | 需要防抖的函数 | 是 | | duration | number | 毫秒 | 否 | | immediate | boolean | 是否立即执行 | 否 |

类型

declare const debounce: <F extends (...args: any[]) => any>(
    func: F,
    duration?: number,
    immediate?: boolean
) => (...args: Parameters<F>) => ReturnType<F>;

示例

import { debounce } from '@network-toolkit/shared';
const fn = debounce(() => {
    console.log('debounce');
}, 300);
fn();

throttle

节流

| 字段名 | 类型 | 描述 | 必传 | | -------- | ----------------------- | -------- | ---- | | func | (...args: any[]) => any | 节流函数 | 是 | | duration | number | 毫秒 | 否 |

类型

declare const throttle: <F extends (...args: any[]) => any>(func: F, duration?: number) => (...args: Parameters<F>) => ReturnType<F>;

示例

import { throttle } from '@network-toolkit/shared';
const fn = throttle(() => {
    console.log('throttle');
}, 1000);
fn();

deepCopy

深拷贝

| 字段名 | 类型 | 描述 | 必传 | | ------ | ------------------------------------------- | ------------ | ---- | | data | Array | Record<string | symbol, any> | 要拷贝的数据 | 是 |

类型

declare const deepCopy: <T extends Array<T> | Record<string | symbol, any>>(data: T) => T;

示例

import { deepCopy } from '@network-toolkit/shared';
const obj = { a: 1, b: 2 };
const copyObj = deepCopy(obj);
console.log(copyObj);