jes-utils
v2.0.8
Published
js通用函数工具封装
Downloads
11
Readme
jes-utils collections for JavaScript
Jes-utils is a front-end tool library, mainly including BOM, DOM, Regex, Calculator, Client, Common, Date, File, Storage, Geo, Canvas, Event and so on. These main aspects encapsulate some Common methods.
Later on, it will be possible to integrate richer toolchains and accumulate more common methods for easier development
Installation
To use with node:
$ npm install jes-utils --save
cmd:
const Utils = require('jes-utils');
amd:
define(['jes-utils'], function(Utils) { // todo })
To use directly in the browser (iife):
<script src="path/dist/jes-utils.js"></script>
or in entry point:
require('jes-utils')
es:
// cmd
const Utils = require('jes-utils')
// es - require module
import { blobToFile } from 'jes-utils/lib/File'
import { getStyle } from 'jes-utils/lib/Dom'
// es - require all
import { Bower, Dom } from 'jes-utils'
// as
import * as Bower from 'jes-utils/lib/Bower'
babel config
.babelrc
"plugin": [["import", {"libraryName": "jes-utils", "libraryDirectory": "lib", "camel2DashComponentName": false}]]
Usage:
Utils.Bower.currentURL()
Bower.currentURL()
getStyle()
The following is the method document provided by js-utils:
Members
Constants
Functions
factorialize ⇒ number
阶乘算法 factorialize
Kind: global variable
Returns: number - - factorialize
| Type | Description | | --- | --- | | number | number |
getFibonacci ⇒ number
生成菲波那切数列 getFibonacci
Kind: global variable
Returns: number - - value
| Type | Description | | --- | --- | | number | number |
stripHTMLTags ⇒ string
删除字符串中的HTMl标签
Kind: global variable
| Type | Description | | --- | --- | | string | str - html模版 |
scrollToTop
平滑滚动到页面顶部
Kind: global constant
smoothScroll
滚动到指定元素区域
Kind: global constant
| Param | Type | | --- | --- | | element | * |
getScrollPosition
返回当前滚动条位置
Kind: global constant
| Param | Type | | --- | --- | | el | * |
requestAnimFrame ⇒ function
requestAnimationFrame 制作动画
Kind: global constant
Returns: function - - 动画全局对象
cancelAnimFrame ⇒ function
cancelAnimFrame 取消动画
Kind: global constant
Returns: function - - 动画全局对象
formatMoney
金钱格式化,三位加逗号
Kind: global constant
| Param | Type | | --- | --- | | num | number |
checkIosAndroidIpad
判断是终端类型,值有ios,android,iPad
Kind: global constant
checkWeixinQqUc
判断是否是微信,qq 或 uc
Kind: global constant
checkIsIphoneX
检查是否是 IphoneX
Kind: global constant
objIsEqual
判断两个对象是否相等,目前只支持对象值为简单数据类型的判断
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | oneObj | Object | 对象 | | twoObj | Object | 对象 |
RandomNum
生成指定范围随机数
Kind: global constant
| Param | Type | | --- | --- | | min | number | | max | number |
getMonthNum
获取当前月天数
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | year | String | 年份 | | month | String | 月份 |
timesToYyMmDd ⇒ date
时间戳转化为年月日
Kind: global constant
Returns: date - - data - 年月日
| Type | Description | | --- | --- | | date | times - 时间戳 | | string | ymd - 格式类型(yyyy-mm-dd,yyyy/mm/dd) | | string | hms - 可选,格式类型(hh,hh:mm,hh:mm:ss) |
hideTag
隐藏所有指定标签 例: hide(document.querySelectorAll('img'))
Kind: global constant
| Type | Description | | --- | --- | | node | el |
getStyle
返回指定元素的生效样式
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | el | element | 元素节点 | | ruleName | string | 指定元素的名称 |
elementContains
检查是否包含子元素
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | parent | element | | | child | element | 例:elementContains(document.querySelector('head'), document.querySelector('title')); // true |
escapeHTML
转义html(防XSS攻击)
Kind: global constant
| Type | Description | | --- | --- | | string | str - 需转义的字符串 |
fileToBase64
file转base64
Kind: global constant
| Type | Description | | --- | --- | | * | file 图片文件 |
blobToFile
blob转file
Kind: global constant
| Type | Description | | --- | --- | | blob | blob | | string | fileName |
base64ToBlob
base64转blob
Kind: global constant
| Param | Type | | --- | --- | | base64 | base64 |
base64ToFile
base64转file
Kind: global constant
| Type | Description | | --- | --- | | base64 | base64 | | string | filename 转换后的文件名 |
isNoWord ⇒ boolean
验证不能包含字母
Kind: global constant
Returns: boolean - - true/false
| Type | Description | | --- | --- | | string | value |
isCHNAndEN ⇒ boolean
验证中文和数字
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isPostcode ⇒ boolean
验证邮政编码(中国)
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isWeChatNum ⇒ boolean
验证微信号,6至20位,以字母开头,字母,数字,减号,下划线
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isColor16 ⇒ boolean
验证16进制颜色
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isIMEI ⇒ boolean
验证手机机身码(IMEI)
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isHttpAndPort ⇒ boolean
验证必须带端口号的网址(或ip)
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isRightWebsite ⇒ boolean
验证网址(支持端口和"?+参数"和"#+参数)
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isEnglishName ⇒ boolean
验证英文姓名
Kind: global constant
Returns: boolean - - true/false
| Param | Type | | --- | --- | | value | string |
isChineseName
验证中文姓名
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isImageUrl
验证图片链接地址(图片格式可按需增删)
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isVersion
验证版本号格式必须为X.Y.Z
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isAccountNumber
验证银行卡号(10到30位, 覆盖对公/私账户, 参考微信支付)
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isLicensePlateNumberNER
验证车牌号(新能源/非新能源)
Kind: global constant
| Type | Description | | --- | --- | | string | value | | string | type(new/normal/default) |
isMPN
验证手机号中国(严谨), 根据工信部2019年最新公布的手机号段
Kind: global constant
| Type | Description | | --- | --- | | string | value | | string | isStrict 是否严格模式验证 |
isLandlineTelephone
验证座机电话(国内),如: 0341-86091234
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isIDCard
验证身份证号, 支持1/2代(15位/18位数字)
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isPassport
验证护照(包含香港、澳门)
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isWebAccount
验证帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isChineseCharacter
验证中文/汉字
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isQQNum
验证qq号格式
Kind: global constant
| Param | Type | | --- | --- | | value | string |
isNumAndStr
验证数字和字母组成
Kind: global constant
| Param | Type | | --- | --- | | value | string |
cookieSet
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 | | value | * | 值 | | expire | String | 过期时间,单位天 |
cookieGet
cookie 获取
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 |
cookieRemove
cookie 删除
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 |
sessionStorageSet
sessionStorage 存贮某一段时间失效
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 | | value | * | 存贮值 | | expire | String | 过期时间,毫秒数 |
sessionStorageRemove
sessionStorage 删除
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 |
sessionStorageGet
sessionStorage 获取
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 |
localStorageSet
localStorage 存贮某一段时间失效
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 | | value | * | 存贮值 | | expire | number | 过期时间,毫秒数 |
localStorageGet
localStorage 获取
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 |
localStorageRemove
localStorage 移除
Kind: global constant
| Param | Type | Description | | --- | --- | --- | | key | String | 属性 |
currentURL() ⇒ string
返回当前url
Kind: global function
getUrlParam() ⇒ null
获取url参数(第一种)
Kind: global function
| Type | Description | | --- | --- | | string | name | | string | origin |
getUrlParams(name, origin)
获取url参数(第二种)
Kind: global function
| Param | Type | | --- | --- | | name | * | | origin | * |
replaceParamVal(paramName, replaceWith)
修改url中的参数
Kind: global function
| Param | Type | | --- | --- | | paramName | string | | replaceWith | string |
funcUrlDel(name)
删除url中指定的参数
Kind: global function
| Param | Type | | --- | --- | | name | string |
getClientHeight()
获取窗口可视范围的高度
Kind: global function
getPageViewWidth()
Kind: global function
getPageWidth()
获取窗口宽度
Kind: global function
getViewportOffset()
获取窗口尺寸
Kind: global function
getPageScrollTop()
获取滚动条距顶部高度
Kind: global function
getPageScrollLeft()
获取滚动条距左边的高度
Kind: global function
| Type | | --- | | * |
launchFullscreen(element)
开启全屏
Kind: global function
| Param | Type | | --- | --- | | element | * |
openWindow(url, windowName, width, height)
打开一个窗口
Kind: global function
| Param | Type | | --- | --- | | url | string | | windowName | string | | width | number | | height | number |
exitFullscreen()
关闭全屏
Kind: global function
getQueryString(name)
获取 url 后面通过?传参的参数
Kind: global function
| Param | Type | | --- | --- | | name | String |
AutoResponse(width)
自适应页面(rem)
Kind: global function
| Param | Type | | --- | --- | | width | number |
getTreeData(data, pid, [pidName], [idName], [childrenName], key) ⇒ array
递归生成树形结构
Kind: global function
| Param | Type | Default | Description | | --- | --- | --- | --- | | data | * | | | | pid | * | | | | [pidName] | string | "'parentId'" | | | [idName] | string | "'id'" | | | [childrenName] | string | "'children'" | | | key | string | | key |
inArray(item, data)
查询数组中是否存在某个元素并返回元素第一次出现的下标
Kind: global function
| Param | Type | | --- | --- | | item | * | | data | array |
countOccurrences(arr, value)
数组中某元素出现的次数
Kind: global function
| Param | Type | | --- | --- | | arr | array | | value | * |
quickSort() ⇒ Array
quick sort calculator
Kind: global function
Returns: Array - - arr - 序列后数组
| Type | Description | | --- | --- | | Array | arr - 数组 |
bubbleSort() ⇒ Array
bubble sort calculator
Kind: global function
Returns: Array - - arr - 序列后数组
| Type | Description | | --- | --- | | Array | arr - 数组 |
bubbleSort~temp
console.time('bubbels') => console.timeEnd('bubbels'): 0.02685546875ms
Kind: inner property of bubbleSort
checkBrowser
判断是浏览器内核()
Kind: global function
OutOsName(osVersion)
Windows根据详细版本号判断当前系统名称
Kind: global function
| Param | Type | | --- | --- | | osVersion | string |
detectDeviceType()
检测移动/PC设备
Kind: global function
深拷贝(<object>) ⇒ cloneObj
Kind: global function
Returns: cloneObj -
| Param | Type | | --- | --- | | | currentObject |
isObject(obj) ⇒
Kind: global function
Returns: boolean
| Param | Type | | --- | --- | | obj | * |
getQueryString(name)
获取 url 后面通过?传参的参数
Kind: global function
| Param | Type | | --- | --- | | name | String |
randomNumInteger(min, max) ⇒ number
生成随机整数
Kind: global function
| Param | Type | | --- | --- | | min | * | | max | * |
equals() ⇒ boolean
equals 是否全等 - object | array | string | number | function
Kind: global function
Returns: boolean - - true/false
isYesterday(val) ⇒ boolean
Kind: global function
Returns: boolean - 返回布尔值
| Param | Type | Description | | --- | --- | --- | | val | string | 需要验证的日期 |
timeToTimestamp(time) ⇒ Number
Kind: global function
Returns: Number - - 返回值为时间毫秒值
| Param | Type | Description | | --- | --- | --- | | time | String | time - 日期字符串,如'2018-8-8','2018,8,8','2018/8/8' |
dom选择器(tagName) ⇒ Object
Kind: global function
Returns: Object - tag
| Param | Type | | --- | --- | | tagName | string |
EventHub() ⇒ Object
EventHub 发布者订阅事件器
Kind: global function
Returns: Object - - 广播/订阅
| Type | Description | | --- | --- | | null | 无返回参数 |
formatFileSize(fileSize)
B转换到KB,MB,GB并保留两位小数
Kind: global function
| Param | Type | | --- | --- | | fileSize | number |
fileToBase64String(file, format, size, formatMsg, sizeMsg) ⇒ Promise.<any>
获取文件base64编码
Kind: global function
| Param | Description | | --- | --- | | file | | | format | 指定文件格式 | | size | 指定文件大小(字节) | | formatMsg | 格式错误提示 | | sizeMsg | 大小超出限制提示 |
memoize() ⇒ Map
缓存函数器
Kind: global function
Returns: Map - - cached - 类数组
| Type | Description | | --- | --- | | function | fn - 函数 |