cc-tools-utils 0.0.1

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/js/bus.ts ADDED
@@ -0,0 +1,24 @@
1
+ class MyBus {
2
+ busList = {};
3
+
4
+ constructor() {}
5
+
6
+ $on(fnName, fn) {
7
+ if (this.busList[fnName]) throw console.error("事件名称重复!");
8
+ this.busList[fnName] = fn;
9
+ }
10
+
11
+ $emit(fnName) {
12
+ const args = [...arguments].slice(1);
13
+ this.busList[fnName](...args);
14
+ }
15
+
16
+ $off(fnName) {
17
+ if (!this.busList[fnName]) return;
18
+ delete this.busList[fnName];
19
+ }
20
+ }
21
+
22
+ let bus = new MyBus();
23
+
24
+ export { bus };
@@ -0,0 +1,238 @@
1
+ /**
2
+ * 动画工具函数
3
+ */
4
+ import TweenMax from "./Anima.js";
5
+
6
+ /**
7
+ * Tween动画函数
8
+ * @param begin - 起始状态对象
9
+ * @param end - 结束状态对象
10
+ * @param time - 动画持续时间(秒)
11
+ * @param onUpdate - 更新回调函数
12
+ * @param onComplete - 完成回调函数
13
+ * @returns Tween动画实例
14
+ */
15
+ export const anima = (
16
+ begin: Record<string, any>,
17
+ end: Record<string, any>,
18
+ time: number,
19
+ onUpdate?: (data: any) => void,
20
+ onComplete?: () => void,
21
+ ) => {
22
+ const newBegin = { ...begin };
23
+ const tween = new TweenMax(newBegin, time, end)
24
+ .eventCallback("onUpdate", () => onUpdate && onUpdate(newBegin))
25
+ .eventCallback("onComplete", () => {
26
+ onComplete && onComplete();
27
+ TweenMax?.killTweensOf(tween);
28
+ });
29
+ return tween;
30
+ };
31
+
32
+ /**
33
+ * 防抖函数
34
+ * @param time - 延迟时间(毫秒)
35
+ * @param fn - 要执行的函数
36
+ * @returns 防抖后的函数
37
+ */
38
+ export const debounce = <T extends (...args: any[]) => any>(
39
+ time: number,
40
+ fn: T,
41
+ ): ((...args: Parameters<T>) => void) => {
42
+ let timeoutID: NodeJS.Timeout | null = null;
43
+ return function (...args: Parameters<T>) {
44
+ if (timeoutID) clearTimeout(timeoutID);
45
+ timeoutID = setTimeout(() => {
46
+ fn.apply(this, args);
47
+ timeoutID = null;
48
+ }, time);
49
+ };
50
+ };
51
+
52
+ /**
53
+ * 节流函数
54
+ * @param time - 节流时间间隔(毫秒)
55
+ * @param fn - 要执行的函数
56
+ * @returns 节流后的函数
57
+ */
58
+ export const throttle = <T extends (...args: any[]) => any>(
59
+ time: number,
60
+ fn: T,
61
+ ): ((...args: Parameters<T>) => void) => {
62
+ let isGong = true;
63
+ return function (...args: Parameters<T>) {
64
+ if (!isGong) return;
65
+ isGong = false;
66
+ const timer = setTimeout(() => {
67
+ fn.apply(this, args);
68
+ isGong = true;
69
+ clearTimeout(timer);
70
+ }, time);
71
+ };
72
+ };
73
+
74
+ /**
75
+ * 蒙版渐隐动画
76
+ * @param parentDom - 父级DOM元素
77
+ */
78
+ export const maskAnima = (parentDom: Element): void => {
79
+ const fn = () => {
80
+ const mask = document.createElement("div");
81
+ parentDom.appendChild(mask);
82
+ mask.style.width = `100%`;
83
+ mask.style.height = `100%`;
84
+ mask.style.position = `absolute`;
85
+
86
+ anima(
87
+ { color: 0 },
88
+ { color: 1 },
89
+ 2,
90
+ (data) => {
91
+ mask.style.backgroundColor = `rgba(0,0,0,${data.color})`;
92
+ },
93
+ () => {
94
+ anima(
95
+ { color: 1 },
96
+ { color: 0 },
97
+ 2,
98
+ (data) => {
99
+ mask.style.backgroundColor = `rgba(0,0,0,${data.color})`;
100
+ },
101
+ () => {
102
+ parentDom.removeChild(mask);
103
+ },
104
+ );
105
+ },
106
+ );
107
+ };
108
+
109
+ debounce(2000, fn);
110
+ };
111
+
112
+ // 递归调用 promise数组函数
113
+ export const callPromises = (arr) => {
114
+ let index = 0;
115
+
116
+ function recursiveCall() {
117
+ if (index >= arr.length) {
118
+ return Promise.resolve(); // 递归结束,返回一个已解决的Promise
119
+ }
120
+
121
+ const currentPromise = arr[index](); // 调用当前的Promise函数
122
+
123
+ index++;
124
+
125
+ return currentPromise.then(recursiveCall); // 递归调用下一个Promise函数
126
+ }
127
+
128
+ return recursiveCall();
129
+ };
130
+
131
+ // 递归 调用数组中的函数
132
+ export const callFunctions = (arr) => {
133
+ for (let i = 0; i < arr.length; i++) {
134
+ if (typeof arr[i] === "function") {
135
+ arr[i](); // 调用函数
136
+ } else if (Array.isArray(arr[i])) {
137
+ callFunctions(arr[i]); // 递归调用数组中的函数
138
+ }
139
+ }
140
+ };
141
+
142
+ // 获取id dom元素
143
+ export const getIdDom = (idName: string) => {
144
+ return document.querySelector(`#${idName}`);
145
+ };
146
+
147
+ // 获取class dom元素
148
+ export const getClassDom = (className: string) => {
149
+ return document.querySelector(`.${className}`);
150
+ };
151
+
152
+ // 检测某个元素是否聚焦
153
+ export const hasFocus = (el) => el === document.activeElement;
154
+
155
+ // 获取某个元素所有的兄弟元素
156
+ export const a = (el) =>
157
+ [].slice.call(el.parentNode.children).filter((child) => child !== el);
158
+
159
+ // 获取当前选择的文本
160
+ export const getSelectedText = () => window.getSelection().toString();
161
+
162
+ // 返回上一个页面
163
+ export const goBack = () => history.go(-1);
164
+
165
+ // 将 URL 参数转换为对象
166
+ export const getUrlParams = (query) =>
167
+ Array.from(new URLSearchParams(query)).reduce(
168
+ (p, [k, v]) =>
169
+ Object.assign({}, p, {
170
+ [k]: p[k] ? (Array.isArray(p[k]) ? p[k] : [p[k]]).concat(v) : v,
171
+ }),
172
+ {},
173
+ );
174
+
175
+ // 检测是否为暗模式
176
+ export const isDarkMode =
177
+ window.matchMedia &&
178
+ window.matchMedia("(prefers-color-scheme: dark)").matches;
179
+
180
+ // 比较两个数组
181
+ export const isEqualArray = (a, b) => JSON.stringify(a) === JSON.stringify(b);
182
+
183
+ // 将数组转为对象
184
+ export const arrayToObject = (arr, key) =>
185
+ arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});
186
+
187
+ // 将数组按照属性计数
188
+ export const countBy = (arr, prop) =>
189
+ arr.reduce(
190
+ (prev, curr) => ((prev[curr[prop]] = ++prev[curr[prop]] || 1), prev),
191
+ {},
192
+ );
193
+
194
+ // 判断数组是否不为空
195
+ export const arrayIsNotEmpty = (arr) =>
196
+ Array.isArray(arr) && Object.keys(arr).length > 0;
197
+
198
+ // 展开多维数组
199
+ export const flat_entries = (arr) => [].concat(...arr);
200
+
201
+ // 获取数组最后一个元素
202
+ export const lastItem = (arr) => arr.slice(-1)[0];
203
+
204
+ // 检测多个对象是否相等
205
+ export const isEqualString = (...objects) =>
206
+ objects.every((obj) => JSON.stringify(obj) === JSON.stringify(objects[0]));
207
+
208
+ // 从对象数组中提取属性值
209
+ export const pluck = (objs, property) => objs.map((obj) => obj[property]);
210
+
211
+ // 反转对象的 key value
212
+ export const invert = (obj) =>
213
+ Object.keys(obj).reduce((res, k) => Object.assign(res, { [obj[k]]: k }), {});
214
+
215
+ // 从对象中删除值为 null 和 undefined 的属性
216
+ export const removeNullAndUndefined = (obj) =>
217
+ Object.entries(obj).reduce(
218
+ (a, [k, v]) => (v == null ? a : ((a[k] = v), a)),
219
+ {},
220
+ );
221
+
222
+ // 按照对象的属性对对象排序
223
+ export const sort = (obj) =>
224
+ Object.keys(obj)
225
+ .sort()
226
+ .reduce((p, c) => ((p[c] = obj[c]), p), {});
227
+
228
+ // 检测对象是否为数组
229
+ export const isArray = (obj) => Array.isArray(obj);
230
+
231
+ // 检测对象是否为 Promise
232
+ export const isPromise = (obj) =>
233
+ !!obj &&
234
+ (typeof obj === "object" || typeof obj === "function") &&
235
+ typeof obj.then === "function";
236
+
237
+ // 交换两个对象
238
+ export const exchange = (a, b) => ([a, b] = [b, a]);
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "cc-tools-utils",
3
+ "version": "0.0.1",
4
+ "description": "A collection of useful JavaScript utilities including animation tools, image processing, and event bus",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "type-check": "tsc --noEmit",
9
+ "test": "echo \"Error: no test specified\" && exit 1",
10
+ "build": "webpack --config webpack.dev.js",
11
+ "dev": "webpack --config webpack.dev.js --mode development"
12
+ },
13
+ "keywords": [
14
+ "utilities",
15
+ "tools",
16
+ "animation",
17
+ "image-processing",
18
+ "event-bus",
19
+ "javascript",
20
+ "typescript"
21
+ ],
22
+ "author": "chencheng",
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://gitee.com/chen-cheng-1998/learn_own.git"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "lib",
31
+ "README.md",
32
+ "LICENSE"
33
+ ],
34
+ "dependencies": {
35
+ "@vue/compiler-sfc": "^3.5.11",
36
+ "css-loader": "^7.1.2",
37
+ "style-loader": "^4.0.0",
38
+ "vue": "3.2.13",
39
+ "vue-loader": "^17.4.2"
40
+ },
41
+ "devDependencies": {
42
+ "@babel/core": "^7.18.0",
43
+ "@babel/plugin-proposal-optional-chaining": "^7.17.12",
44
+ "@babel/preset-env": "^7.18.2",
45
+ "@babel/preset-react": "^7.17.12",
46
+ "@types/node": "^22.7.5",
47
+ "babel-loader": "^8.2.5",
48
+ "clean-webpack-plugin": "^4.0.0",
49
+ "copy-webpack-plugin": "^11.0.0",
50
+ "ts-loader": "^9.2.6",
51
+ "typescript": "^4.5.4",
52
+ "webpack": "^5.65.0",
53
+ "webpack-bundle-analyzer": "^4.5.0",
54
+ "webpack-cli": "^4.9.1",
55
+ "webpack-parallel-uglify-plugin": "^2.0.0"
56
+ }
57
+ }