JSPM

  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q49326F
  • License ISC

npm包

Package Exports

  • jun-utils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jun-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jun-utils

git地址

使用

## Install globally or locally
$ npm i jun-utils -S
import { check } from 'jun-utils';

// 手机号码校验
check.cellphone('13456789012'); // true

索引:

check

校验库

import { check } from 'jun-utils';

cellphone(value)

手机校验 11位数字,首位1

check.cellphone('13456789012'); // true

telphone(value)

固定电话校验 3-4位区号,7-8位直拨号码

check.telphone('0571-85735888'); // true

check.telphone('057185735888'); // true

check.telphone('85735888'); // true

phone(value)

电话【手机和固定电话】校验

check.phone('057185735888'); // true

check.phone('13456789012'); // true

email(value)

邮箱校验 登录名@主机名.域名

check.email('test@163.com'); // true

check.email('te_st@sima.vip.com'); // true

postcode(value)

邮编校验 6位数字

check.postcode('310000'); // true

isNull(value)

空数据校验

空数据集合 undefined,'undefined',null,'null','(null)','NaN',''

check.isNull(); // true

check.isNull('undefined'); // true

isNumber(value)

数字校验

check.isNumber('20'); // true

check.isNumber('-20'); // true

isInteger(value)

整数校验

check.isInteger('20'); // true

check.isInteger('-20'); // true

check.isInteger('0.2'); // false

check.isInteger('020'); // false

isDecimal(value)

小数校验

check.isDecimal('0.2'); // true

check.isDecimal('-0.2'); // true

check.isDecimal('20'); // false

check.isDecimal('00.2'); // false

money(value)

金额【元】判断

check.money('-20'); // true

check.money('-20.00'); // true

check.money('20.002'); // false

check.money('002'); // false

hasChinese(value)

中文判断

check.hasChinese('中文'); // true

check.hasChinese('。'); // true

idCard(value)

身份证校验:一代身份证【15位】或二代身份证【18位】

check.idCard('330000199001017865'); // true

check.idCard('33000019900101786X'); // true

check.idCard('330000900101786'); // true

ip(value)

ip地址校验

check.ip('192.168.0.1'); // true

alipay(value)

支付宝账号校验 邮箱或手机号

check.alipay('test@163.com'); // true

check.alipay('13456789012'); // true

pwdIntensity(value)

弱密码校验 1-弱|2-中|3-强

规则:

  1. 位数为6-32位,包括6位或32位
  2. 包含以下任意两种或以上组成元素:
    • 数字
    • 大写字母
    • 小写字母
    • 符号【键盘上可以打出来的符号】
check.pwdIntensity('123456'); // 1

check.pwdIntensity('123456abc'); // 2

check.pwdIntensity('123456abcABC'); // 3

stringUtil

字符串处理

import { stringUtil } from 'jun-utils';

filterNull(str, [format=''])

空数据过滤

stringUtil.filterNull('xxx'); // xxx

stringUtil.filterNull(); //

stringUtil.filterNull(null, '--'); // --

pinyinUtil

汉字与拼音互转

字符范围:Unicode字符中4E00(19968)-9FA5(40869)共计20902

import { pinyinUtil } from 'jun-utils';

getPinyin(chinese, [splitter=''], [withtone=false])

根据汉字获取拼音

pinyinUtil.getPinyin('小明', ' '); // xiao ming

pinyinUtil.getPinyin('小明', ' ', true); // xiǎo míng

pinyinUtil.getPinyin('小明plus', ' '); // xiao ming plus

getCityPinyin(city, [splitter=''], [withtone=false])

获取城市拼音【城市多音字已处理】

pinyinUtil.getCityPinyin('重庆市'); // chongqingshi

pinyinUtil.getCityPinyin('西藏', ' ', true); // xī zhàng

getHanzi(pinyin)

单个拼音转汉字

pinyinUtil.getHanzi('diu'); // 丟丢銩铥颩

floatUtil

浮点数运算【解决精度问题】

import { floatUtil } from 'jun-utils';

add(arg1, arg2, [format=''])

加法

floatUtil.add(0.1, 0.2); // 0.3

floatUtil.add(2.22, 0.1); // 2.32

floatUtil.add(2.22, 'xx', '--'); // --

subtract(arg1, arg2, [format=''])

减法

floatUtil.subtract(1.5, 1.2); // 0.3

floatUtil.subtract(0.3, 0.2); // 0.1

multiply(arg1, arg2, [format=''])

乘法

floatUtil.multiply(19.9, 100); // 1990

floatUtil.multiply(0.7, 180); // 126

divide(arg1, arg2, [format=''])

除法

floatUtil.divide(0.3, 0.1); // 3

floatUtil.divide(0.69, 10); // 0.069

treeUtil

树结构数据操作

import { treeUtil } from 'jun-utils';

dataConvert(source, attributes)

数据转换

const source = [
  { id: '330000', value: '浙江省', parentId: '100000' },
  { id: '330100', value: '杭州市', parentId: '330000' },
  { id: '330200', value: '宁波市', parentId: '330000' },
  { id: '320000', value: '江苏省', parentId: '100000' },
  { id: '320100', value: '南京市', parentId: '320000' },
  { id: '320200', value: '无锡市', parentId: '320000' },
];
const attributes = { rootId: '100000', pId: 'parentId', name: 'value' };
treeUtil.dataConvert(source, attributes);
// => 
[{
  id: '330000',
  name: '浙江省', 
  children: [
    { id: '330100', name: '杭州市' },
    { id: '330200', name: '宁波市' },
  ]
}, { 
  id: '320000',
  name: '江苏省',
  children: [
    { id: '320100', name: '南京市' },
    { id: '320200', name: '无锡市' },
  ]
}];

