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 🙏

© 2024 – Pkg Stats / Ryan Hefner

changlin-util

v2.6.2

Published

some utils

Downloads

19

Readme

changlin-util

整理的一些工具函数

language npm version Build Status npm

快速使用

npm install changlin-util

import {
    isType,
    isObject,
    isString,
    extend,
    regex,
    trim,
    removeFromArray,
    is,
    shuffle,
    randomInteger,
    toArray,
    whatIs
} from 'changlin-util'
//或者
import {isType,isString} from  'changlin-util/dist/is'

API

Members

Constants

Functions

regex

常用正则表达式

Kind: global variable
Properties

| Name | | --- | | number | | empty | | integer | | positiveInteger | | positiveNumber | | url | | tel | | mobilePhone | | email | | account | | IdCard | | ip | | numberWithUnit | | relativeNumberWithUnit |

ONE_SEC

Kind: global constant

ONE_MIN

Kind: global constant

ONE_HOUR

Kind: global constant

ONE_DAY

Kind: global constant

ONE_MONTH

Kind: global constant

ONE_YEAR

Kind: global constant

trim(string, fe, char) ⇒ string

字符串两端剪切

Kind: global function

| Param | Type | Description | | --- | --- | --- | | string | string | | | fe | string | f or e or fe | | char | string | |

Example

trim('   abc   ')//=>'abc'
trim('   abc   ','f')//=>'abc   '
trim('   abc   ','e')//=>'   abc'
trim('**abc**','*')//=>'abc'

encodeToUnicode(str) ⇒ string

字符转unicode

Kind: global function

| Param | Type | Description | | --- | --- | --- | | str | string | 需要转码的字符串 |

Example

 encodeToUnicode('啊abc123.')
 //=>"\u554a\u0061\u0062\u0063\u0031\u0032\u0033\u002e"

decodeUnicode(str) ⇒ string

unicode字符串解码

Kind: global function

| Param | Type | Description | | --- | --- | --- | | str | string | 需要解码的字符串 |

Example

 decodeUnicode('\u554a\u0061\u0062\u0063\u0031\u0032\u0033\u002e')
 //=>"啊abc123."

firstUpperCase(string) ⇒ string

Capitalize the first letter

Kind: global function

| Param | Type | | --- | --- | | string | string |

Example

 firstUpperCase('abc')//=>'Abc'

firstLowerCase(string) ⇒ string

Lowercase first letter

Kind: global function

| Param | Type | | --- | --- | | string | string |

Example

 firstLowerCase('Abc')//=>'abc'

splitUnit(value, relative) ⇒ object

split number with unit

Kind: global function

| Param | Type | | --- | --- | | value | string | | relative | boolean |

Example

 splitUnit('123px')//=>{value:123,unit:'px'}
 splitUnit('123%')//=>{value:123,unit:'%'}
 splitUnit('+123%')//=>{value:123,unit:'%'}
 splitUnit('-123%')//=>{value:-123,unit:'%'}
 splitUnit('-=123%',true)//=>{value:'-=123',unit:'%'}
 splitUnit('+=123%',true)//=>{value:'+=123',unit:'%'}

randomInteger(min, max) ⇒ number

生成一定范围内的随机整数 (包括端点)

Kind: global function

| Param | Type | | --- | --- | | min | number | | max | number |

Example

let res=randomInteger(4)
res>=0&&res<=4      //true
isType('integer',res)//true

createCombination(array, combinationLength)

从一个数组中获取一定长度的所有组合

Kind: global function

| Param | Type | | --- | --- | | array | Array | | combinationLength | Number |

computeFactorial(number) ⇒ number

计算阶乘

computeFactorial(0) //=>1
computeFactorial(3)  //=>6

Kind: global function

| Param | Type | | --- | --- | | number | number |

computeCombinationLength(elementNumber, combinationLength) ⇒ number

计算 组合数

Kind: global function

| Param | | --- | | elementNumber | | combinationLength |

toArray(s) ⇒ Array

类数组对象转化为数组

Kind: global function

| Param | Type | | --- | --- | | s | Object |

Example

toArray({'0':123,'2':456,length:3})
//=>[123,456,undefined]

removeFromArray(arr, condition, number) ⇒ Array

从数组中移除某些项

Kind: global function

