0.0.4 • Published 3 years ago

alaco v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

alaco v0.0.4

The alaco is a helper of tools for Javascript .

Installation

  1. Using npm :
$ npm i --save alaco
  1. Using <script> tag :
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- it's usually `node_modules/dist/alaco.js` or other cdn uri. -->
  <script src="path/to/alaco.js"></script>
</head>
<body>
  <script>
    console.log(alaco)
  </script>
</body>
</html>

Usage

  1. Using es module :

import * as tool from "alaco"
console.log(tool)
  1. Using CommonJS
const alaco = require("./dist/alaco")
console.log(alaco)
  1. Using <script> tag :
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="dist/alaco.js"></script>
</head>
<body>
  <script>
    console.log(alaco)
  </script>
</body>
</html>

alaco API

String

getBytes(str, encodedType)

  • @description 计算字符串在特定编码下占用的字节数
  • @param {String} str 字符串, 必填
  • @param {String} encodedType 编码类型,默认值 'utf8', 可选
  • @returns {Number} 字符串 str 在 encodedType 编码下占用的字节数
import { getBytes } from "alaco" 
getBytes("aaa啊aaa") // 9
getBytes("aaa啊aaa","utf-16") // 14

Fuctions

curry(fn, ...args)

  • @description 柯里化函数
  • @param {Function} fn 将要被柯里化的函数, 必填
  • @param {Array} args 将要传递给 fn 的参数列表, 可选
  • @returns {Function} 目标函数, 可接收不定长参数
import { curry } from "alaco"
const sum = (a, b, c) => a + b + c
const curriedSum = curry(sum,1)
const res = curriedSum(1,2) // 4
const res1 = curriedSum(3)(3) // 7

const curriedSum2 = curry(sum)
const res2 = curriedSum2(1)(2)(3) // 6

before(targetFn, beforeFn, maxTimes)

  • @description 拦截函数, 在目标函数执行之前, 先执行自定义函数
  • @param {Function} targetFn 目标函数, 必填
  • @param {Function} beforeFn 自定义函数, 必填
  • @param {Number} maxTimes beforeFn最多执行的次数, 可选
  • @returns {Function} 目标函数, 可接收不定长参数
import { before } from "alaco"
const logName = name => console.log(name)
const bFn = ()=> console.log('this executed bofore...')
const beforeLogName = before(logName,bFn)
beforeLogName('Lucy')

// beforeLogName2 执行两次后便不再执行 
const beforeLogName2 = before(logName,bFn,2)
beforeLogName2('Lucy')
beforeLogName2('Lili')
beforeLogName2('Tom')

after(targetFn, afterFn, maxTimes)

  • @description 拦截函数, 在目标函数执行之后, 再执行自定义函数
  • @param {Function} targetFn 目标函数, 必填
  • @param {Function} afterFn 自定义函数, 必填
  • @param {Number} maxTimes afterFn最多执行的次数
  • @returns {Function} 目标函数, 可接收不定长参数
const logName = name => console.log(name)
const aFn = () => console.log('this executed after logName...')
const afterLogName = after(logName, aFn)
afterLogName("Sven")

// afterLogName2 执行两次后便不再执行 
const afterLogName2 = after(logName,aFn,2)
afterLogName2('Lucy')
afterLogName2('Lili')
afterLogName2('Tom')

util

isFunc(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是函数类型返回 true,否则返回 false

isNum(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是数字类型返回 true,否则返回 false

isDate(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是日期类型返回 true,否则返回 false

isReg(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是正则类型返回 true,否则返回 false

isArray(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是数组类型返回 true,否则返回 false

isNull(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 null 返回 true,否则返回 false

isUnd(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 undefined 返回 true,否则返回 false

isSym(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 Symbol 类型返回 true,否则返回 false

isObj(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 Object 类型返回 true,否则返回 false

isBool(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是布尔类型返回 true,否则返回 false

isSet(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 Set 集合类型返回 true,否则返回 false

isMap(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 Map 映射类型返回 true,否则返回 false

isUN(data)

  • data 将要检测的数据类型, 可选
  • @returns {Boolean} 是 null 或者是 undefined 返回 true,否则返回 false