-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
175 lines (153 loc) · 5.17 KB
/
core.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
/**
* Collection of function to work with the DOM
*
* @requires prototypes.js
*/
/**
* This function will check if the provided obj is empty. If white space flag set it will consider
* the object empty if it only has white spaces
* @param {*} obj Object to check
* @param {boolean} [whiteSpaceIsEmpty=false] Treat white spaces as empty
* @returns {boolean} Result of checking if object is empty
*/
function isEmpty(obj, whiteSpaceIsEmpty = false) {
if(isUndefinedOrNull(obj)){
return true;
}
// Empty Array
if(Array.isArray(obj)){
return obj.length === 0;
}
// Regular Object with no properties
return (obj.constructor === Object && Object.entries(obj).length === 0)
// Object with property of length and set to 0
|| (typeof obj === 'object' && obj['length'] && obj.length === 0)
// Empty string
|| (!whiteSpaceIsEmpty && obj === '')
|| (whiteSpaceIsEmpty && obj.trim() === '');
};
/**
* This function will test if the provided value is boolean
* @param {*} potentialBoolean object to test if boolean
* @returns {boolean} Returns outcome of test
*/
function isBoolean(potentialBoolean) {
if(typeof potentialBoolean === 'boolean'){
return true;
}
if(typeof potentialBoolean === 'string'){
return potentialBoolean.equalsIgnoreCase('true')
|| potentialBoolean.equalsIgnoreCase('false');
}
return false;
}
/**
* This function will check to see if the provided object is a Function
* @param {*} obj Object to check if it's a function
* @returns {boolean} Returns outcome of test
*/
function isFunction(obj) {
return !isUndefinedOrNull(obj) && typeof obj === 'function';
}
/**
* This function will check if the provided number|string is a number
* @param {number|string} potentialNumber
* @returns {boolean} Result of check
*/
function isNumber(potentialNumber) {
return !isEmpty(potentialNumber)
&& !Number.isNaN(potentialNumber)
&& (typeof potentialNumber === 'number' || !isNaN(potentialNumber));
}
/**
* This function will check if the provided obj is undefined or null
* @param {*} obj Object to check
* @returns {boolean} Result of check
*/
function isUndefinedOrNull(obj) {
return obj === undefined || obj === null;
}
/**
* This function will check if the provided value is Undefined or Null or Empty (if the
* replaceOnEmpty flag is set) and if so return the default value
* @param {*} self Value to check
* @param {*} defaultValue Default Value to use if self is missing
* @param {boolean} [replaceOnEmpty=false] Flag to indicate if replace on empty should be done
* @return {*}
*/
function selfOrDefault(self, defaultValue, replaceOnEmpty = false) {
if(replaceOnEmpty && isEmpty(self)){
return defaultValue;
}
if(isUndefinedOrNull(self)){
return defaultValue;
}
return self;
};
/**
* This method will check if object is Array, if not convert it to array. If value is undefined or
* null then empty list is returned.
* @param potentialArray Object to check if Array
* @returns {*[]} Returns an Array
*/
function toArray(potentialArray) {
if(Array.isArray(potentialArray)){
return potentialArray;
}
return isUndefinedOrNull(potentialArray) ? [] : [potentialArray];
}
/**
* Will convert value to boolean
* @param booleanString
* @returns {boolean}
*/
function toBoolean(booleanString) {
if(isUndefinedOrNull(booleanString)){
return false;
}
if(isBoolean(booleanString) && typeof booleanString !== 'string') {
return booleanString;
}
return booleanString.equalsIgnoreCase('true');
}
const currencyFormatter = Intl.NumberFormat('en-us', {
style: 'currency',
currency: 'USD'
});
/**
* Function to format a number to currency format
* @param {number|string} moneyValue Value to format as currency
* @param {boolean} [withSymbol=true] Should the formatted value include the currency symbol
* @returns {string} Formatted number as currency
*/
function toCurrency(moneyValue, withSymbol = true){
if(typeof moneyValue === 'string'){
moneyValue = toFloat(moneyValue);
}
const value = currencyFormatter.format(moneyValue);
return withSymbol ? value : value.replaceAll('$', '').trim();
}
/**
* Function to convert the provided string/number to a float
* @param {number|string} potentialNumber Potential string/number to convert to float
* @param {number} [defaultValue=0.0] Default value to use if potential number cannot be used
* @returns
*/
function toFloat(potentialNumber, defaultValue) {
if(!isNumber(potentialNumber)){
return selfOrDefault(defaultValue, 0.0);
}
return (typeof potentialNumber === 'number') ? potentialNumber : parseFloat(potentialNumber);
}
/**
* Function to convert the provided string/number to a integer
* @param {number|string} potentialNumber Potential string/number to convert to integer
* @param {number} [defaultValue=0] Default value to use if potential number cannot be used
* @returns
*/
function toInteger(potentialNumber, defaultValue) {
if(!isNumber(potentialNumber)){
return selfOrDefault(defaultValue, 0);
}
return parseInt(potentialNumber, 10);
};