@capillarytech/cap-ui-utils 1.3.6 → 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.6",
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
  }
@@ -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
+ };