legions-utils-tool

常用工具类操作方法

Usage no npm install needed!

<script type="module">
  import legionsUtilsTool from 'https://cdn.skypack.dev/legions-utils-tool';
</script>

README

legions-utils-tool

TODO: 常用工具类函数

Usage

npm install --save legions-utils-tool

API

cookie

设置cookie值

import {setCookie} from 'legions-utils-tool/cookie'

获取cookie值

import { getCookie } from 'legions-utils-tool/cookie';

移除cookie

import { removeCookie } from 'legions-utils-tool/cookie';

清空cookie

import { clearAllCookie } from 'legions-utils-tool/cookie';

debounce防抖函数

import {debounce} from 'legions-utils-tool/debounce';
debounce(()=>{
    //doSth
},1000)

DOM节点事件

DOM节点绑定事件

import {on} from 'legions-utils-tool/dom'

卸载DOM元素节点事件

import {off} from 'legions-utils-tool/dom'

download

支持一次下载多份文件

import {download} from 'legions-utils-tool/download'
download([文件链接数组])

format-date日期格式化

将日期格式化成指定格式的字符串

import {formatDate} from 'legions-utils-tool/format.date'

将字符串解析成日期

import {parseDate} from 'legions-utils-tool/format.date'

将一个日期格式化成友好格式,比如,1分钟以内的返回“刚刚”, 当天的返回时分,当年的返回月日,否则,返回年月日

import {formatDateToFriendly} from 'legions-utils-tool/format.date'

将一段时长转换成友好格式,如:1581->“26分21秒”

import {formatDurationToFriendly} from 'legions-utils-tool/format.date'

将时间转换成MM:SS形式

import {formatTimeToFriendly} from 'legions-utils-tool/format.date'

判断某一年是否是闰年

import {isLeapYear} from 'legions-utils-tool/format.date'

获取某一年某一月的总天数,没有任何参数时获取当前月份的

import {getMonthDays} from 'legions-utils-tool/format.date'

计算2日期之间的天数,用的是比较毫秒数的方法

import {countDays} from 'legions-utils-tool/format.date'

获取指定日期月数最后一天

import {getLastDay} from 'legions-utils-tool/format.date'

fomate-string字符串格式化

字符串去除空格

import {formatTrim} from 'legions-utils-tool/format.string'

千位分割

import {formatLocaleString} from 'legions-utils-tool/format.string'

保留指定位数小数

import {number} from 'legions-utils-tool/format.string'

金额保留两位小数

import {amount} from 'legions-utils-tool/format.string'

判断字符串是否是json

import {isJSON} from 'legions-utils-tool/format.string'

获取字符串长度

import {getStringLen} from 'legions-utils-tool/format.string'

object-utils对象的操作方法

对数组或对象进行自然排序

import {sort} from 'legions-utils-tool/object.utils'
// 升序
sort([3,2,4]) === [2, 3, 4]
// 根据指定的属性值进行排序
sort([{age:1,name:'jucy'},{age:4,name:'cody'},{age:2,name:'cinlo'}],'name') === [{age:2,name:'cinlo'},{age:4,name:'cody'},{age:1,name:'jucy'}]

通用排序

import {compare} from 'legions-utils-tool/object.utils'
compare('4','2') === 1
compare('2','2') === 0
compare('1','2') === -1

排除指定对象属性

import {excludeObj} from 'legions-utils-tool/object.utils'
excludeObj({age:1,name:'jucy',grade:2},{age:1,name:'jucy',sex:'girl',like:'football'}) === {sex:'girl',like:'football'}

promiseTry

import {promiseTry} from 'legions-utils-tool/object.utils'
promiseTry(()=>'xixi').then(res=>res === 'xixi')

proxyGetters

import {proxyGetters} from 'legions-utils-tool/object.utils'
let objTest = {}
proxyGetters(objTest,{'age':1,'name':'cici','sex':'girl'},['name'])
console.log(objTest['name'])//'cici'

regex常用正则表达式校验

import {RegExChk,commonRegex,validatorType} from 'legions-utils-tool/regex'
RegExChk(commonRegex.PRICE,'10000000000.3111111')//使用已封装好的正则表达式或者自己书写的正则表达式
RegExChk(validatorType.chinese,'中国,我爱中国')//使用已有的正则表达式类型

type-validation数据基本类型判断

import {isObject,isUndefined,isNull,isPromise,isNullUndefined} from 'legions-utils-tool/type.validation'

storage

设置相应键值缓存信息

import {setStorageItems} from 'legions-utils-tool/storage'

获取相应键值缓存信息

import {getStorageItem} from 'legions-utils-tool/storage'

state.machine

used

import {stateMachine} from 'legions-utils-tool/state.machine';

var fsm = new stateMachine({
    init: 'solid',
    transitions: [
      { name: 'melt',     from: 'solid',  to: 'liquid' },
      { name: 'freeze',   from: 'liquid', to: 'solid'  },
      { name: 'vaporize', from: 'liquid', to: 'gas'    },
      { name: 'condense', from: 'gas',    to: 'liquid' }
    ],
    methods: {
      onMelt:   function() { console.log('I melted')    },
      onFreeze:   function() { console.log('I froze')     },
      onVaporize: function() { console.log('I vaporized') },
      onCondense: function() { console.log('I condensed') }
    }
  });

... which creates an object with a current state property:

  • fsm.state

... methods to transition to a different state:

  • fsm.melt()
  • fsm.freeze()
  • fsm.vaporize()
  • fsm.condense()

... observer methods called automatically during the lifecycle of a transition:

  • onMelt()
  • onFreeze()
  • onVaporize()
  • onCondense()

... along with the following helper methods:

  • fsm.is(s) - return true if state s is the current state
  • fsm.can(t) - return true if transition t can occur from the current state
  • fsm.cannot(t) - return true if transition t cannot occur from the current state
  • fsm.transitions() - return list of transitions that are allowed from the current state
  • fsm.allTransitions() - return list of all possible transitions
  • fsm.allStates() - return list of all possible states

Terminology

A state machine consists of a set of States

  • solid
  • liquid
  • gas

A state machine changes state by using Transitions

  • melt
  • freeze
  • vaporize
  • condense

A state machine can perform actions during a transition by observing Lifecycle Events

  • onBeforeMelt
  • onAfterMelt
  • onLeaveSolid
  • onEnterLiquid
  • ...