UNPKG

7.22 kBJavaScriptView Raw
1'use strict';
2
3var moment = require('moment');
4
5/**
6 * @return {object}
7 */
8
9module.exports = {
10
11 WEEKDAYS: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
12
13 MONTHS: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
14
15 alphabetizeArrayOfObjects: function(array, property) {
16 return array.sort(function(a, b) {
17 if (a[property].toUpperCase() < b[property].toUpperCase()) {
18 return -1;
19 } else if (a[property].toUpperCase() > b[property].toUpperCase()) {
20 return 1;
21 } else {
22 return 0;
23 }
24 });
25 },
26
27 capitalize: function(string) {
28 return string.charAt(0).toUpperCase() + string.slice(1);
29 },
30
31 commonSort: function(entity) {
32 var propertyValue = entity[this.state.searchProperty];
33 if (typeof propertyValue === "string" || propertyValue instanceof String) {
34 return propertyValue.toLowerCase();
35 } else if (typeof propertyValue == "boolean") {
36 return propertyValue.toString().toLowerCase();
37 } else {
38 return propertyValue;
39 }
40 },
41
42 convertBooleanToTFString: function(boolean) {
43 return boolean ? 't' : 'f';
44 },
45
46 convertObjectKeysToUnderscore: function(object) {
47 var result = {};
48 Object.keys(object).forEach(function(key) {
49 result[module.exports.convertToUnderscore(key)] = object[key];
50 });
51 return result;
52 },
53
54 convertTFStringsToBoolean: function(string) {
55 if (string === "t") {
56 return true;
57 } else if (string === "f") {
58 return false;
59 } else {
60 return string;
61 }
62 },
63
64 convertToUnderscore: function(input) {
65 return input.replace(/([A-Z1-9])/g, function($1) { return "_" + $1.toLowerCase(); }).replace('__', '_');
66 },
67
68 deepCopy: function(obj) {
69 if (typeof obj == 'object') {
70 if (Array.isArray(obj)) {
71 var l = obj.length;
72 var r = new Array(l);
73 for (var i = 0; i < l; i++) {
74 r[i] = this.deepCopy(obj[i]);
75 }
76 return r;
77 } else {
78 var r = {};
79 r.prototype = obj.prototype;
80 for (var k in obj) {
81 r[k] = this.deepCopy(obj[k]);
82 }
83 return r;
84 }
85 }
86 return obj;
87 },
88
89 ellipsis: function(string, n) {
90 if (string.length > n) {
91 return string.slice(0, n) + "...";
92 } else {
93 return string;
94 }
95 },
96
97 filterArrayOfDateStrings: function(array, property, obj) {
98 if (obj.startDate && obj.endDate) {
99 return array.filter(function(element) {
100 return (+moment(element[property]).format('x') >= obj.startDate) && (+moment(element[property]).format('x') <= obj.endDate);
101 });
102 } else if (obj.startDate) {
103 return array.filter(function(element) {
104 return +moment(element[property]).format('x') >= obj.startDate;
105 });
106 } else if (obj.endDate) {
107 return array.filter(function(element) {
108 return +moment(element[property]).format('x') <= obj.endDate;
109 });
110 } else {
111 return array;
112 }
113 },
114
115 modalSelectStyles: function() {
116 return {
117 overlay: {
118 background: 'rgba(0, 0, 0, 0.50)'
119 },
120 content: {
121 background: '#FFFFFF',
122 margin: 'auto',
123 maxWidth: 540,
124 height: '90%',
125 border: 'solid 1px #5F5F5F',
126 borderRadius: '6px',
127 textAlign: 'center',
128 color: '#5F5F5F'
129 }
130 };
131 },
132
133 objectsAreEqual: function(obj1, obj2) {
134 return JSON.stringify(obj1) === JSON.stringify(obj2);
135 },
136
137 ordinatize: function(input) {
138 var j = input % 10,
139 k = input % 100;
140 if (j == 1 && k != 11) {
141 return input + "st";
142 }
143 if (j == 2 && k != 12) {
144 return input + "nd";
145 }
146 if (j == 3 && k != 13) {
147 return input + "rd";
148 }
149 return input + "th";
150 },
151
152 params: function() {
153 var result = {};
154 var urlParamString = window.location.search.substring(1);
155 if (urlParamString) {
156 var urlParams = urlParamString.split('&');
157 urlParams.forEach(function(param) {
158 var paramKeyValuePair = param.split('=');
159 var key = paramKeyValuePair[0];
160 var value = paramKeyValuePair[1];
161 var isArray = (key.slice(-2) === '[]');
162 if (isArray) {
163 if (result[key]) {
164 result[key].push(value);
165 } else {
166 result[key] = [value];
167 }
168 } else {
169 result[key] = value;
170 }
171 });
172 }
173 return result;
174 },
175
176 pluralize: function(string, n) {
177 if (n === 1) {
178 return string;
179 } else {
180 return string + 's';
181 }
182 },
183
184 rearrangeFields: function(currentOrder, draggedIndex, dropZoneIndex) {
185 var result = {};
186 if (dropZoneIndex == -1) {
187 result[0] = currentOrder[draggedIndex];
188 delete currentOrder[draggedIndex];
189 }
190 var currentValues = Object.values(currentOrder);
191 for (var i = 0; i < Object.keys(currentOrder).length; i++) {
192 if (i != draggedIndex) {
193 result[Object.keys(result).length] = currentValues[i];
194 }
195 if (i == dropZoneIndex) {
196 result[Object.keys(result).length] = currentValues[draggedIndex];
197 }
198 }
199 return result;
200 },
201
202 removeFinanceSymbols: function(string) {
203 return string.replace('$', '').replace(',', '');
204 },
205
206 removeFromArray: function(array, element) {
207 var index = array.indexOf(element);
208 if (index >= 0) {
209 array.splice(index, 1);
210 }
211 return array;
212 },
213
214 resetNiceSelect: function(obj) {
215 var $dropDowns = $(obj.selector);
216 $dropDowns.niceSelect('destroy');
217 $dropDowns.unbind('change');
218 $dropDowns.niceSelect().on('change', obj.func);
219 },
220
221 setUpNiceSelect: function(obj) {
222 var $dropDowns = $(obj.selector);
223 if ($dropDowns[0] && !$dropDowns[0].nextSibling.classList.contains('nice-select')) {
224 $dropDowns.niceSelect().on('change', obj.func);
225 }
226 },
227
228 stringifyDate: function(date) {
229 return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear().toString().slice(-2);
230 },
231
232 stringifyDateWithHyphens: function(date) {
233 return date.getFullYear().toString() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
234 },
235
236 sortArrayOfDateStrings: function(array, property) {
237 return array.sort(function(a, b) {
238 if (+moment(a[property]).format('x') < +moment(b[property]).format('x')) {
239 return -1;
240 } else if (+moment(a[property]).format('x') > +moment(b[property]).format('x')) {
241 return 1;
242 } else {
243 return 0;
244 }
245 });
246 },
247
248 sortArrayOfObjects: function(array, arg) {
249 let property = Array.isArray(arg) ? arg[0] : arg;
250 return array.sort(function(a, b) {
251 if (parseInt(a[property]) < parseInt(b[property])) {
252 return -1;
253 } else if (parseInt(a[property]) > parseInt(b[property])) {
254 return 1;
255 } else {
256 if (Array.isArray(arg)) {
257 property = arg[1];
258 if (parseInt(a[property]) < parseInt(b[property])) {
259 return -1;
260 } else if (parseInt(a[property]) > parseInt(b[property])) {
261 return 1;
262 } else {
263 return 0;
264 }
265 } else {
266 return 0;
267 }
268 }
269 });
270 }
271};