@qy_better_lib/core 0.0.2 → 0.0.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qy_better_lib/core",
3
3
  "private": false,
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "description": "qy better lib core",
6
6
  "author": "luhuiming",
7
7
  "license": "ISC",
@@ -1,115 +0,0 @@
1
- import { isElement, isClient } from "./../utils";
2
- import type {
3
- ComponentPublicInstance,
4
- DirectiveBinding,
5
- ObjectDirective,
6
- } from "vue";
7
-
8
- type DocumentHandler = <T extends MouseEvent>(mouseup: T, mousedown: T) => void;
9
-
10
- type FlushList = Map<
11
- HTMLElement,
12
- {
13
- documentHandler: DocumentHandler;
14
- bindingFn: (...args: unknown[]) => unknown;
15
- }[]
16
- >;
17
-
18
- const nodeList: FlushList = new Map();
19
-
20
- let startClick: MouseEvent;
21
-
22
- if (isClient) {
23
- document.addEventListener("mousedown", (e: MouseEvent) => (startClick = e));
24
- document.addEventListener("mouseup", (e: MouseEvent) => {
25
- for (const handlers of nodeList.values()) {
26
- for (const { documentHandler } of handlers) {
27
- documentHandler(e as MouseEvent, startClick);
28
- }
29
- }
30
- });
31
- }
32
-
33
- function createDocumentHandler(
34
- el: HTMLElement,
35
- binding: DirectiveBinding
36
- ): DocumentHandler {
37
- let excludes: HTMLElement[] = [];
38
- if (Array.isArray(binding.arg)) {
39
- excludes = binding.arg;
40
- } else if (isElement(binding.arg)) {
41
- excludes.push(binding.arg as unknown as HTMLElement);
42
- }
43
- return function (mouseup, mousedown) {
44
- const popperRef = (
45
- binding.instance as ComponentPublicInstance<{
46
- popperRef: HTMLElement | null;
47
- }>
48
- ).popperRef;
49
- const mouseUpTarget = mouseup.target as Node;
50
- const mouseDownTarget = mousedown?.target as Node;
51
- const isBound = !binding || !binding.instance;
52
- const isTargetExists = !mouseUpTarget || !mouseDownTarget;
53
- const isContainedByEl =
54
- el.contains(mouseUpTarget) || el.contains(mouseDownTarget);
55
- const isSelf = el === mouseUpTarget;
56
-
57
- const isTargetExcluded =
58
- (excludes.length &&
59
- excludes.some((item) => item?.contains(mouseUpTarget))) ||
60
- (excludes.length && excludes.includes(mouseDownTarget as HTMLElement));
61
- const isContainedByPopper =
62
- popperRef &&
63
- (popperRef.contains(mouseUpTarget) ||
64
- popperRef.contains(mouseDownTarget));
65
- if (
66
- isBound ||
67
- isTargetExists ||
68
- isContainedByEl ||
69
- isSelf ||
70
- isTargetExcluded ||
71
- isContainedByPopper
72
- ) {
73
- return;
74
- }
75
- binding.value(mouseup, mousedown);
76
- };
77
- }
78
-
79
- const ClickOutside: ObjectDirective = {
80
- beforeMount(el: HTMLElement, binding: DirectiveBinding) {
81
- if (!nodeList.has(el)) {
82
- nodeList.set(el, []);
83
- }
84
-
85
- nodeList.get(el)?.push({
86
- documentHandler: createDocumentHandler(el, binding),
87
- bindingFn: binding.value,
88
- });
89
- },
90
- updated(el: HTMLElement, binding: DirectiveBinding) {
91
- if (!nodeList.has(el)) {
92
- nodeList.set(el, []);
93
- }
94
-
95
- const handlers = nodeList.get(el);
96
- const oldHandlerIndex = handlers?.findIndex(
97
- (item) => item.bindingFn === binding.oldValue
98
- );
99
- const newHandler = {
100
- documentHandler: createDocumentHandler(el, binding),
101
- bindingFn: binding.value,
102
- };
103
-
104
- if (oldHandlerIndex && oldHandlerIndex >= 0) {
105
- handlers?.splice(oldHandlerIndex, 1, newHandler);
106
- } else {
107
- handlers?.push(newHandler);
108
- }
109
- },
110
- unmounted(el: HTMLElement) {
111
- nodeList.delete(el);
112
- },
113
- };
114
-
115
- export default ClickOutside;
@@ -1,3 +0,0 @@
1
- import ClickOutside from "./click-outside";
2
-
3
- export { ClickOutside };
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./utils/index";
2
- export * from "./directives/index";
package/src/utils/dom.ts DELETED
@@ -1,15 +0,0 @@
1
- /**
2
- * 根据类名获取父元素
3
- * @param dom dom元素
4
- * @param className css类名
5
- * @return dom | null
6
- */
7
- export function getParentByClass(dom: any, className: string): any {
8
- if (!dom || dom.tagName === "BODY") {
9
- return null;
10
- }
11
- if (dom.classList.contains(className)) {
12
- return dom;
13
- }
14
- return getParentByClass(dom.parentNode, className);
15
- }
@@ -1,7 +0,0 @@
1
- export * from "./random";
2
- export * from "./is";
3
- export * from "./tree";
4
- export * from "./number";
5
- export * from "./storage";
6
- export * from "./object";
7
- export * from "./dom";
package/src/utils/is.ts DELETED
@@ -1,67 +0,0 @@
1
- /**
2
- * 判断是否为undefined
3
- * @param val
4
- * @returns
5
- */
6
- export function isUndefined(val: any): boolean {
7
- return val === undefined;
8
- }
9
- /**
10
- * 是否为数组
11
- * @param val
12
- * @returns
13
- */
14
- export function isArray(val: any) {
15
- return Object.prototype.toString.call(val) === "[object Array]";
16
- }
17
- /**
18
- * 是否为对象
19
- * @param val
20
- * @returns
21
- */
22
- export function isObject(val: any) {
23
- return (
24
- typeof val === "object" &&
25
- Object.prototype.toString.call(val) === "[object Object]"
26
- );
27
- }
28
-
29
- /**
30
- * 是否为日期时间
31
- * @param val
32
- * @returns
33
- */
34
- export function isDate(val: any) {
35
- return Object.prototype.toString.call(val) === "[object Date]";
36
- }
37
-
38
- /**
39
- * 是否为空
40
- * @param val
41
- * @returns
42
- */
43
- export function isEmpty(val: unknown) {
44
- return (
45
- (!val && val !== 0) ||
46
- (isArray(val) && (<Array<unknown>>val).length === 0) ||
47
- (isObject(val) && !Object.keys(val as any).length)
48
- );
49
- }
50
-
51
- /**
52
- * 判断对象是否为DOM元素
53
- * @param e
54
- * @returns
55
- */
56
- export function isElement(e: unknown): e is Element {
57
- if (typeof Element === "undefined") return false;
58
- return e instanceof Element;
59
- }
60
-
61
- /**
62
- * 是否是客户端
63
- */
64
- export const isClient = typeof window !== "undefined";
65
-
66
- /**是否未数值 */
67
- export const isNumber = (val: any) => typeof val === "number";
@@ -1,14 +0,0 @@
1
- /**
2
- * 数字转成千分位
3
- * @param num
4
- * @returns
5
- */
6
- export function thousandSeparator(num: number | any): string {
7
- if (!num) {
8
- return "0";
9
- }
10
- if (Object.prototype.toString.call(1) !== "[object Number]") {
11
- return `${num}`;
12
- }
13
- return `${num}`.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,");
14
- }
@@ -1,76 +0,0 @@
1
- import { isObject } from "./is";
2
-
3
- // 深拷贝对象
4
- export function deepClone(obj: any): any {
5
- const _toString: Function = Object.prototype.toString;
6
- // null, undefined, non-object, function
7
- if (!obj || typeof obj !== "object") {
8
- return obj;
9
- }
10
- // DOM Node
11
- if (obj.nodeType && "cloneNode" in obj) {
12
- return obj.cloneNode(true);
13
- }
14
- // Date
15
- if (_toString.call(obj) === "[object Date]") {
16
- return new Date(obj.getTime());
17
- }
18
- // RegExp
19
- if (_toString.call(obj) === "[object RegExp]") {
20
- const flags: Array<string> = [];
21
- if (obj.global) {
22
- flags.push("g");
23
- }
24
- if (obj.multiline) {
25
- flags.push("m");
26
- }
27
- if (obj.ignoreCase) {
28
- flags.push("i");
29
- }
30
- return new RegExp(obj.source, flags.join(""));
31
- }
32
- if (_toString.call(obj) === "[object FormData]") {
33
- const formData = new FormData();
34
- for (const [key, value] of obj.entries()) {
35
- formData.append(key, value);
36
- }
37
- return formData;
38
- }
39
- const result: any = Array.isArray(obj)
40
- ? []
41
- : obj.constructor
42
- ? new obj.constructor()
43
- : {};
44
-
45
- for (const key in obj) {
46
- result[key] = deepClone(obj[key]);
47
- }
48
- return result;
49
- }
50
-
51
- /**
52
- *深度合并多个对象的方法
53
- */
54
- export function deepAssign(...arg: any[]) {
55
- let len: number = arg.length,
56
- target: Record<string, any> = arg[0];
57
- if (!isObject(target)) {
58
- target = {};
59
- }
60
- for (let i = 1; i < len; i++) {
61
- let source = arg[i];
62
- if (isObject(source)) {
63
- for (let s in source) {
64
- if (s === "__proto__" || target === source[s]) {
65
- continue;
66
- }
67
- if (isObject(source[s])) {
68
- target[s] = deepAssign(target[s], source[s]);
69
- } else {
70
- target[s] = source[s];
71
- }
72
- }
73
- }
74
- }
75
- return target;
76
- }
@@ -1,40 +0,0 @@
1
- /**
2
- * ==============================================
3
- * 随机数
4
- * ==============================================
5
- */
6
-
7
- /**
8
- * 生成[0-10000]的随机数,针对数据小量使用
9
- * @returns 随机数
10
- */
11
- export function generateId(): number {
12
- return Math.floor(Math.random() * 10000);
13
- }
14
-
15
- /**
16
- * 生成[0-max]的随机数
17
- * @returns 随机数
18
- */
19
- export function getRandomInt(max: number) {
20
- return Math.floor(Math.random() * Math.floor(max));
21
- }
22
-
23
- /**
24
- * 生成GUID
25
- * @returns
26
- */
27
- export function guiID() {
28
- const gs = generateString;
29
- return `${gs()}${gs()}-${gs()}-${gs()}-${gs()}-${gs()}${gs()}${gs()}`;
30
- }
31
-
32
- /**
33
- * 随机字符串
34
- * @returns
35
- */
36
- export function generateString(): string {
37
- return Math.floor((1 + Math.random()) * 0x10000)
38
- .toString(16)
39
- .substring(1);
40
- }
@@ -1,86 +0,0 @@
1
- /**
2
- * 获取用户token,包含过期时间判断
3
- */
4
- export function getToken(): any | null {
5
- const session = localStorage.getItem("token");
6
- if (!session) {
7
- return undefined;
8
- }
9
- const data = JSON.parse(session);
10
- if (data !== null) {
11
- if (data.expirse != null && data.expirse < new Date().getTime()) {
12
- localStorage.removeItem("token");
13
- } else {
14
- return data.value;
15
- }
16
- }
17
- return null;
18
- }
19
-
20
- /**
21
- * 设置用户token,有效期默认七天
22
- * @param {*} value
23
- */
24
- export function setToken(value: any, expires?: number): void {
25
- (!expires && (expires = 7 * 24 * 60 * 60 * 1000)) ||
26
- (expires = expires * 1000);
27
- const data = { value: value, expirse: new Date().getTime() + expires };
28
- localStorage.setItem("token", JSON.stringify(data));
29
- }
30
-
31
- /**
32
- * 移除token
33
- */
34
- export function removeToken(): void {
35
- localStorage.removeItem("token");
36
- }
37
- /**
38
- * 设置缓存
39
- * @param key key
40
- * @param value 值
41
- */
42
- export function setStorage(
43
- key: string,
44
- value: any,
45
- tpye: "local" | "session" = "local"
46
- ): void {
47
- const storage = tpye == "local" ? localStorage : sessionStorage;
48
- value != undefined && storage.setItem(key, JSON.stringify(value));
49
- }
50
- /**
51
- * 获取缓存
52
- * @param key
53
- * @returns
54
- */
55
- export function getStorage(
56
- key: string,
57
- tpye: "local" | "session" = "local"
58
- ): any {
59
- const storage = tpye == "local" ? localStorage : sessionStorage;
60
- let session = storage.getItem(key);
61
- if (session) {
62
- return JSON.parse(session || "");
63
- }
64
- return session;
65
- }
66
-
67
- /**
68
- * 删除指定key的缓存
69
- * @param key
70
- */
71
- export function removeStorage(
72
- key: string,
73
- tpye: "local" | "session" = "local"
74
- ): void {
75
- const storage = tpye == "local" ? localStorage : sessionStorage;
76
- storage.removeItem(key);
77
- }
78
-
79
- /**
80
- * 清除所有缓存
81
- * @param tpye
82
- */
83
- export function clearStorage(tpye: "local" | "session" = "local") {
84
- const storage = tpye == "local" ? localStorage : sessionStorage;
85
- storage.clear();
86
- }
package/src/utils/tree.ts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * 树形数据扁平化处理
3
- * @param data 数据
4
- * @param key key属性
5
- * @param children children属性
6
- */
7
- export function treeFlat(
8
- listData: any[],
9
- key: string = "key",
10
- children: string = "children"
11
- ): Record<string, any> {
12
- const map: Record<string, any> = {};
13
- const flat = (data: any[], parentKey?: string) => {
14
- for (let i = 0; i < data.length; i++) {
15
- const item = data[i];
16
- const mapKey = item[key];
17
- if (map[mapKey]) {
18
- } else {
19
- map[mapKey] = item;
20
- if (parentKey) map[mapKey].parent = map[parentKey];
21
- const itemChildren = item[children];
22
- if (itemChildren && itemChildren.length > 0) {
23
- flat(itemChildren, item[key]);
24
- }
25
- }
26
- }
27
- };
28
- flat(listData);
29
- return map;
30
- }