Skip to content

Latest commit

 

History

History
689 lines (492 loc) · 11.5 KB

File metadata and controls

689 lines (492 loc) · 11.5 KB

APIs

fetools 详细 API 文档。

Object

View contents

Array

View contents

FP

View contents

Function

View contents

Number

View contents

String

View contents

Date

View contents

Other

View contents

BOM

View contents

isPlainOject

描述:判断传进去的某个值是否为 Object。

$isPlainOject(val: any): Boolean;

参数:

  • val: 需要检测的值。

返回值:

  • 如果值是 Object,则为true; 否则为false。
Examples
$isPlainOject({}) // true
$isPlainOject([]) // false


⬆ Back to top

isObjectEquals

描述:判断两个输入 Object 类型值是否相等。

$isObjectEquals(obj1: Object | Array, obj2: Object | Array): Boolean;

参数:

  • obj1: 需要检测的值。
  • obj2: 需要检测的值。

返回值:

  • 如果值相等,则为true; 否则为false。
Examples
$isObjectEquals({}, {}) // true
$isObjectEquals({}, []) // false
$isObjectEquals([1, 2, 3, [2, 3, 4]], [1, 2, 3, [2, 3, 4]]) // true
$isObjectEquals([1, 2, 3, [2, 3, 4]], [1, 2, 3, [2, 3]]) // false


⬆ Back to top

pluckDeep

描述:对 Object 安全取值,基本可以避免: "TypeError: Cannot read property 'x' of undefined"

$pluckDeep(obj: Object, path?: string | string[]): any | null;

参数:

  • obj: 需要取值的Object。
  • path?: 取值路径。

返回值:

  • 如果值存在则返回该值,否则返回null。
Examples
const a = { b: { c: [{ d: 'hello world' }] } }
$pluckDeep(a, 'b.c.0.d') // 'hello world'
$pluckDeep(a, 'b.c.1.d') // null

const result = $pluckDeep(a)
result('b.c.0.d') // 'hello world'
result('b.c.1.d') // null


⬆ Back to top

flattenObject

描述:对 Object 数据结构进行调整,缩短取值链路。

$flattenObject(obj: Object): Object;

参数:

  • obj: 需要调整的 Object。

返回值:

  • 返回调整后的 Object。
Examples
$flattenObject({ a: { b: { c: 1 } }, d: 1 }) // { 'a.b.c': 1, d: 1 }
$flattenObject({ a: { b: { c: [1, 2] } }, d: 1 }) // { 'a.b.c': [1, 2], d: 1 }


⬆ Back to top

isArray

描述:判断传进去的某个值是否为 Array。

$isArray(val: any): Boolean;

参数:

  • val: 需要检测的值。

返回值:

  • 如果值是 Array,则为true; 否则为false。
Examples
$isArray({}) // false
$isArray([]) // true


⬆ Back to top

shuffle

描述:将传进入的 Array 使用 shuffle 算法进行乱序后返回 Array。

$shuffle(val: any[]): any[];

参数:

  • val: 需要乱序的 Array。

返回值:

  • 乱序后的 Array。
Examples
$shuffle([1, 2, 3]) // [2, 3, 1]


⬆ Back to top

flattenArray

描述:将传进入的 Array 展开并返回新的 Array。

$flattenArray(arr: any[], depth?: number): any[];

参数:

  • arr: 需要扁平化的 Array。
  • depth?: 扁平化后保留成几维数组,默认是一维。

返回值:

  • 返回新的扁平化后的 Array。
Examples
$flattenArray([[1, 2, 3], [[4, 5, 6], 7]]) // [1, 2, 3, 4, 5, 6, 7]
$flattenArray([[1, 2, 3], [[4, 5, 6], 7]], -1) // [[1, 2, 3], [[4, 5, 6], 7]]
$flattenArray([[1, 2, 3], [[[4, 5, 6], 7], 8]], 2) // [1, 2, 3, [4, 5, 6], 7, 8]


⬆ Back to top

MayBe

描述:MayBe 函子,有of、map、value链式操作。

$MayBe.of(val: any).map(fn: Function).value(): any;

参数:

  • val: 需要进行链式操作的值。
  • fn: 对传进去的值所要进行的操作。

返回值:

  • 使用 value 返回最后的值,或者不使用不需要返回。
Examples
const res = { data: { lists: [{ name: 'leeper'}] } }

$MayBe.of(res)
  .map(res => res.data)
  .map(data => data.lists)
  .map(lists => lists[0])
  .map(lists => lists.name)
  .map(name => name.toUpperCase())
  .value() //'LEEPER'

$MayBe.of(res)
  .map(res => res.data)
  .map(data => data.lists)
  .map(lists => lists[0])
  .map(lists => lists.keyword)
  .map(keyword => keyword.name)
  .value() // null


⬆ Back to top

continous

描述:需要对某些值进行某种重复的操作。

$continous(fn: Function): Function;

参数:

  • fn: 重复操作。

返回值:

  • 返回一个新的函数,该函数接受某些值。
Examples
const add = $continous((x, y) => x + y)
const mul = $continous((x, y) => x * y)

add(1, 2, 3, 4) // 10
mul(1, 2, 3, 4) // 24


