@evolis/evolis-library 1.0.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/.eslintrc.json +17 -0
- package/checkers.js +231 -0
- package/converters.js +164 -0
- package/functions.js +56 -0
- package/index.js +3 -0
- package/package.json +28 -0
package/.eslintrc.json
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
{
|
2
|
+
"env": {
|
3
|
+
"browser": true,
|
4
|
+
"es2021": true
|
5
|
+
},
|
6
|
+
"extends": "eslint:recommended",
|
7
|
+
"parserOptions": {
|
8
|
+
"ecmaVersion": 12,
|
9
|
+
"sourceType": "module"
|
10
|
+
},
|
11
|
+
"rules": {
|
12
|
+
"indent": [ "error", "tab" ],
|
13
|
+
"linebreak-style": [ "error", "windows" ],
|
14
|
+
"quotes": [ "error", "double" ],
|
15
|
+
"semi": [ "error", "always" ]
|
16
|
+
}
|
17
|
+
}
|
package/checkers.js
ADDED
@@ -0,0 +1,231 @@
|
|
1
|
+
/**
|
2
|
+
* @module checkers
|
3
|
+
* @author EmpireDemocratiqueDuPoulpe
|
4
|
+
* @version 1.0.4
|
5
|
+
* @license MIT
|
6
|
+
*/
|
7
|
+
|
8
|
+
/*****************************************************
|
9
|
+
* Type checkers
|
10
|
+
*****************************************************/
|
11
|
+
|
12
|
+
/* ---- Global ---------------------------------- */
|
13
|
+
/**
|
14
|
+
* @function isDefined
|
15
|
+
*
|
16
|
+
* @param {*} value - The value to test
|
17
|
+
* @returns {Boolean} Return true if the value is not undefined or null
|
18
|
+
*
|
19
|
+
* @example
|
20
|
+
* isDefined("") // return true
|
21
|
+
* isDefined(65) // return true
|
22
|
+
* isDefined(false) // return true
|
23
|
+
* isDefined(null) // return false
|
24
|
+
* isDefined(undefined) // return false
|
25
|
+
*/
|
26
|
+
function isDefined(value) {
|
27
|
+
return value !== undefined && value !== null;
|
28
|
+
}
|
29
|
+
|
30
|
+
/**
|
31
|
+
* @function atLeastOneOf
|
32
|
+
*
|
33
|
+
* @param {Array<*>} values - The values to test
|
34
|
+
* @returns {Boolean} Return true if at least one of the values is not undefined or null
|
35
|
+
*
|
36
|
+
* @example
|
37
|
+
* atLeastOneOf([null, null, ""]) // return true
|
38
|
+
* atLeastOneOf([]) // return false
|
39
|
+
*/
|
40
|
+
function atLeastOneOf(values) {
|
41
|
+
return values.some(value => isDefined(value));
|
42
|
+
}
|
43
|
+
|
44
|
+
/* ---- String ---------------------------------- */
|
45
|
+
/**
|
46
|
+
* @function isString
|
47
|
+
*
|
48
|
+
* @param {any} value - The value to test
|
49
|
+
* @returns {boolean} Return true if the value is a string
|
50
|
+
*/
|
51
|
+
function isString(value) {
|
52
|
+
return Object.prototype.toString.call(value) === "[object String]";
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* @function isEmptyString
|
57
|
+
*
|
58
|
+
* @param {any} value - The value to test
|
59
|
+
* @returns {boolean} Return true if the value is a string and if its empty
|
60
|
+
*/
|
61
|
+
function isEmptyString(value) {
|
62
|
+
return value === "";
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* @function isBlankString
|
67
|
+
*
|
68
|
+
* @param {any} value - The value to test
|
69
|
+
* @returns {boolean} Return true if the value is a string and if its blank
|
70
|
+
*
|
71
|
+
* @example
|
72
|
+
* isBlankString(" "); // return true
|
73
|
+
*/
|
74
|
+
function isBlankString(value) {
|
75
|
+
return isString(value) && value.trim().length === 0;
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* @function strInRange
|
80
|
+
*
|
81
|
+
* @param {*|Array<*>} value - The value to test
|
82
|
+
* @param {Number} min - The minimum length of the string (nullable)
|
83
|
+
* @param {Number} max - The maximum length of the string (nullable)
|
84
|
+
* @param {Boolean} [includeEndpoint=true] - Include the min and max bounds in the range
|
85
|
+
* @param {Boolean} [canBeNull=false] - Authorize or not the string to be null or undefined
|
86
|
+
* @returns {Boolean} Return true if the value is a string between min and max or if it's an undefined/null with canBeNull set at true.
|
87
|
+
*
|
88
|
+
* @example
|
89
|
+
* strInRange("never gonna give you up", null, 100); // return true
|
90
|
+
* strInRange("oof", 5); // return false
|
91
|
+
*/
|
92
|
+
function strInRange(value, min, max, includeBounds = true, canBeNull = false) {
|
93
|
+
if (isArray(value)) {
|
94
|
+
return value.every(s => strInRange(s, min, max, includeBounds, canBeNull));
|
95
|
+
}
|
96
|
+
|
97
|
+
return isDefined(value)
|
98
|
+
? isString(value)
|
99
|
+
? includeBounds
|
100
|
+
? (isDefined(min) ? value.length >= min : true) && (isDefined(max) ? value.length <= max : true)
|
101
|
+
: (isDefined(min) ? value.length > min : true) && (isDefined(max) ? value.length < max : true)
|
102
|
+
: false
|
103
|
+
: canBeNull;
|
104
|
+
}
|
105
|
+
|
106
|
+
/* ---- Number ---------------------------------- */
|
107
|
+
/**
|
108
|
+
* @function isNumber
|
109
|
+
*
|
110
|
+
* @param {any} value - The value to test
|
111
|
+
* @param {boolean} [parse=true] - If set to true, the function will parse the value to find a integer (base 10)
|
112
|
+
* @returns {boolean} Return true if the value is a number
|
113
|
+
*/
|
114
|
+
function isNumber(value, parse = true) {
|
115
|
+
const val = parse ? (isString(value) ? parseInt(value, 10) : value) : value;
|
116
|
+
return typeof val === "number" && !isNaN(val);
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* @function isFloat
|
121
|
+
*
|
122
|
+
* @param {any} value - The value to test
|
123
|
+
* @param {boolean} [parse=true] - If set to true, the function will parse the value to find a float
|
124
|
+
* @returns {boolean} Return true if the value is a float
|
125
|
+
*/
|
126
|
+
function isFloat(value, parse = true) {
|
127
|
+
const val = parse ? (isString(value) ? parseFloat(value) : value) : value;
|
128
|
+
return typeof val === "number" && !isNaN(val);
|
129
|
+
}
|
130
|
+
|
131
|
+
/* ---- Array ----------------------------------- */
|
132
|
+
/**
|
133
|
+
* @function isArray
|
134
|
+
*
|
135
|
+
* @param {any} value - The value to test
|
136
|
+
* @returns {boolean} Return true if the value is an array
|
137
|
+
*/
|
138
|
+
function isArray(value) {
|
139
|
+
return Array.isArray(value);
|
140
|
+
}
|
141
|
+
|
142
|
+
/* ---- Date ------------------------------------ */
|
143
|
+
/**
|
144
|
+
* @function isDate
|
145
|
+
*
|
146
|
+
* @param {any} value - The value to test
|
147
|
+
* @returns {boolean} Return true if the value is a valid date
|
148
|
+
*/
|
149
|
+
function isDate(value) {
|
150
|
+
return Object.prototype.toString.call(value) === "[object Date]" && !!value.getDate();
|
151
|
+
}
|
152
|
+
|
153
|
+
/*****************************************************
|
154
|
+
* Regex checkers
|
155
|
+
*****************************************************/
|
156
|
+
|
157
|
+
/* ---- E-mail ---------------------------------- */
|
158
|
+
/**
|
159
|
+
* @function isEmail
|
160
|
+
*
|
161
|
+
* @param {any} value - The value to test
|
162
|
+
* @returns {boolean} Return true if the value is a string containing a valid email address
|
163
|
+
*/
|
164
|
+
function isEmail(value) {
|
165
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
166
|
+
}
|
167
|
+
|
168
|
+
/* ---- Passwords ------------------------------- */
|
169
|
+
/**
|
170
|
+
* @typedef {Object} PasswordRules
|
171
|
+
*
|
172
|
+
* @property {object} mustContain - What type of character the password must contain
|
173
|
+
* @property {boolean} [mustContain.lowerCase] - Must contain at least one lower cased character
|
174
|
+
* @property {boolean} [mustContain.upperCase] - Must contain at least one upper cased character
|
175
|
+
* @property {boolean} [mustContain.digit] - Must contain at least one number
|
176
|
+
* @property {boolean} [mustContain.special] - Must contain at least one special character
|
177
|
+
* @property {number} minLength - The minimal length
|
178
|
+
*/
|
179
|
+
|
180
|
+
/**
|
181
|
+
* @const
|
182
|
+
* @type {PasswordRules}
|
183
|
+
*/
|
184
|
+
export const passwordRules = {
|
185
|
+
mustContain: {
|
186
|
+
lowerCase: true,
|
187
|
+
upperCase: true,
|
188
|
+
digit: true,
|
189
|
+
special: true
|
190
|
+
},
|
191
|
+
minLength: 8
|
192
|
+
};
|
193
|
+
|
194
|
+
/**
|
195
|
+
* @function isEmail
|
196
|
+
*
|
197
|
+
*
|
198
|
+
* @param {string|Array} password - One or multiple passwords
|
199
|
+
* @param {PasswordRules} [rules] - An object that describe security rules to follow
|
200
|
+
* @returns {boolean} Return true if the value is a safe password
|
201
|
+
*/
|
202
|
+
function isPasswordSafe(password, rules = passwordRules) {
|
203
|
+
if (isArray(password)) {
|
204
|
+
return password.every(pwd => isPasswordSafe(pwd, rules));
|
205
|
+
}
|
206
|
+
|
207
|
+
let regex = "";
|
208
|
+
|
209
|
+
if (rules.mustContain.lowerCase) regex += "(?=.*[a-z])";
|
210
|
+
if (rules.mustContain.upperCase) regex += "(?=.*[A-Z])";
|
211
|
+
if (rules.mustContain.digit) regex += "(?=.*[0-9])";
|
212
|
+
if (rules.mustContain.special) regex += `(?=.*[\\"'!?@#$£%^&:;<>\\[\\]()\\\\\\-_+=*.])`;
|
213
|
+
regex += `.{${rules.minLength},}$`;
|
214
|
+
|
215
|
+
const strongPwd = new RegExp(regex);
|
216
|
+
return strongPwd.test(password);
|
217
|
+
}
|
218
|
+
|
219
|
+
/*****************************************************
|
220
|
+
* Export
|
221
|
+
*****************************************************/
|
222
|
+
|
223
|
+
export default {
|
224
|
+
isDefined, atLeastOneOf,
|
225
|
+
isString, isEmptyString, isBlankString, strInRange,
|
226
|
+
isNumber, isFloat,
|
227
|
+
isArray,
|
228
|
+
isDate,
|
229
|
+
isEmail,
|
230
|
+
isPasswordSafe
|
231
|
+
};
|
package/converters.js
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
/**
|
2
|
+
* @module converters
|
3
|
+
* @author EmpireDemocratiqueDuPoulpe
|
4
|
+
* @version 1.0.6
|
5
|
+
* @license MIT
|
6
|
+
*/
|
7
|
+
import checkers from "./checkers.js";
|
8
|
+
import functions from "./functions.js";
|
9
|
+
|
10
|
+
/*****************************************************
|
11
|
+
* Converters
|
12
|
+
*****************************************************/
|
13
|
+
|
14
|
+
/* ---- Array ----------------------------------- */
|
15
|
+
/**
|
16
|
+
* @function arrayToSerialComma
|
17
|
+
*
|
18
|
+
* @param {Array<*>} array - The array to convert
|
19
|
+
* @returns {string} A string formatted as a serial comma
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* arrayToSerialComma([]) // return ""
|
23
|
+
* arrayToSerialComma(["one", 2, "three"]) // return "one, 2, three"
|
24
|
+
*/
|
25
|
+
function arrayToSerialComma(array) {
|
26
|
+
return checkers.isArray(array) && array.length > 0
|
27
|
+
? array.length === 1
|
28
|
+
? array[0]
|
29
|
+
: `${array.slice(0, -1).join(", ")} et ${array.slice(-1)}`
|
30
|
+
: "";
|
31
|
+
}
|
32
|
+
|
33
|
+
/* ---- Date ------------------------------------ */
|
34
|
+
/**
|
35
|
+
* @function dateToString
|
36
|
+
*
|
37
|
+
* @param {Date} date - The date to convert
|
38
|
+
* @param {Boolean} [withTime=true] - Include time in the result
|
39
|
+
* @param {string} [locale="fr-FR"] - The locale used in the conversion. It may changed the returned string format
|
40
|
+
* @returns {string} A date formatted like "dd/MM/yyyy [hh:mm:ss]"
|
41
|
+
*
|
42
|
+
* @example
|
43
|
+
* dateToString(new Date()) // return "13/07/2021, 14:59:48"
|
44
|
+
*/
|
45
|
+
function dateToString(date, withTime = true, locale = "fr-FR") {
|
46
|
+
return withTime ? date.toLocaleString(locale) : date.toLocaleDateString(locale);
|
47
|
+
}
|
48
|
+
|
49
|
+
/**
|
50
|
+
* @function dateToTimeString
|
51
|
+
*
|
52
|
+
* @param {Date} date - The date to convert
|
53
|
+
* @param {string} [locale="fr-FR"] - The locale used in the conversion. It may changed the returned string format
|
54
|
+
* @returns {string} A date formatted like "hh:mm:ss"
|
55
|
+
*
|
56
|
+
* @example
|
57
|
+
* dateToTimeString(new Date()) // return "14:59:48"
|
58
|
+
*/
|
59
|
+
function dateToTimeString(date, locale = "fr-FR") {
|
60
|
+
return date.toLocaleTimeString(locale);
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* @function dateToFieldString
|
65
|
+
*
|
66
|
+
* @param {Date} date - The date to convert
|
67
|
+
* @returns {string} A date formatted like "yyyy-MM-dd"
|
68
|
+
*
|
69
|
+
* @example
|
70
|
+
* dateToFieldString(new Date()) // return "2021-07-13"
|
71
|
+
*/
|
72
|
+
function dateToFieldString(date) {
|
73
|
+
return checkers.isDate(date)
|
74
|
+
? `${date.getFullYear()}-${functions.padLeft(date.getMonth() + 1)}-${functions.padLeft(date.getDate())}`
|
75
|
+
: null;
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* @function timeStrToDate
|
80
|
+
*
|
81
|
+
* @param {string} time - The time to convert
|
82
|
+
* @param {string} [format="hh:mm"] - The format used for conversion
|
83
|
+
* @returns {Date} A date object filled with the time string provided
|
84
|
+
* @throws {Error} In case of invalid format/time string
|
85
|
+
*
|
86
|
+
* @example
|
87
|
+
* timeStrToDate("12:57") // works
|
88
|
+
* timeStrToDate("9:21") // works
|
89
|
+
* timeStrToDate("9::21") // error
|
90
|
+
*/
|
91
|
+
function timeStrToDate(time, format = "hh:mm") {
|
92
|
+
if (!checkers.isString(time) || !checkers.isString(format)) return null;
|
93
|
+
|
94
|
+
const now = new Date();
|
95
|
+
const timeSplit = time.split(":");
|
96
|
+
const formatSplit = format.split(":");
|
97
|
+
|
98
|
+
formatSplit.forEach((f, index) => {
|
99
|
+
if (index >= timeSplit.length) throw Error("timeStrToDate - Invalid format");
|
100
|
+
const t = parseInt(timeSplit[index], 10);
|
101
|
+
|
102
|
+
if (f === "hh") now.setHours(t);
|
103
|
+
else if (f === "mm") now.setMinutes(t);
|
104
|
+
else if (f === "ss") now.setSeconds(t);
|
105
|
+
});
|
106
|
+
|
107
|
+
return now;
|
108
|
+
}
|
109
|
+
|
110
|
+
/**
|
111
|
+
* @function toDate
|
112
|
+
*
|
113
|
+
* @param {*} d - The thing to convert
|
114
|
+
* @returns {Date|null|undefined|NaN} A date object, null/undefined if the provided value is null/undefined and NaN if it's not convertible.
|
115
|
+
*
|
116
|
+
* @example
|
117
|
+
* toDate(null) // return null
|
118
|
+
* toDate(new Date().toISOString()) // return a Date object
|
119
|
+
*/
|
120
|
+
function toDate(d) {
|
121
|
+
return (
|
122
|
+
d === null ? d :
|
123
|
+
d === undefined ? d :
|
124
|
+
d.constructor === Date ? d :
|
125
|
+
d.constructor === Array ? new Date(d[0],d[1],d[2]) :
|
126
|
+
d.constructor === Number ? new Date(d) :
|
127
|
+
d.constructor === String ? new Date(d) :
|
128
|
+
typeof d === "object" ? new Date(d.year,d.month,d.date) :
|
129
|
+
NaN
|
130
|
+
);
|
131
|
+
}
|
132
|
+
|
133
|
+
/* ---- Bytes ----------------------------------- */
|
134
|
+
/**
|
135
|
+
* @function bytesToReadable
|
136
|
+
*
|
137
|
+
* @param {number} bytes - Bytes to convert
|
138
|
+
* @param {number} [decimals=2] - How many decimals
|
139
|
+
* @returns {string} The converted bytes size to the closest unit
|
140
|
+
*
|
141
|
+
* @example
|
142
|
+
* bytesToReadable(8000000) // return "7.63 MB"
|
143
|
+
* bytesToReadable(8000000, 0) // return "8 MB"
|
144
|
+
*/
|
145
|
+
function bytesToReadable(bytes, decimals = 2) {
|
146
|
+
if (bytes === 0) return "0 Bytes";
|
147
|
+
|
148
|
+
const k = 1024;
|
149
|
+
const dm = decimals < 0 ? 0 : decimals;
|
150
|
+
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
151
|
+
|
152
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
153
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
154
|
+
}
|
155
|
+
|
156
|
+
/*****************************************************
|
157
|
+
* Export
|
158
|
+
*****************************************************/
|
159
|
+
|
160
|
+
export default {
|
161
|
+
arrayToSerialComma,
|
162
|
+
dateToString, dateToTimeString, dateToFieldString, timeStrToDate, toDate,
|
163
|
+
bytesToReadable
|
164
|
+
};
|
package/functions.js
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
/**
|
2
|
+
* @module functions
|
3
|
+
* @author EmpireDemocratiqueDuPoulpe
|
4
|
+
* @version 1.0.5
|
5
|
+
* @license MIT
|
6
|
+
*/
|
7
|
+
import checkers from "./checkers.js";
|
8
|
+
|
9
|
+
/*****************************************************
|
10
|
+
* Functions
|
11
|
+
*****************************************************/
|
12
|
+
|
13
|
+
/* ---- Date ------------------------------------ */
|
14
|
+
/**
|
15
|
+
* @function dayDifference
|
16
|
+
*
|
17
|
+
* @param {Date} start - Start date
|
18
|
+
* @param {Date} end - End date
|
19
|
+
* @returns {Number} How many days between these two dates
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* dayDifference(new Date("07/14/2021"), new Date("07/18/2021")) // return 4
|
23
|
+
* dayDifference(new Date("07/14/2021"), new Date("07/01/2021")) // return -13
|
24
|
+
*/
|
25
|
+
function dayDifference(start, end) {
|
26
|
+
start.setHours(0, 0, 0, 0);
|
27
|
+
end.setHours(0, 0, 0, 0);
|
28
|
+
|
29
|
+
return Math.round((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
|
30
|
+
}
|
31
|
+
|
32
|
+
/* ---- Other ----------------------------------- */
|
33
|
+
/**
|
34
|
+
* @function padLeft
|
35
|
+
*
|
36
|
+
* @param {*} value - The array to pad
|
37
|
+
* @param {string} [pad="00"] - With what to pad
|
38
|
+
* @returns {string} The padded string
|
39
|
+
*
|
40
|
+
* @example
|
41
|
+
* padLeft(5, "000") // return "005"
|
42
|
+
* padLeft("796", "00") // return "796"
|
43
|
+
*/
|
44
|
+
function padLeft(value, pad = "00") {
|
45
|
+
return checkers.isDefined(value)
|
46
|
+
? (value.toString.length >= pad.length) ? value : (pad + value).slice(-pad.length)
|
47
|
+
: pad;
|
48
|
+
}
|
49
|
+
|
50
|
+
/*****************************************************
|
51
|
+
* Export
|
52
|
+
*****************************************************/
|
53
|
+
|
54
|
+
export default {
|
55
|
+
dayDifference, padLeft
|
56
|
+
};
|
package/index.js
ADDED
package/package.json
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
{
|
2
|
+
"name": "@evolis/evolis-library",
|
3
|
+
"version": "1.0.6",
|
4
|
+
"description": "Dependencies used both in Evolis API and Evolis UI",
|
5
|
+
"repository": {
|
6
|
+
"type": "git",
|
7
|
+
"url": "git+https://github.com/EmpireDemocratiqueDuPoulpe/evolis-library.git"
|
8
|
+
},
|
9
|
+
"main": "index.js",
|
10
|
+
"type": "module",
|
11
|
+
"scripts": {
|
12
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
13
|
+
},
|
14
|
+
"keywords": [
|
15
|
+
"evolis",
|
16
|
+
"library"
|
17
|
+
],
|
18
|
+
"author": "EmpireDemocratiqueDuPoulpe",
|
19
|
+
"license": "MIT",
|
20
|
+
"devDependencies": {
|
21
|
+
"eslint": "^7.30.0"
|
22
|
+
},
|
23
|
+
"bugs": {
|
24
|
+
"url": "https://github.com/EmpireDemocratiqueDuPoulpe/evolis-library/issues"
|
25
|
+
},
|
26
|
+
"homepage": "https://github.com/EmpireDemocratiqueDuPoulpe/evolis-library#readme",
|
27
|
+
"dependencies": {}
|
28
|
+
}
|