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