@churchapps/helpers 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/DateHelper.ts CHANGED
@@ -1,122 +1,122 @@
1
- import { format as dateFormat } from "date-fns"
2
- import dayjs from "dayjs"
3
-
4
- export class DateHelper {
5
-
6
- //Fixes timezone issues when you just need the date.
7
- static toDate(input: any) {
8
- return new Date(Date.parse(input.toString().replace("Z", "")));
9
- }
10
-
11
- static toDateTime(input: any) {
12
- return new Date(Date.parse(input.toString()));
13
- }
14
-
15
- //obsolete. Do not use
16
- static convertToDate(input: any) {
17
- return this.toDateTime(input);
18
- }
19
-
20
- static addDays(date: Date, days: number) {
21
- date.setDate(date.getDate() + days);
22
- return date;
23
- }
24
-
25
- static prettyDate(date: Date) {
26
- if (date === undefined || date === null) return "";
27
- return this.formatDateTime(date, "MMM d, yyyy");
28
- }
29
-
30
- static prettyDateTime(date: Date) {
31
- if (date === undefined || date === null) return "";
32
- return this.formatDateTime(date, "MMM d, yyyy h:mm a");
33
- }
34
-
35
- static prettyTime(date: Date) {
36
- if (date === undefined || date === null) return "";
37
- return this.formatDateTime(date, "h:mm a");
38
- }
39
-
40
- static getLastSunday() {
41
- let result = new Date();
42
- while (result.getDay() !== 0) result.setDate(result.getDate() - 1);
43
- return result;
44
- }
45
-
46
- static getWeekSunday(year: number, week: number) {
47
- let result = new Date(year, 0, 1);
48
- while (result.getDay() !== 0) result.setDate(result.getDate() + 1);
49
- result.setDate(result.getDate() + ((week - 1) * 7));
50
- return result;
51
- }
52
-
53
- static formatHtml5Date(date: Date): string {
54
- let result = "";
55
- if (date !== undefined && date !== null) {
56
- try {
57
- result = new Date(date).toISOString().split("T")[0];
58
- } catch { }
59
- }
60
- return result;
61
- }
62
-
63
- static formatHtml5Time(time: Date): string {
64
- let h = time.getHours();
65
- let m = time.getMinutes();
66
- let s = time.getSeconds();
67
- return `${h < 10 ? ("0" + h) : h}:${m < 10 ? ("0" + m) : m}:${s < 10 ? ("0" + s) : s}`;
68
- }
69
-
70
- static formatHtml5DateTime(date: Date): string {
71
- if (date === undefined || date === null) return "";
72
- else {
73
- return this.formatDateTime(date, "yyyy-MM-dd") + "T" + this.formatDateTime(date, "HH:mm");
74
- }
75
- }
76
-
77
- static getDisplayDuration(d: Date): string {
78
- let seconds = Math.round((new Date().getTime() - d.getTime()) / 1000);
79
- if (seconds > 86400) {
80
- let days = Math.floor(seconds / 86400);
81
- return (days === 1) ? "1d" : days.toString() + "d";
82
- }
83
- else if (seconds > 3600) {
84
- let hours = Math.floor(seconds / 3600);
85
- return (hours === 1) ? "1h" : hours.toString() + "h";
86
- }
87
- else if (seconds > 60) {
88
- let minutes = Math.floor(seconds / 60);
89
- return (minutes === 1) ? "1m" : minutes.toString() + "m";
90
- }
91
- else return (seconds === 1) ? "1s" : Math.floor(seconds).toString() + "s";
92
- }
93
-
94
- static getShortDate(d: Date): string {
95
- return (d.getMonth() + 1).toString() + "/" + (d.getDate()).toString() + "/" + d.getFullYear().toString();
96
- }
97
-
98
- static convertDatePickerFormat(d: Date): Date {
99
- const date = this.formatHtml5Date(d).split("-");
100
- if (date.length === 3) return new Date(`${date[1]}-${date[2]}-${date[0]}`);
101
- return new Date();
102
- }
103
-
104
- private static formatDateTime(date: Date, format: string) {
105
- try {
106
- return dateFormat(date, format);
107
- } catch { return ""; }
108
- }
109
-
110
- public static toMysqlDate(d: Date) {
111
- if (d === null || d === undefined) {
112
- return undefined;
113
- }
114
- return dayjs(d).format("YYYY-MM-DD HH:mm:ss")
115
- }
116
-
117
- public static subtractHoursFromNow(hour: number) {
118
- const now = new Date();
119
- return new Date(now.setHours(now.getHours() - hour));
120
- }
121
-
122
- }
1
+ import { format as dateFormat } from "date-fns"
2
+ import dayjs from "dayjs"
3
+
4
+ export class DateHelper {
5
+
6
+ //Fixes timezone issues when you just need the date.
7
+ static toDate(input: any) {
8
+ return new Date(Date.parse(input.toString().replace("Z", "")));
9
+ }
10
+
11
+ static toDateTime(input: any) {
12
+ return new Date(Date.parse(input.toString()));
13
+ }
14
+
15
+ //obsolete. Do not use
16
+ static convertToDate(input: any) {
17
+ return this.toDateTime(input);
18
+ }
19
+
20
+ static addDays(date: Date, days: number) {
21
+ date.setDate(date.getDate() + days);
22
+ return date;
23
+ }
24
+
25
+ static prettyDate(date: Date) {
26
+ if (date === undefined || date === null) return "";
27
+ return this.formatDateTime(date, "MMM d, yyyy");
28
+ }
29
+
30
+ static prettyDateTime(date: Date) {
31
+ if (date === undefined || date === null) return "";
32
+ return this.formatDateTime(date, "MMM d, yyyy h:mm a");
33
+ }
34
+
35
+ static prettyTime(date: Date) {
36
+ if (date === undefined || date === null) return "";
37
+ return this.formatDateTime(date, "h:mm a");
38
+ }
39
+
40
+ static getLastSunday() {
41
+ let result = new Date();
42
+ while (result.getDay() !== 0) result.setDate(result.getDate() - 1);
43
+ return result;
44
+ }
45
+
46
+ static getWeekSunday(year: number, week: number) {
47
+ let result = new Date(year, 0, 1);
48
+ while (result.getDay() !== 0) result.setDate(result.getDate() + 1);
49
+ result.setDate(result.getDate() + ((week - 1) * 7));
50
+ return result;
51
+ }
52
+
53
+ static formatHtml5Date(date: Date): string {
54
+ let result = "";
55
+ if (date !== undefined && date !== null) {
56
+ try {
57
+ result = new Date(date).toISOString().split("T")[0];
58
+ } catch { }
59
+ }
60
+ return result;
61
+ }
62
+
63
+ static formatHtml5Time(time: Date): string {
64
+ let h = time.getHours();
65
+ let m = time.getMinutes();
66
+ let s = time.getSeconds();
67
+ return `${h < 10 ? ("0" + h) : h}:${m < 10 ? ("0" + m) : m}:${s < 10 ? ("0" + s) : s}`;
68
+ }
69
+
70
+ static formatHtml5DateTime(date: Date): string {
71
+ if (date === undefined || date === null) return "";
72
+ else {
73
+ return this.formatDateTime(date, "yyyy-MM-dd") + "T" + this.formatDateTime(date, "HH:mm");
74
+ }
75
+ }
76
+
77
+ static getDisplayDuration(d: Date): string {
78
+ let seconds = Math.round((new Date().getTime() - d.getTime()) / 1000);
79
+ if (seconds > 86400) {
80
+ let days = Math.floor(seconds / 86400);
81
+ return (days === 1) ? "1d" : days.toString() + "d";
82
+ }
83
+ else if (seconds > 3600) {
84
+ let hours = Math.floor(seconds / 3600);
85
+ return (hours === 1) ? "1h" : hours.toString() + "h";
86
+ }
87
+ else if (seconds > 60) {
88
+ let minutes = Math.floor(seconds / 60);
89
+ return (minutes === 1) ? "1m" : minutes.toString() + "m";
90
+ }
91
+ else return (seconds === 1) ? "1s" : Math.floor(seconds).toString() + "s";
92
+ }
93
+
94
+ static getShortDate(d: Date): string {
95
+ return (d.getMonth() + 1).toString() + "/" + (d.getDate()).toString() + "/" + d.getFullYear().toString();
96
+ }
97
+
98
+ static convertDatePickerFormat(d: Date): Date {
99
+ const date = this.formatHtml5Date(d).split("-");
100
+ if (date.length === 3) return new Date(`${date[1]}-${date[2]}-${date[0]}`);
101
+ return new Date();
102
+ }
103
+
104
+ private static formatDateTime(date: Date, format: string) {
105
+ try {
106
+ return dateFormat(date, format);
107
+ } catch { return ""; }
108
+ }
109
+
110
+ public static toMysqlDate(d: Date) {
111
+ if (d === null || d === undefined) {
112
+ return undefined;
113
+ }
114
+ return dayjs(d).format("YYYY-MM-DD HH:mm:ss")
115
+ }
116
+
117
+ public static subtractHoursFromNow(hour: number) {
118
+ const now = new Date();
119
+ return new Date(now.setHours(now.getHours() - hour));
120
+ }
121
+
122
+ }
@@ -1,26 +1,26 @@
1
- export class DonationHelper {
2
-
3
- static getInterval(intervalName:string) {
4
- let intervalCount = 1;
5
- let intervalType = "month";
6
- let parts = intervalName.split("_");
7
- if (parts.length === 2) {
8
- switch (parts[0])
9
- {
10
- case "two": intervalCount = 2; break;
11
- case "three": intervalCount = 3; break;
12
- }
13
- intervalType = parts[1];
14
- }
15
- let result = { interval_count: intervalCount, interval: intervalType };
16
- return result;
17
- }
18
-
19
- static getIntervalKeyName(intervalCount: number, intervalType: string) {
20
- let firstPart = "one";
21
- if (intervalCount === 2) firstPart = "two";
22
- else if (intervalCount === 3) firstPart = "three";
23
- return firstPart + "_" + intervalType;
24
- }
25
-
26
- }
1
+ export class DonationHelper {
2
+
3
+ static getInterval(intervalName:string) {
4
+ let intervalCount = 1;
5
+ let intervalType = "month";
6
+ let parts = intervalName.split("_");
7
+ if (parts.length === 2) {
8
+ switch (parts[0])
9
+ {
10
+ case "two": intervalCount = 2; break;
11
+ case "three": intervalCount = 3; break;
12
+ }
13
+ intervalType = parts[1];
14
+ }
15
+ let result = { interval_count: intervalCount, interval: intervalType };
16
+ return result;
17
+ }
18
+
19
+ static getIntervalKeyName(intervalCount: number, intervalType: string) {
20
+ let firstPart = "one";
21
+ if (intervalCount === 2) firstPart = "two";
22
+ else if (intervalCount === 3) firstPart = "three";
23
+ return firstPart + "_" + intervalType;
24
+ }
25
+
26
+ }
@@ -1,36 +1,36 @@
1
- import { ErrorLogInterface, ErrrorAppDataInterface } from "./interfaces/Error";
2
- import { ApiHelper } from "./ApiHelper";
3
-
4
-
5
- export class ErrorHelper {
6
-
7
- static getAppData:() => { churchId: string, userId: string, originUrl: string, application: string};
8
- static customErrorHandler:(errorLog:ErrorLogInterface) => void;
9
-
10
- static init = (getAppData:() => ErrrorAppDataInterface, customErrorHandler:(errorLog:ErrorLogInterface) => void) => {
11
- ErrorHelper.getAppData = getAppData;
12
- ErrorHelper.customErrorHandler = customErrorHandler;
13
- }
14
-
15
- static logError = (errorType:string, message:string, details:string) => {
16
- if (this.getAppData)
17
- {
18
- const data = this.getAppData();
19
- const log:ErrorLogInterface = {
20
- application: data.application,
21
- errorTime: new Date(),
22
- userId: data.userId,
23
- churchId: data.churchId,
24
- originUrl: data.originUrl,
25
- errorType: errorType,
26
- message: message,
27
- details: details
28
- }
29
-
30
- if (log.errorType==="401" && log.message.indexOf("/users/login")>-1) return;
31
- ApiHelper.postAnonymous("/errors", [log], "MembershipApi");
32
- if (ErrorHelper.customErrorHandler) ErrorHelper.customErrorHandler(log);
33
- }
34
- }
35
-
36
- }
1
+ import { ErrorLogInterface, ErrrorAppDataInterface } from "./interfaces/Error";
2
+ import { ApiHelper } from "./ApiHelper";
3
+
4
+
5
+ export class ErrorHelper {
6
+
7
+ static getAppData:() => { churchId: string, userId: string, originUrl: string, application: string};
8
+ static customErrorHandler:(errorLog:ErrorLogInterface) => void;
9
+
10
+ static init = (getAppData:() => ErrrorAppDataInterface, customErrorHandler:(errorLog:ErrorLogInterface) => void) => {
11
+ ErrorHelper.getAppData = getAppData;
12
+ ErrorHelper.customErrorHandler = customErrorHandler;
13
+ }
14
+
15
+ static logError = (errorType:string, message:string, details:string) => {
16
+ if (this.getAppData)
17
+ {
18
+ const data = this.getAppData();
19
+ const log:ErrorLogInterface = {
20
+ application: data.application,
21
+ errorTime: new Date(),
22
+ userId: data.userId,
23
+ churchId: data.churchId,
24
+ originUrl: data.originUrl,
25
+ errorType: errorType,
26
+ message: message,
27
+ details: details
28
+ }
29
+
30
+ if (log.errorType==="401" && log.message.indexOf("/users/login")>-1) return;
31
+ ApiHelper.postAnonymous("/errors", [log], "MembershipApi");
32
+ if (ErrorHelper.customErrorHandler) ErrorHelper.customErrorHandler(log);
33
+ }
34
+ }
35
+
36
+ }
@@ -1,49 +1,49 @@
1
- import { EventInterface } from "./interfaces";
2
- import { RRule, datetime } from "rrule";
3
- import { ParsedOptions } from "rrule/dist/esm/types";
4
-
5
- export class EventHelper {
6
-
7
- static getRange = (event:EventInterface, startDate:Date, endDate:Date) => {
8
- const start = new Date(event.start);
9
- const rrule = EventHelper.getFullRRule(event);
10
-
11
- const dates = rrule.between(startDate, endDate);
12
-
13
- dates.forEach(d => {
14
- d.setHours(start.getHours());
15
- d.setMinutes(start.getMinutes());
16
- d.setSeconds(start.getSeconds());
17
- })
18
- return dates;
19
- }
20
-
21
- static getFullRRule = (event:EventInterface) => {
22
- let rrule = RRule.fromString(event.recurrenceRule);
23
- rrule.options.dtstart = new Date(event.start);
24
- return rrule;
25
- }
26
-
27
- static removeExcludeDates = (events:EventInterface[]) => {
28
- for (let i=events.length-1; i>=0; i--) {
29
- if (events[i].exceptionDates?.length>0)
30
- {
31
- const parsedDates = events[i].exceptionDates.map(d=>new Date(d).toISOString());
32
- if (parsedDates.indexOf(events[i].start.toISOString())>-1) events.splice(i,1);
33
- }
34
- }
35
- }
36
-
37
- static getPartialRRuleString = (options:ParsedOptions) => {
38
- const parts = new RRule(options).toString().split("RRULE:");
39
- const result = parts.length===2 ? parts[1] : "";
40
- return result;
41
- }
42
-
43
- static cleanRule = (options:ParsedOptions) => {
44
- options.byhour = undefined;
45
- options.byminute = undefined;
46
- options.bysecond = undefined;
47
- }
48
-
49
- }
1
+ import { EventInterface } from "./interfaces";
2
+ import { RRule, datetime } from "rrule";
3
+ import { ParsedOptions } from "rrule/dist/esm/types";
4
+
5
+ export class EventHelper {
6
+
7
+ static getRange = (event:EventInterface, startDate:Date, endDate:Date) => {
8
+ const start = new Date(event.start);
9
+ const rrule = EventHelper.getFullRRule(event);
10
+
11
+ const dates = rrule.between(startDate, endDate);
12
+
13
+ dates.forEach(d => {
14
+ d.setHours(start.getHours());
15
+ d.setMinutes(start.getMinutes());
16
+ d.setSeconds(start.getSeconds());
17
+ })
18
+ return dates;
19
+ }
20
+
21
+ static getFullRRule = (event:EventInterface) => {
22
+ let rrule = RRule.fromString(event.recurrenceRule);
23
+ rrule.options.dtstart = new Date(event.start);
24
+ return rrule;
25
+ }
26
+
27
+ static removeExcludeDates = (events:EventInterface[]) => {
28
+ for (let i=events.length-1; i>=0; i--) {
29
+ if (events[i].exceptionDates?.length>0)
30
+ {
31
+ const parsedDates = events[i].exceptionDates.map(d=>new Date(d).toISOString());
32
+ if (parsedDates.indexOf(events[i].start.toISOString())>-1) events.splice(i,1);
33
+ }
34
+ }
35
+ }
36
+
37
+ static getPartialRRuleString = (options:ParsedOptions) => {
38
+ const parts = new RRule(options).toString().split("RRULE:");
39
+ const result = parts.length===2 ? parts[1] : "";
40
+ return result;
41
+ }
42
+
43
+ static cleanRule = (options:ParsedOptions) => {
44
+ options.byhour = undefined;
45
+ options.byminute = undefined;
46
+ options.bysecond = undefined;
47
+ }
48
+
49
+ }
package/src/FileHelper.ts CHANGED
@@ -1,31 +1,31 @@
1
- import axios from "axios";
2
-
3
- export class FileHelper {
4
-
5
- static postPresignedFile = (presigned: any, uploadedFile: File, progressCallback: (percent: number) => void) => {
6
- const formData = new FormData();
7
- formData.append("key", presigned.key);
8
- formData.append("acl", "public-read");
9
- formData.append("Content-Type", uploadedFile.type);
10
- for (const property in presigned.fields) formData.append(property, presigned.fields[property]);
11
- formData.append("file", uploadedFile);
12
-
13
- const axiosConfig = {
14
- headers: { "Content-Type": "multipart/form-data" },
15
- onUploadProgress: (data: any) => {
16
- progressCallback(Math.round((100 * data.loaded) / data.total));
17
- }
18
- };
19
-
20
- return axios.post(presigned.url, formData, axiosConfig);
21
- };
22
-
23
- static dataURLtoBlob(dataurl: string) {
24
- let arr = dataurl.split(","), mime = arr[0].match(/:(.*?);/)[1],
25
- bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
26
- while (n--) {
27
- u8arr[n] = bstr.charCodeAt(n);
28
- }
29
- return new Blob([u8arr], { type: mime });
30
- }
31
- }
1
+ import axios from "axios";
2
+
3
+ export class FileHelper {
4
+
5
+ static postPresignedFile = (presigned: any, uploadedFile: File, progressCallback: (percent: number) => void) => {
6
+ const formData = new FormData();
7
+ formData.append("key", presigned.key);
8
+ formData.append("acl", "public-read");
9
+ formData.append("Content-Type", uploadedFile.type);
10
+ for (const property in presigned.fields) formData.append(property, presigned.fields[property]);
11
+ formData.append("file", uploadedFile);
12
+
13
+ const axiosConfig = {
14
+ headers: { "Content-Type": "multipart/form-data" },
15
+ onUploadProgress: (data: any) => {
16
+ progressCallback(Math.round((100 * data.loaded) / data.total));
17
+ }
18
+ };
19
+
20
+ return axios.post(presigned.url, formData, axiosConfig);
21
+ };
22
+
23
+ static dataURLtoBlob(dataurl: string) {
24
+ let arr = dataurl.split(","), mime = arr[0].match(/:(.*?);/)[1],
25
+ bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
26
+ while (n--) {
27
+ u8arr[n] = bstr.charCodeAt(n);
28
+ }
29
+ return new Blob([u8arr], { type: mime });
30
+ }
31
+ }