changlin-util
整理的一些工具函数
快速使用
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
- regex
-
常用正则表达式
Constants
Functions
-
trim(string, fe, char) ⇒
string
-
字符串两端剪切
-
encodeToUnicode(str) ⇒
string
-
字符转unicode
-
decodeUnicode(str) ⇒
string
-
unicode字符串解码
-
firstUpperCase(string) ⇒
string
-
Capitalize the first letter
-
firstLowerCase(string) ⇒
string
-
Lowercase first letter
-
splitUnit(value, relative) ⇒
object
-
split number with unit
-
randomInteger(min, max) ⇒
number
-
生成一定范围内的随机整数 (包括端点)
- createCombination(array, combinationLength)
-
从一个数组中获取一定长度的所有组合
-
computeFactorial(number) ⇒
number
-
计算阶乘
computeFactorial(0) //=>1 computeFactorial(3) //=>6
-
computeCombinationLength(elementNumber, combinationLength) ⇒
number
-
计算 组合数
-
toArray(s) ⇒
Array
-
类数组对象转化为数组
-
removeFromArray(arr, condition, number) ⇒
Array
-
从数组中移除某些项
-
sort(arr, compare) ⇒
Array
-
排序
-
find(array, fn) ⇒
any
-
找出数组某一个元素
-
shuffle(arr) ⇒
Array
-
乱序。返回原(类)数组
- lastOneOf(arr)
-
获取数组最后一个元素
-
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]
- getOrSetProp(obj, prop)
-
返回或设置对象的属性值
-
dateFormat(date, format) ⇒
string
-
时间格式化
-
fromTime(from, now) ⇒
string
-
获取时间段
-
extend(deep, target, source) ⇒
object
-
对象扩展
-
isType(type, string) ⇒
boolean
-
类型判断
-
isFunction() ⇒
boolean
-
判断值是否为function
-
isUndefined() ⇒
boolean
-
判断值是否为undefined
-
isWindow() ⇒
boolean
-
判断值是否为window
-
isString() ⇒
boolean
-
判断值是否为string
-
isNumber() ⇒
boolean
-
判断值是否为number
-
isObject() ⇒
boolean
-
判断值是否为object(注意:此方法使用Object.prototype.toString.call(value)进行判断)
-
isGeneralizedObject() ⇒
boolean
-
判断值是否为广义的object(注意:此方法使用typeof进行判断)
-
isDate() ⇒
boolean
-
判断值是否为Date
-
isPlainObject() ⇒
boolean
-
判断值是否为Plain Object
-
isLikeArray() ⇒
boolean
-
判断值是否类似array
-
isArray() ⇒
boolean
-
判断值是否为Array
-
isBoolean() ⇒
boolean
-
判断值是否为boolean
-
whatIs() ⇒
string
-
判断值的类型
-
isDOM() ⇒
boolean
-
判断值是否为dom对象
regex
常用正则表达式
Kind: global variable
Properties
Name |
---|
number |
empty |
integer |
positiveInteger |
positiveNumber |
url |
tel |
mobilePhone |
account |
IdCard |
ip |
numberWithUnit |
relativeNumberWithUnit |
ONE_SEC
ONE_MIN
ONE_HOUR
ONE_DAY
ONE_MONTH
ONE_YEAR
string
trim(string, fe, char) ⇒ 字符串两端剪切
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'
string
encodeToUnicode(str) ⇒ 字符转unicode
Kind: global function
Param | Type | Description |
---|---|---|
str | string |
需要转码的字符串 |
Example
encodeToUnicode('啊abc123.')
//=>"\u554a\u0061\u0062\u0063\u0031\u0032\u0033\u002e"
string
decodeUnicode(str) ⇒ unicode字符串解码
Kind: global function
Param | Type | Description |
---|---|---|
str | string |
需要解码的字符串 |
Example
decodeUnicode('\u554a\u0061\u0062\u0063\u0031\u0032\u0033\u002e')
//=>"啊abc123."
string
firstUpperCase(string) ⇒ Capitalize the first letter
Kind: global function
Param | Type |
---|---|
string | string |
Example
firstUpperCase('abc')//=>'Abc'
string
firstLowerCase(string) ⇒ Lowercase first letter
Kind: global function
Param | Type |
---|---|
string | string |
Example
firstLowerCase('Abc')//=>'abc'
object
splitUnit(value, relative) ⇒ 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:'%'}
number
randomInteger(min, max) ⇒ 生成一定范围内的随机整数 (包括端点)
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 |
number
computeFactorial(number) ⇒ 计算阶乘
computeFactorial(0) //=>1
computeFactorial(3) //=>6
Kind: global function
Param | Type |
---|---|
number | number |
number
computeCombinationLength(elementNumber, combinationLength) ⇒ 计算 组合数
Kind: global function
Param |
---|
elementNumber |
combinationLength |
Array
toArray(s) ⇒ 类数组对象转化为数组
Kind: global function
Param | Type |
---|---|
s | Object |
Example
toArray({'0':123,'2':456,length:3})
//=>[123,456,undefined]
Array
removeFromArray(arr, condition, number) ⇒ 从数组中移除某些项
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}]
Array
sort(arr, compare) ⇒ 排序
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]
any
find(array, fn) ⇒ 找出数组某一个元素
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
Array
shuffle(arr) ⇒ 乱序。返回原(类)数组
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
Array
excludeTheSame(array, isSame) ⇒ 数组去重,不对传入对象进行操作,返回一个新的数组
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
string
dateFormat(date, format) ⇒ 时间格式化
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
string
fromTime(from, now) ⇒ 获取时间段
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) //=>刚刚
object
extend(deep, target, source) ⇒ 对象扩展
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
boolean
isType(type, string) ⇒ 类型判断
Kind: global function
Param | Type | Description |
---|---|---|
type | string |
url tel mobilePhone email account IdCard ip...参考regex 模块导出对象的属性 |
string |
string | number
|
Example
isType('email','user@163.com') //=>true
boolean
isFunction() ⇒ 判断值是否为function
boolean
isUndefined() ⇒ 判断值是否为undefined
boolean
isWindow() ⇒ 判断值是否为window
boolean
isString() ⇒ 判断值是否为string
boolean
isNumber() ⇒ 判断值是否为number
boolean
isObject() ⇒ 判断值是否为object(注意:此方法使用Object.prototype.toString.call(value)进行判断)
boolean
isGeneralizedObject() ⇒ 判断值是否为广义的object(注意:此方法使用typeof进行判断)
boolean
isDate() ⇒ 判断值是否为Date
boolean
isPlainObject() ⇒ 判断值是否为Plain Object
boolean
isLikeArray() ⇒ 判断值是否类似array
boolean
isArray() ⇒ 判断值是否为Array
boolean
isBoolean() ⇒ 判断值是否为boolean
string
whatIs() ⇒ 判断值的类型
Kind: global function
Example
whatIs(new Date())//=>'date'
whatIs(null)//=>'null'
boolean
isDOM() ⇒ 判断值是否为dom对象
Kind: global function
Example
isDOM(document.querySelector('div'))