
js常用工具函数 tools utils
重要提示:
在2.0.0版本之后,函数名将为大写字母开头
1.1.4之前: import { cutRadixPoint } from 'js-func-tools';
2.0.0之后: import { CutRadixPoint } from 'js-func-tools';
安装
- npm安装
npm install js-func-tools
- yarn安装
yarn add js-func-tools
- pnpm安装
pnpm add js-func-tools
使用
<script src="https://unpkg.com/js-func-tools@[version]/dist/browser.js"></script>
<script>
const num = JSFT.CutRadixPoint(807.34691);
</script>
import { CutRadixPoint } from 'js-func-tools';
const num = CutRadixPoint(807.34691);
demo data
const arr = [
{'id': 1, 'name': '一级1', 'parentId': -1},
{'id': 121, 'name': '二级1', 'parentId': 1},
{'id': 1211, 'name': '三级1', 'parentId': 121},
{'id': 1212, 'name': '三级2', 'parentId': 121},
{'id': 122, 'name': '二级2', 'parentId': 1},
{'id': 2, 'name': '一级2', 'parentId': -1},
{'id': 221, 'name': '二级21', 'parentId': 2},
{'id': 222, 'name': '二级22', 'parentId': 2}
];
const tree = [
{
'name': '一级1',
'id': 1,
'parentId': -1,
'children': [
{
'name': '二级1',
'id': 121,
'parentId': 1,
'children': [{'name': '三级1', 'id': 1211, 'parentId': 121}, {'name': '三级2', 'id': 1212, 'parentId': 121}]
},
{'name': '二级2', 'id': 122, 'parentId': 1}
]
},
{
'name': '一级2',
'id': 2,
'parentId': -1,
'children': [{'name': '二级21', 'id': 221, 'parentId': 2}, {'name': '二级22', 'id': 222, 'parentId': 2}]
}
];
const str = AddZero(3);
const str = AmountThousands(1234567.8809);
const arr = [
{id: '1', name: '1'},
{id: '1', name: '2'},
{id: '3', name: '3'},
];
const result = ArrayDeWeight(arr, 'id')
const tree = ArrayToTree(arr);
const tree2 = ArrayToTree(arr, 0);
const num = Average([1, 2, 3, 4]);
const result1 = Base64.encode('Hello base64!');
const result2 = Base64.decode(result1);
const status = BottomVisible();
const status = ByteSize('hello');
const str = Capitalise('hello');
const result = CheckColor16('#fff')
const result2 = CheckColor16('#ffffff')
-
CheckCreditCode 验证统一社会信用代码
const result = CheckCreditCode('xxxxxxx');
const result = CheckEmail('xxx@163.com')
const result = CheckIDCard('身份证号')
const result = CheckObjType('abc', 'string')
const result2 = CheckObjType('abc', 'String')
const good = CheckPassword('qweasd')
const better = CheckPasswordBetter('asd123')
const best = CheckPasswordBest('asd123@#zdfcerty')
const result = CheckPhoneNumber('13880808080')
const result = CheckPlateNumber('川A66666')
const result = CheckPostcode('610000')
const result = CheckUrl('https://www.xxx.com')
-
HasClassName el是否包含某个class
const result = HasClassName(el, className);
AddClassName(el, className);
-
RemoveClassName el去除某个class
RemoveClassName(el, className);
const result = CloneDeep(tree);
ConvertorTranslate.gps84_To_Bd09(30.401247, 104.041680);
const result = CloneDeepComplex(tree);
const status = CopyToBoard('这是复制的内容');
const num = CutRadixPoint(807.34691);
const inp = document.getElementById('inp');
inp.addEventListener('input', Debounce((args) => {
console.log(11111, args.target.value)
}, 500))
-
DomContainsTarget dom中是否包含目标元素
const dom = document.getElementById('dom');
const target = document.getElementById('target');
DomContainsTarget(dom, target);
const result = DayOfYear('2021.10.30');
const result = DaysBetween('2021.10.30', '2021.11.02');
const result = DayOfWeek('2021.10.30');
const result = WeekOfYear('2021.10.30');
const result = FormatDate('yyyy.mm.dd');
const result = GetCurrentDay('yyyy.mm.dd');
const result = GetPrevDay('yyyy.mm.dd');
const result = GetNextDay('yyyy.mm.dd');
const result = GetTime('hms');
const result2 = GetTime('hms', '2022.01.24');
const result = GetDays();
const result2 = GetDays(2022, 2);
-
GetCurrentWeek/GetPrevWeek/GetNextWeek 获取(当前/上一/下一)周
const cur = GetCurrentWeek();
const cur2 = GetCurrentWeek(0, '2022.02.24');
const pre = GetPrevWeek();
const nex = GetNextWeek();
-
GetCurrentMonth/GetPrevMonth/GetNextMonth 获取(当前/上一/下一)月
const cur = GetCurrentMonth();
const cur2 = GetCurrentMonth('2022.02.24');
const pre = GetPrevMonth();
const nex = GetNextMonth();
-
GetYMDByYear 根据年获取当前所有年月日
const result = GetYMDByYear();
-
HasKeyByObj 判断一个对象是否包含某个属性
const obj = {name: 'eks'}, key = 'name';
const result = HasKeyByObj(obj, key);
const result = DownloadByUrl(url, name);
-
FilterTreeByFunc 根据条件筛选出符合条件的树形结构数据
const filterFunc = (item) => item.id === 11;
const result = FilterTreeByFunc(tree, filterFunc);
-
FindTarget 通过当前Element查找需要的Element
const target = FindTarget([target], [tagName]);
const result = FormatMoney(1234567);
const status = FormValidate({
rules, value, success, fail, confirmValue
});
const result = GetAllLeaf(tree);
-
GetFormData 对象转化为FormData对象
const obj = {id: 1, name: 'eks'};
const result = GetFormData(obj);
-
GetItemByData 根据参数获取当前item
const result = GetItemByData(tree, 'id', '122');
const result = GetNodePath(tree, 12);
const resultByIndex = GetNodePath(tree, 12, true);
const obj = {name: 'eks'};
const result = GetObjType(obj);
const result2 = GetObjType(obj, 'Object');
-
GetPlaceByTrigger 根据触发器获取dom定位位置
const { left, top, isDown } = GetPlaceByTrigger({ trigger: target, dom: div, isRight: false });
const result = GetTextWidth('Hello');
const result = GetUrlParams(name);
const result = IsNullObj(obj);
const obj1 = {a: '1', b: '2', c: [1,2,{a:1}]};
const obj2 = {a: '1', b: '2', c: [1,2,{a:12}]};
const result = IsObjValEqual(obj1, obj2);
const result = Hex2Rgb('#f0f0f0');
const result = IsWeekday('2021.1.24');
-
LargeNumbersAddition 两个大数相加
const result = LargeNumbersAddition(123456, Math.pow(2, 53) - 1);
-
MatchesByValue 根据value匹配字符串(搜索字符串并替换,做高亮使用)
const result = MatchesByValue(
'今天是一个A好日a子',
'A',
'--',
'=='
);
const result = Obj2Url({name: 'Lucy', age: 18}, 'http://www.xxx.com');
const hexColor = RandomHexColor();
-
RemoveAttrByParam 根据参数删除树的属性
const result = RemoveAttrByParam(tree, 'id');
const result = RemoveEmpty(obj);
-
RemoveEmptyChildren 删除树中的空children
const result = RemoveEmptyChildren(tree);
const result = RemoveHtmlTag(str);
const result = Rgb2Hex('rgb(255, 255, 255)');
const result = Round(1.345);
ScrollToTop();
ScrollToPlace({ key: 'scrollLeft' });
-
SensitiveEscape 敏感符号转义 (预防xss攻击)
const result = SensitiveEscape(str);
-
SetParamsByIndex 根据索引设置某个参数的值
const result = SetParamsByIndex(tree, [0, 0], 'open', true);
const result = SortHandle(arr, { key: 'name' });
const box = document.getElementById('box');
box.addEventListener('mousemove', Throttle((args) => {
console.log(11111, args.target.value)
}, 500))
-
TextEllipsis 设置html标签的title
<div id="box" onmouseenter="TextEllipsis">文字内容</div>
const arr = TreeToArray(tree);
const str = Uuid(5);
Xsync({
method: 'GET',
url: 'url/xxx',
params: {id: 123},
token: 'token-token'
}).then(res => {
console.log('then:', res);
}).catch(err => {
console.log('catch:', err);
});