README
toolkit
Commonly used functions.
Usage
npm i wsp-toolkit
// script: js文件位于 /dist/umd/index.min.js
<script src="/dist/umd/index.min.js"></script>
window.toolkit.isExist('')
// esmodule
import { isExist } from "wsp-toolkit";
// node
const { typeOf } = require("wsp-toolkit");
API
- addition
- and
- currying
- debounce
- identity
- isEmpty
- isExist
- isNumber
- isSafeNumber
- notExist
- once
- or
- pipe
- sleep
- throttle
- toArray
- toNumber
- typeOf
- unique
- Point
- LinkedList
- Vector
addition
addition(1, 2, 3) // 6
addition([1, 2, 3, 4]) // 10
addition(1, 2, [7, 10]) // 20
and
and(1, 0) // false
and(1, 0 !== null) // true
currying
function add(l: number, r: number): number {
return l + r;
}
const curryFn = currying(add);
curryFn(1, 2) // 3
curryFn(1)(2) // 3
curryFn()(1)(2) // 3
debounce
interface IOpts {
leading?: boolean; // default false
trailing?: boolean; // default true
}
type debounce<T extends Function> = (func: T, wait: number, options?: IOpts) => T;
identity
identity(null) // null
identity(1) // 1
identity({a: 1}) // {a: 1}
isEmpty
isEmpty('') // true
isEmpty(' ') // true
isEmpty([]) // true
isEmpty({}) // true
isEmpty(new Map()) // true
isEmpty(new Set()) // true
isEmpty(() => {}) // false
isEmpty(0) // false
isExist
isExist(null) // false
isExist(undefined) // false
isExist(0) // true
isExist('') // true
isExist([]) // true
isExist([1,2,3]) // true
isNumber
isNumber(0) // true
isNumber(NaN) // true
isNumber(Infinity) // true
isNumber('0') // false
isNumber([]) // false
isSafeNumber
isSafeNumber(NaN) // false
isSafeNumber(Infinity) // false
isSafeNumber(0) // true
isSafeNumber(Number.MIN_SAFE_INTEGER) // true
isSafeNumber(Number.MAX_SAFE_INTEGER) // true
notExist
notExist(null) // true
notExist(undefined) // true
notExist(0) // false
notExist('') // false
notExist({}) // false
once
const arr: number[] = [];
function push(x: number) {
arr.push(x)
}
const oncePush = once(push);
oncePush(1);
oncePush(2);
oncePush(3);
arr; // [1]
or
or(0, 1, false) // true
or(null, undefined, 0, "") // false
pipe
const res = pipe(
(str: string, count: number) => str.repeat(count),
(str: string) => str.toUpperCase()
)("a", 2);
res // "AA"
sleep
// do someting
await sleep(10_000); // wait for a timeout 10s
// do someting
throttle
interface IOpts {
leading?: boolean; // default: false 开始时是否调用函数
trailing?: boolean; // default: true 结束时是否调用函数
maxWait?: number; // default: wait 为0则开始到结束之间的事件不调用函数
}
type debounce<T extends Function> = (func: T, wait: number, options?: IOpts) => T;
toArray
toArray('abc') // ['a', 'b', 'c']
toArray([]) // []
toArray(null) // []
toArray(undefined) // []
toArray({length: 2}) // [undefined, undefined]
toArray(document.querySelectorAll('div')) // NodeList: [div, div]
toArray(0) // [0]
toArray(false) // [false]
toArray(new Set([1, 2])) // [1, 2]
toNumber
toNumber(100) // 100
toNumber('10,000') // 10000
toNumber('-870,123,001.20') // 870123001.2
toNumber(null) // 0
toNumber(null, {defaultValue: 100}) // 100
toNumber(NaN, {defaultValue: 100}) // 100
toNumber(0, {defaultValue: 100}) // 0
typeOf
typeOf('') // String
typeOf(0) // Number
typeOf(NaN) // Number
typeOf(Infinity) // Number
typeOf(Symbol) // Function
typeOf(Symbol('wsp')) // Symbol
typeOf([]) // Array
typeOf({}) // Object
typeOf(new Map()) // Map
typeOf(new FormData()) // FormData
unique
unique([1, 1, 3]) // [1, 3]
arr = [{id: 1}, {id: 2}, {id: 1}, {id: 2}];
unique(arr, 'id') // [{id: 1}, {id: 2}]
arr = [{info: {id: 1}}, {info: {id: 2}}, {info: {id: 3}}, {info: {id: 2}}];
unique(arr, x => x.info.id)
// [
// {info: {id: 1}},
// {info: {id: 2}},
// {info: {id: 3}},
// ]
Point
LinkedList
Vector
ICoord
interface ICoord {
x: number;
y: number;
}
两种构造Vector的方式
new Vector(p1: ICoord, p2: ICoord)
Vector.of(p: ICoord)
import {Vector} from "wsp-toolkit";
const v = Vector.of({x: 10, y: 0});
const v1 = Vector.of({x: 0, y: 10});
const v2 = new Vector({x: 0, y: 3}, {x: 4, y: 0});
instance.getLength()
获取向量长度
v1.getLength() // 10
v2.getLength() // 5
instance.getAngle(v: Vector)
获取旋转角度
v.getAngle(v1) // 90
v1.getAngle(v) // -90
instance.getScale(v: Vector)
获取缩放比例
import {Vector} from "./Vector";
const v1 = Vector.of({x: 5, y: 0});
const v2 = Vector.of({x: 10, y: 0});
v1.getScale(v2); // 0.5
v2.getScale(v1); // 2