Util

工具库 / Util

这里提供了Vimo使用的工具库, 当然业务代码中也可以按需使用.

源码:

用法 / Usages

import {isBoolean, isString} from 'vimo/lib/util/util'

方法 / Methods

(static) clamp(min, n, max)

源码:

如果n的大小在max和min之间,则返回n, 否则返回最大最小值

示例 / Example
clamp(1,5,10)  -> 5
clamp(6,5,10)  -> 6
clamp(1,5,4)   -> 4
参数 / Parameters:
Name Type Description
min number

最小值

n number

测试值

max number

最大值

(static) defaults(dest)

源码:

优先使用最左边的对象中的数据,即保持默认值,当在第一个对象中没找到key时才添加新key

示例 / Example
defaults({a:1},{b:1,a:2},{b:10}) => 返回第一个对象 {a: 1, b: 10}
参数 / Parameters:
Name Type Description
dest any

the destination to apply defaults to.

(static) docReady(callbackopt) → {Promise}

源码:

document的ready事件监听

参数 / Parameters:
Name Type Attributes Description
callback function <optional>

回调函数

返回 / Returns:
  • 返回promise,completed后自动解绑

(static) firstUpperCase(str) → {string}

源码:

首字母大写

参数 / Parameters:
Name Type Description
str string

传入string

(static) focusOutActiveElement() → {boolean}

源码:

blur out TEXTAREA或者INPUT的状态

(static) hasClass()

源码:

元素的class操作

(static) hasFocus(ele) → {boolean}

源码:

判断元素是否在focus状态, 比如input

参数 / Parameters:
Name Type Description
ele HTMLElement

元素

(static) isActive(ele) → {boolean}

源码:

判断元素是否在激活状态, 比如input

参数 / Parameters:
Name Type Description
ele HTMLElement

元素

(static) isArray(val) → {Boolean}

源码:

判断传入值为 Array

参数 / Parameters:
Name Type Description
val *

判断值

(static) isBlank(val) → {Boolean}

源码:

判断传入值为空

参数 / Parameters:
Name Type Description
val *

判断值

(static) isBoolean(val) → {Boolean}

源码:

判断传入值是否为 Boolean

参数 / Parameters:
Name Type Description
val *

判断值

(static) isCheckedProperty(a, b)

源码:

判断Checked属性的值
判断是否相等,比如:'true'和true,'0'和0

示例 / Example
undefined == null
undefined == ''
null == ''
true == 'true'
false == 'false'
0 == '0'
参数 / Parameters:
Name Type Description
a *

判断的第一个值

b *

判断的第二个值
return {Boolean}

(static) isDate(val) → {Boolean}

源码:

判断传入值为 Date类型

参数 / Parameters:
Name Type Description
val *

判断值

(static) isDefined(val) → {Boolean}

源码:

判断传入值已定义

参数 / Parameters:
Name Type Description
val *

判断值

(static) isFunction(val) → {Boolean}

源码:

判断传入值是否为 Function

参数 / Parameters:
Name Type Description
val *

判断值

(static) isNumber(val) → {Boolean}

源码:

判断传入值是否为 Number

参数 / Parameters:
Name Type Description
val *

判断值

(static) isObject(val) → {Boolean}

源码:

判断传入值为 Object

参数 / Parameters:
Name Type Description
val *

判断值

(static) isPassive() → {Boolean}

源码:

判断的当前浏览器是否支持isPassive属性

(static) isPlainObject(val) → {Boolean}

源码:

判断传入值为 纯对象

参数 / Parameters:
Name Type Description
val *

判断值

(static) isPresent(val) → {Boolean}

源码:

判断传入值不为空

参数 / Parameters:
Name Type Description
val *

判断值

(static) isPrimitive(val) → {Boolean}

源码:

判断传入值为 基础变量

参数 / Parameters:
Name Type Description
val *

判断值

(static) isRegexp(val) → {Boolean}

源码:

判断传入值为 Regexp

参数 / Parameters:
Name Type Description
val *

判断值

(static) isString(val) → {Boolean}

源码:

判断传入值是否为 String

参数 / Parameters:
Name Type Description
val *

判断值

(static) isTrueProperty(val)

源码:

判断元素属性是否存在设置
一般vue的props不需要这个, 因为其内部会进行这个判断, 没有传入值则为false

示例 / Example
'true' => true
'on' => true
'' => true
参数 / Parameters:
Name Type Description
val string

判断值

(static) isUndefined(val) → {Boolean}

源码:

判断传入值未定义

参数 / Parameters:
Name Type Description
val *

判断值

(static) parsePxUnit(val) → {number}

源码:

将带px单位的string转化为数字

示例 / Example
10px -> 10
参数 / Parameters:
Name Type Description
val string

传入的string

(static) pointerCoord(ev) → {PointerCoordinates}

源码:

根据click或者touch的事件对象, 获取event事件对象中的点击位置(坐标xy值)

参数 / Parameters:
Name Type Description
ev any

事件对象

(static) registerListener(ele, eventName, callback, optsopt, unregisterListenersCollectionopt) → {function}

源码:

给addEventListener增加passive属性, 如果不支持将降级使用!!opts.capture, 事件的关闭需要自己手动解除, 切记!!

参数 / Parameters:
Name Type Attributes Description
ele any

监听的元素

eventName string

监听的名称

callback function

回调

opts object <optional>

addEventListener的第三个参数 EventListenerOptions

Properties
Name Type Attributes Description
capture object <optional>

capture

passive object <optional>

passive

unregisterListenersCollection array <optional>

如果提供Function[], 则unReg将压如这个列表中

返回 / Returns:
  • 返回removeEventListener的函数

(static) setElementClass(ele, className, add)

源码:

元素的class操作

参数 / Parameters:
Name Type Description
ele HTMLElement

添加、删除class的元素

className string
add boolean

是添加还是删除

(static) transitionEnd(el, callbackFn) → {function}

源码:

transitionEnd事件注册,绑定的函数触发后会自动解绑

参数 / Parameters:
Name Type Description
el HTMLElement

绑定的元素

callbackFn function

绑定的函数

返回 / Returns:
  • 取消绑定的函数

(static) urlChange(callback) → {function}

源码:

urlChange(popstate)注册,绑定的函数触发后会自动解绑

参数 / Parameters:
Name Type Description
callback function

回调函数

返回 / Returns:
  • 解绑函数

类型定义 / Type Definitions

PointerCoordinates

源码:
属性 / Properties:
Name Type Description
x number

x坐标

y number

y坐标

坐标对象

类型 / Type:
  • Object