-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.ts
68 lines (59 loc) · 1.61 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
import { ReactElement } from 'react';
import { IClusterMapProps } from './typings';
const PACKAGE_PROPS = [
'isClusterExpandClick',
'superClusterOptions',
'renderClusterMarker',
'style',
'onMapReady',
'onRegionChangeComplete',
'region',
'onClusterClick',
'priorityMarker',
'onZoomChange',
];
export const formatChildren = (
children: ReactElement[] | ReactElement,
isInCluster: boolean
) => {
if (!children) {
return [];
}
const childrenList = !Array.isArray(children) ? [children] : children;
return childrenList
.flat(1)
.filter((child: ReactElement) =>
isInCluster && child.props
? child.props.isOutsideCluster !== true
: child.props.isOutsideCluster
);
};
export const serializeProps = (userProps: IClusterMapProps) => {
return Object.keys(userProps).reduce(
(newProps, propKey: keyof IClusterMapProps) => {
if (PACKAGE_PROPS.find((prop) => prop === propKey)) {
return newProps;
}
return { ...newProps, [propKey]: userProps[propKey] };
},
{}
);
};
export const makeId = () => {
let id = '';
const possibleChar =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 10; i++) {
id += possibleChar.charAt(Math.floor(Math.random() * possibleChar.length));
}
return id;
};
export const calculateDelta = (x: number, y: number): number =>
x > y ? x - y : y - x;
export const calculateAverage = (...args: number[]): number => {
const argList = [...args];
if (!argList.length) {
return 0;
}
return argList.reduce((sum, num: number) => sum + num, 0) / argList.length;
};