-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathutils.ts
More file actions
152 lines (126 loc) · 4 KB
/
Copy pathutils.ts
File metadata and controls
152 lines (126 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
StyleObject,
PointerData,
BoundingRect,
AppTouchEvent,
} from './../types'
// check element is exist
export const isElementExist = (value: HTMLElement | string): boolean => {
return document.contains(getElement(value))
}
// check value is a plain object
export const isPlainObject = (value: object | void): boolean => {
if (!value) return false
try {
const { constructor } = value
const { prototype } = constructor
const { hasOwnProperty } = Object.prototype
return constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf')
} catch (error) {
return false
}
}
// check value is function
export const isFunction = (value: any): boolean => {
return typeof value === 'function'
}
// check value is number
export const isNumber = (value: any): boolean => {
return !isNaN(Number(value))
}
// check value is null or undefined
export const isNull = (value: any): boolean => {
return value === null || value === undefined
}
// if value is an element then return value, if not then query value
export const getElement = (value: HTMLElement | string): HTMLElement => {
return value instanceof HTMLElement ? value : document.querySelector(value)!
}
// get closest element
export const getClosestElement = (target: HTMLElement, className: string): HTMLElement | null => {
return className ? target.closest(`.${className}`) : null
}
// get mouse clientX, clientY
export const getPointer = (e: MouseEvent | AppTouchEvent): PointerData => {
const event = 'touches' in e ? e.touches[0] : e
return {
clientX: event.clientX,
clientY: event.clientY
}
}
// get pinch pointer center
export const getTouchesCenter = (touches: TouchList): PointerData => {
return {
clientX: [...touches].map(touch => touch.clientX).reduce((prev, current) => prev + current) / touches.length,
clientY: [...touches].map(touch => touch.clientY).reduce((prev, current) => prev + current) / touches.length
}
}
// getBoundingClientRect
export const getBoundingRect = (target: HTMLElement): BoundingRect => {
const { width, height, top, left, bottom } = target.getBoundingClientRect()
return {
width,
height,
top,
left,
bottom
}
}
// get two fingers center
export const getPinchHypot = (touches: AppTouchEvent['touches']): number => {
return touches.length >= 2 ? Math.hypot(touches[0].clientX - touches[1].clientX, touches[0].clientY - touches[1].clientY) : 0
}
// set styles
export const setStyle = (element: HTMLElement, value: StyleObject): void => {
for (const [k, v] of Object.entries(value)) {
if (typeof v === 'string') {
element.style.setProperty(k, v)
}
}
}
// set attributes
export const setAttributes = (element: HTMLElement, value: Record<string, string>): void => {
for (const [k, v] of Object.entries(value)) {
element.setAttribute(k, v)
}
}
// set object key and value
type Entries<T> = {
[K in keyof T]: [K, T [K]]
} [keyof T] []
export const setObject = <T, K extends keyof T>(obj: T, value: Pick<T, K>): void => {
for (const [k, v] of Object.entries(value) as Entries<Pick<T, K>>) {
obj[k] = v
}
}
// get value between min and max
export const minmax = (value: number, min: number, max: number): number => {
return Math.min(Math.max(value, min), max)
}
// like .toFixed(2)
export const roundToTwo = (value: number): number => {
const roundValue = +(Math.round(Number(value + "e+2")) + "e-2")
return isNaN(roundValue) ? 0 : roundValue
}
// throw Error msg
export const useError = (msg: string): never => {
throw new Error(msg)
}
// throw Error msg
export const useWarn = (msg: string): void => {
return console.warn(msg)
}
// create element
export const createElement = (tagName: string = 'div', className?: string, attributes?: Record<string, string>, children?: string): HTMLElement => {
const el = document.createElement(tagName)
if (className) {
el.classList.add(...className.split(' '))
}
if (attributes) {
setAttributes(el, attributes)
}
if (children) {
el.innerHTML = children
}
return el
}