@staryea/network-shared
v0.1.2
Published
staryea-network-shared
Downloads
199
Readme
@staryea/network-shared
工具包
安装
with pnpm
pnpm add @staryea/network-shared
with yarn
yarn add @staryea/network-shared
with npm
npm install @staryea/network-shared
apis
getQueryString
获取 url 中的参数
| 字段名 | 类型 | 描述 | 必传 | | ------ | ------------- | -------------- | ---- | | query | string | 要获取的参数名 | 是 | | href | string |null | 地址 | 否 |
类型
declare const getQueryString: (query: string, href?: string) => string | null;
示例
import { getQueryString } from '@staryea/network-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 '@staryea/network-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 '@staryea/network-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 '@staryea/network-shared';
const obj = { a: 1, b: 2 };
const copyObj = deepCopy(obj);
console.log(copyObj);