@capillarytech/cap-ui-utils 1.3.5 → 1.3.7

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,5 +1,7 @@
1
- export { default as Auth } from './auth';
2
- export { default as GA } from './GA';
3
- export { default as multipleOrgSwitch } from './utils/multipleOrgSwitch';
4
- export { default as utilsSessionStorageApi } from './utils/utilsSessionStorageApi';
5
- export { default as dateHelper } from './utils/date/dateHelper';
1
+ export { default as Auth } from "./auth";
2
+ export { default as GA } from "./GA";
3
+ export { default as multipleOrgSwitch } from "./utils/multipleOrgSwitch";
4
+ export { default as utilsSessionStorageApi } from "./utils/utilsSessionStorageApi";
5
+ export { default as dateHelper } from "./utils/date/dateHelper";
6
+ export { default as formatter } from "./utils/formatter";
7
+ export { default as validationHelper } from "./utils/validationHelper";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,6 +12,7 @@
12
12
  "react-ga": "^2.7.0",
13
13
  "tti-polyfill": "^0.2.2",
14
14
  "moment-timezone": "^0.5.25",
15
- "react-intl": "2.7.2"
15
+ "react-intl": "2.7.2",
16
+ "lodash": "4.17.11"
16
17
  }
17
18
  }
@@ -15,28 +15,28 @@ let _intl = {
15
15
  formatNumer: () => {},
16
16
  };
17
17
 
