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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
import AsyncStorage from '@react-native-community/async-storage';
import moment from 'moment';
import {Linking} from 'react-native';
import {getAll} from 'react-native-contacts';
import {BROWSABLE_SOCIAL_URLS, TOGGLE_BUTTON_TYPE} from '../constants';
import {
ContactType,
NotificationType,
UniversityBadgeType,
UniversityType,
} from './../types/types';
export const getToggleButtonText: (
buttonType: string,
state: boolean,
) => string | null = (buttonType, state) => {
switch (buttonType) {
case TOGGLE_BUTTON_TYPE.FRIEND_UNFRIEND:
return state ? 'Unfriend' : 'Add Friend';
case TOGGLE_BUTTON_TYPE.BLOCK_UNBLOCK:
return state ? 'Unblock' : 'Block';
default:
return null;
}
};
export const handleOpenSocialUrlOnBrowser = (
handle: string | undefined,
social: string,
) => {
if (handle && social in BROWSABLE_SOCIAL_URLS) {
Linking.openURL(BROWSABLE_SOCIAL_URLS[social] + `${handle}/`);
}
};
//Returns university class just like we would like to display on profile page
export const getUniversityClass = (universityClass: number) => {
return `Class of '${(universityClass % 2000).toString()}`;
};
export const getDateAge: (
date: moment.Moment,
) => 'today' | 'yesterday' | 'thisWeek' | 'unknown' = (date: moment.Moment) => {
const today = moment().startOf('day');
const yesterday = moment().subtract(1, 'days').startOf('day');
const weekOld = moment().subtract(7, 'days').startOf('day');
if (date.isSame(today, 'd')) {
return 'today';
} else if (date.isSame(yesterday, 'd')) {
return 'yesterday';
} else if (date.isAfter(weekOld)) {
return 'thisWeek';
} else {
// this can be longer than a week or in the future
return 'unknown';
}
};
export const moveCategory: (
categories: string[],
category: string,
moveUp: boolean,
) => string[] = (categories, category, moveUp) => {
const i = categories.indexOf(category);
const swapTarget = moveUp ? i - 1 : i + 1;
if ((moveUp && i === 0) || (!moveUp && i > categories.length)) {
return categories;
}
const tmp = categories[i];
categories[i] = categories[swapTarget];
categories[swapTarget] = tmp;
return categories;
};
export const checkImageUploadStatus = (statusMap: object) => {
for (const value of Object.values(statusMap)) {
if (value !== 'Success') {
return false;
}
}
return true;
};
export const haveUnreadNotifications = async (
notifications: NotificationType[],
): Promise<boolean> => {
for (const n of notifications) {
const notificationDate = moment(n.timestamp);
const prevLastViewed = await AsyncStorage.getItem('notificationLastViewed');
const lastViewed: moment.Moment | undefined =
prevLastViewed == null ? moment.unix(0) : moment(prevLastViewed);
const dateAge = getDateAge(notificationDate);
if (dateAge === 'unknown') {
continue;
}
const unread = lastViewed ? lastViewed.diff(notificationDate) < 0 : false;
if (unread) {
return true;
}
}
return false;
};
// https://stackoverflow.com/a/2450976
export const shuffle = (array: any[]) => {
var currentIndex = array.length,
temporaryValue,
randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
export const extractContacts = async () => {
let retrievedContacts: Array<ContactType> = [];
await getAll().then((contacts) => {
contacts.map((contact) => {
contact.phoneNumbers.map((phoneNumber) => {
retrievedContacts.push({
first_name: contact.givenName,
last_name: contact.familyName,
phone_number: phoneNumber.number,
});
});
});
});
return retrievedContacts;
};
export const hasSeenBadgeTutorial = async () => {
if ((await AsyncStorage.getItem('hasSeenBadgeTutorial')) === 'true') {
return true;
}
return false;
};
export const getUniversityBadge = (
university: UniversityType,
type: UniversityBadgeType,
) => {
switch (type) {
case 'Search':
return _searchPageIcon(university);
case 'Crest':
return _crestIcon(university);
default:
return require('../assets/images/bwbadges.png');
}
};
const _searchPageIcon = (university: UniversityType) => {
switch (university) {
case UniversityType.Cornell:
return require('../assets/universities/cornell-search.png');
case UniversityType.Brown:
return require('../assets/images/bwbadges.png');
default:
return require('../assets/images/bwbadges.png');
}
};
const _crestIcon = (university: UniversityType) => {
switch (university) {
case UniversityType.Cornell:
return require('../assets/universities/cornell.png');
case UniversityType.Brown:
return require('../assets/universities/brown.png');
default:
return require('../assets/images/bwbadges.png');
}
};
export const validateImageLink = async (url: string | undefined) => {
if (!url) {
return false;
}
return fetch(url)
.then((res) => {
if (res.status === 200) {
return true;
} else {
return false;
}
})
.catch((_) => {
return false;
});
};
// Documentation: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
export const numberWithCommas = (digits: number) => {
return digits.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
|