-
Notifications
You must be signed in to change notification settings - Fork 536
/
utils.ts
149 lines (132 loc) · 4.4 KB
/
utils.ts
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
import { WidthAndHeight } from './types';
/** @internal */
export function numberToPixels(value: number): string {
return value.toString(10) + 'px';
}
/** @internal */
export function pixelsToNumber(value: string): number {
const numberStr = value.replace("px", "");
return parseFloat(numberStr);
}
/** @internal */
export function getElementWidth(element: HTMLElement): number {
return element.offsetWidth;
}
/** @internal */
export function setElementWidth(element: HTMLElement, width: number): void {
const widthAsPixels = numberToPixels(width);
element.style.width = widthAsPixels;
}
/** @internal */
export function getElementHeight(element: HTMLElement): number {
return element.offsetHeight;
}
/** @internal */
export function setElementHeight(element: HTMLElement, height: number): void {
const heightAsPixels = numberToPixels(height);
element.style.height = heightAsPixels;
}
/** @internal */
export function getElementWidthAndHeight(element: HTMLElement): WidthAndHeight {
return {
width: element.offsetWidth,
height: element.offsetHeight,
};
}
/** @internal */
export function setElementDisplayVisibility(element: HTMLElement, visible: boolean): void {
if (visible) {
element.style.display = '';
} else {
element.style.display = 'none';
}
}
/** @internal */
export function ensureElementPositionAbsolute(element: HTMLElement): void {
const absolutePosition = 'absolute';
if (element.style.position !== absolutePosition) {
element.style.position = absolutePosition;
}
}
/**
* Replacement for JQuery $.extend(target, obj)
* @internal
*/
export function extend(target: Record<string, unknown>, obj: Record<string, unknown>): Record<string, unknown> {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
target[key] = obj[key];
}
}
return target;
}
/**
* Replacement for JQuery $.extend(true, target, obj)
* @internal
*/
export function deepExtend(target: Record<string, unknown>, obj: Record<string, unknown> | undefined): Record<string, unknown> {
if (obj !== undefined) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
const existingTarget = target[key];
target[key] = deepExtendValue(existingTarget, value);
}
}
}
return target;
}
/** @internal */
export function deepExtendValue(existingTarget: unknown, value: unknown): unknown {
if (typeof value !== 'object') {
return value;
} else {
if (Array.isArray(value)) {
const length = value.length;
const targetArray = new Array<unknown>(length);
for (let i = 0; i < length; i++) {
const element = value[i];
targetArray[i] = deepExtendValue({}, element);
}
return targetArray;
} else {
if (value === null) {
return null;
} else {
const valueObj = value as Record<string, unknown>;
if (existingTarget === undefined) {
return deepExtend({}, valueObj); // overwrite
} else {
if (typeof existingTarget !== "object") {
return deepExtend({}, valueObj); // overwrite
} else {
if (Array.isArray(existingTarget)) {
return deepExtend({}, valueObj); // overwrite
} else {
if (existingTarget === null) {
return deepExtend({}, valueObj); // overwrite
} else {
const existingTargetObj = existingTarget as Record<string, unknown>;
return deepExtend(existingTargetObj, valueObj); // merge
}
}
}
}
}
}
}
}
/** @internal */
export function removeFromArray<T>(item: T, array: T[]): void {
const index = array.indexOf(item);
if (index === -1) {
throw new Error('Can\'t remove item from array. Item is not in the array');
}
array.splice(index, 1);
}
/** @internal */
export function getUniqueId(): string {
return (Math.random() * 1000000000000000)
.toString(36)
.replace('.', '');
}