@asarua/tools

some tools functions

Usage no npm install needed!

<script type="module">
  import asaruaTools from 'https://cdn.skypack.dev/@asarua/tools';
</script>

README

tools

一些辅助性的方法

下载

使用npm

npm i @asarua/tools -S

使用yarn

yarn add @asarua/tools -S

例子

import {
  after,
  assert,
  before,
  compose,
  curry,
  deepClone,
  getUrlParams,
  is,
  memorize,
  noop
} from '@asarua/tools'

after

after是一个后置调用包装者,after函数提供三个参数

  • fn: 要进行包装的函数
  • afterFn: 要后置调用的函数
  • isEffect?: 是否通过后置函数改变返回值

after根据第三个参数的传递与否提供了两种不同的模式与类型声明

  • aop模式,不切入业务,只进行辅助功能
import { after } from '@asarua/tools'

const func = after(
  function fn(data: { a: 1, b: 2 }) {
    return {
      ...data,
      c: 3
    }
  },
  function afterFn(data) {
    return data.a
  }
)

// ReturnType<typeof func> => ReturnType<typeof fn>
  • 副作用模式,即第三个参数为trueafterFn的参数接收的类型,将会由fn的返回值决定(如果是使用的ts,那么afterFn的参数类型将会具有强约束),func的返回值将会是afterFn的类型
import { after } from '@asarua/tools'

const func = after(
  function fn(data: { a: 1, b: 2 }) {
    return {
      ...data,
      c: 3
    }
  },
  /*
    此处的data的类型,已经是确定好的,也就是fn的返回值类型
  */
  function afterFn(data) {
    return data.a
  },
  true
)

// ReturnType<typeof func> => ReturnType<typeof afterFn>

assert

assert是一个断言函数,它有两个参数

  • condition: 要断言的语句
  • msg?: 断言失败之后要抛出的错误
import { assert } from '@asarua/tools'

assert(1, 'error!')

before

after类似,before是一个前置调用的包装者,同样提供了三个参数

  • beforeFn: 进行前置调用的函数
  • fn: 要进行包装的函数
  • isEffect?: 是否通过前置函数改变fn的参数

before同样根据第三个参数的传递与否提供了两种不同的模式与类型声明

  • aop模式,不切入业务,只进行辅助功能
import { before } from '@asarua/tools'

type s = {
  a: 1,
  b: 2
}

const func = before(
  function beforeFn(data: s) {
    return {
      ...data,
      c: 3
    }
  },
  function fn(data) {
    return 1
  }
)

// ReturnType<typeof func> => ReturnType<typeof fn>
  • 副作用模式,即第三个参数为truefn的参数接收的类型,将会由beforeFn的返回值决定(如果是使用的ts,那么fn的参数类型将会具有强约束),func的返回值始终都是fn的返回值
import { before } from '@asarua/tools'

type s = {
  a: 1,
  b: 2
}

const func = before(
  function beforeFn(data: s) {
    return {
      ...data,
      c: 3
    }
  },
  function fn(data) {
    return 1
  },
  true
)

// Parameters<typeof fn>[number] => ReturnType<typeof beforeFn>

compose

compose方法提供对一组参数、返回值都统一的方法的合并功能

import { compose } from '@asarua/tools'

const func = compose(
  function(num: string) {
    return num + 3
  },
  function(num: number) {
    return num + '2'
  }
)

func(1) // => 123

// const func: (...args: number[]) => string

curry

curry是一个柯里化函数制造者,它接收一个函数,返回值是这个函数的柯里化版本

const add = (a, b, c) => a + b + 
const curried = curry(add)

curried(1)(2)(3) // => 6
curried(1, 2)(3) // => 6
curried(1)(2, 3) // => 6
curried(1, 2, 3) // => 6

deepClone

deepClone提供了一个对象、数组的深拷贝的方式

const data = {
  a: 1,
  b: 2,
  c: {
    d: 4
  }
}
const dataTwo = deepClone(data)

// dataTwo !== data

getUrlParams

getUrlParams提供了从一个url中提取参数的功能,并提供了配套了类型支持,它提供了两个参数

  • name: 要从路径中提取的key
  • url?: 要获取值的url,不传默认为location.href
import { getUrlParams } from '@asarua/tools'

const param = getUrlParams('a', 'baidu.com?a=1')
// param的类型为 => "1"

is

is提供了一组用与鉴别数据类型的方法,并为其提供了类型保护

  • isString
  • isBoolean
  • isNull
  • isUndefined
  • isNumber
  • isSymbol
  • isBigInt
  • isObject
  • isFunction
  • isArray
  • isDate
  • isMap
  • isSet
  • isWeakMap
  • isWeakSet
  • isRegExp
  • isWindow
  • isError
import { is } from '@asarua/tools'

const { isMap } = is

let map: Map<1, 2> | Map<3, 4>

if (isMap<1, 2>(map)) {
  map.get(1)
}

memorize

memorize提供了一个记忆话函数生成器,接收一个函数作为参数,返回一个记忆化的函数,函数中存在一个cache属性可以查看缓存的值,一般用于大型计算做性能优化

import { memorize } from '@asarua/tools'

function add(a, b) {
  return a + b
}

const memo = memorize(add)
const val = memo(1, 2)

console.log(memo) // [Function: memo] { cache: { '[1,2]': 3 } }

noop

noop是一个空函数,一般用于cps短路运算

import { noop } from '@asarua/tools'

function operation(val, opera = noop) {
  return opera(val)
}

License

MIT