Package Exports
- kayran
- kayran/dist/style.css
Readme
kayran / 七零八碎工具函数合集
Installation

waitFor / 以优雅方式书写 await
/**
* @param {Promise} 需要包装的Promise
* @returns {any[]} [0]为Promise.prototype.then返回的结果,[1]为Promise.prototype.catch返回的结果
*/
import { waitFor } from 'kayran'
const [res] = await waitFor(new Promise((resolve, reject) => {
resolve('res')
}))
console.log(res) // 'res'
const [, err] = await waitFor(new Promise((resolve, reject) => {
reject('err')
}))
console.log(err) // 'err'getMediaDuration / 获取音视频文件的时长
/**
* @param {File|string} 音视频url或二进制文件
* @returns {Promise<number>} 时长 单位秒 四舍五入
*/
import { getMediaDuration } from 'kayran'
getMediaDuration('https://xxx.mp4')typeOf / 获取变量的精确类型
动机:原生js的typeof等类型检测手段都存在各种缺陷
/**
* @param {any} 需要判断的变量
* @returns {string} 变量类型(全小写) 如string null undefined file...
*/
import { typeOf } from 'kayran'
typeOf(1) // 'number'highlightError / 高亮提示错误(可用于校验失败的表单项)
平滑滚动至校验失败的第一个表单项、并对所有校验失败的表单项产生震动效果
import 'kayran/dist/style.css'
import { highlightError } from 'kayran'
/**
* @param {string|Element|NodeList} [el='.el-form .el-form-item.is-error'] 错误元素或其DOMString
*/
highlightError(el)
// 或
/**
* @param {object} config? 配置
* @param {string|Element|NodeList} [config.el='.el-form .el-form-item.is-error'] 错误元素或其DOMString
* @param {(Element) => void} [config.scrollIntoView=el => el.scrollIntoView({
* behavior: 'smooth',
* block: 'center'
* })
* ] 定位至错误元素的实现
* @param {(Element) => boolean} [config.isVisible=element => {
* const rect = element.getBoundingClientRect()
* const yInView = rect.top < window.innerHeight && rect.bottom > 0
* const xInView = rect.left < window.innerWidth && rect.right > 0
* return yInView && xInView
* }
* ] 判断错误元素是否处于可见范围的实现
*/
highlightError(config)isEllipsis / 判断dom是否触发溢出省略(text-overflow: ellipsis)
/**
* @param {Element} 需要判断的元素
* @returns {boolean} 是否触发溢出省略
*/
import { isEllipsis } from 'kayran'
isEllipsis(document.querySelector('.text'))isEmpty & notEmpty / 判空 & 判非空
notEmpty() 等同于 !isEmpty()
/**
* @param {any} 需要判断的变量
* @returns {boolean} 结果
*/
import { isEmpty, notEmpty } from 'kayran'
isEmpty(0) // false
isEmpty({}) // true
isEmpty([]) // truejsonToFormData / json转FormData
/**
* @param {object} 需要转换的对象
* @returns {FormData} 结果
*/
import { jsonToFormData } from 'kayran'
const formData = jsonToFormData({
a: 1
})
formData.get('a') // 1paramFilter / 接口参数过滤器
/**
* @param {object} innerText style标签的innerText 或传对象指定style的各项属性
* @returns {Promise<Element>}
*/
import { paramFilter } from 'kayran'
await paramFilter({
a: 1,
b: NaN
})isSameOrigin / 判断是否同源
/**
* @param {string} url 需要判断的url
* @returns {boolean}
*/
import { isSameOrigin } from 'kayran'
isSameOrigin('www.google.com')parseQueryString / 获取 url 中某个查询参数的值
/**
* 获取当前url某个查询参数的值
* 支持微信公众号授权特例:授权后会重定向回来并在链接中加入一个code参数 但微信没有考虑hash路由的情况 所以获取这个code需要特殊处理
* @param {object}
* {string} key 查询参数的key 如果为空 则返回查询参数映射的对象 可以解构使用
* {string} [mode='hash'] router模式 可选值'history'/'hash'
* {boolean} del 是否在url中删除该值(删除会引发页面刷新)
* @returns {string|object} 查询参数中key对应的value / 如果key为空 则返回查询参数整个对象
*/
import { parseQueryString } from 'kayran'
const code = parseQueryString('code')
//or
const { code } = parseQueryString()注意:如果search和hash中同时包含code 如http://localhost:8080/?code=1#/?code=2 取的是search中的code 即返回1
loadLink / 动态加载link
/**
* @param {string|object} src link url
* @returns {Promise}
*/
import { loadLink } from 'kayran'
await loadLink('https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css')loadScript / 动态加载js
/**
* @param {string|object} src 脚本url
* @returns {Promise}
*/
import { loadScript } from 'kayran'
loadScript('https://cdn.jsdelivr.net/npm/vue/dist/vue.js').then(e => {
console.log(Vue)
})loadStyle / 动态加载style
/**
* @param {string|object|HTMLElement} arg style元素的innerText 或传对象指定style的各项属性 或style元素本身
* @returns {Promise<HTMLElement>}
*/
import { loadStyle } from 'kayran'
await loadStyle(`
ul {
list-style: none;
}
`)validator / 输入校验
/**
* @param {string} 需要校验的内容
* @returns {string} 校验失败提示信息 如果校验通过则返回''
*/
import { validator } from 'kayran'
const { idCard } = validator
const errMsg = idCard('xxx')
if (errMsg) {
Toast(errMsg) // '格式不正确'
return
}该方法不是通过true和false来标识校验是否通过 因为错误往往需要一个友好的原因提示
// 如果仅仅想知道是否校验通过
!idCard('xxx') // true通过 false失败lng / 经度
/**
* @param {string|number} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { lng } = validatorlat / 纬度
/**
* @param {string|number} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { lat } = validatorphone / 手机
/**
* @param {string|number} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { phone } = validatoripv4
/**
* @param {string} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { ipv4 } = validatoripv6
/**
* @param {string} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { ipv6 } = validatorurl
含ipv4 不含ipv6
/**
* @param {string} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { url } = validatorbase64
/**
* @param {string} 需要判断的字符串
* @param {object}
* {string} mediaType 媒体类型,可选值参考 https://en.wikipedia.org/wiki/Data_URI_scheme
* {boolean} [scheme=true] 是否包含前缀
* @returns {boolean} 结果
*/
import { validator } from 'kayran'
const { base64 } = validator
base64(1, {
mediaType: 'image',
scheme: false
}) // falseidCard / 身份证
/**
* @param {string|number} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { idCard } = validatorpostcode / 邮编
/**
* @param {string|number} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { postcode } = validatortel / 座机
/**
* @param {string|number} 需要判断的值
* @param {object}
* {boolean} [multiple=true] 是否允许多个
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { tel } = validatoremail / 邮箱
/**
* @param {string} 需要判断的值
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { email } = validatorlen / 字符长度
/**
* @param {string} 需要判断的值
* @param {
* number |
* [number, number] |
* {
* min?: number
* max?: number
* }
* }
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { len } = validator
len('123', [1, 3])int / 整数
/**
* @param {string|number} 需要判断的值
* @param {
* string |
* {
* range?: string
* positiveSign?: boolean
* }
* }
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { int } = validator
int(0, '(0,2)')decimal / 小数
/**
* @param {string|number} 需要判断的值
* @param {
* string |
* {
* range?: string,
* decimalPlaces?: number | [number, number]
* }
* }
* @returns {string} 判断结果
*/
import { validator } from 'kayran'
const { decimal } = validator
decimal(80, '[80,+∞]')
decimal('80.1', {
decimalPlaces: [0, 1]
})