-
Notifications
You must be signed in to change notification settings - Fork 3
/
Trellinator.js
1659 lines (1437 loc) · 46.6 KB
/
Trellinator.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @class Trellinator
* @memberof module:TrellinatorCore
* @constructor
* @classdesc The Trellinator object is a special
* instance of Member, which gets it's username
* from the token in the Configuration tab of the
* Trellinator spreadsheet. As such, this is a
* Member object that is always the Trello
* member on behalf of which Trellinator will
* act when making Trello API calls
*
* This is particularly useful when you need to
* avoid taking action on notifications which have
* been caused by actions taken by Trellinator, and
* it is the Trellinator class that is used by
* the Member.notTrellinator method.
*
* Another common use case is to find another
* board other than the one in which the
* notification occurred.
*
* Trellinator also has a useful now() method which
* can be used so that, when you are creating your
* Murphy tests, you will be able to inject a "fake"
* time to act as the current date/time so you can
* get consistent output when testing.
*
* The Trellinator object is also where a bunch of
* Date convenience functions are added to the Date
* prototype, and those functions are documented here
*
* Methods that are added to the Date prototype in
* this class show up as "static" and belonging to
* Date#functionName, but they're not static. If you
* know how to make these not show as static, please
* send a pull request :)
*
* @example
* if(new Notification(posted).member().notTrellinator())
* @example
* new Trellinator().board("Board Name").card("Ohai")
* @example
* new Trellinator().name()
*/
var Trellinator = function()
{
if(!Trellinator.data)
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
Trellinator.data = WekanApi.login();
}
else
{
var trello_token = null;
if(Trellinator.isGoogleAppsScript())
{
var col = Trellinator.fastGetSheetByName(CONFIG_NAME_).getDataRange().getValues();
new IterableCollection(col).each(function(row)
{
if(row[0] == "Trello Token")
{
trello_token = row[1];
}
});
}
else if(Trellinator.override_token)
{
trello_token = Trellinator.override_token;
}
else
{
trello_token = process.argv[3];
}
if(trello_token)
Trellinator.data = TrelloApi.get("tokens/"+trello_token+"/member");
else
throw new Error("Could not find trello token");
}
}
this.member = new Member(Trellinator.data);
for(var key in this.member)
this[key] = this.member[key];
}
/**
* Cache active spreadsheet sheets in an
* object referenced by name to reduce time to call
* (for example when executing via node
* on the command line)
* @memberof module:TrellinatorCore.Trellinator
*/
Trellinator.fastGetSheetByName = function(name)
{
if(!Trellinator.fastGetSheetByName.sheets)
{
Trellinator.fastGetSheetByName.sheets = {};
var all = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for(var i = 0;i < all.length;i++)
{
Trellinator.fastGetSheetByName.sheets[all[i].getName()] = all[i];
}
}
return Trellinator.fastGetSheetByName.sheets[name];
}
Trellinator.fastGetSheetByName.sheets = null;
Trellinator.data = null;
Trellinator.override_token = null;
/**
* Return true if we are running in Google
* Apps Script environment, false if not
* (for example when executing via node
* on the command line)
* @memberof module:TrellinatorCore.Trellinator
*/
Trellinator.isGoogleAppsScript = function()
{
return (typeof DriveApp !== "undefined");
}
/**
* Create a log entry in the Info Log
* when running in Google Apps Script
* or console.log when running in
* node
* @memberof module:TrellinatorCore.Trellinator
* @param msg {string} the log entry
* @example
* Trellinator.log("Oops, what went wrong?");
*/
Trellinator.log = function(msg)
{
writeInfo_(msg);
}
/**
* Add a board name to a global command group.
* This is used when you create a new board
* using Trellinator and need that board to
* inherit a bunch of functionality from a
* global command group.
* @memberof module:TrellinatorCore.Trellinator
* @param board {Board} a Board object to add to the global command group
* @param group_name {string} the name of the group to add this board to
* @example
* var trellinator = new Trellinator();
* var copied = trellinator.board("Template").copy("Project",trellinator.team("Whitefish"));
* Trellinator.addBoardToGlobalCommandGroup(copied,"Project Boards");
*/
Trellinator.addBoardToGlobalCommandGroup = function(board,group_name)
{
if(Trellinator.isGoogleAppsScript())
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var globSheet = Trellinator.fastGetSheetByName(GLOBAL_GROUP_NAME_);
var globData = globSheet.getDataRange().getValues();
var added = false;
for(var row = 1; row < globData.length; row++)
{
if(globData[row][0] == group_name)
{
var value = globData[row][1].trim();
var to_add = board.name()+" ["+board.id()+"]";
var to_test = board.id();
var arr = new IterableCollection(value.split(GLOBAL_GROUP_SEPARATOR_)).find(function(elem)
{
if(elem && (new RegExp("^[^]+ \\[(.+)\\]$").exec(elem.trim())[1].trim() == to_test))
{
return false;
}
else
{
return elem;
}
}).asArray();
arr.push(to_add);
globSheet.getRange(row+1, 2).setValue(arr.join(GLOBAL_GROUP_SEPARATOR_));
getBoardData_.cache = null;
getBoardNamesFromGlobalCommandGroups.cache = null;
getBoardNames4mGroup_.grpData = null;
getBoardNames4mGroup_.board_names_by_group = {};
timeTrigger4NewBoard_(board.id());
writeInfo_("Added "+board.name()+" to "+group_name);
added = true;
}
}//loop for all global commmands ends
if(!added)
{
globSheet.appendRow([group_name,board.name()+" ["+board.id()+"]"]);
getBoardData_.cache = null;
getBoardNamesFromGlobalCommandGroups.cache = null;
getBoardNames4mGroup_.grpData = null;
getBoardNames4mGroup_.board_names_by_group = {};
timeTrigger4NewBoard_(board.id());
writeInfo_("Added "+board.name()+" to NEW global command group "+group_name);
}
}
return board;
}
Trellinator.boardsInGlobalCommandGroup = function(group_name)
{
var ret = false;
if(Trellinator.isGoogleAppsScript())
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var globSheet = Trellinator.fastGetSheetByName(GLOBAL_GROUP_NAME_);
var globData = globSheet.getDataRange().getValues();
var added = false;
for(var row = 1; row < globData.length; row++)
{
if(globData[row][0] == group_name)
{
ret = new IterableCollection(globData[row][1].trim().split(";;;")).find(function(id)
{
try
{
return new Board({id: /.+\[([A-Za-z0-9]+)\]/.exec(id)[1]}).load();
}
catch(e)
{
return false;
}
});
}
}//loop for all global commmands ends
}
return ret;
}
Trellinator.boardIsInGlobalCommandGroup = function(board,group_name)
{
var ret = false;
if(Trellinator.isGoogleAppsScript())
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var globSheet = Trellinator.fastGetSheetByName(GLOBAL_GROUP_NAME_);
var globData = globSheet.getDataRange().getValues();
var added = false;
for(var row = 1; row < globData.length; row++)
{
if(globData[row][0] == group_name)
{
var value = globData[row][1].trim();
if(value.indexOf(board.id()) > -1)
ret = true;
}
}//loop for all global commmands ends
}
return ret;
}
/**
* Remove a board name from a global command group.
* @memberof module:TrellinatorCore.Trellinator
* @param board {Board} a Board object to remove from the global command group
* @param group_name {string} the name of the group to remove this board from
* @example
* Trellinator.removeBoardFromGlobalCommandGroup(new Notification(posted).board(),"Project Boards");
*/
Trellinator.removeBoardFromGlobalCommandGroup = function(board,group_name)
{
if(Trellinator.isGoogleAppsScript())
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var globSheet = Trellinator.fastGetSheetByName(GLOBAL_GROUP_NAME_);
var globData = globSheet.getDataRange().getValues();
var added = false;
for(var row = 1; row < globData.length; row++)
{
if(globData[row][0] == group_name)
{
var value = globData[row][1].trim();
var to_remove = board.id();
globSheet.getRange(row+1, 2).setValue(
new IterableCollection(value.split(GLOBAL_GROUP_SEPARATOR_)).find(function(elem)
{
if(elem && (new RegExp("^[^]+ \\[(.+)\\]$").exec(elem.trim())[1].trim() == to_remove))
return false;
else
return elem;
}).asArray().join(GLOBAL_GROUP_SEPARATOR_)
);
//We need to do this, but calling this now will
//also clear all triggers for the board that are
//not from this global command group, so put this
//back in when the Trigger system is more consistent
//and better refactored
//clearTimeTriggers4Board_(board.id());
writeInfo_("Removed "+board.name()+" from "+group_name);
}
}//loop for all global commmands ends
}
}
Trellinator.removeBoardFromGlobalCommandGroupById = function(board_id,group_name)
{
if(Trellinator.isGoogleAppsScript())
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var globSheet = Trellinator.fastGetSheetByName(GLOBAL_GROUP_NAME_);
var globData = globSheet.getDataRange().getValues();
var added = false;
for(var row = 1; row < globData.length; row++)
{
if(globData[row][0] == group_name)
{
var value = globData[row][1].trim();
var to_remove = board_id;
globSheet.getRange(row+1, 2).setValue(
new IterableCollection(value.split(GLOBAL_GROUP_SEPARATOR_)).find(function(elem)
{
if(elem && (new RegExp("^[^]+ \\[(.+)\\]$").exec(elem.trim())[1].trim() == to_remove))
return false;
else
return elem;
}).asArray().join(GLOBAL_GROUP_SEPARATOR_)
);
//We need to do this, but calling this now will
//also clear all triggers for the board that are
//not from this global command group, so put this
//back in when the Trigger system is more consistent
//and better refactored
//clearTimeTriggers4Board_(board.id());
writeInfo_("Removed "+board_id+" from "+group_name);
}
}//loop for all global commmands ends
}
}
//USED INTERNALLY
Trellinator.getStack = function()
{
var stack = "";
try
{
throw new Error("Whoops!");
}
catch (e)
{
stack = e.stack;
}
return stack;
}
//USED INTERNALLY
Trellinator.fakeNow = function()
{
return new Date(Trellinator.fake_now);
}
/**
* You can use this instead of new Date()
* and this means you will be able to get
* consistency when writing tests
* by setting Trellinator.fake_now to
* fixed date and time
* @memberof module:TrellinatorCore.Trellinator
*/
Trellinator.now = function()
{
return (Trellinator.fake_now) ? Trellinator.fakeNow() : new Date();
}
Trellinator.username = null;
Trellinator.fake_now = null;
/**
* Get a random number between 2 numbers
* @memberof module:TrellinatorCore.Trellinator
*/
Trellinator.getRandomArbitrary = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
/**
* Any Date object will have this method, allowing
* you to tell if the time is between 2 values
* @param start {string} A time in 24 hour format eg. 17:00
* @param finish {string} A time in 24 hour format eg. 13:00
* @memberof module:TrellinatorCore.Trellinator
* @example
* if(Trellinator.now().timeIsBetween("9:00","17:00"))
*/
Date.prototype.timeIsBetween = function(start,finish)
{
var start_parts = start.split(":");
var finish_parts = finish.split(":");
var start = new Date(this);
start.setHours(start_parts[0],start_parts[1],0,0);
var finish = new Date(this);
finish.setHours(finish_parts[0],finish_parts[1],0,0);
return ((this.getTime() >= start.getTime()) && (this.getTime() <= finish.getTime()));
}
/**
* Returns true if this is monday - friday
* @memberof module:TrellinatorCore.Trellinator
* @example
* if(Trellinator.now().isWeekDay())
*/
Date.prototype.isWeekDay = function()
{
return !((this.getDay() == 6) || (this.getDay() == 0));
}
//DEPRECATED: use on()
Date.prototype.onDate = function(date)
{
this.setDate(date);
return this;
}
/**
* Set the day component of this date
* to the given day, eg. 27
* @memberof module:TrellinatorCore.Trellinator
* @param date {int} the day to set this date to, eg. 27
* @example
* //1st day of next month
* Trellinator.now().addMonths(1).on(1);
*/
Date.prototype.on = function(date)
{
this.setDate(date);
return this;
}
/**
* Set the time component of this date
* to the given time in 24 format, HH:MM
* seconds are not supportd
* @memberof module:TrellinatorCore.Trellinator
* @param time {string} format HH:MM in 24 hour time
* @example
* //9am tomorrow
* Trellinator.now().addDays(1).at("9:00");
*/
Date.prototype.at = function(time)
{
var time_parts = time.split(":");
this.setHours(time_parts[0],time_parts[1],0,0);
return this;
}
/**
* Add X minutes to the date
* @memberof module:TrellinatorCore.Trellinator
* @param minutes {int} number of minutes to add
* @example
* Trellinator.now().addMinutes(20);
*/
Date.prototype.addMinutes = function(minutes)
{
this.setMinutes(this.getMinutes() + parseInt(minutes));
return this;
}
/**
* Minus X minutes from the date
* @memberof module:TrellinatorCore.Trellinator
* @param minutes {int} number of minutes to minus
* @example
* Trellinator.now().minusMinutes(20);
*/
Date.prototype.minusMinutes = function(minutes)
{
this.setMinutes(this.getMinutes() - parseInt(minutes));
return this;
}
/**
* Add X hours to the date
* @memberof module:TrellinatorCore.Trellinator
* @param hours {int} number of hours to add
* @example
* Trellinator.now().addHours(1);
*/
Date.prototype.addHours = function(hours)
{
return this.addMinutes(parseFloat(hours)*60);
}
/**
* Minus X hours from the date
* @memberof module:TrellinatorCore.Trellinator
* @param hours {int} number of hours to minus
* @example
* Trellinator.now().minusHours(1);
*/
Date.prototype.minusHours = function(hours)
{
return this.minusMinutes(parseFloat(hours)*60);
}
/**
* Add X weekdays to the date
* @memberof module:TrellinatorCore.Trellinator
* @param days {int} number of days to add
* @example
* //Tomorrow
* Trellinator.now().addWeekDays(1);
*/
Date.prototype.addWeekDays = function(days)
{
var days = parseInt(days);
var cur_day = 1;
while(days > 0)
{
this.setDate(this.getDate() + cur_day);
if(this.isWeekDay())
days--;
}
return this;
}
/**
* Add X days to the date
* @memberof module:TrellinatorCore.Trellinator
* @param days {int} number of days to add
* @example
* //Tomorrow
* Trellinator.now().addDays(1);
*/
Date.prototype.addDays = function(days)
{
this.setDate(this.getDate() + parseInt(days));
return this;
}
/**
* Add X weeks to the date
* @memberof module:TrellinatorCore.Trellinator
* @param weeks {int} number of weeks to add
* @example
* Trellinator.now().addWeeks(1);
*/
Date.prototype.addWeeks = function(weeks)
{
return this.addDays(parseInt(weeks)*7);
}
/**
* Subtract X weeks from the date
* @memberof module:TrellinatorCore.Trellinator
* @param weeks {int} number of weeks to subtract
* @example
* Trellinator.now().minusWeeks(1);
*/
Date.prototype.minusWeeks = function(weeks)
{
return this.minusDays(parseInt(weeks)*7);
}
Date.isLeapYear = function (year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
};
Date.getDaysInMonth = function (year, month) {
return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};
Date.prototype.isLeapYear = function () {
return Date.isLeapYear(this.getFullYear());
};
Date.prototype.getDaysInMonth = function () {
return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};
/**
* Add X months to the date
* @memberof module:TrellinatorCore.Trellinator
* @param months {int} number of months to add
* @example
* Trellinator.now().addMonths(1);
*/
Date.prototype.addMonths = function(value)
{
var n = this.getDate();
this.setDate(1);
this.setMonth(this.getMonth() + parseInt(value));
this.setDate(Math.min(n, this.getDaysInMonth()));
return this;
}
/**
* Subtract X months from the date
* @memberof module:TrellinatorCore.Trellinator
* @param months {int} number of months to subtract
* @example
* Trellinator.now().minusMonths(1);
*/
Date.prototype.minusMonths = function(months)
{
this.setMonth(this.getMonth() - parseInt(months));
return this;
}
/**
* Return the week of the month, starting
* with 0. Useful if you want to say that
* you are in the 3rd week of August, for
* example
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().weekOfMonth()
*/
Date.prototype.weekOfMonth = function()
{
return this.getWeekOfMonth();
}
//DEPRECATED: use weekOfMonth
Date.prototype.getWeekOfMonth = function() {
var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
var offsetDate = this.getDate() + firstWeekday - 1;
return Math.floor(offsetDate / 7);
}
/**
* Find out the name of the day, returned
* as the full day name eg. Monday
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().dayName();
*/
Date.prototype.dayName = function()
{
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
return weekday[this.getDay()];
}
/**
* Find out the name of the day, returned
* as the short day name eg. Mon
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().shortDayName();
*/
Date.prototype.shortDayName = function()
{
var weekday = new Array(7);
weekday[0] = "Sun";
weekday[1] = "Mon";
weekday[2] = "Tue";
weekday[3] = "Wed";
weekday[4] = "Thu";
weekday[5] = "Fri";
weekday[6] = "Sat";
return weekday[this.getDay()];
}
/**
* Find out the name of the month, returned
* as the full month name eg. August
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().monthName();
*/
Date.prototype.monthName = function()
{
return this.getMonthName();
}
/**
* Find out the name of the month, returned
* as the short month name eg. Aug
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().shortMonthName();
*/
Date.prototype.shortMonthName = function()
{
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return monthNames[this.getMonth()];
}
//DEPRECATED use monthName
Date.prototype.getMonthName = function()
{
var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
return monthNames[this.getMonth()];
}
/**
* Find the previous instance of the given
* day of the week, before the current date
* @memberof module:TrellinatorCore.Trellinator
* @param day {string} the name of the day of the
* week you wish to find the previous instance of
* @example
* Trellinator.now().previous("Monday")
*/
Date.prototype.previous = function(day)
{
index = new Array();
index["sunday"] = 0;
index["monday"] = 1;
index["tuesday"] = 2;
index["wednesday"] = 3;
index["thursday"] = 4;
index["friday"] = 5;
index["saturday"] = 6;
var target = index[day.toLowerCase()];
var daynum = this.getDay();
var diff = ((target - daynum)-7);
if(diff != -7)
var offset = diff % 7;
else
var offset = diff;
this.setDate(this.getDate() + offset);
return this;
}
/**
* Find the next instance of the given
* day of the week, after the current date
* @memberof module:TrellinatorCore.Trellinator
* @param day {string} the name of the day of the
* week you wish to find the next instance of
* @example
* Trellinator.now().next("Monday")
*/
Date.prototype.next = function(day)
{
var index = new Array();
index["sunday"] = 0;
index["monday"] = 1;
index["tuesday"] = 2;
index["wednesday"] = 3;
index["thursday"] = 4;
index["friday"] = 5;
index["saturday"] = 6;
var day_to_find = index[day.toLowerCase()];
var diff = (day_to_find + 7) - this.getDay();
if(diff != 7)
var offset = diff % 7;
else
var offset = diff;
this.setDate(this.getDate() + offset);
return this;
}
/**
* Return the current date in the format
* MONTH DAY, YEAR where MONTH is the full
* month such as August or September,
* day is the date without leading zeroes
* such as 1 or 19 and YEAR is the current
* year with 4 digits such as 2018.
* This method is created to return the date
* in the same format that Butler for Trello
* stores in the {date} variable
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().butlerDefaultDate();
*/
Date.prototype.butlerDefaultDate = function()
{
//May 8, 2018
return this.monthName()+" "+this.getDate()+", "+this.getFullYear();
}
/**
* Find the last day of the current month
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().lastDayOfMonth();
*/
Date.prototype.lastDayOfMonth = function()
{
return new Date(this.getFullYear(), this.getMonth()+1, 0);
}
/**
* Subtract X weekdays from the current date
* @memberof module:TrellinatorCore.Trellinator
* @param days {int} number of days to subtract
* @example
* //3 days ago
* Trellinator.now().minusWeekDays(3);
*/
Date.prototype.minusWeekDays = function(days)
{
var days = parseInt(days);
var cur_day = 1;
while(days > 0)
{
this.setDate(this.getDate()-cur_day);
if(this.isWeekDay())
days--;
}
return this;
}
/**
* Subtract X days from the current date
* @memberof module:TrellinatorCore.Trellinator
* @param days {int} number of days to subtract
* @example
* //3 days ago
* Trellinator.now().minusDays(3);
*/
Date.prototype.minusDays = function(days)
{
this.setDate(this.getDate()-parseInt(days));
return this;
}
/**
* Return a string formatted date from an object
* @memberof module:TrellinatorCore.Trellinator
* @param format {string} the desired format. Currently supports
* only 2 formats: YYYY-MM-DD and HH:MM
* @example
* Trellinator.now().stringFormat("YYYY-MM-DD");
*/
Date.prototype.stringFormat = function(format)
{
if(format == "YYYY-MM-DD")
{
var year = this.getFullYear();
var month = this.getMonth()+1;
var day = this.getDate();
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = '0' + month;
}
var ret = format.replace("YYYY",year).replace("MM",month).replace("DD",day);
}
else if(format == "HH:MM")
{
var hours = this.getHours();
var minutes = this.getMinutes();
if (hours < 10) {
day = '0' + day;
}
if (minutes < 10) {
month = '0' + month;
}
var ret = format.replace("HH",hours).replace("MM",minutes);
}
else
throw new Error("Unsupported format passed to Date.stringFormat: "+format+" add more formats!");
return ret;
}
/**
* Return a string that is the day number
* followed by ordinal suffix (th,rd,st)
* @memberof module:TrellinatorCore.Trellinator
* @example
* Trellinator.now().ordinalDay();
*/
Date.prototype.ordinalDay = function()
{
return new Number(this.getDate()).nth();
}
//https://stackoverflow.com/a/15397539
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
Trellinator.googleDriveIdRegExp = function()
{
return /.*[^-\w]([-\w]{25,})[^-\w]?.*/;
}
////////////////////////////////////////////////////////////////////////////////////////
function getFolderByURL_(folderUrl)
{
var folderID = folderUrl.match(Trellinator.googleDriveIdRegExp())[1];
var folder = DriveApp.getFolderById(folderID);
return folder;
}
////////////////////////////////////////////////////////////////////////////////////////
Trellinator.getFileByURL = function(fileUrl)
{
var fileID = fileUrl.match(Trellinator.googleDriveIdRegExp());
if(fileID)
fileID = fileID[1];
else
throw new InvalidDataException("Could not get file by URL: "+fileUrl);
var file = DriveApp.getFileById(fileID);
return file;
}
////////////////////////////////////////////////////////////////////////////////////////
Trellinator.getFolderByURL = function(folderUrl)
{
var folderID = folderUrl.match(/.*[^-\w]([-\w]{25,})[^-\w]?.*/);
if(folderID)
folderID = folderID[1];
else
throw new InvalidDataException("Could not get folder by URL: "+folderUrl);
var folder = DriveApp.getFolderById(folderID);
return folder;
}
Trellinator.findOrCreateFileByName = function(filename,creator,parent)
{
if(!parent)
{
parent = DriveApp.getRootFolder();
}
if(!creator)
{
creator = DocumentApp;
}
try
{
var ret = Trellinator.getFileByName(filename,parent);
}
catch(e)
{
Notification.expectException(InvalidDataException,e);
var ret = DriveApp.getFileById(creator.create(filename).getId());
DriveApp.getRootFolder().removeFile(ret);
parent.addFile(ret);
}
return ret;
}