ph-utils 0.20.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/web.js DELETED
@@ -1,100 +0,0 @@
1
- /**
2
- * web(浏览器) 端工具类
3
- */
4
- import { isBlank } from "./index.js";
5
- /**
6
- * 解析 Form 表单中的 input 元素的数据为 JSON 格式,key: input-name;value: input-value
7
- * @param form {object} Form 节点对象
8
- */
9
- export const formJson = function (form) {
10
- let elems = form.elements;
11
- let value = {};
12
- for (let i = 0, len = elems.length; i < len; i++) {
13
- let item = elems[i];
14
- if (!isBlank(item.name)) {
15
- if ((item.tagName === "INPUT" || item.tagName === "TEXTAREA") &&
16
- !isBlank(item.value)) {
17
- let dataType = item.getAttribute("data-type");
18
- if (dataType === "number") {
19
- value[item.name] = Number(item.value);
20
- }
21
- else {
22
- value[item.name] = item.value;
23
- }
24
- }
25
- else if (item.tagName === "SELECT") {
26
- value[item.name] = item.options[item.selectedIndex].value;
27
- }
28
- }
29
- }
30
- return value;
31
- };
32
- /**
33
- * 获取 url query 参数 (get 请求的参数)
34
- * @param search 如果是 React 应用就需要传递 useLocation().search
35
- * @returns
36
- */
37
- export function query(search) {
38
- if (isBlank(search)) {
39
- search = location.search;
40
- }
41
- const searchParams = new URLSearchParams(search);
42
- let query = {};
43
- for (const [key, value] of searchParams) {
44
- let oldValue = query[key];
45
- let newValue = value;
46
- if (oldValue != null) {
47
- if (oldValue instanceof Array) {
48
- oldValue.push(value);
49
- newValue = oldValue;
50
- }
51
- else {
52
- newValue = [value, oldValue];
53
- }
54
- }
55
- query[key] = newValue;
56
- }
57
- return query;
58
- }
59
- /**
60
- * 函数节流 - 每隔单位时间,只执行一次
61
- * @param cb 待节流的函数
62
- * @param wait 间隔时间
63
- * @returns
64
- */
65
- export function throttle(fn, wait = 500) {
66
- // 上一次的请求时间
67
- let last = 0;
68
- return (...args) => {
69
- // 当前时间戳
70
- const now = Date.now();
71
- if (now - last > wait) {
72
- fn(...args);
73
- last = now;
74
- }
75
- };
76
- }
77
- /**
78
- * 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
79
- * @param fn 防抖函数
80
- * @param interval 间隔时间段
81
- * @returns
82
- */
83
- export function debounce(fn, interval = 500) {
84
- let _t;
85
- const handle = (...args) => {
86
- if (_t)
87
- clearTimeout(_t);
88
- _t = setTimeout(() => {
89
- //@ts-ignore
90
- fn.apply(this, args);
91
- }, interval);
92
- };
93
- handle.cancel = function () {
94
- if (_t) {
95
- clearTimeout(_t);
96
- _t = null;
97
- }
98
- };
99
- return handle;
100
- }