ph-utils 0.2.0 → 0.2.3
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/README.md +2 -2
- package/lib/date.js +4 -4
- package/lib/dom.d.ts +16 -3
- package/lib/dom.js +23 -1
- package/lib/index_m.js +1 -1
- package/lib/server.js +1 -1
- package/package.json +3 -3
- package/LICENSE +0 -21
package/README.md
CHANGED
@@ -4,8 +4,8 @@
|
|
4
4
|
|
5
5
|
### 后端(node server)
|
6
6
|
|
7
|
-
包含了一些 node 后端开发(web server) 时常用的工具类,包括 `index` 基础工具类(这个类里面的函数同样也适用于前端)、`date` 跟日期相关的工具类、`file` 文件操作相关工具类、`server` 一些只适用于后端的工具(使用了后端的 API 列表)
|
7
|
+
包含了一些 node 后端开发(web server) 时常用的工具类,包括 `index` 基础工具类(这个类里面的函数同样也适用于前端)、`date` 跟日期相关的工具类、`file` 文件操作相关工具类、`server` 一些只适用于后端的工具(使用了后端的 API 列表)、`validator` 数据验证
|
8
8
|
|
9
9
|
### 前端(web)
|
10
10
|
|
11
|
-
包含了一些前端网站开发时常用的工具类,包括 `index` 基础工具类(这个类里面的函数同样也适用于前端)、`date` 跟日期相关的工具类、`dom` 浏览器节点操作相关、`web`
|
11
|
+
包含了一些前端网站开发时常用的工具类,包括 `index` 基础工具类(这个类里面的函数同样也适用于前端)、`date` 跟日期相关的工具类、`dom` 浏览器节点操作相关、`web` 一些只适用于前端相关的工具、`validator` 数据验证
|
package/lib/date.js
CHANGED
@@ -100,10 +100,10 @@ function startOf(date, unit, isEnd = false) {
|
|
100
100
|
exports.startOf = startOf;
|
101
101
|
/**
|
102
102
|
* 日期加上指定时间后的日期
|
103
|
-
* @param date
|
104
|
-
* @param num
|
105
|
-
* @param unit
|
106
|
-
* @param fmt
|
103
|
+
* @param date 指定的日期
|
104
|
+
* @param num 需要添加的数字, 如果这个参数传递一个小于0的数字,则就是日期减去相应的数字
|
105
|
+
* @param unit 需要添加的单位,date - 加减天数
|
106
|
+
* @param fmt 可选参数,如果传递了格式化的单位,则返回格式化后的日期, 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒
|
107
107
|
* @returns {Date | string} 如果传递了 fmt 参数,则返回 string,否则返回 Date
|
108
108
|
*/
|
109
109
|
function add(date, num, unit, fmt) {
|
package/lib/dom.d.ts
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
* 根据选择器获取节点
|
3
3
|
* @param {string} selector 选择器
|
4
4
|
*/
|
5
|
-
export declare function elem(selector: string, dom?: HTMLElement): NodeListOf<HTMLElement
|
5
|
+
export declare function elem(selector: string | HTMLElement, dom?: HTMLElement): NodeListOf<HTMLElement> | HTMLElement[];
|
6
6
|
/**
|
7
7
|
* 为节点添加 class
|
8
8
|
* @param {HTMLElement} elem 待添加 class 的节点
|
@@ -15,6 +15,13 @@ export declare function addClass(elem: HTMLElement, clazz: string): void;
|
|
15
15
|
* @param {string} clazz 需要移除的 class
|
16
16
|
*/
|
17
17
|
export declare function removeClass(elem: HTMLElement, clazz: string): void;
|
18
|
+
/**
|
19
|
+
* 判断节点是否包含某个 class
|
20
|
+
* @param elem 待判断 class 的节点
|
21
|
+
* @param clazz 待判断的 class
|
22
|
+
* @returns
|
23
|
+
*/
|
24
|
+
export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
|
18
25
|
/**
|
19
26
|
* 为节点添加 transition 属性,包括浏览器前缀
|
20
27
|
* @param {HTMLElement} element 需要添加 css Transition 属性的节点
|
@@ -32,7 +39,7 @@ export declare function transform(element: HTMLElement, value: string): void;
|
|
32
39
|
* @param {HTMLElement} element 添加事件的节点
|
33
40
|
* @param {string} listener 事件名称
|
34
41
|
* @param {function} event 事件处理函数
|
35
|
-
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string
|
42
|
+
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
36
43
|
*/
|
37
44
|
export declare function on(element: HTMLElement, listener: string, fn: (e: Event, target?: HTMLElement, flag?: string) => void, once?: boolean | {
|
38
45
|
once?: boolean;
|
@@ -58,7 +65,7 @@ export declare function text(element: HTMLElement, textstr?: string): string;
|
|
58
65
|
* @param elems
|
59
66
|
* @param fn 遍历到节点时的回调,回调第一个参数为遍历到的节点,第2个参数为 index;如果回调函数返回 true,则会终止遍历(break)
|
60
67
|
*/
|
61
|
-
export declare function iterate(elems: NodeList, fn: (el: HTMLElement, index: number) => any): void;
|
68
|
+
export declare function iterate(elems: NodeList | HTMLElement[], fn: (el: HTMLElement, index: number) => any): void;
|
62
69
|
/**
|
63
70
|
* 设置或获取节点 data-* 属性
|
64
71
|
* @param elem
|
@@ -67,3 +74,9 @@ export declare function iterate(elems: NodeList, fn: (el: HTMLElement, index: nu
|
|
67
74
|
* @returns
|
68
75
|
*/
|
69
76
|
export declare function attr(elem: HTMLElement, key: string, value?: string): string;
|
77
|
+
/**
|
78
|
+
* 获取指定节点的父节点
|
79
|
+
* @param el
|
80
|
+
* @returns
|
81
|
+
*/
|
82
|
+
export declare function parent(el: HTMLElement): HTMLElement;
|
package/lib/dom.js
CHANGED
@@ -9,7 +9,12 @@ const vendorPrefix = ['', '-webkit', '-moz-'];
|
|
9
9
|
* @param {string} selector 选择器
|
10
10
|
*/
|
11
11
|
export function elem(selector, dom) {
|
12
|
-
|
12
|
+
if (typeof selector === 'string') {
|
13
|
+
return (dom || document).querySelectorAll(selector);
|
14
|
+
}
|
15
|
+
else {
|
16
|
+
return [selector];
|
17
|
+
}
|
13
18
|
}
|
14
19
|
/**
|
15
20
|
* 为节点添加 class
|
@@ -27,6 +32,15 @@ export function addClass(elem, clazz) {
|
|
27
32
|
export function removeClass(elem, clazz) {
|
28
33
|
elem.classList.remove(clazz);
|
29
34
|
}
|
35
|
+
/**
|
36
|
+
* 判断节点是否包含某个 class
|
37
|
+
* @param elem 待判断 class 的节点
|
38
|
+
* @param clazz 待判断的 class
|
39
|
+
* @returns
|
40
|
+
*/
|
41
|
+
export function hasClass(elem, clazz) {
|
42
|
+
return elem.classList.contains(clazz);
|
43
|
+
}
|
30
44
|
/**
|
31
45
|
* 为节点添加 transition 属性,包括浏览器前缀
|
32
46
|
* @param {HTMLElement} element 需要添加 css Transition 属性的节点
|
@@ -142,3 +156,11 @@ export function attr(elem, key, value) {
|
|
142
156
|
return elem.getAttribute('data-' + key);
|
143
157
|
}
|
144
158
|
}
|
159
|
+
/**
|
160
|
+
* 获取指定节点的父节点
|
161
|
+
* @param el
|
162
|
+
* @returns
|
163
|
+
*/
|
164
|
+
export function parent(el) {
|
165
|
+
return el.parentNode;
|
166
|
+
}
|
package/lib/index_m.js
CHANGED
@@ -36,7 +36,7 @@ export function shieldMobile(mobile) {
|
|
36
36
|
* @returns true 是数字, false 不是数字
|
37
37
|
*/
|
38
38
|
export function isNumeric(str, numericParam) {
|
39
|
-
numericParam =
|
39
|
+
numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
|
40
40
|
let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
|
41
41
|
let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
|
42
42
|
return new RegExp('^' + symbol + main + '$').test(str);
|
package/lib/server.js
CHANGED
@@ -59,7 +59,7 @@ module.exports = {
|
|
59
59
|
options = { ...(options || {}) };
|
60
60
|
const errorName = options.errorName || 'ExecError';
|
61
61
|
delete options.errorName;
|
62
|
-
child_process_1.exec(cmd, options, (error, stdout, stderr) => {
|
62
|
+
(0, child_process_1.exec)(cmd, options, (error, stdout, stderr) => {
|
63
63
|
let err = null;
|
64
64
|
let rs = null;
|
65
65
|
if (error) {
|
package/package.json
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
"module": "lib/index_m.js",
|
6
6
|
"types": "lib/index.d.ts",
|
7
7
|
"browser": "lib/index_m.js",
|
8
|
-
"version": "0.2.
|
8
|
+
"version": "0.2.3",
|
9
9
|
"repository": {
|
10
10
|
"type": "git",
|
11
11
|
"url": "git+https//gitee.com/towardly/ph.git",
|
@@ -18,8 +18,8 @@
|
|
18
18
|
},
|
19
19
|
"homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
|
20
20
|
"devDependencies": {
|
21
|
-
"@types/node": "^
|
22
|
-
"typescript": "^4.
|
21
|
+
"@types/node": "^17.0.21",
|
22
|
+
"typescript": "^4.5.5"
|
23
23
|
},
|
24
24
|
"scripts": {
|
25
25
|
"build": "node scripts/build.js"
|
package/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2021 Tenny
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|