@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/README.md CHANGED
@@ -3,6 +3,7 @@ A simple package that groups together frequently used functions in front-end and
3
3
 
4
4
  ## Note
5
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
+ > Warning: Breaking changes since 1.2.0
6
7
 
7
8
  ## Install
8
9
  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):
@@ -10,120 +11,137 @@ This is a [Node.js](https://nodejs.org/en/) module available through the [npm
10
11
  `$ npm install --save @evolis/evolis-library`
11
12
 
12
13
  ## 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
14
+ This module is divided into six sub-modules with "Evo" as prefix: `EvoArray`, `EvoDate`, `EvoMisc`, `EvoNumber`, `EvoObject` and `EvoString`.
78
15
  Import it into your script using:
79
16
 
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*
17
+ `import { EvoArray, EvoMisc, ... } from "@evolis/evolis-library";`
18
+
19
+ ## EvoArray
20
+ - ### Checkers
21
+ - **isArray(value: any)**
22
+ - value: The value to test
23
+ - *return: true if the value is an array*
24
+ - **isDeepEqual(array1: Array, array2: Array, [sortBy]: String)**
25
+ - array1: First array
26
+ - array2: Second array
27
+ - [sortBy]: Property name used to sort objects in the array. Two identical arrays in different order will not be equal (default: null)
28
+ - *return: true if the value is an array*
29
+ - ### Converters
30
+ - **toSerialComma(array: Array)**
31
+ - array: The array to convert
32
+ - *return: A string formatted as a serial comma*
33
+ - ### Functions
34
+ - **shuffle(array: Array)**
35
+ - array: The array to shuffle
36
+ - *return: The shuffled array*
37
+
38
+ ## EvoDate
39
+ - ### Checkers
40
+ - **isDate(value: any)**
41
+ - value: The value to test
42
+ - *return: true if the value is a valid date*
43
+ - **isMySQLDate(value: any)**
44
+ - value: The value to test
45
+ - *return: true if the value is a string with the valid MySQL date format (yyyy-mm-dd)*
46
+ - ### Converters
47
+ - **toDate(d: any)**
48
+ - s: The thing to convert
49
+ - *return: A date object, null/undefined if the provided value is null/undefined and NaN if it's not convertible.*
50
+ - **toString(date: Date, [withTime]: Bool, [locale]: String)**
51
+ - date: The value to convert
52
+ - [withTime]: Include time in the result (default: true)
53
+ - [locale]: The locale used in the conversion. It may changed the returned string format (default: "fr-FR")
54
+ - *return: A date formatted like "dd/MM/yyyy [hh:mm:ss]"*
55
+ - **toTimeString(date: Date, [locale]: String)**
56
+ - date: The date to convert
57
+ - [locale]: The locale used in the conversion. It may changed the returned string format (default: "fr-FR")
58
+ - *return: A date formatted like "hh:mm:ss"*
59
+ - **toFieldString(date: Date)**
60
+ - date: The value to convert
61
+ - *return: A date formatted like "yyyy-MM-dd"*
62
+ - **timeStrToDate(time: String, [format]: String)**
63
+ - time: The date to convert
64
+ - [format]: The format used for conversion (default: "hh:mm")
65
+ - *return: A date object filled with the time string provided*
66
+ - **secondsToReadable(seconds: Number)**
67
+ - seconds: Seconds to convert
68
+ - *return: The converted seconds and epoch*
69
+ - ### Functions
70
+ - **dayDiff(start: Date, end: Date)**
71
+ - start: Start date
72
+ - end: End date
73
+ - *return: How many days between these two dates*
74
+
75
+ ## EvoMisc
76
+ - ### Checkers
77
+ - **isDefined(value: any)**
78
+ - value: The value to test
79
+ - *return: true if the value is not undefined or null*
80
+ - **isEmail(value: any)**
81
+ - value: The value to test
82
+ - *return: true if the value is a string containing a valid email address*
83
+ - **isPasswordSafe(value: any|[any])**
84
+ - value: The value(s) to test
85
+ - rules: Object with these properties:
86
+ ```js
87
+ {
88
+ mustContain: {
89
+ lowerCase: true|false (default: true),
90
+ upperCase: true|false (default: true),
91
+ digit: true|false (default: true),
92
+ special: true|false (default: true),
93
+ },
94
+ minLength: Number (default: 8)
95
+ }
96
+ ```
97
+ - *return: true if the value is a safe password*
98
+ - ### Converters
99
+ - **bytesToReadable(bytes: Number, [decimals]: Number)**
100
+ - bytes: Bytes to convert
101
+ - [decimals]: How many decimals (default: 2)
102
+ - *return: The converted bytes size to the closest unit*
103
+ - ### Functions
104
+ - **padLeft(value: any, [pad]: String)**
105
+ - value: The value to pad
106
+ - [pad]: With what to pad (default: "00")
107
+ - *return: The padded string*
108
+
109
+ ## EvoNumber
110
+ - ### Checkers
111
+ - **isNumber(value: any, [parse]: Bool)**
112
+ - value: The value to test
113
+ - [parse]: If set to true, the function will parse the value to find a integer (base 10) (default: false)
114
+ - *return: true if the value is a number*
115
+ - **isFloat(value: any, [parse]: Bool)**
116
+ - value: The value to test
117
+ - [parse]: If set to true, the function will parse the value to find a float (default: false)
118
+ - *return: true if the value is a float*
119
+
120
+ ## EvoObject
121
+ - ### Checkers
122
+ - **isDeepEqual(obj1: Object, obj2: Object)**
123
+ - obj1: First object
124
+ - obj2: Second object
125
+ - *return: true if the two objects are deeply equal*
126
+
127
+ ## EvoString
128
+ - ### Checkers
129
+ - **isString(value: any)**
130
+ - value: The value to test
131
+ - *return: true if the value is a string*
132
+ - **isEmpty(value: any)**
133
+ - value: The value to test
134
+ - *return: true if the value is a string and if its empty*
135
+ - **isBlank(value: any)**
136
+ - value: The value to test
137
+ - *return: true if the value is a string and if its blank*
138
+ - **inRange(value: any|[any], [min]: Number, [max]: Number, [includeBounds]: Bool, [canBeNull]: Bool)**
139
+ - value: The value(s) to test
140
+ - [min]: Minimum string length (default: null)
141
+ - [max]: Maximum string length (default: null)
142
+ - [includeBounds]: Include the min and max bounds in the range (default: true)
143
+ - [canBeNull]: Authorize or not the string to be null or undefined (default: false)
144
+ - *return: true if the value is a string between min and max or if it's an undefined/null with canBeNull set at true*
127
145
 
128
146
  ## License
129
147
 
package/index.js CHANGED
@@ -1,3 +1,6 @@
1
- export { default as check } from "./checkers.js";
2
- export { default as convert } from "./converters.js";
3
- export { default as functions } from "./functions.js";
1
+ export { default as EvoArray } from "./EvoArray.js";
2
+ export { default as EvoDate } from "./EvoDate.js";
3
+ export { default as EvoMisc } from "./EvoMisc.js";
4
+ export { default as EvoNumber } from "./EvoNumber.js";
5
+ export { default as EvoObject } from "./EvoObject.js";
6
+ export { default as EvoString } from "./EvoString.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evolis/evolis-library",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Dependencies used both in Evolis API and Evolis UI",
5
5
  "repository": {
6
6
  "type": "git",
package/checkers.js DELETED
@@ -1,242 +0,0 @@
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} [includeBounds=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
- * @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
-
163
- /*****************************************************
164
- * Regex checkers
165
- *****************************************************/
166
-
167
- /* ---- E-mail ---------------------------------- */
168
- /**
169
- * @function isEmail
170
- *
171
- * @param {any} value - The value to test
172
- * @returns {boolean} Return true if the value is a string containing a valid email address
173
- */
174
- function isEmail(value) {
175
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
176
- }
177
-
178
- /* ---- Passwords ------------------------------- */
179
- /**
180
- * @typedef {Object} PasswordRules
181
- *
182
- * @property {object} mustContain - What type of character the password must contain
183
- * @property {boolean} [mustContain.lowerCase] - Must contain at least one lower cased character
184
- * @property {boolean} [mustContain.upperCase] - Must contain at least one upper cased character
185
- * @property {boolean} [mustContain.digit] - Must contain at least one number
186
- * @property {boolean} [mustContain.special] - Must contain at least one special character
187
- * @property {number} minLength - The minimal length
188
- */
189
-
190
- /**
191
- * @const
192
- * @type {PasswordRules}
193
- */
194
- export const passwordRules = {
195
- mustContain: {
196
- lowerCase: true,
197
- upperCase: true,
198
- digit: true,
199
- special: true
200
- },
201
- minLength: 8
202
- };
203
-
204
- /**
205
- * @function isPasswordSafe
206
- *
207
- * @param {string|Array} password - One or multiple passwords
208
- * @param {PasswordRules} [rules] - An object that describe security rules to follow
209
- * @returns {boolean} Return true if the value is a safe password
210
- */
211
- function isPasswordSafe(password, rules) {
212
- const usedRules = rules ? { ...passwordRules, ...rules } : passwordRules;
213
-
214
- if (isArray(password)) {
215
- return password.every(pwd => isPasswordSafe(pwd, usedRules));
216
- }
217
-
218
- let regex = "";
219
-
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},}$`;
225
-
226
- const strongPwd = new RegExp(regex);
227
- return strongPwd.test(password);
228
- }
229
-
230
- /*****************************************************
231
- * Export
232
- *****************************************************/
233
-
234
- export default {
235
- isDefined, atLeastOneOf,
236
- isString, isEmptyString, isBlankString, strInRange,
237
- isNumber, isFloat,
238
- isArray,
239
- isDate, isMySQLDate,
240
- isEmail,
241
- isPasswordSafe
242
- };