@capillarytech/cap-ui-utils 1.3.4 → 1.3.6

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/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as Auth } from './auth';
2
2
  export { default as GA } from './GA';
3
3
  export { default as multipleOrgSwitch } from './utils/multipleOrgSwitch';
4
- export { default as utilsSessionStorageApi } from './utils/utilsSessionStorageApi';
4
+ export { default as utilsSessionStorageApi } from './utils/utilsSessionStorageApi';
5
+ export { default as dateHelper } from './utils/date/dateHelper';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,6 +10,8 @@
10
10
  "dependencies": {
11
11
  "react": "^16.13.0",
12
12
  "react-ga": "^2.7.0",
13
- "tti-polyfill": "^0.2.2"
13
+ "tti-polyfill": "^0.2.2",
14
+ "moment-timezone": "^0.5.25",
15
+ "react-intl": "2.7.2"
14
16
  }
15
17
  }
@@ -0,0 +1,5 @@
1
+ export const DATE_FORMAT = "DD/MM/YYYY";
2
+ export const DATE_DISPLAY_FORMAT = "DD MMM YYYY";
3
+ export const TIME_DISPLAY_FORMAT = "hh:mm a";
4
+
5
+ export const STANDARD_DATE_FORMAT = 'YYYY-MM-DD';
@@ -0,0 +1,167 @@
1
+ import moment from "moment-timezone";
2
+ import range from "lodash/range";
3
+ import messages from "./messages";
4
+ import * as constants from "./constants";
5
+
6
+ const {
7
+ DATE_DISPLAY_FORMAT,
8
+ DATE_FORMAT,
9
+ TIME_DISPLAY_FORMAT,
10
+ STANDARD_DATE_FORMAT,
11
+ } = constants;
12
+
13
+ let _intl = {
14
+ formatMessage: () => {},
15
+ formatNumer: () => {},
16
+ };
17
+
18
+ const initializeIntl = (intl) => {
19
+ _intl = intl;
20
+ };
21
+
22
+ const isPreviousDate = (current) =>
23
+ current < moment().subtract(1, "days").endOf("day");
24
+
25
+ const getPreviousHours = (date) => {
26
+ const momentDate = date ? moment(date) : moment();
27
+ return range(momentDate.hour());
28
+ };
29
+
30
+ const getPreviousMinutes = (date) => {
31
+ const momentDate = date ? moment(date) : moment();
32
+ return range(momentDate.minute());
33
+ };
34
+
35
+ const disableDateRange = (startDate, endDate) => (current) =>
36
+ moment(endDate).endOf("day") < current ||
37
+ current < moment(startDate).startOf("day");
38
+
39
+ const getRelativeDay = (date) => {
40
+ const selectedFormat = "Do MMM";
41
+ const todayDate = moment().format(selectedFormat);
42
+ const yesterdayDate = moment().subtract(1, "days").format(selectedFormat);
43
+ const tomorrowDate = moment().add(1, "days").format(selectedFormat);
44
+
45
+ return moment(date).calendar(null, {
46
+ sameDay: `[${_intl.formatMessage(messages.today)}, ${todayDate}]`,
47
+ nextDay: `[${_intl.formatMessage(messages.tomorrow)}, ${tomorrowDate}]`,
48
+ lastDay: `[${_intl.formatMessage(messages.yesterday)}, ${yesterdayDate}]`,
49
+ sameElse: DATE_DISPLAY_FORMAT,
50
+ nextWeek: DATE_DISPLAY_FORMAT,
51
+ lastWeek: DATE_DISPLAY_FORMAT,
52
+ });
53
+ };
54
+
55
+ const getEpochTimeStamp = ({ timezone, date, withMilliSeconds = true }) => {
56
+ let method = "valueOf"; // Unix time in milliseconds: http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/
57
+ if (!withMilliSeconds) {
58
+ method = "unix"; // unix time in seconds: http://momentjs.com/docs/#/displaying/unix-timestamp/
59
+ }
60
+ if (timezone) {
61
+ return moment(date).tz(timezone)[method](); //eslint-disable-line prettier/prettier
62
+ } else {
63
+ return moment(date)[method]();
64
+ }
65
+ };
66
+
67
+ /**
68
+ * Gives ISO day of the week with 1 being Monday and 7 being Sunday in numbers for org settings
69
+ * Org Monday = 0, Tuesday = 1, Wed = 2, Thur =3, Fri = 4, Sat = 5, Sun = 6
70
+ * @param weekDay
71
+ * @returns {number}
72
+ */
73
+ function getISOWeekDayNumberForOrgSetting(weekDay) {
74
+ if (isNaN(Number(weekDay)) || Number(weekDay) > 6 || Number(weekDay) < 0) {
75
+ return 1;
76
+ }
77
+ if (Number(weekDay) === 6) return 0; //to handle Sunday of this week and not next sunday
78
+ return Number(weekDay) + 1;
79
+ }
80
+
81
+ /**
82
+ * base date range for last period
83
+ * @param lastSyncDate
84
+ * @param baseFilter
85
+ * @param periodValue
86
+ * @param weekStart
87
+ * @returns {{start, end}}
88
+ */
89
+ function getDateRangeForPeriod(
90
+ lastSyncDate,
91
+ baseFilter,
92
+ periodValue,
93
+ weekStart = 0
94
+ ) {
95
+ const dateRange = {};
96
+ const now = moment(lastSyncDate);
97
+ if (baseFilter === "week") {
98
+ const weekStartDay = getISOWeekDayNumberForOrgSetting(weekStart);
99
+ const startOfWeek = now.clone().isoWeekday(weekStartDay);
100
+ if (startOfWeek.isAfter(now)) {
101
+ startOfWeek.subtract(1, "week");
102
+ }
103
+ dateRange.start = startOfWeek.clone().subtract(periodValue, baseFilter);
104
+ dateRange.end = startOfWeek.clone();
105
+ } else if (baseFilter === "month") {
106
+ dateRange.start = now.clone().subtract(periodValue, baseFilter);
107
+ dateRange.end = now.clone();
108
+ } else {
109
+ dateRange.start = moment()
110
+ .subtract(periodValue, baseFilter)
111
+ .startOf(baseFilter);
112
+ dateRange.end = now.clone();
113
+ }
114
+ return getFormattedDateRange(dateRange);
115
+ }
116
+
117
+ /**
118
+ * Return formatted values for start and end of date range
119
+ * @param dateRange
120
+ * @param dateFormat
121
+ * @returns {{start, end}}
122
+ */
123
+ function getFormattedDateRange(dateRange, dateFormat = STANDARD_DATE_FORMAT) {
124
+ return {
125
+ start: dateRange.start.clone().format(dateFormat),
126
+ end: dateRange.end.clone().format(dateFormat),
127
+ };
128
+ }
129
+
130
+ const getDayDifference = (currentDate, compareDate) => {
131
+ // startDate, endDate can't be epoch time in seconds, as the parsing is different for that
132
+ const formattedCurrentDate = moment(currentDate).format(DATE_FORMAT);
133
+ const formattedCompareDate = moment(compareDate).format(DATE_FORMAT);
134
+ return moment(formattedCompareDate, DATE_FORMAT).diff(
135
+ moment(formattedCurrentDate, DATE_FORMAT),
136
+ "day"
137
+ );
138
+ };
139
+
140
+ const msToDateFormat = (date) => moment(date).format(DATE_DISPLAY_FORMAT);
141
+
142
+ const msToTimeFormat = (date) => moment(date).format(TIME_DISPLAY_FORMAT);
143
+
144
+ /* unix() function gives number of seconds since epoch time,
145
+ but for the api need to app number of days since epoch time.
146
+ so deviding number of seconds since epoch with number of seconds a day to get number of days since epoch
147
+ */
148
+
149
+ const getEpochDays = (date) =>
150
+ date && Math.floor(moment(date.clone()).unix() / 86400); // 86400 === number of seconds a day
151
+
152
+ export default {
153
+ initializeIntl,
154
+ isPreviousDate,
155
+ getPreviousHours,
156
+ getPreviousMinutes,
157
+ disableDateRange,
158
+ getRelativeDay,
159
+ getEpochTimeStamp,
160
+ getISOWeekDayNumberForOrgSetting,
161
+ getDateRangeForPeriod,
162
+ getFormattedDateRange,
163
+ getDayDifference,
164
+ msToDateFormat,
165
+ msToTimeFormat,
166
+ getEpochDays,
167
+ };
@@ -0,0 +1,24 @@
1
+ /*
2
+ * dateHelper Messages
3
+ *
4
+ * This contains all the text for the dateHelper.
5
+ */
6
+
7
+ import { defineMessages } from "react-intl";
8
+
9
+ export const scope = "uiCommonUtils.utils.date.dateHelper";
10
+
11
+ export default defineMessages({
12
+ today: {
13
+ id: `${scope}.today`,
14
+ defaultMessage: "Today",
15
+ },
16
+ tomorrow: {
17
+ id: `${scope}.tomorrow`,
18
+ defaultMessage: "Tomorrow",
19
+ },
20
+ yesterday: {
21
+ id: `${scope}.yesterday`,
22
+ defaultMessage: "Yesterday",
23
+ },
24
+ });