2.6.2 • Published 6 years ago

changlin-util v2.6.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

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

ParamTypeDescription
stringstring
festringf or e or fe
charstring

Example

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

encodeToUnicode(str) ⇒ string

字符转unicode

Kind: global function

ParamTypeDescription
strstring需要转码的字符串

Example

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

decodeUnicode(str) ⇒ string

unicode字符串解码

Kind: global function

ParamTypeDescription
strstring需要解码的字符串

Example

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

firstUpperCase(string) ⇒ string

Capitalize the first letter

Kind: global function

ParamType
stringstring

Example

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

firstLowerCase(string) ⇒ string

Lowercase first letter

Kind: global function

ParamType
stringstring

Example

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

splitUnit(value, relative) ⇒ object

split number with unit

Kind: global function

ParamType
valuestring
relativeboolean

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

ParamType
minnumber
maxnumber

Example

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

createCombination(array, combinationLength)

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

Kind: global function

ParamType
arrayArray
combinationLengthNumber

computeFactorial(number) ⇒ number

计算阶乘

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

Kind: global function

ParamType
numbernumber

computeCombinationLength(elementNumber, combinationLength) ⇒ number

计算 组合数

Kind: global function

Param
elementNumber
combinationLength

toArray(s) ⇒ Array

类数组对象转化为数组

Kind: global function

ParamType
sObject

Example

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

removeFromArray(arr, condition, number) ⇒ Array

从数组中移除某些项

Kind: global function

ParamTypeDescription
arrArray
conditionNumber | functionif(number&&arrnumber remove arrnumber ; if(fn(item))remove item
numberNumber

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

ParamTypeDescription
arrArray
comparefunction比较函数

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

ParamTypeDescription
arrayArray
fnfunction过滤函数

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

ParamType
arrArray

Example

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

lastOneOf(arr)

获取数组最后一个元素

Kind: global function

ParamType
arrArray

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

ParamType
arrayArray | likeArray
isSamefunction | undefined

getOrSetProp(obj, prop)

返回或设置对象的属性值

Kind: global function

ParamTypeDescription
objobject
propstring必须以'.'分割

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

ParamType
dateDate | string | number
formatstring

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

ParamTypeDescription
fromDate | string | number较远的时间
nowDate | 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

ParamType
deepboolean | object
targetobject
sourceobject

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

ParamTypeDescription
typestringurl tel mobilePhone email account IdCard ip...参考regex 模块导出对象的属性
stringstring | number

Example

isType('email','user@163.com')   //=>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'))
2.6.2

6 years ago

2.6.1

6 years ago

2.6.0

6 years ago

2.5.2

6 years ago

2.5.1

6 years ago

2.5.0

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.4

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.7

7 years ago

2.2.6

7 years ago

2.2.5

7 years ago

2.2.4

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.16

7 years ago

2.1.14

7 years ago

2.1.13

7 years ago

2.1.12

7 years ago

2.1.11

7 years ago

2.1.10

7 years ago

2.1.9

7 years ago

2.1.8

7 years ago

2.1.7

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

1.0.4

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.3

7 years ago