18
- export const initializeIntl = (intl) => {
18
+ const initializeIntl = (intl) => {
19
19
  _intl = intl;
20
20
  };
21
21
 
22
- export const isPreviousDate = (current) =>
22
+ const isPreviousDate = (current) =>
23
23
  current < moment().subtract(1, "days").endOf("day");
24
24
 
25
- export const getPreviousHours = (date) => {
25
+ const getPreviousHours = (date) => {
26
26
  const momentDate = date ? moment(date) : moment();
27
27
  return range(momentDate.hour());
28
28
  };
29
29
 
30
- export const getPreviousMinutes = (date) => {
30
+ const getPreviousMinutes = (date) => {
31
31
  const momentDate = date ? moment(date) : moment();
32
32
  return range(momentDate.minute());
33
33
  };
34
34
 
35
- export const disableDateRange = (startDate, endDate) => (current) =>
35
+ const disableDateRange = (startDate, endDate) => (current) =>
36
36
  moment(endDate).endOf("day") < current ||
37
37
  current < moment(startDate).startOf("day");
38
38
 
39
- export const getRelativeDay = (date) => {
39
+ const getRelativeDay = (date) => {
40
40
  const selectedFormat = "Do MMM";
41
41
  const todayDate = moment().format(selectedFormat);
42
42
  const yesterdayDate = moment().subtract(1, "days").format(selectedFormat);
@@ -51,11 +51,8 @@ export const getRelativeDay = (date) => {
51
51
  lastWeek: DATE_DISPLAY_FORMAT,
52
52
  });
53
53
  };
54
- export const getEpochTimeStamp = ({
55
- timezone,
56
- date,
57
- withMilliSeconds = true,
58
- }) => {
54
+
55
+ const getEpochTimeStamp = ({ timezone, date, withMilliSeconds = true }) => {
59
56
  let method = "valueOf"; // Unix time in milliseconds: http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/
60
57
  if (!withMilliSeconds) {
61
58
  method = "unix"; // unix time in seconds: http://momentjs.com/docs/#/displaying/unix-timestamp/
@@ -73,7 +70,7 @@ export const getEpochTimeStamp = ({
73
70
  * @param weekDay
74
71
  * @returns {number}
75
72
  */
76
- export function getISOWeekDayNumberForOrgSetting(weekDay) {
73
+ function getISOWeekDayNumberForOrgSetting(weekDay) {
77
74
  if (isNaN(Number(weekDay)) || Number(weekDay) > 6 || Number(weekDay) < 0) {
78
75
  return 1;
79
76
  }
@@ -89,7 +86,7 @@ export function getISOWeekDayNumberForOrgSetting(weekDay) {
89
86
  * @param weekStart
90
87
  * @returns {{start, end}}
91
88
  */
92
- export function getDateRangeForPeriod(
89
+ function getDateRangeForPeriod(
93
90
  lastSyncDate,
94
91
  baseFilter,
95
92
  periodValue,
@@ -123,16 +120,14 @@ export function getDateRangeForPeriod(
123
120
  * @param dateFormat
124
121
  * @returns {{start, end}}
125
122
  */
126
- export function getFormattedDateRange(
127
- dateRange,
128
- dateFormat = STANDARD_DATE_FORMAT
129
- ) {
123
+ function getFormattedDateRange(dateRange, dateFormat = STANDARD_DATE_FORMAT) {
130
124
  return {
131
125
  start: dateRange.start.clone().format(dateFormat),
132
126
  end: dateRange.end.clone().format(dateFormat),
133
127
  };
134
128
  }
135
- export const getDayDifference = (currentDate, compareDate) => {
129
+
130
+ const getDayDifference = (currentDate, compareDate) => {
136
131
  // startDate, endDate can't be epoch time in seconds, as the parsing is different for that
137
132
  const formattedCurrentDate = moment(currentDate).format(DATE_FORMAT);
138
133
  const formattedCompareDate = moment(compareDate).format(DATE_FORMAT);
@@ -142,16 +137,31 @@ export const getDayDifference = (currentDate, compareDate) => {
142
137
  );
143
138
  };
144
139
 
145
- export const msToDateFormat = (date) =>
146
- moment(date).format(DATE_DISPLAY_FORMAT);
140
+ const msToDateFormat = (date) => moment(date).format(DATE_DISPLAY_FORMAT);
147
141
 
148
- export const msToTimeFormat = (date) =>
149
- moment(date).format(TIME_DISPLAY_FORMAT);
142
+ const msToTimeFormat = (date) => moment(date).format(TIME_DISPLAY_FORMAT);
150
143
 
151
144
  /* unix() function gives number of seconds since epoch time,
152
145
  but for the api need to app number of days since epoch time.
153
146
  so deviding number of seconds since epoch with number of seconds a day to get number of days since epoch
154
147
  */
155
148
 
156
- export const getEpochDays = (date) =>
149
+ const getEpochDays = (date) =>
157
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
+ };
@@ -6,7 +6,7 @@
6
6
 
7
7
  import { defineMessages } from "react-intl";
8
8
 
9
- export const scope = "uiUtils.utils.date.dateHelper";
9
+ export const scope = "uiCommonUtils.utils.date.dateHelper";
10
10
 
11
11
  export default defineMessages({
12
12
  today: {
@@ -0,0 +1,122 @@
1
+ import { isEmpty } from "lodash";
2
+ import { isNegativeNumber } from "./validationHelper";
3
+
4
+ const snakeCaseToCamelCase = (string) => {
5
+ const regex = /(\_\w)/g; //eslint-disable-line
6
+ const camelCaseString = string.replace(regex, (matches) =>
7
+ matches[1].toUpperCase()
8
+ );
9
+
10
+ return camelCaseString;
11
+ };
12
+
13
+ const camelize = (string = "") => {
14
+ const updatedString = string.replace(/[-_\s.]+(.)?/g, (_, c) =>
15
+ c ? c.toUpperCase() : ""
16
+ );
17
+ return updatedString.substr(0, 1).toLowerCase() + updatedString.substr(1);
18
+ };
19
+
20
+ function formatNumberWithSymbol(d, settings, currencySymbol, currencySettings) {
21
+ const applyDecimals =
22
+ settings && settings.decimals >= 0 ? settings.decimals : 2;
23
+ const type = (settings && settings.units) || "";
24
+ const isNegative = isNegativeNumber(d);
25
+ const numberSettings = getNumberRepresentation(currencySettings);
26
+ let num = d;
27
+ let formattedNumber = d;
28
+ if (isNegative) {
29
+ num = Math.abs(d);
30
+ }
31
+ let formatted = false;
32
+ const symbol = !isEmpty(currencySymbol) ? currencySymbol : "";
33
+ switch (type) {
34
+ case "currency":
35
+ for (let i = 0; i < numberSettings.length; i += 1) {
36
+ if (num >= numberSettings[i].value) {
37
+ const value = num / numberSettings[i].value;
38
+ formattedNumber = isNegative
39
+ ? truncateNumber(value, 2) * -1
40
+ : truncateNumber(value, 2);
41
+ formattedNumber = new Intl.NumberFormat().format(formattedNumber);
42
+ formattedNumber = `${symbol}${formattedNumber}${numberSettings[i].symbol}`;
43
+ formatted = true;
44
+ break;
45
+ }
46
+ }
47
+ if (!formatted) {
48
+ formattedNumber = isNegative
49
+ ? truncateNumber(num, applyDecimals) * -1
50
+ : truncateNumber(num, applyDecimals);
51
+ formattedNumber = new Intl.NumberFormat().format(formattedNumber);
52
+ formattedNumber = `${symbol}${formattedNumber}`;
53
+ }
54
+ break;
55
+ default:
56
+ case "number":
57
+ for (let i = 0; i < numberSettings.length; i += 1) {
58
+ if (num >= numberSettings[i].value) {
59
+ const value = num / numberSettings[i].value;
60
+ formattedNumber = isNegative
61
+ ? truncateNumber(value, 2) * -1
62
+ : truncateNumber(value, 2);
63
+ formattedNumber = new Intl.NumberFormat().format(formattedNumber);
64
+ formattedNumber = `${formattedNumber}${numberSettings[i].symbol}`;
65
+ formatted = true;
66
+ break;
67
+ }
68
+ }
69
+ if (!formatted) {
70
+ formattedNumber = isNegative
71
+ ? truncateNumber(num, applyDecimals) * -1
72
+ : truncateNumber(num, applyDecimals);
73
+ formattedNumber = new Intl.NumberFormat().format(formattedNumber);
74
+ }
75
+ break;
76
+ case "percent":
77
+ formattedNumber = new Intl.NumberFormat([], {
78
+ style: "percent",
79
+ minimumFractionDigits: 0,
80
+ maximumFractionDigits: applyDecimals,
81
+ }).format(formattedNumber / 100);
82
+ break;
83
+ }
84
+ return formattedNumber;
85
+ }
86
+
87
+ function getNumberRepresentation(currencySettings) {
88
+ const numberScaleType =
89
+ currencySettings && currencySettings.label ? currencySettings.label : "";
90
+ if (numberScaleType && numberScaleType.toUpperCase() === "INR") {
91
+ return numberScaleINR;
92
+ }
93
+ return numberScale;
94
+ }
95
+
96
+ const numberScale = [
97
+ { value: 1e12, symbol: "T" },
98
+ { value: 1e9, symbol: "B" },
99
+ { value: 1e6, symbol: "M" },
100
+ { value: 1e3, symbol: "K" },
101
+ ];
102
+
103
+ const numberScaleINR = [
104
+ { value: 1e7, symbol: "Cr" },
105
+ { value: 1e5, symbol: "L" },
106
+ { value: 1e3, symbol: "K" },
107
+ ];
108
+
109
+ function truncateNumber(num, places) {
110
+ const numPower = 10 ** places;
111
+ return Math.trunc(num * numPower) / numPower;
112
+ }
113
+
114
+ export default {
115
+ truncateNumber,
116
+ numberScaleINR,
117
+ numberScale,
118
+ getNumberRepresentation,
119
+ formatNumberWithSymbol,
120
+ camelize,
121
+ snakeCaseToCamelCase,
122
+ };
@@ -0,0 +1,52 @@
1
+ import { isEmpty, isEqual, isString } from "lodash";
2
+
3
+ const reduceValidators = ({ value, validatorFuncs, errorsMsgs }) => {
4
+ const validate = validatorFuncs.reduce(
5
+ (acc, func, index) => {
6
+ if (acc.isValid) {
7
+ const values = Array.isArray(value) ? value : [value];
8
+ const isValid = func.apply(null, values);
9
+ if (!isValid) {
10
+ return {
11
+ isValid: false,
12
+ errorMsg: errorsMsgs[index],
13
+ };
14
+ } else {
15
+ return acc;
16
+ }
17
+ } else {
18
+ // doing AND operation
19
+ return acc;
20
+ }
21
+ },
22
+ { errorMsg: "", isValid: true }
23
+ );
24
+ return validate;
25
+ };
26
+ const isFieldNotEmpty = (value) =>
27
+ typeof value === "number" ? value : !isEmpty(value);
28
+
29
+ const areValuesNotEqual = (value1, value2) => !isEqual(value1, value2);
30
+
31
+ const isPhoneNumber = (value) => {
32
+ const digitsRegex = new RegExp(/^\d+$/gi); //eslint-disable-line
33
+ return digitsRegex.test(value);
34
+ };
35
+ const isEmail = (value) => {
36
+ const emailRegex = new RegExp(
37
+ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ //eslint-disable-line
38
+ );
39
+ return emailRegex.test(value);
40
+ };
41
+ const isNegativeNumber = (value) => value < 0;
42
+ const getBoolean = (value) => (isString(value) ? value === "true" : value);
43
+
44
+ export default {
45
+ reduceValidators,
46
+ isFieldNotEmpty,
47
+ areValuesNotEqual,
48
+ isPhoneNumber,
49
+ isEmail,
50
+ isNegativeNumber,
51
+ getBoolean,
52
+ };