@evolis/evolis-library 1.0.7 → 1.1.1-b
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +10 -4
- package/checkers.js +11 -1
- package/converters.js +27 -1
- package/functions.js +45 -3
- package/package.json +28 -28
package/README.md
CHANGED
@@ -14,6 +14,7 @@ This module is divided into three sub-modules: `check`, `convert` and `functions
|
|
14
14
|
|
15
15
|
## check.js
|
16
16
|
Import it into your script using:
|
17
|
+
|
17
18
|
`import { check } from "@evolis/evolis-library";`
|
18
19
|
|
19
20
|
Then use one of these functions:
|
@@ -75,6 +76,7 @@ Then use one of these functions:
|
|
75
76
|
|
76
77
|
## convert.js
|
77
78
|
Import it into your script using:
|
79
|
+
|
78
80
|
`import { convert } from "@evolis/evolis-library";`
|
79
81
|
|
80
82
|
Then use one of these functions:
|
@@ -94,9 +96,12 @@ Then use one of these functions:
|
|
94
96
|
- date: The value to convert
|
95
97
|
- *return: A date formatted like "yyyy-MM-dd"*
|
96
98
|
- **timeStrToDate(time: String, [format]: String)**
|
97
|
-
|
98
|
-
|
99
|
-
|
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*
|
100
105
|
- **toDate(d: any)**
|
101
106
|
- s: The thing to convert
|
102
107
|
- *return: A date object, null/undefined if the provided value is null/undefined and NaN if it's not convertible.*
|
@@ -104,9 +109,10 @@ Then use one of these functions:
|
|
104
109
|
- bytes: Bytes to convert
|
105
110
|
- [decimals]: How many decimals (default: 2)
|
106
111
|
- *return: The converted bytes size to the closest unit*
|
107
|
-
|
112
|
+
|
108
113
|
## functions.js
|
109
114
|
Import it into your script using:
|
115
|
+
|
110
116
|
`import { functions } from "@evolis/evolis-library";`
|
111
117
|
|
112
118
|
Then use one of these functions:
|
package/checkers.js
CHANGED
@@ -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
|
*****************************************************/
|
@@ -226,7 +236,7 @@ export default {
|
|
226
236
|
isString, isEmptyString, isBlankString, strInRange,
|
227
237
|
isNumber, isFloat,
|
228
238
|
isArray,
|
229
|
-
isDate,
|
239
|
+
isDate, isMySQLDate,
|
230
240
|
isEmail,
|
231
241
|
isPasswordSafe
|
232
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,50 @@ 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
|
+
|
37
|
+
/**
|
38
|
+
* @function deepEqual
|
39
|
+
*
|
40
|
+
* @param {Object} obj1 - First object
|
41
|
+
* @param {Object} obj2 - Second object
|
42
|
+
* @returns {Boolean} Are the two objects deeply equal ?
|
43
|
+
*
|
44
|
+
* @example
|
45
|
+
* deepEqual({id: 1, title: "skibidi wap-pa-pa"}, {title: "skibidi wap-pa-pa", id: 1}) // return true
|
46
|
+
* deepEqual({id: 7, title: "skibidi wap-pa-pa"}, {title: "skibidi woosh-oosh-oosh", id: 1}) // return false
|
47
|
+
*/
|
48
|
+
function deepEqual(obj1, obj2) {
|
49
|
+
if (typeof obj1 === "object" && Object.keys(obj1).length > 0) {
|
50
|
+
return Object.keys(obj1).length === Object.keys(obj2).length &&
|
51
|
+
Object.keys(obj1).every(prop => deepEqual(obj1[prop], obj2[prop]));
|
52
|
+
} else {
|
53
|
+
return obj1 === obj2;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
13
57
|
/* ---- Date ------------------------------------ */
|
14
58
|
/**
|
15
59
|
* @function dayDifference
|
@@ -51,6 +95,4 @@ function padLeft(value, pad = "00") {
|
|
51
95
|
* Export
|
52
96
|
*****************************************************/
|
53
97
|
|
54
|
-
export default {
|
55
|
-
dayDifference, padLeft
|
56
|
-
};
|
98
|
+
export default { shuffle, deepEqual, dayDifference, padLeft };
|
package/package.json
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
{
|
2
|
-
"name": "@evolis/evolis-library",
|
3
|
-
"version": "1.
|
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.1b",
|
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
|
+
}
|