⬆ Back to top

toggle

描述:重复进行某些操作。

$toggle([fn: Function]): Function;

参数:

  • fn: 将某些需要重复的操作使用数组包装起来。

返回值:

  • 返回一个新的函数,该函数接受某些参数。
Examples
const testFn = $toggle(() => 'green', () => 'red', () => 'yellow')
testFn() // 'green'
testFn() // 'red'
testFn() // 'yellow'
testFn() // 'yellow'


⬆ Back to top

pipe

描述:将某些函数进行管道操作。

$toggle(fn1, fn2, ...): Function;

参数:

  • fn: 需要进行管道操作的函数。

返回值:

  • 返回一个新的函数,该函数接受某些参数。
Examples
const addOne = (x: number) => x + 1;
const addTwo = (x: number) => x + 2;
const mulFive = (x: number) => x * 5;
const testFn = $pipe(addOne, mulFive, addTwo)

testFn(1) // 12
testFn(1) // 17


⬆ Back to top

once

描述:限制传入的函数只能执行一次。

$once(fn): Function;

参数:

  • fn: 需要进行限制的函数。

返回值:

  • 返回一个新的函数,该函数接受某些参数。
Examples
const onceFn = $once(() => { console.log('once') })

onceFn() // 'once'
onceFn() // 不再执行


⬆ Back to top

debounce

描述:防抖操作。

$debounce(fn): Function;

参数:

  • fn: 需要进行防抖的函数。

返回值:

  • 返回一个新的函数,该函数接受某些参数。
Examples
const debounceFn = $debounce(() => { console.log('debounce') })

debounceFn() // 'debounce'


⬆ Back to top

throttle

描述:节流操作。

$throttle(fn): Function;

参数:

  • fn: 需要进行节流的函数。

返回值:

  • 返回一个新的函数,该函数接受某些参数。
Examples
const throttleFn = $throttle(() => { console.log('throttle') })

throttleFn() // 'throttle'


⬆ Back to top

isNumber

描述:判断输入值是否为 Number。

$isNumber(val: any): Boolean;

参数:

  • val: 需要检测的值。

返回值:

  • 如果值是 Number,则为true; 否则为false。
Examples
$isNumber(1) // true
$isNumber([]) // false


⬆ Back to top

randomIntegerInRange

描述:获取某范围内的随机整数。

$randomIntegerInRange(min: Number, max: Number): Number;

参数:

  • min: 范围的最小值。
  • max: 范围的最大值。

返回值:

  • 区间范围内的随机整数。
Examples
$randomIntegerInRange(0, 6) // 5


⬆ Back to top

randomNumberInRange

描述:获取某范围内的随机小数。

$randomNumberInRange(min: Number, max: Number): Number;

参数:

  • min: 范围的最小值。
  • max: 范围的最大值。

返回值:

  • 区间范围内的随机小数。
Examples
$randomNumberInRange(0, 6) // 4.826308609206913


⬆ Back to top

isString

描述:判断输入值是否为 String。

$isString(val: any): Boolean;

参数:

  • val: 需要检测的值。

返回值:

  • 如果值是 String,则为true; 否则为false。
Examples
$isString(1) // false
$isString('') // true


⬆ Back to top

addZero

描述:进行补 0 操作。

$addZero(val: String): String;

参数:

  • val: 需要进行补 0 的字符串。

返回值:

  • 补 0 后的字符串。
Examples
$addZero('6') // '06'
$isString(99) // '99'


⬆ Back to top

getDaysDiffBetweenDates

描述:获取两个日期间隔的天数。

$getDaysDiffBetweenDates(beginDate: Date, endDate: Date): Number;

参数:

  • beginDate: 开始时间。
  • endDate: 结束时间。

返回值:

  • 两个日期间隔的天数。
Examples
$getDaysDiffBetweenDates(new Date('2019-01-01'), new Date('2019-10-24')) // 296


⬆ Back to top

whichType

描述:检测输入的值属于什么类型。

$whichType(val: any): String;

参数:

  • val: 需要检测的值。

返回值:

  • 值的类型,有: "string", "number", "null", "undefined", "boolean", "symbol", "function", "object", "regexp", "array"。
Examples
$whichType('') // 'string'
$whichType({}) // 'object'


⬆ Back to top

isMobile

描述:检测客户端是否为移动端。

$isMobile(): Boolean;

参数:

  • 无。

返回值:

  • true / false。
Examples
$isMobile('') // true


⬆ Back to top

localStorage

描述:封装一个具有过期时间功能的localStorage,存储值时加上过期时间,取值前先判断当前数据是否已过期,如果已过期,返回参数${key}已过期,否则返回真实的值。

$localStorage.setItem(key: String, ...val)
$localStorage.removeItem(key: String);
// 移除所有
$localStorage.clear();

参数:

  • 看 Example。

返回值:

  • 无。
Examples
$localStorage.setItem('test','222',2000)
$localStorage.getItem('test') //'222'
setTimeout(()=>{
  $localStorage.getItem('test') //'参数test已过期'
},3000)

$localStorage.removeItem('myCat')
$localStorage.clear()


⬆ Back to top