@evolis/evolis-library 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Alexis Lecomte
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # Evolis Library
2
+ A simple package that groups together frequently used functions in front-end and in back-end at Evolis.
3
+
4
+ ## Note
5
+ This package is marked as public and you can use it under the MIT license. But most of this stuff may be useless to you and you can probably find another good library that fits your needs.
6
+
7
+ ## Install
8
+ This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
9
+
10
+ `$ npm install --save @evolis/evolis-library`
11
+
12
+ ## Usage
13
+ This module is divided into three sub-modules: `check`, `convert` and `functions`.
14
+
15
+ ## check.js
16
+ Import it into your script using:
17
+
18
+ `import { check } from "@evolis/evolis-library";`
19
+
20
+ Then use one of these functions:
21
+ - **isDefined(value: any)**
22
+ - value: The value to test
23
+ - *return: true if the value is not undefined or null*
24
+ - **atLeastOneOf(values: [any])**
25
+ - value: The values to test
26
+ - *return: true if at least one of the values is not undefined or null*
27
+ - **isString(value: any)**
28
+ - value: The value to test
29
+ - *return: true if the value is a string*
30
+ - **isEmptyString(value: any)**
31
+ - value: The value to test
32
+ - *return: true if the value is a string and if its empty*
33
+ - **isBlankString(value: any)**
34
+ - value: The value to test
35
+ - *return: true if the value is a string and if its blank*
36
+ - **strInRange(value: any|[any], [min]: Number, [max]: Number, [includeBounds]: Bool, [canBeNull]: Bool)**
37
+ - value: The value(s) to test
38
+ - [min]: Minimum string length (default: null)
39
+ - [max]: Maximum string length (default: null)
40
+ - [includeBounds]: Include the min and max bounds in the range (default: true)
41
+ - [canBeNull]: Authorize or not the string to be null or undefined (default: false)
42
+ - *return: true if the value is a string between min and max or if it's an undefined/null with canBeNull set at true*
43
+ - **isNumber(value: any, [parse]: Bool)**
44
+ - value: The value to test
45
+ - [parse]: If set to true, the function will parse the value to find a integer (base 10) (default: true)
46
+ - *return: true if the value is a number*
47
+ - **isFloat(value: any, [parse]: Bool)**
48
+ - value: The value to test
49
+ - [parse]: If set to true, the function will parse the value to find a float (default: true)
50
+ - *return: true if the value is a float*
51
+ - **isArray(value: any)**
52
+ - value: The value to test
53
+ - *return: true if the value is an array*
54
+ - **isDate(value: any)**
55
+ - value: The value to test
56
+ - *return: true if the value is a valid date*
57
+ - **isEmail(value: any)**
58
+ - value: The value to test
59
+ - *return: true if the value is a string containing a valid email address*
60
+ - **isPasswordSafe(value: any|[any])**
61
+ - value: The value(s) to test
62
+ - rules: Object with these properties:
63
+ ```js
64
+ {
65
+ mustContain: {
66
+ lowerCase: true|false (default: true),
67
+ upperCase: true|false (default: true),
68
+ digit: true|false (default: true),
69
+ special: true|false (default: true),
70
+ },
71
+ minLength: Number (default: 8)
72
+ }
73
+ ```
74
+ - *return: true if the value is a safe password*
75
+
76
+
77
+ ## convert.js
78
+ Import it into your script using:
79
+
80
+ `import { convert } from "@evolis/evolis-library";`
81
+
82
+ Then use one of these functions:
83
+ - **arrayToSerialComma(array: Array)**
84
+ - array: The array to convert
85
+ - *return: A string formatted as a serial comma*
86
+ - **dateToString(date: Date, [withTime]: Bool, [locale]: String)**
87
+ - date: The value to convert
88
+ - [withTime]: Include time in the result (default: true)
89
+ - [locale]: The locale used in the conversion. It may changed the returned string format (default: "fr-FR")
90
+ - *return: A date formatted like "dd/MM/yyyy [hh:mm:ss]"*
91
+ - **dateToTimeString(date: Date, [locale]: String)**
92
+ - date: The date to convert
93
+ - [locale]: The locale used in the conversion. It may changed the returned string format (default: "fr-FR")
94
+ - *return: A date formatted like "hh:mm:ss"*
95
+ - **dateToFieldString(date: Date)**
96
+ - date: The value to convert
97
+ - *return: A date formatted like "yyyy-MM-dd"*
98
+ - **timeStrToDate(time: String, [format]: String)**
99
+ - time: The date to convert
100
+ - [format]: The format used for conversion (default: "hh:mm")
101
+ - *return: A date object filled with the time string provided*
102
+ - **secondsToReadable(seconds: Number)**
103
+ - seconds: Seconds to convert
104
+ - *return: The converted seconds and epoch*
105
+ - **toDate(d: any)**
106
+ - s: The thing to convert
107
+ - *return: A date object, null/undefined if the provided value is null/undefined and NaN if it's not convertible.*
108
+ - **bytesToReadable(bytes: Number, [decimals]: Number)**
109
+ - bytes: Bytes to convert
110
+ - [decimals]: How many decimals (default: 2)
111
+ - *return: The converted bytes size to the closest unit*
112
+
113
+ ## functions.js
114
+ Import it into your script using:
115
+
116
+ `import { functions } from "@evolis/evolis-library";`
117
+
118
+ Then use one of these functions:
119
+ - **dayDifference(start: Date, end: Date)**
120
+ - start: Start date
121
+ - end: End date
122
+ - *return: How many days between these two dates*
123
+ - **padLeft(value: any, [pad]: String)**
124
+ - value: The value to pad
125
+ - [pad]: With what to pad (default: "00")
126
+ - *return: The padded string*
127
+
128
+ ## License
129
+
130
+ [MIT](https://github.com/EmpireDemocratiqueDuPoulpe/evolis-library/blob/main/LICENSE).
package/checkers.js CHANGED
@@ -81,7 +81,7 @@ function isBlankString(value) {
81
81
  * @param {*|Array<*>} value - The value to test
82
82
  * @param {Number} min - The minimum length of the string (nullable)
83
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
84
+ * @param {Boolean} [includeBounds=true] - Include the min and max bounds in the range
85
85
  * @param {Boolean} [canBeNull=false] - Authorize or not the string to be null or undefined
86
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
87
  *
@@ -150,6 +150,16 @@ function isDate(value) {
150
150
  return Object.prototype.toString.call(value) === "[object Date]" && !!value.getDate();
151
151
  }
152
152
 
153
+ /**
154
+ * @function isMySQLDate
155
+ *
156
+ * @param {any} value - The value to test
157
+ * @returns {boolean} Return true if the value is a string with the valid MySQL date format (yyyy-mm-dd)
158
+ */
159
+ function isMySQLDate(value) {
160
+ return isString(value) && !isBlankString(value) && /^\d{4}-\d{1,2}-\d{1,2}$/.test(value);
161
+ }
162
+
153
163
  /*****************************************************
154
164
  * Regex checkers
155
165
  *****************************************************/
@@ -192,25 +202,26 @@ export const passwordRules = {
192
202
  };
193
203
 
194
204
  /**
195
- * @function isEmail
196
- *
205
+ * @function isPasswordSafe
197
206
  *
198
207
  * @param {string|Array} password - One or multiple passwords
199
208
  * @param {PasswordRules} [rules] - An object that describe security rules to follow
200
209
  * @returns {boolean} Return true if the value is a safe password
201
210
  */
202
- function isPasswordSafe(password, rules = passwordRules) {
211
+ function isPasswordSafe(password, rules) {
212
+ const usedRules = rules ? { ...passwordRules, ...rules } : passwordRules;
213
+
203
214
  if (isArray(password)) {
204
- return password.every(pwd => isPasswordSafe(pwd, rules));
215
+ return password.every(pwd => isPasswordSafe(pwd, usedRules));
205
216
  }
206
217
 
207
218
  let regex = "";
208
219
 
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},}$`;
220
+ if (usedRules.mustContain.lowerCase) regex += "(?=.*[a-z])";
221
+ if (usedRules.mustContain.upperCase) regex += "(?=.*[A-Z])";
222
+ if (usedRules.mustContain.digit) regex += "(?=.*[0-9])";
223
+ if (usedRules.mustContain.special) regex += `(?=.*[\\"'!?@#$£%^&:;<>\\[\\]()\\\\\\-_+=*.])`;
224
+ regex += `.{${usedRules.minLength},}$`;
214
225
 
215
226
  const strongPwd = new RegExp(regex);
216
227
  return strongPwd.test(password);
@@ -225,7 +236,7 @@ export default {
225
236
  isString, isEmptyString, isBlankString, strInRange,
226
237
  isNumber, isFloat,
227
238
  isArray,
228
- isDate,
239
+ isDate, isMySQLDate,
229
240
  isEmail,
230
241
  isPasswordSafe
231
242
  };
package/converters.js CHANGED
@@ -107,6 +107,32 @@ function timeStrToDate(time, format = "hh:mm") {
107
107
  return now;
108
108
  }
109
109
 
110
+ /**
111
+ * @function secondsToReadable
112
+ *
113
+ * @param {number} seconds - Seconds to convert
114
+ * @returns {string} The converted seconds and epoch
115
+ *
116
+ * @example
117
+ * secondsToReadable(120) // return "2 minutes"
118
+ * secondsToReadable(3600) // return "1 heure"
119
+ */
120
+ function secondsToReadable(seconds) {
121
+ const epochs = { "années": 31536000, "mois": 2592000, "jours": 86400, "heures": 3600, "minutes": 60, "secondes": 1 };
122
+ const singular = { "années": "ans", "mois": "mois", "jours": "jour", "heures": "heure", "minutes": "minute", "secondes": "seconde" };
123
+ const epoch = Object.entries(epochs).filter(([, value]) => seconds >= value).shift();
124
+ const readable = {
125
+ epoch: epoch[0],
126
+ interval: Math.trunc(seconds / epoch[1])
127
+ };
128
+
129
+ if (readable.interval === 1) {
130
+ readable.epoch = singular[readable.epoch];
131
+ }
132
+
133
+ return `${readable.interval} ${readable.epoch}`;
134
+ }
135
+
110
136
  /**
111
137
  * @function toDate
112
138
  *
@@ -159,6 +185,6 @@ function bytesToReadable(bytes, decimals = 2) {
159
185
 
160
186
  export default {
161
187
  arrayToSerialComma,
162
- dateToString, dateToTimeString, dateToFieldString, timeStrToDate, toDate,
188
+ dateToString, dateToTimeString, dateToFieldString, timeStrToDate, secondsToReadable, toDate,
163
189
  bytesToReadable
164
190
  };
package/functions.js CHANGED
@@ -10,6 +10,30 @@ import checkers from "./checkers.js";
10
10
  * Functions
11
11
  *****************************************************/
12
12
 
13
+ /* ---- Array ----------------------------------- */
14
+ /**
15
+ * @function shuffle
16
+ *
17
+ * @param {Array<*>} array - The array to shuffle
18
+ * @returns {Array<*>} The shuffled array
19
+ *
20
+ * @example
21
+ * shuffle([1, 2, 3]) // return [2, 1, 3] or [3, 2, 1] or [1, 3, 2] or ...
22
+ */
23
+ function shuffle(array) {
24
+ let arr = [...array], length = array.length, element, cache;
25
+
26
+ while (length) {
27
+ element = Math.floor(Math.random() * length--);
28
+
29
+ cache = arr[length];
30
+ arr[length] = arr[element];
31
+ arr[element] = cache;
32
+ }
33
+
34
+ return arr;
35
+ }
36
+
13
37
  /* ---- Date ------------------------------------ */
14
38
  /**
15
39
  * @function dayDifference
@@ -33,7 +57,7 @@ function dayDifference(start, end) {
33
57
  /**
34
58
  * @function padLeft
35
59
  *
36
- * @param {*} value - The array to pad
60
+ * @param {*} value - The value to pad
37
61
  * @param {string} [pad="00"] - With what to pad
38
62
  * @returns {string} The padded string
39
63
  *
@@ -51,6 +75,4 @@ function padLeft(value, pad = "00") {
51
75
  * Export
52
76
  *****************************************************/
53
77
 
54
- export default {
55
- dayDifference, padLeft
56
- };
78
+ export default { shuffle, dayDifference, padLeft };
package/package.json CHANGED
@@ -1,28 +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
- }
1
+ {
2
+ "name": "@evolis/evolis-library",
3
+ "version": "1.1.0",
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
+ }