dataPick(treeData, values, [attributes])

数据提取

const treeData = [{
  id: '330000',
  name: '浙江省', 
  children: [
    { id: '330100', name: '杭州市' },
    { id: '330200', name: '宁波市' },
  ]
}, { 
  id: '320000',
  name: '江苏省',
  children: [
    { id: '320100', name: '南京市' },
    { id: '320200', name: '无锡市' },
  ]
}];
treeUtil.dataPick(treeData, ['330000', '330100']); // ['浙江省', '杭州市']

dataMatch(treeData, value, [attributes])

判断数据是否存在

const treeData = [{
  id: '330000',
  name: '浙江省', 
  children: [
    { id: '330100', name: '杭州市' },
    { id: '330200', name: '宁波市' },
  ]
}, { 
  id: '320000',
  name: '江苏省',
  children: [
    { id: '320100', name: '南京市' },
    { id: '320200', name: '无锡市' },
  ]
}];
treeUtil.dataMatch(treeData, '杭州市', { key: 'name' }); // true

appUtil

app交互

import { appUtil } from 'jun-utils';

isIos()

IOS环境判断

isAndroid()

Android环境判断

isMobile()

移动端【手机、平板设备】环境判断

isWeChat()

微信客户端判断

isAliPay()

支付宝客户端判断

isTaobao()

淘宝客户端判断

alipayJSBridgeReady([callback])

监听alipay容器初始化

alipayTitle(title, [subtitle])

支付宝设置标题

appUtil.alipayTitle('标题', '副标题');

alipayPopWindow()

支付宝关闭当前页面

alipayExitApp()

支付宝退出当前应用


convert

数据转换

import { convert } from 'jun-utils';

bytesToSize(value, [digit=1], [format='0B'])

数据容量单位换算

convert.bytesToSize(10240); // 10.0KB

convert.bytesToSize(1024*1024, 2); // 1.00MB

convert.bytesToSize('xx'); // 0B

fenToYuan(value, [format='0.00'])

分转化成元

convert.fenToYuan(2000); // 20.00

convert.fenToYuan(2000.45); // 20.00

convert.fenToYuan(); // 0.00

convert.fenToYuan(null, '--'); // --

yuanToFen(value, [format='0'])

元转化成分

convert.yuanToFen(20); // 2000

convert.yuanToFen(0.02); // 2

convert.yuanToFen(0.002); // 0

convert.yuanToFen(); // 0

stringUtil.yuanToFen(null, '--'); // --

currencyToCn(value, [format='零元整'])

数字金额转换为中文人民币大写

最大处理数字 999999999999.99

convert.currencyToCn(0); // 零元整

convert.currencyToCn(); // 零元整

convert.currencyToCn(100000000); // 壹亿元整

convert.currencyToCn(100000001); // 壹亿零壹元整

convert.currencyToCn(1.01); // 壹元零壹分

convert.currencyToCn(1.10); // 壹元壹角

common

通用方法

import { common } from 'jun-utils';

generateUUID()

生成uuid

common.generateUUID(); // cd2f4b1f-daf2-451c-a9a6-db716c1d82bb

getParameter(name, [url=window.location.search])

获取url中的参数

common.getParameter('name', 'http://www.w3school.com?name=xxx'); // xxx

loadScript(url, [callback])

动态加载js

common.loadScript('https://xxx.js', () => {
  console.log('loaded');
});

stopPropagation(evt)

阻止事件冒泡

preventDefault(evt);

阻止事件默认行为

addEvent(target, type, handler, [useCapture=false]);

添加事件监听

const handler = () => {
  console.log('onload');
};
common.addEvent(window, 'load', handler);

removeEvent(target, type, handler, [useCapture=false]);

移除事件监听

const handler = () => {
  console.log('onload');
};
common.removeEvent(window, 'load', handler);

getCookie(name)

读取cookie

setCookie(name, value, [options={}])

创建cookie

// 一天后过期
common.setCookie('name', 'value', {
  maxAge: 60*60*24,
});

delCookie(name)

删除cookie

getWinHeight()

获取窗口可视区的高度

getWinWidth()

获取窗口可视区的宽度

getWinScrollHeight()

获取窗口可视区内容的总高度

getWinScrollWidth()

获取窗口可视区内容的总宽度

getWinScrollTop()

获取窗口可视区滚动条垂直偏移

getWinScrollLeft()

获取窗口可视区滚动条水平偏移

selectText(textNode, [start=0], [length])

选中文本

<input type="text" value="12元" />

// 鼠标停留在’元‘前面
common.selectText(document.querySelector('input'), 2, 0);

// 选中所有
common.selectText(document.querySelector('input'));

crypt

加密解密【用于暴露在url中的重要参数】

import { crypt } from 'jun-utils';

encode(value)

加密

crypt.encode('123456'); // CJ8pD3Ks

decode(value)

解密

crypt.decode('CJ8pD3Ks'); // 123456