jes-utils collections for JavaScript
Jes-utils is a front-end tool library, mainly including BOM, DOM, Regex, calculator, client, Common, Date, File, Storage, 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:
import { blobToFile } from 'jes-utils'
import { getStyle } from 'jes-utils'
import { Bower, Dom } from 'jes-utils'
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:
Constants
- scrollToTop
-
平滑滚动到页面顶部
- smoothScroll
-
滚动到指定元素区域
- getScrollPosition
-
返回当前滚动条位置
- formatMoney
-
金钱格式化,三位加逗号
- checkIosAndroidIpad
-
判断是终端类型,值有ios,android,iPad
- checkWeixinQqUc
-
判断是否是微信,qq 或 uc
- checkIsIphoneX
-
检查是否是 IphoneX
- objIsEqual
-
判断两个对象是否相等,目前只支持对象值为简单数据类型的判断
- RandomNum
-
生成指定范围随机数
- getMonthNum
-
获取当前月天数
-
timesToYyMmDd ⇒
date
-
时间戳转化为年月日
- hideTag
-
隐藏所有指定标签 例: hide(document.querySelectorAll('img'))
- getStyle
-
返回指定元素的生效样式
- elementContains
-
检查是否包含子元素
- escapeHTML
-
转义html(防XSS攻击)
- fileToBase64
-
file转base64
- blobToFile
-
blob转file
- base64ToBlob
-
base64转blob
- base64ToFile
-
base64转file
-
isNoWord ⇒
boolean
-
验证不能包含字母
-
isCHNAndEN ⇒
boolean
-
验证中文和数字
-
isPostcode ⇒
boolean
-
验证邮政编码(中国)
-
isWeChatNum ⇒
boolean
-
验证微信号,6至20位,以字母开头,字母,数字,减号,下划线
-
isColor16 ⇒
boolean
-
验证16进制颜色
-
isIMEI ⇒
boolean
-
验证手机机身码(IMEI)
-
isHttpAndPort ⇒
boolean
-
验证必须带端口号的网址(或ip)
-
isRightWebsite ⇒
boolean
-
验证网址(支持端口和"?+参数"和"#+参数)
-
isEnglishName ⇒
boolean
-
验证英文姓名
- isChineseName
-
验证中文姓名
- isImageUrl
-
验证图片链接地址(图片格式可按需增删)
- isVersion
-
验证版本号格式必须为X.Y.Z
- isAccountNumber
-
验证银行卡号(10到30位, 覆盖对公/私账户, 参考微信支付)
- isLicensePlateNumberNER
-
验证车牌号(新能源/非新能源)
- isMPN
-
验证手机号中国(严谨), 根据工信部2019年最新公布的手机号段
- isLandlineTelephone
-
验证座机电话(国内),如: 0341-86091234
- isIDCard
-
验证身份证号, 支持1/2代(15位/18位数字)
- isPassport
-
验证护照(包含香港、澳门)
- isWebAccount
-
验证帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线组合
- isChineseCharacter
-
验证中文/汉字
- isQQNum
-
验证qq号格式
- isNumAndStr
-
验证数字和字母组成
- cookieSet
- cookieGet
-
cookie 获取
- cookieRemove
-
cookie 删除
- sessionStorageSet
-
sessionStorage 存贮某一段时间失效
- sessionStorageRemove
-
sessionStorage 删除
- sessionStorageGet
-
sessionStorage 获取
- localStorageSet
-
localStorage 存贮某一段时间失效
- localStorageGet
-
localStorage 获取
- localStorageRemove
-
localStorage 移除
Functions
-
currentURL() ⇒
string
-
返回当前url
-
getUrlParam() ⇒
null
-
获取url参数(第一种)
- getUrlParams(name, origin)
-
获取url参数(第二种)
- replaceParamVal(paramName, replaceWith)
-
修改url中的参数
- funcUrlDel(name)
-
删除url中指定的参数
- getClientHeight()
-
获取窗口可视范围的高度
- getPageViewWidth()
- getPageWidth()
-
获取窗口宽度
- getViewportOffset()
-
获取窗口尺寸
- getPageScrollTop()
-
获取滚动条距顶部高度
- getPageScrollLeft()
-
获取滚动条距左边的高度
- launchFullscreen(element)
-
开启全屏
- openWindow(url, windowName, width, height)
-
打开一个窗口
- exitFullscreen()
-
关闭全屏
- getQueryString(name)
-
获取 url 后面通过?传参的参数
- AutoResponse(width)
-
自适应页面(rem)
-
getTreeData(data, pid, [pidName], [idName], [childrenName], key) ⇒
array
-
递归生成树形结构
- inArray(item, data)
-
查询数组中是否存在某个元素并返回元素第一次出现的下标
- countOccurrences(arr, value)
-
数组中某元素出现的次数
- checkBrowser 判断是浏览器内核()
- OutOsName(osVersion)
-
Windows根据详细版本号判断当前系统名称
- detectDeviceType()
-
检测移动/PC设备
-
深拷贝(<object>) ⇒
cloneObj
- isObject(obj) ⇒
- getQueryString(name)
-
获取 url 后面通过?传参的参数
-
randomNumInteger(min, max) ⇒
number
-
生成随机整数
-
isYesterday(val) ⇒
boolean
-
timeToTimestamp(time) ⇒
Number
-
dom选择器(tagName) ⇒
Object
- formatFileSize(fileSize)
-
B转换到KB,MB,GB并保留两位小数
-
fileToBase64String(file, format, size, formatMsg, sizeMsg) ⇒
Promise.<any>
-
获取文件base64编码
scrollToTop
平滑滚动到页面顶部
smoothScroll
滚动到指定元素区域
Kind: global constant
Param | Type |
---|---|
element | * |
getScrollPosition
返回当前滚动条位置
Kind: global constant
Param | Type |
---|---|
el | * |
formatMoney
金钱格式化,三位加逗号
Kind: global constant
Param | Type |
---|---|
num | number |
checkIosAndroidIpad
判断是终端类型,值有ios,android,iPad
checkWeixinQqUc
判断是否是微信,qq 或 uc
checkIsIphoneX
检查是否是 IphoneX
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 |
月份 |
date
timesToYyMmDd ⇒ 时间戳转化为年月日
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 转换后的文件名 |
boolean
isNoWord ⇒ 验证不能包含字母
Kind: global constant
Returns: boolean
- - true/false
Type | Description |
---|---|
string |
value |
boolean
isCHNAndEN ⇒ 验证中文和数字
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isPostcode ⇒ 验证邮政编码(中国)
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isWeChatNum ⇒ 验证微信号,6至20位,以字母开头,字母,数字,减号,下划线
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isColor16 ⇒ 验证16进制颜色
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isIMEI ⇒ 验证手机机身码(IMEI)
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isHttpAndPort ⇒ 验证必须带端口号的网址(或ip)
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isRightWebsite ⇒ 验证网址(支持端口和"?+参数"和"#+参数)
Kind: global constant
Returns: boolean
- - true/false
Param | Type |
---|---|
value | string |
boolean
isEnglishName ⇒ 验证英文姓名
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 |
属性 |
string
currentURL() ⇒ 返回当前url
null
getUrlParam() ⇒ 获取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()
获取窗口可视范围的高度
getPageViewWidth()
getPageWidth()
获取窗口宽度
getViewportOffset()
获取窗口尺寸
getPageScrollTop()
获取滚动条距顶部高度
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()
关闭全屏
getQueryString(name)
获取 url 后面通过?传参的参数
Kind: global function
Param | Type |
---|---|
name | String |
AutoResponse(width)
自适应页面(rem)
Kind: global function
Param | Type |
---|---|
width | number |
array
getTreeData(data, pid, [pidName], [idName], [childrenName], key) ⇒ 递归生成树形结构
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 | * |
checkBrowser
判断是浏览器内核()
Kind: global function
OutOsName(osVersion)
Windows根据详细版本号判断当前系统名称
Kind: global function
Param | Type |
---|---|
osVersion | string |
detectDeviceType()
检测移动/PC设备
cloneObj
深拷贝(<object>) ⇒ 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 |
number
randomNumInteger(min, max) ⇒ 生成随机整数
Kind: global function
Param | Type |
---|---|
min | * |
max | * |
boolean
isYesterday(val) ⇒ Kind: global function
Returns: boolean
- 返回布尔值
Param | Type | Description |
---|---|---|
val | string |
需要验证的日期 |
Number
timeToTimestamp(time) ⇒ Kind: global function
Returns: Number
- - 返回值为时间毫秒值
Param | Type | Description |
---|---|---|
time | String |
time - 日期字符串,如'2018-8-8','2018,8,8','2018/8/8' |
Object
dom选择器(tagName) ⇒ Kind: global function
Returns: Object
- tag
Param | Type |
---|---|
tagName | string |
formatFileSize(fileSize)
B转换到KB,MB,GB并保留两位小数
Kind: global function
Param | Type |
---|---|
fileSize | number |
Promise.<any>
fileToBase64String(file, format, size, formatMsg, sizeMsg) ⇒ 获取文件base64编码
Kind: global function
Param | Description |
---|---|
file | |
format | 指定文件格式 |
size | 指定文件大小(字节) |
formatMsg | 格式错误提示 |
sizeMsg | 大小超出限制提示 |