1.0.5 • Published 8 months ago
wil-utils v1.0.5
wil-utils
常用工具集合
- query: 获取url参数值
- throttle: 截流函数
- debounce:延迟/弹簧函数
- cookie 操作
- webview webview 环境检测
- hash 查询/添加 location.hash
- scrollToDistance 缓动滚动到某段距离
- emoji2Unicode emoji表情字符转Unicode
安装
npm install wil-utils --save
API
query 模块
import { query } from 'wil-utils';
// http://192.168.31.74:8080/test.html?a=122
console.log('query a的值为:', query.get('a')); // 122
console.log('query a=100:', query.add(location.href, 'a', 100)); // 返回http://192.168.31.74:8080/test.html?a=100
query.get(key)
返回location.href的query值
属性 | 类型 | 默认值 | 是否必填 | 说明 |
---|---|---|---|---|
key | String | 无 | 是 |
query.add(url, key, value)
给指定的url字符串增加query值并返回
属性 | 类型 | 默认值 | 是否必填 | 说明 |
---|---|---|---|---|
url | String | 无 | 是 | |
key | String | 无 | 是 | |
value | String|Number | 无 | 是 |
throttle 模块
截流函数
import { throttle } from 'wil-utils'; throttle(function () { console.log('touch move'); }, 150);
Option
参数 | 类型 | 是否必填 | 描述 |
---|---|---|---|
fn | Function | 是 | 传入需要截流的函数 |
interval | Number | 是 | 间隔执行时间,单位是毫秒 |
immediately | Boolean | 否 | 是否需要立即执行一次 |
debounce 模块
防抖函数
import { debounce } from 'wil-utils'; debounce(function () { console.log('handle click'); }, 150);
Option
param | 类型 | 描述 |
---|---|---|
fn | Function | 实际要执行 debounce的函数 |
delay | Number | 延迟时间,单位是毫秒 |
cookie 模块
import { cookie } from 'wil-utils';
cookie.set('tel', '18600000001');
cookie.get('tel');
cookie.get(key)
param | 类型 | 描述 |
---|---|---|
Key | String | 获取的cookie键值 |
cookie.set(key, value, domain, second, path)
param | 类型 | 描述 | 默认值 |
---|---|---|---|
Key | String | 设置的cookie key | 必填 |
value | String | 设置的cookie value | 必填 |
domain | String | 设置的cookie domain | 无 |
second | Number | 设置的cookie 的过期时间(秒) | 无 |
path | String | 设置的cookie path | 无 |
webview 模块
检测当前浏览器环境
import { webview } from 'wil-utils'; console.log(webview.isApple);
属性 说明 isWeibo 是否是微博客户端内 isQQ 是否是QQ客户端内 isDingtalk 是否是钉钉客户端内 isWeixin 是否是微信客户端内 isAndroid 是否是安卓设备 isApple 是否是苹果设备 isIphone 是否是IPhone设备 iphoneXSet 是否是全面屏的iPhone设备 isTablet 是否是平板设备 isIpad 是否是iPad设备 isAndroidTable 是否是安卓平板设备
hash 模块
import { hash } from 'wil-utils';
console.log(hash.get('tel'));
hash.get(key)
返回location.hash中某个key的值
属性 | 类型 | 默认值 | 是否必填 | 说明 |
---|---|---|---|---|
key | String | 无 | 是 | 要查询的字段名 |
hash.add(key, value)
location.hash添加某个格式为key=value的值
属性 | 类型 | 默认值 | 是否必填 | 说明 |
---|---|---|---|---|
key | String | 无 | 是 | 字段名 |
value | String|Number | 无 | 是 | 字段值 |
scrollToDistance
scrollToDistance(container, distance, duration, cb);
参数 | 类型 | 默认值 | 是否必填 | 说明 |
---|---|---|---|---|
container | dom | 无 | 是 | 滚动容器 |
distance | Number | 无 | 是 | 相对当前所处位置的滚动距离,负值表示向下滚动,正值表示向上滚动 |
duration | Number | 无 | 是 | 滚动时长,毫秒 |
cb | Function | 无 | 是 | 滚动结束回调 |
emoji2Unicode
emoji2Unicode(str);
参数 | 类型 | 默认值 | 是否必填 | 说明 |
---|---|---|---|---|
str | string | 无 | 是 | 带emoji(😊)表情的字符串 |
1.0.5
8 months ago