-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.js
114 lines (102 loc) · 3.41 KB
/
utils.js
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
import { AlertIOS } from 'react-native';
/**
* Validate a user is logged in, if not
* show alert
* @param {Boolean} loggedIn Whether the user is logged in
* @param {String} action The action to show
* @return {Boolean} Whether the user is logged in
*/
const validateUserLoggedIn = (loggedIn, action) => {
if (!loggedIn) {
AlertIOS.alert(`Cannot ${action}`, 'Please login and try again.');
return false;
}
return true;
};
/**
* Capitalize a string
* @param {String} str The string to capitalize
* @return {String} The capitalized string
*/
const capitalize = str => str[0].toUpperCase() + str.slice(1);
/**
* Truncate a given string to a given length
* @param {String} str The string to truncate
* @param {Number} len The length to truncate to
* @return {String} The truncated string with an ellipsis
*/
const truncate = (str, len) => {
if (str.length < len) {
return str;
}
const truncatedStr = str.replace(/(^\w+:|^)\/\//, '').replace('www.', '');
return `${truncatedStr.substring(0, len)}...`;
};
/**
* Add an ID to a users account
* @param {Object} accounts Current persisted accounts
* @param {Object} user Current logged in user
* @param {Number} id The ID to add
* @param {String} type The collection to add to (i.e. upvoted)
* @return {Object} The new accounts object with the added ID
*/
const addToUserAccount = (accounts, user, id, type) => {
// Deep copy to new object
const newAccounts = JSON.parse(JSON.stringify(accounts));
const userAccount = newAccounts[user.username];
// User has an entry
if (userAccount) {
// If user account doesnt have type, add it first
if (userAccount[type] === undefined) {
userAccount[type] = [];
}
// If user doesnt have the id saved/upvoted
if (userAccount[type].indexOf(id) === -1) {
userAccount[type].unshift(id);
} else if (type !== 'upvoted') {
// User already has the id saved
const index = userAccount[type].indexOf(id);
userAccount[type].splice(index, 1);
}
newAccounts[user.username] = userAccount;
} else {
// User has no record, create one and add id
newAccounts[user.username] = {
[type]: [id],
};
}
return newAccounts;
};
/**
* Remove an ID from a users account
* @param {Object} accounts Current persisted accounts
* @param {Object} user Current logged in user
* @param {Number} id The ID to remove
* @param {String} type The collection to remove from (i.e. saved)
* @return {Object} The new accounts object with the removed ID
*/
const removeFromUserAccount = (accounts, user, id, type) => {
// Deep copy to new object
const newAccounts = JSON.parse(JSON.stringify(accounts));
const userAccount = newAccounts[user.username];
// User has an entry
if (userAccount) {
// If user account doesnt have type, add it first
if (userAccount[type] === undefined) {
userAccount[type] = [];
}
// If user has the id already, remove it
if (userAccount[type].indexOf(id) !== -1) {
const index = userAccount[type].indexOf(id);
userAccount[type].splice(index, 1);
}
newAccounts[user.username] = userAccount;
} else {
// User has no record, create one set to empty
newAccounts[user.username] = {
[type]: [],
};
}
return newAccounts;
};
export { validateUserLoggedIn, capitalize, truncate, addToUserAccount, removeFromUserAccount };