npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

legions-utils-tool

v0.0.11

Published

常用工具类操作方法

Downloads

4

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
  • ...