@evolis/evolis-library 1.1.0 → 1.2.1
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/EvoArray.js +113 -0
- package/EvoDate.js +192 -0
- package/EvoMisc.js +148 -0
- package/EvoNumber.js +44 -0
- package/EvoObject.js +40 -0
- package/EvoString.js +88 -0
- package/README.md +130 -112
- package/index.js +6 -3
- package/package.json +1 -1
- package/checkers.js +0 -242
- package/converters.js +0 -190
- package/functions.js +0 -78
package/converters.js
DELETED
@@ -1,190 +0,0 @@
|
|
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 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
|
-
|
136
|
-
/**
|
137
|
-
* @function toDate
|
138
|
-
*
|
139
|
-
* @param {*} d - The thing to convert
|
140
|
-
* @returns {Date|null|undefined|NaN} A date object, null/undefined if the provided value is null/undefined and NaN if it's not convertible.
|
141
|
-
*
|
142
|
-
* @example
|
143
|
-
* toDate(null) // return null
|
144
|
-
* toDate(new Date().toISOString()) // return a Date object
|
145
|
-
*/
|
146
|
-
function toDate(d) {
|
147
|
-
return (
|
148
|
-
d === null ? d :
|
149
|
-
d === undefined ? d :
|
150
|
-
d.constructor === Date ? d :
|
151
|
-
d.constructor === Array ? new Date(d[0],d[1],d[2]) :
|
152
|
-
d.constructor === Number ? new Date(d) :
|
153
|
-
d.constructor === String ? new Date(d) :
|
154
|
-
typeof d === "object" ? new Date(d.year,d.month,d.date) :
|
155
|
-
NaN
|
156
|
-
);
|
157
|
-
}
|
158
|
-
|
159
|
-
/* ---- Bytes ----------------------------------- */
|
160
|
-
/**
|
161
|
-
* @function bytesToReadable
|
162
|
-
*
|
163
|
-
* @param {number} bytes - Bytes to convert
|
164
|
-
* @param {number} [decimals=2] - How many decimals
|
165
|
-
* @returns {string} The converted bytes size to the closest unit
|
166
|
-
*
|
167
|
-
* @example
|
168
|
-
* bytesToReadable(8000000) // return "7.63 MB"
|
169
|
-
* bytesToReadable(8000000, 0) // return "8 MB"
|
170
|
-
*/
|
171
|
-
function bytesToReadable(bytes, decimals = 2) {
|
172
|
-
if (bytes === 0) return "0 Bytes";
|
173
|
-
|
174
|
-
const k = 1024;
|
175
|
-
const dm = decimals < 0 ? 0 : decimals;
|
176
|
-
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
177
|
-
|
178
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
179
|
-
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
180
|
-
}
|
181
|
-
|
182
|
-
/*****************************************************
|
183
|
-
* Export
|
184
|
-
*****************************************************/
|
185
|
-
|
186
|
-
export default {
|
187
|
-
arrayToSerialComma,
|
188
|
-
dateToString, dateToTimeString, dateToFieldString, timeStrToDate, secondsToReadable, toDate,
|
189
|
-
bytesToReadable
|
190
|
-
};
|
package/functions.js
DELETED
@@ -1,78 +0,0 @@
|
|
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
|
-
/* ---- 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
|
-
/* ---- Date ------------------------------------ */
|
38
|
-
/**
|
39
|
-
* @function dayDifference
|
40
|
-
*
|
41
|
-
* @param {Date} start - Start date
|
42
|
-
* @param {Date} end - End date
|
43
|
-
* @returns {Number} How many days between these two dates
|
44
|
-
*
|
45
|
-
* @example
|
46
|
-
* dayDifference(new Date("07/14/2021"), new Date("07/18/2021")) // return 4
|
47
|
-
* dayDifference(new Date("07/14/2021"), new Date("07/01/2021")) // return -13
|
48
|
-
*/
|
49
|
-
function dayDifference(start, end) {
|
50
|
-
start.setHours(0, 0, 0, 0);
|
51
|
-
end.setHours(0, 0, 0, 0);
|
52
|
-
|
53
|
-
return Math.round((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
|
54
|
-
}
|
55
|
-
|
56
|
-
/* ---- Other ----------------------------------- */
|
57
|
-
/**
|
58
|
-
* @function padLeft
|
59
|
-
*
|
60
|
-
* @param {*} value - The value to pad
|
61
|
-
* @param {string} [pad="00"] - With what to pad
|
62
|
-
* @returns {string} The padded string
|
63
|
-
*
|
64
|
-
* @example
|
65
|
-
* padLeft(5, "000") // return "005"
|
66
|
-
* padLeft("796", "00") // return "796"
|
67
|
-
*/
|
68
|
-
function padLeft(value, pad = "00") {
|
69
|
-
return checkers.isDefined(value)
|
70
|
-
? (value.toString.length >= pad.length) ? value : (pad + value).slice(-pad.length)
|
71
|
-
: pad;
|
72
|
-
}
|
73
|
-
|
74
|
-
/*****************************************************
|
75
|
-
* Export
|
76
|
-
*****************************************************/
|
77
|
-
|
78
|
-
export default { shuffle, dayDifference, padLeft };
|