| Param | Type | Description | | --- | --- | --- | | arr | Array | | | condition | Number | function | if(number&&arr[number] remove arr[number] ; if(fn(item))remove item | | number | Number | |

Example

let a=[1,2,3];
removeFromArray(a,1)//=>[2]
a//=>[1,3]

let b=[{id:1},{id:2},{id:3}];
removeFromArray(b,(n)=>n.id===3)//=>[{id:3}]
b//=>[{id:1},{id:2}]

sort(arr, compare) ⇒ Array

排序

Kind: global function

| Param | Type | Description | | --- | --- | --- | | arr | Array | | | compare | function | 比较函数 |

Example

let a=[1,3,,,2];
sort(a,()=>true)//=>[2,3,1,undefined,undefined]
a//=>[2,3,1,undefined,undefined]

let arrb=[1,3,5,4,2,7,6]
sort(arrb,(a,b)=>a>b)//[1,2,3,4,5,6,7]

find(array, fn) ⇒ any

找出数组某一个元素

Kind: global function

| Param | Type | Description | | --- | --- | --- | | array | Array | | | fn | function | 过滤函数 |

Example

find([1,2,'2',3,4,5],function(a){return a==='2'})//=>'2'
find([1,2,'2',3,4,5],function(a){return a===8})//=>undefined

shuffle(arr) ⇒ Array

乱序。返回原(类)数组

Kind: global function

| Param | Type | | --- | --- | | arr | Array |

Example

let arr1=[1,2,3];
let res=shuffle(arr1);
res===arr1//=>true
res.length===3//true

lastOneOf(arr)

获取数组最后一个元素

Kind: global function

| Param | Type | | --- | --- | | arr | Array |

Example


lastOneOf([1,2,3])//=>3

excludeTheSame(array, isSame) ⇒ Array

数组去重,不对传入对象进行操作,返回一个新的数组


excludeTheSame([1, 2, , 2, , , 5])//=> [1,2,undefined,5]
excludeTheSame([1, 2, , 2, , , 5],(a,b)=>a===b)//=> [1,2,undefined,5]

Kind: global function

| Param | Type | | --- | --- | | array | Array | likeArray | | isSame | function | undefined |

getOrSetProp(obj, prop)

返回或设置对象的属性值

Kind: global function

| Param | Type | Description | | --- | --- | --- | | obj | object | | | prop | string | 必须以'.'分割 |

Example

let obj={a:{b:{c:{d:3}}}}
getOrSetProp(obj,'a.b.c.d') //=>3
getOrSetProp(obj,'a.b.c.d',4) //=>4

dateFormat(date, format) ⇒ string

时间格式化

Kind: global function

| Param | Type | | --- | --- | | date | Date | string | number | | format | string |

Example

dateFormat(new Date(), 'yyyy/MM/dd hh:mm:ss')
dateFormat(1478836800000, 'yyyy-MM-dd') //=>2016-11-11

fromTime(from, now) ⇒ string

获取时间段

Kind: global function

| Param | Type | Description | | --- | --- | --- | | from | Date | string | number | 较远的时间 | | now | Date | string | number | undefined | 较近的时间 |

Example

 let t1 = new Date(1478836800000);
 let t2 = new Date(1478836800100);
  fromTime(t1, t2) //=>刚刚

extend(deep, target, source) ⇒ object

对象扩展

Kind: global function

| Param | Type | | --- | --- | | deep | boolean | object | | target | object | | source | object |

Example

//deep  false
 const source  = {a: 1, b: 2, c: {c1: 1}};
const res  = extend(false, {}, source);
source.c.c1 = 4;
res.c.c1===4//=>true

//deep true
const source  = {a: 1, b: 2, c: {c1: 1}};
const res  = extend(true, {}, source);
source.c.c1 = 4;
res.c.c1===4//=>false

//extend(true,  source)
const source={a: 1, b: 2, c: {c1: 1}}
const target = extend(true,  source);
target.c.c1=2
source.c.c1//=>1

isType(type, string) ⇒ boolean

类型判断

Kind: global function

| Param | Type | Description | | --- | --- | --- | | type | string | url tel mobilePhone email account IdCard ip...参考regex 模块导出对象的属性 | | string | string | number | |

Example

isType('email','[email protected]')   //=>true

isFunction() ⇒ boolean

判断值是否为function

Kind: global function

isUndefined() ⇒ boolean

判断值是否为undefined

Kind: global function

isWindow() ⇒ boolean

判断值是否为window

Kind: global function

isString() ⇒ boolean

判断值是否为string

Kind: global function

isNumber() ⇒ boolean

判断值是否为number

Kind: global function

isObject() ⇒ boolean

判断值是否为object(注意:此方法使用Object.prototype.toString.call(value)进行判断)

Kind: global function

isGeneralizedObject() ⇒ boolean

判断值是否为广义的object(注意:此方法使用typeof进行判断)

Kind: global function

isDate() ⇒ boolean

判断值是否为Date

Kind: global function

isPlainObject() ⇒ boolean

判断值是否为Plain Object

Kind: global function

isLikeArray() ⇒ boolean

判断值是否类似array

Kind: global function

isArray() ⇒ boolean

判断值是否为Array

Kind: global function

isBoolean() ⇒ boolean

判断值是否为boolean

Kind: global function

whatIs() ⇒ string

判断值的类型

Kind: global function
Example

whatIs(new Date())//=>'date'
whatIs(null)//=>'null'

isDOM() ⇒ boolean

判断值是否为dom对象

Kind: global function
Example

isDOM(document.querySelector('div'))