@holyer-lib/utils 0.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/README.md ADDED
File without changes
@@ -0,0 +1,145 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * 类型判断工具
7
+ */
8
+ function isString(val) {
9
+ return typeof val === 'string';
10
+ }
11
+
12
+ function isNumber(val) {
13
+ return typeof val === 'number' && !isNaN(val);
14
+ }
15
+
16
+ function isFunction(val) {
17
+ return typeof val === 'function';
18
+ }
19
+
20
+ function isObject(val) {
21
+ return val !== null && typeof val === 'object';
22
+ }
23
+
24
+ function isArray(val) {
25
+ return Array.isArray(val);
26
+ }
27
+
28
+ function isUndefined(val) {
29
+ return val === undefined;
30
+ }
31
+
32
+ function isNull(val) {
33
+ return val === null;
34
+ }
35
+
36
+ function isPlainObject(obj) {
37
+ return Object.prototype.toString.call(obj) === '[object Object]';
38
+ }
39
+
40
+ /**
41
+ * 简单 DOM 工具(兼容 IE9+)
42
+ */
43
+ function hasClass(el, cls) {
44
+ if (!el || !cls) return false;
45
+ return el.classList
46
+ ? el.classList.contains(cls)
47
+ : (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
48
+ }
49
+
50
+ function addClass(el, cls) {
51
+ if (!el || !cls) return;
52
+ if (el.classList) {
53
+ el.classList.add(cls);
54
+ } else if (!hasClass(el, cls)) {
55
+ el.className = el.className + ' ' + cls;
56
+ }
57
+ }
58
+
59
+ function removeClass(el, cls) {
60
+ if (!el || !cls) return;
61
+ if (el.classList) {
62
+ el.classList.remove(cls);
63
+ } else {
64
+ el.className = el.className.replace(
65
+ new RegExp('(^|\\b)' + cls.split(' ').join('|') + '(\\b|$)', 'gi'),
66
+ ' '
67
+ );
68
+ }
69
+ }
70
+
71
+ function getScrollTop() {
72
+ return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
73
+ }
74
+
75
+ function setScrollTop(value) {
76
+ window.scrollTo(0, value);
77
+ }
78
+
79
+ /**
80
+ * 通用辅助函数
81
+ */
82
+ function noop() {}
83
+
84
+ function once(fn) {
85
+ let called = false;
86
+ return function () {
87
+ if (!called) {
88
+ called = true;
89
+ fn.apply(this, arguments);
90
+ }
91
+ };
92
+ }
93
+
94
+ function throttle(func, delay) {
95
+ let timer = null;
96
+ return function (...args) {
97
+ if (!timer) {
98
+ timer = setTimeout(() => {
99
+ func.apply(this, args);
100
+ timer = null;
101
+ }, delay);
102
+ }
103
+ };
104
+ }
105
+
106
+ function debounce(func, delay) {
107
+ let timeout;
108
+ return function (...args) {
109
+ clearTimeout(timeout);
110
+ timeout = setTimeout(() => func.apply(this, args), delay);
111
+ };
112
+ }
113
+
114
+ function uuid() {
115
+ // 尝试使用现代浏览器的 crypto API(优先使用)
116
+ if (typeof crypto === 'object' && crypto.randomUUID) {
117
+ return crypto.randomUUID();
118
+ }
119
+
120
+ // 回退方案:手动生成 v4 UUID
121
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
122
+ const r = (Math.random() * 16) | 0;
123
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
124
+ return v.toString(16);
125
+ });
126
+ }
127
+
128
+ exports.addClass = addClass;
129
+ exports.debounce = debounce;
130
+ exports.getScrollTop = getScrollTop;
131
+ exports.hasClass = hasClass;
132
+ exports.isArray = isArray;
133
+ exports.isFunction = isFunction;
134
+ exports.isNull = isNull;
135
+ exports.isNumber = isNumber;
136
+ exports.isObject = isObject;
137
+ exports.isPlainObject = isPlainObject;
138
+ exports.isString = isString;
139
+ exports.isUndefined = isUndefined;
140
+ exports.noop = noop;
141
+ exports.once = once;
142
+ exports.removeClass = removeClass;
143
+ exports.setScrollTop = setScrollTop;
144
+ exports.throttle = throttle;
145
+ exports.uuid = uuid;
@@ -0,0 +1,124 @@
1
+ /**
2
+ * 类型判断工具
3
+ */
4
+ function isString(val) {
5
+ return typeof val === 'string';
6
+ }
7
+
8
+ function isNumber(val) {
9
+ return typeof val === 'number' && !isNaN(val);
10
+ }
11
+
12
+ function isFunction(val) {
13
+ return typeof val === 'function';
14
+ }
15
+
16
+ function isObject(val) {
17
+ return val !== null && typeof val === 'object';
18
+ }
19
+
20
+ function isArray(val) {
21
+ return Array.isArray(val);
22
+ }
23
+
24
+ function isUndefined(val) {
25
+ return val === undefined;
26
+ }
27
+
28
+ function isNull(val) {
29
+ return val === null;
30
+ }
31
+
32
+ function isPlainObject(obj) {
33
+ return Object.prototype.toString.call(obj) === '[object Object]';
34
+ }
35
+
36
+ /**
37
+ * 简单 DOM 工具(兼容 IE9+)
38
+ */
39
+ function hasClass(el, cls) {
40
+ if (!el || !cls) return false;
41
+ return el.classList
42
+ ? el.classList.contains(cls)
43
+ : (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
44
+ }
45
+
46
+ function addClass(el, cls) {
47
+ if (!el || !cls) return;
48
+ if (el.classList) {
49
+ el.classList.add(cls);
50
+ } else if (!hasClass(el, cls)) {
51
+ el.className = el.className + ' ' + cls;
52
+ }
53
+ }
54
+
55
+ function removeClass(el, cls) {
56
+ if (!el || !cls) return;
57
+ if (el.classList) {
58
+ el.classList.remove(cls);
59
+ } else {
60
+ el.className = el.className.replace(
61
+ new RegExp('(^|\\b)' + cls.split(' ').join('|') + '(\\b|$)', 'gi'),
62
+ ' '
63
+ );
64
+ }
65
+ }
66
+
67
+ function getScrollTop() {
68
+ return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
69
+ }
70
+
71
+ function setScrollTop(value) {
72
+ window.scrollTo(0, value);
73
+ }
74
+
75
+ /**
76
+ * 通用辅助函数
77
+ */
78
+ function noop() {}
79
+
80
+ function once(fn) {
81
+ let called = false;
82
+ return function () {
83
+ if (!called) {
84
+ called = true;
85
+ fn.apply(this, arguments);
86
+ }
87
+ };
88
+ }
89
+
90
+ function throttle(func, delay) {
91
+ let timer = null;
92
+ return function (...args) {
93
+ if (!timer) {
94
+ timer = setTimeout(() => {
95
+ func.apply(this, args);
96
+ timer = null;
97
+ }, delay);
98
+ }
99
+ };
100
+ }
101
+
102
+ function debounce(func, delay) {
103
+ let timeout;
104
+ return function (...args) {
105
+ clearTimeout(timeout);
106
+ timeout = setTimeout(() => func.apply(this, args), delay);
107
+ };
108
+ }
109
+
110
+ function uuid() {
111
+ // 尝试使用现代浏览器的 crypto API(优先使用)
112
+ if (typeof crypto === 'object' && crypto.randomUUID) {
113
+ return crypto.randomUUID();
114
+ }
115
+
116
+ // 回退方案:手动生成 v4 UUID
117
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
118
+ const r = (Math.random() * 16) | 0;
119
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
120
+ return v.toString(16);
121
+ });
122
+ }
123
+
124
+ export { addClass, debounce, getScrollTop, hasClass, isArray, isFunction, isNull, isNumber, isObject, isPlainObject, isString, isUndefined, noop, once, removeClass, setScrollTop, throttle, uuid };
@@ -0,0 +1,151 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.HolyerUtils = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ * 类型判断工具
9
+ */
10
+ function isString(val) {
11
+ return typeof val === 'string';
12
+ }
13
+
14
+ function isNumber(val) {
15
+ return typeof val === 'number' && !isNaN(val);
16
+ }
17
+
18
+ function isFunction(val) {
19
+ return typeof val === 'function';
20
+ }
21
+
22
+ function isObject(val) {
23
+ return val !== null && typeof val === 'object';
24
+ }
25
+
26
+ function isArray(val) {
27
+ return Array.isArray(val);
28
+ }
29
+
30
+ function isUndefined(val) {
31
+ return val === undefined;
32
+ }
33
+
34
+ function isNull(val) {
35
+ return val === null;
36
+ }
37
+
38
+ function isPlainObject(obj) {
39
+ return Object.prototype.toString.call(obj) === '[object Object]';
40
+ }
41
+
42
+ /**
43
+ * 简单 DOM 工具(兼容 IE9+)
44
+ */
45
+ function hasClass(el, cls) {
46
+ if (!el || !cls) return false;
47
+ return el.classList
48
+ ? el.classList.contains(cls)
49
+ : (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
50
+ }
51
+
52
+ function addClass(el, cls) {
53
+ if (!el || !cls) return;
54
+ if (el.classList) {
55
+ el.classList.add(cls);
56
+ } else if (!hasClass(el, cls)) {
57
+ el.className = el.className + ' ' + cls;
58
+ }
59
+ }
60
+
61
+ function removeClass(el, cls) {
62
+ if (!el || !cls) return;
63
+ if (el.classList) {
64
+ el.classList.remove(cls);
65
+ } else {
66
+ el.className = el.className.replace(
67
+ new RegExp('(^|\\b)' + cls.split(' ').join('|') + '(\\b|$)', 'gi'),
68
+ ' '
69
+ );
70
+ }
71
+ }
72
+
73
+ function getScrollTop() {
74
+ return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
75
+ }
76
+
77
+ function setScrollTop(value) {
78
+ window.scrollTo(0, value);
79
+ }
80
+
81
+ /**
82
+ * 通用辅助函数
83
+ */
84
+ function noop() {}
85
+
86
+ function once(fn) {
87
+ let called = false;
88
+ return function () {
89
+ if (!called) {
90
+ called = true;
91
+ fn.apply(this, arguments);
92
+ }
93
+ };
94
+ }
95
+
96
+ function throttle(func, delay) {
97
+ let timer = null;
98
+ return function (...args) {
99
+ if (!timer) {
100
+ timer = setTimeout(() => {
101
+ func.apply(this, args);
102
+ timer = null;
103
+ }, delay);
104
+ }
105
+ };
106
+ }
107
+
108
+ function debounce(func, delay) {
109
+ let timeout;
110
+ return function (...args) {
111
+ clearTimeout(timeout);
112
+ timeout = setTimeout(() => func.apply(this, args), delay);
113
+ };
114
+ }
115
+
116
+ function uuid() {
117
+ // 尝试使用现代浏览器的 crypto API(优先使用)
118
+ if (typeof crypto === 'object' && crypto.randomUUID) {
119
+ return crypto.randomUUID();
120
+ }
121
+
122
+ // 回退方案:手动生成 v4 UUID
123
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
124
+ const r = (Math.random() * 16) | 0;
125
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
126
+ return v.toString(16);
127
+ });
128
+ }
129
+
130
+ exports.addClass = addClass;
131
+ exports.debounce = debounce;
132
+ exports.getScrollTop = getScrollTop;
133
+ exports.hasClass = hasClass;
134
+ exports.isArray = isArray;
135
+ exports.isFunction = isFunction;
136
+ exports.isNull = isNull;
137
+ exports.isNumber = isNumber;
138
+ exports.isObject = isObject;
139
+ exports.isPlainObject = isPlainObject;
140
+ exports.isString = isString;
141
+ exports.isUndefined = isUndefined;
142
+ exports.noop = noop;
143
+ exports.once = once;
144
+ exports.removeClass = removeClass;
145
+ exports.setScrollTop = setScrollTop;
146
+ exports.throttle = throttle;
147
+ exports.uuid = uuid;
148
+
149
+ Object.defineProperty(exports, '__esModule', { value: true });
150
+
151
+ }));
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@holyer-lib/utils",
3
+ "version": "0.0.0",
4
+ "description": "",
5
+ "main": "dist/utils.cjs.js",
6
+ "module": "dist/utils.esm.js",
7
+ "unpkg": "dist/utils.umd.js",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "keywords": [],
16
+ "author": "",
17
+ "license": "ISC",
18
+ "packageManager": "pnpm@10.15.0",
19
+ "scripts": {
20
+ "build": "node ../../scripts/build-utils.js"
21
+ }
22
+ }