-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
51 lines (44 loc) · 1.53 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
import { Dimensions, PixelRatio, AsyncStorage } from "react-native";
import { Feeding } from "./types";
export const widthPtoDP = (widthPercent: number) => {
const screenWidth = Dimensions.get("window").width;
const elemWidth = widthPercent;
return PixelRatio.roundToNearestPixel((screenWidth * elemWidth) / 100);
};
export const heightPtoDP = (heightPercent: number) => {
const screenHeight = Dimensions.get("window").height;
const elemHeight = heightPercent;
return PixelRatio.roundToNearestPixel((screenHeight * elemHeight) / 100);
};
export const returnTimeString = (time: number, showBig = true) => {
const bigValue = Math.floor(time / 60);
const smallValue = time % 60;
if (!showBig && bigValue === 0) {
return `${smallValue}`;
} else {
return `${bigValue >= 10 ? bigValue : `0${bigValue}`}:${
smallValue >= 10 ? smallValue : `0${smallValue}`
}`;
}
};
export const IndexKeyExtractor = (item: any, index: number) => `${index}`;
export const updateLocalStorage = async (storage: string, data = {}) => {
await AsyncStorage.setItem(storage, JSON.stringify(data));
};
export const getLocalStorage = async (storage: string) => {
return await AsyncStorage.getItem(storage);
};
export const findIndexToRemove = (array: Feeding[], toRemove: Feeding) => {
let index = null;
for (let i = 0; i < array.length; i++) {
const element = array[i];
if (
element.timeStart === toRemove.timeStart &&
element.timeEnd === toRemove.timeEnd
) {
index = i;
break;
}
}
return index;
};