clear-af.js 1.0.0
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/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/index.d.ts +139 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +246 -0
- package/package.json +32 -0
- package/src/index.ts +260 -0
- package/tsconfig.json +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Frost
|
|
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,97 @@
|
|
|
1
|
+
# clear-af.js
|
|
2
|
+
|
|
3
|
+
Un package JavaScript rempli d'utilitaires pour rendre votre code **lisible af**. Parce que la clarté, c'est la clé de la survie en programmation.
|
|
4
|
+
|
|
5
|
+
> **Le projet est encore en cours de developpement, la plupart des fonctions listées dans la présentation ne sont pas encore disponible et le package NPM n'est pas encore installable.**
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install clear-af
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 🚀 Utilisation rapide
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
const clearaf = require('clear-af');
|
|
21
|
+
|
|
22
|
+
// Vos utilitaires arrivent ici
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## ✨ Fonctionnalités
|
|
28
|
+
|
|
29
|
+
### 🔍 Debugging amélioré
|
|
30
|
+
Parce que `console.log()` c'est pour les amateurs.
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
clear.prettyLog(data);
|
|
34
|
+
clear.prettyError(error);
|
|
35
|
+
clear.prettyWarn(warning);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 📊 Formatage lisible
|
|
39
|
+
Transformez vos données en quelque chose de compréhensible.
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
clear.formatJSON(obj);
|
|
43
|
+
clear.formatArray(arr);
|
|
44
|
+
clear.prettyPrint(anything);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 🎯 Validation facile
|
|
48
|
+
Clarifiez votre logique de validation.
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
if (clear.isValid(data)) {
|
|
52
|
+
// Do something
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 🤔 FAQ
|
|
59
|
+
|
|
60
|
+
**Q: Pourquoi "-af" dans le nom ?**
|
|
61
|
+
R: Parce que votre code doit être lisible *af*, mec.
|
|
62
|
+
|
|
63
|
+
**Q: C'est lourd à utiliser ?**
|
|
64
|
+
R: Non, c'est ultra minimaliste. Just import and go.
|
|
65
|
+
|
|
66
|
+
**Q: Compatible avec TypeScript ?**
|
|
67
|
+
R: Oui ! Les types TypeScript arrivent bientôt.
|
|
68
|
+
|
|
69
|
+
**Q: Ça ralentit mon app ?**
|
|
70
|
+
R: Non, c'est juste des helpers utilitaires. Zéro overhead en production si vous l'utilisez à bon escient.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 📝 License
|
|
75
|
+
|
|
76
|
+
MIT - Libre de l'utiliser comme bon vous semble.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 🤝 Contribution
|
|
81
|
+
|
|
82
|
+
Les contributions sont bienvenues ! Fork, crée une branche, commit, et ouvre une PR.
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
git clone https://github.com/FroostDev/clear-af.js
|
|
86
|
+
cd clear-af.js
|
|
87
|
+
npm install
|
|
88
|
+
npm run test
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
> *Créé par un développeur qui en avait marre de lire du code illisible.*
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
**clear-af.js** - *Parce que la clarté, c'est la santé mentale du dev.*
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* =======================
|
|
3
|
+
* Functions for utilities
|
|
4
|
+
* =======================
|
|
5
|
+
*/
|
|
6
|
+
declare function getDate(time?: boolean): string;
|
|
7
|
+
/**
|
|
8
|
+
* Show a pretty error log message with optional date information
|
|
9
|
+
* @function prettyError
|
|
10
|
+
* @param err - Error message to display
|
|
11
|
+
* @param time - Include or not a timestamp
|
|
12
|
+
*/
|
|
13
|
+
declare function prettyError(err: string, time?: boolean): void;
|
|
14
|
+
/**
|
|
15
|
+
* Show a pretty warn log message with optional date information
|
|
16
|
+
* @function prettyWarn
|
|
17
|
+
* @param warn - Warning message to display
|
|
18
|
+
* @param time - Include or not a timestamp
|
|
19
|
+
*/
|
|
20
|
+
declare function prettyWarn(warn: string, time?: boolean): void;
|
|
21
|
+
/**
|
|
22
|
+
* Show a pretty success log message with optional date information
|
|
23
|
+
* @function prettySuccess
|
|
24
|
+
* @param success - Success message to display
|
|
25
|
+
* @param time - Include or not a timestamp
|
|
26
|
+
*/
|
|
27
|
+
declare function prettySuccess(success: string, time?: boolean): void;
|
|
28
|
+
/**
|
|
29
|
+
* Show a pretty information log message with optional date information
|
|
30
|
+
* @function prettyInfo
|
|
31
|
+
* @param info - Information message to display
|
|
32
|
+
* @param time - Include or not a timestamp
|
|
33
|
+
*/
|
|
34
|
+
declare function prettyInfo(info: string, time?: boolean): void;
|
|
35
|
+
/**
|
|
36
|
+
* Show a pretty debug log message with optional date information
|
|
37
|
+
* @function prettyDebug
|
|
38
|
+
* @param debug - Debug message to display
|
|
39
|
+
* @param time - Include or not a timestamp
|
|
40
|
+
*/
|
|
41
|
+
declare function prettyDebug(debug: string, time?: boolean): void;
|
|
42
|
+
/**
|
|
43
|
+
* Show a separator line in console
|
|
44
|
+
* @function logSeparator
|
|
45
|
+
*/
|
|
46
|
+
declare function logSeparator(): void;
|
|
47
|
+
/**
|
|
48
|
+
* Display a header with a title in the center
|
|
49
|
+
* @function logHeader
|
|
50
|
+
* @param title - The title of the header
|
|
51
|
+
*/
|
|
52
|
+
declare function logHeader(title: string): void;
|
|
53
|
+
/**
|
|
54
|
+
*
|
|
55
|
+
* Validation utilities
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
/**
|
|
59
|
+
* Check if any type of variable is empty
|
|
60
|
+
* @function isEmpty
|
|
61
|
+
* @param value - The variable to check
|
|
62
|
+
* @returns {boolean} True if the value is empty, false otherwise
|
|
63
|
+
*/
|
|
64
|
+
declare function isEmpty(value: unknown): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Check if the variable is of the chosen type
|
|
67
|
+
* @function isType
|
|
68
|
+
* @param value - The variable to check
|
|
69
|
+
* @param type - The type you want
|
|
70
|
+
* @returns {boolean} True if the value is of the chosen type, false otherwise
|
|
71
|
+
*/
|
|
72
|
+
declare function isType(value: unknown, type: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Check if an email is valid
|
|
75
|
+
* @function isEmail
|
|
76
|
+
* @param email - The email to check
|
|
77
|
+
* @returns {boolean} True if the email is valid, false otherwise
|
|
78
|
+
*/
|
|
79
|
+
declare function isEmail(email: string): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Check if a URL is valid
|
|
82
|
+
* @function isURL
|
|
83
|
+
* @param url - The URL to check
|
|
84
|
+
* @returns {boolean} True if the URL is valid, false otherwise
|
|
85
|
+
*/
|
|
86
|
+
declare function isURL(url: string): boolean;
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* Object manipulation
|
|
90
|
+
*
|
|
91
|
+
*/
|
|
92
|
+
/**
|
|
93
|
+
* Create a deep clone of any object or array
|
|
94
|
+
* @function deepClone
|
|
95
|
+
* @param obj - The object or array to clone
|
|
96
|
+
* @returns A deep clone of the object
|
|
97
|
+
*/
|
|
98
|
+
declare function deepClone<T>(obj: T): T;
|
|
99
|
+
/**
|
|
100
|
+
* Remove duplicate values from an array
|
|
101
|
+
* @function noTwins
|
|
102
|
+
* @param arr - The array to remove duplicates from
|
|
103
|
+
* @returns {unknown[]} A new array with unique values only
|
|
104
|
+
*/
|
|
105
|
+
declare function noTwins(arr: unknown[]): unknown[];
|
|
106
|
+
/**
|
|
107
|
+
*
|
|
108
|
+
* Transforamtion utilites
|
|
109
|
+
*
|
|
110
|
+
*/
|
|
111
|
+
/**
|
|
112
|
+
* Transform a string to camelCase format
|
|
113
|
+
* @function camelify
|
|
114
|
+
* @param str - The string to transform
|
|
115
|
+
* @returns {string} The string in camelCase format
|
|
116
|
+
*/
|
|
117
|
+
declare function camelify(str: string): string;
|
|
118
|
+
/**
|
|
119
|
+
* Transform a string to kebab-case format
|
|
120
|
+
* @function kebabify
|
|
121
|
+
* @param str - The string to transform
|
|
122
|
+
* @returns {string} The string in kebab-case format
|
|
123
|
+
*/
|
|
124
|
+
declare function kebabify(str: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Transform a string to snake_case format
|
|
127
|
+
* @function snakify
|
|
128
|
+
* @param str - The string to transform
|
|
129
|
+
* @returns {string} The string in snake_case format
|
|
130
|
+
*/
|
|
131
|
+
declare function snakify(str: string): string;
|
|
132
|
+
/**
|
|
133
|
+
* Capitalize the first character of a string
|
|
134
|
+
* @function capitalize
|
|
135
|
+
* @param str - The string to capitalize
|
|
136
|
+
* @returns {string} The string with the first character in uppercase
|
|
137
|
+
*/
|
|
138
|
+
declare function capitalize(str: string): string;
|
|
139
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA;;;;GAIG;AACH,iBAAS,OAAO,CAAC,IAAI,GAAE,OAAe,GAAG,MAAM,CAE9C;AAQD;;;;;GAKG;AACH,iBAAS,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,UAAQ,GAAG,IAAI,CAGpD;AAED;;;;;GAKG;AACH,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,UAAQ,GAAG,IAAI,CAGpD;AAED;;;;;GAKG;AACH,iBAAS,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,UAAQ,GAAG,IAAI,CAG1D;AAED;;;;;GAKG;AACH,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,UAAQ,GAAG,IAAI,CAGpD;AAED;;;;;GAKG;AACH,iBAAS,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,UAAQ,GAAG,IAAI,CAGtD;AAED;;;GAGG;AACH,iBAAS,YAAY,IAAI,IAAI,CAE5B;AAED;;;;GAIG;AACH,iBAAS,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAItC;AAED;;;;GAIG;AAEH;;;;;GAKG;AACH,iBAAS,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAMxC;AAED;;;;;;GAMG;AACH,iBAAS,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;GAKG;AACH,iBAAS,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAGvC;AAED;;;;;GAKG;AACH,iBAAS,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAOnC;AAED;;;;GAIG;AAEH;;;;;GAKG;AACH,iBAAS,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAU/B;AAED;;;;;GAKG;AACH,iBAAS,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,CAE1C;AAED;;;;GAIG;AAEH;;;;;GAKG;AACH,iBAAS,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAarC;AAED;;;;;GAKG;AACH,iBAAS,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CASrC;AAED;;;;;GAKG;AACH,iBAAS,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CASpC;AAED;;;;;GAKG;AACH,iBAAS,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAGvC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
____ ____ __ ____ __ ____ __ ____
|
|
4
|
+
( _ \( __) / _\ ( \ ___ / _\ ( __) _( )/ ___)
|
|
5
|
+
) / ) _) / \ ) D ((___) / \ ) _) _ / \) \\___ \
|
|
6
|
+
(__\_)(____)\_/\_/(____/ \_/\_/(__) (_) \____/(____/
|
|
7
|
+
|
|
8
|
+
Made by FroostDev | https://github.com/FroostDev - MIT Licence
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* =======================
|
|
12
|
+
* Functions for utilities
|
|
13
|
+
* =======================
|
|
14
|
+
*/
|
|
15
|
+
function getDate(time = false) {
|
|
16
|
+
return time ? `[${new Date().toLocaleString()}]` : '';
|
|
17
|
+
}
|
|
18
|
+
/*
|
|
19
|
+
* =================
|
|
20
|
+
* Logging utilities
|
|
21
|
+
* =================
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Show a pretty error log message with optional date information
|
|
25
|
+
* @function prettyError
|
|
26
|
+
* @param err - Error message to display
|
|
27
|
+
* @param time - Include or not a timestamp
|
|
28
|
+
*/
|
|
29
|
+
function prettyError(err, time = false) {
|
|
30
|
+
const date = getDate(time);
|
|
31
|
+
console.log(`\x1b[1;41m ✕ ${date} - ${err} \x1b[0m`);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Show a pretty warn log message with optional date information
|
|
35
|
+
* @function prettyWarn
|
|
36
|
+
* @param warn - Warning message to display
|
|
37
|
+
* @param time - Include or not a timestamp
|
|
38
|
+
*/
|
|
39
|
+
function prettyWarn(warn, time = false) {
|
|
40
|
+
const date = getDate(time);
|
|
41
|
+
console.log(`\x1b[1;43m ⚠ ${date} - ${warn} \x1b[0m`);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Show a pretty success log message with optional date information
|
|
45
|
+
* @function prettySuccess
|
|
46
|
+
* @param success - Success message to display
|
|
47
|
+
* @param time - Include or not a timestamp
|
|
48
|
+
*/
|
|
49
|
+
function prettySuccess(success, time = false) {
|
|
50
|
+
const date = getDate(time);
|
|
51
|
+
console.log(`\x1b[1;42m ✔ ${date} - ${success} \x1b[0m`);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Show a pretty information log message with optional date information
|
|
55
|
+
* @function prettyInfo
|
|
56
|
+
* @param info - Information message to display
|
|
57
|
+
* @param time - Include or not a timestamp
|
|
58
|
+
*/
|
|
59
|
+
function prettyInfo(info, time = false) {
|
|
60
|
+
const date = getDate(time);
|
|
61
|
+
console.log(`\x1b[1;44m ℹ ${date} - ${info} \x1b[0m`);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Show a pretty debug log message with optional date information
|
|
65
|
+
* @function prettyDebug
|
|
66
|
+
* @param debug - Debug message to display
|
|
67
|
+
* @param time - Include or not a timestamp
|
|
68
|
+
*/
|
|
69
|
+
function prettyDebug(debug, time = false) {
|
|
70
|
+
const date = getDate(time);
|
|
71
|
+
console.log(`\x1b[1;97m ⚙ ${date} - ${debug} \x1b[0m`);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Show a separator line in console
|
|
75
|
+
* @function logSeparator
|
|
76
|
+
*/
|
|
77
|
+
function logSeparator() {
|
|
78
|
+
console.log(`\x1b[1m${'═'.repeat(50)}\x1b[0m`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Display a header with a title in the center
|
|
82
|
+
* @function logHeader
|
|
83
|
+
* @param title - The title of the header
|
|
84
|
+
*/
|
|
85
|
+
function logHeader(title) {
|
|
86
|
+
console.log(`\x1b[1m╔${'═'.repeat(title.length + 2)}╗\x1b[0m`);
|
|
87
|
+
console.log(`\x1b[1m║ ${title} ║\x1b[0m`);
|
|
88
|
+
console.log(`\x1b[1m╚${'═'.repeat(title.length + 2)}╝\x1b[0m`);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
*
|
|
92
|
+
* Validation utilities
|
|
93
|
+
*
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* Check if any type of variable is empty
|
|
97
|
+
* @function isEmpty
|
|
98
|
+
* @param value - The variable to check
|
|
99
|
+
* @returns {boolean} True if the value is empty, false otherwise
|
|
100
|
+
*/
|
|
101
|
+
function isEmpty(value) {
|
|
102
|
+
if (value === null || value === undefined)
|
|
103
|
+
return true;
|
|
104
|
+
if (typeof value === "string")
|
|
105
|
+
return value.trim().length === 0;
|
|
106
|
+
if (Array.isArray(value))
|
|
107
|
+
return value.length === 0;
|
|
108
|
+
if (typeof value === "object")
|
|
109
|
+
return Object.keys(value).length === 0;
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Check if the variable is of the chosen type
|
|
114
|
+
* @function isType
|
|
115
|
+
* @param value - The variable to check
|
|
116
|
+
* @param type - The type you want
|
|
117
|
+
* @returns {boolean} True if the value is of the chosen type, false otherwise
|
|
118
|
+
*/
|
|
119
|
+
function isType(value, type) {
|
|
120
|
+
return typeof value === type;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Check if an email is valid
|
|
124
|
+
* @function isEmail
|
|
125
|
+
* @param email - The email to check
|
|
126
|
+
* @returns {boolean} True if the email is valid, false otherwise
|
|
127
|
+
*/
|
|
128
|
+
function isEmail(email) {
|
|
129
|
+
const regex = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
130
|
+
return regex.test(email);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Check if a URL is valid
|
|
134
|
+
* @function isURL
|
|
135
|
+
* @param url - The URL to check
|
|
136
|
+
* @returns {boolean} True if the URL is valid, false otherwise
|
|
137
|
+
*/
|
|
138
|
+
function isURL(url) {
|
|
139
|
+
try {
|
|
140
|
+
new URL(url);
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
*
|
|
149
|
+
* Object manipulation
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
152
|
+
/**
|
|
153
|
+
* Create a deep clone of any object or array
|
|
154
|
+
* @function deepClone
|
|
155
|
+
* @param obj - The object or array to clone
|
|
156
|
+
* @returns A deep clone of the object
|
|
157
|
+
*/
|
|
158
|
+
function deepClone(obj) {
|
|
159
|
+
if (obj === null || typeof obj !== "object")
|
|
160
|
+
return obj;
|
|
161
|
+
if (Array.isArray(obj))
|
|
162
|
+
return obj.map(item => deepClone(item));
|
|
163
|
+
const cloned = {};
|
|
164
|
+
for (const key in obj) {
|
|
165
|
+
if (obj.hasOwnProperty(key))
|
|
166
|
+
cloned[key] = deepClone(obj[key]);
|
|
167
|
+
}
|
|
168
|
+
return cloned;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Remove duplicate values from an array
|
|
172
|
+
* @function noTwins
|
|
173
|
+
* @param arr - The array to remove duplicates from
|
|
174
|
+
* @returns {unknown[]} A new array with unique values only
|
|
175
|
+
*/
|
|
176
|
+
function noTwins(arr) {
|
|
177
|
+
return Array.from(new Set(arr));
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
*
|
|
181
|
+
* Transforamtion utilites
|
|
182
|
+
*
|
|
183
|
+
*/
|
|
184
|
+
/**
|
|
185
|
+
* Transform a string to camelCase format
|
|
186
|
+
* @function camelify
|
|
187
|
+
* @param str - The string to transform
|
|
188
|
+
* @returns {string} The string in camelCase format
|
|
189
|
+
*/
|
|
190
|
+
function camelify(str) {
|
|
191
|
+
let camelCased = "";
|
|
192
|
+
if (!isEmpty(str)) {
|
|
193
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
194
|
+
let toCamelify = cleaned.split(" ");
|
|
195
|
+
camelCased = toCamelify.map((word, index) => {
|
|
196
|
+
if (index === 0)
|
|
197
|
+
return word.toLowerCase();
|
|
198
|
+
return word[0].toUpperCase() + word.slice(1).toLowerCase();
|
|
199
|
+
})
|
|
200
|
+
.join('');
|
|
201
|
+
}
|
|
202
|
+
return camelCased;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Transform a string to kebab-case format
|
|
206
|
+
* @function kebabify
|
|
207
|
+
* @param str - The string to transform
|
|
208
|
+
* @returns {string} The string in kebab-case format
|
|
209
|
+
*/
|
|
210
|
+
function kebabify(str) {
|
|
211
|
+
let kebabised = "";
|
|
212
|
+
if (!isEmpty(str)) {
|
|
213
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
214
|
+
let toKebabify = cleaned.split(" ");
|
|
215
|
+
kebabised = toKebabify.map(word => word.toLowerCase())
|
|
216
|
+
.join("-");
|
|
217
|
+
}
|
|
218
|
+
return kebabised;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Transform a string to snake_case format
|
|
222
|
+
* @function snakify
|
|
223
|
+
* @param str - The string to transform
|
|
224
|
+
* @returns {string} The string in snake_case format
|
|
225
|
+
*/
|
|
226
|
+
function snakify(str) {
|
|
227
|
+
let snaked = "";
|
|
228
|
+
if (!isEmpty(str)) {
|
|
229
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
230
|
+
let toSnakify = cleaned.split(" ");
|
|
231
|
+
snaked = toSnakify.map(word => word.toLowerCase())
|
|
232
|
+
.join("_");
|
|
233
|
+
}
|
|
234
|
+
return snaked;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Capitalize the first character of a string
|
|
238
|
+
* @function capitalize
|
|
239
|
+
* @param str - The string to capitalize
|
|
240
|
+
* @returns {string} The string with the first character in uppercase
|
|
241
|
+
*/
|
|
242
|
+
function capitalize(str) {
|
|
243
|
+
if (isEmpty(str))
|
|
244
|
+
return str;
|
|
245
|
+
return str[0].toUpperCase() + str.slice(1);
|
|
246
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clear-af.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Make your code readable af with zero bullsh*t utilities.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"javascript",
|
|
7
|
+
"debug",
|
|
8
|
+
"utility",
|
|
9
|
+
"readable",
|
|
10
|
+
"clear-af",
|
|
11
|
+
"formatting",
|
|
12
|
+
"logging"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/FroostDev/clear-af.js#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/FroostDev/clear-af.js/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/FroostDev/clear-af.js.git"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "FroostDev",
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"main": "index.js",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"typescript": "^6.0.2"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/*
|
|
2
|
+
____ ____ __ ____ __ ____ __ ____
|
|
3
|
+
( _ \( __) / _\ ( \ ___ / _\ ( __) _( )/ ___)
|
|
4
|
+
) / ) _) / \ ) D ((___) / \ ) _) _ / \) \\___ \
|
|
5
|
+
(__\_)(____)\_/\_/(____/ \_/\_/(__) (_) \____/(____/
|
|
6
|
+
|
|
7
|
+
Made by FroostDev | https://github.com/FroostDev - MIT Licence
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* =======================
|
|
12
|
+
* Functions for utilities
|
|
13
|
+
* =======================
|
|
14
|
+
*/
|
|
15
|
+
function getDate(time: boolean = false): string {
|
|
16
|
+
return time ? `[${new Date().toLocaleString()}]` : '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* =================
|
|
21
|
+
* Logging utilities
|
|
22
|
+
* =================
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Show a pretty error log message with optional date information
|
|
27
|
+
* @function prettyError
|
|
28
|
+
* @param err - Error message to display
|
|
29
|
+
* @param time - Include or not a timestamp
|
|
30
|
+
*/
|
|
31
|
+
function prettyError(err: string, time = false): void {
|
|
32
|
+
const date = getDate(time);
|
|
33
|
+
console.log(`\x1b[1;41m ✕ ${date} - ${err} \x1b[0m`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Show a pretty warn log message with optional date information
|
|
38
|
+
* @function prettyWarn
|
|
39
|
+
* @param warn - Warning message to display
|
|
40
|
+
* @param time - Include or not a timestamp
|
|
41
|
+
*/
|
|
42
|
+
function prettyWarn(warn: string, time = false): void {
|
|
43
|
+
const date = getDate(time);
|
|
44
|
+
console.log(`\x1b[1;43m ⚠ ${date} - ${warn} \x1b[0m`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Show a pretty success log message with optional date information
|
|
49
|
+
* @function prettySuccess
|
|
50
|
+
* @param success - Success message to display
|
|
51
|
+
* @param time - Include or not a timestamp
|
|
52
|
+
*/
|
|
53
|
+
function prettySuccess(success: string, time = false): void {
|
|
54
|
+
const date = getDate(time);
|
|
55
|
+
console.log(`\x1b[1;42m ✔ ${date} - ${success} \x1b[0m`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Show a pretty information log message with optional date information
|
|
60
|
+
* @function prettyInfo
|
|
61
|
+
* @param info - Information message to display
|
|
62
|
+
* @param time - Include or not a timestamp
|
|
63
|
+
*/
|
|
64
|
+
function prettyInfo(info: string, time = false): void {
|
|
65
|
+
const date = getDate(time);
|
|
66
|
+
console.log(`\x1b[1;44m ℹ ${date} - ${info} \x1b[0m`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Show a pretty debug log message with optional date information
|
|
71
|
+
* @function prettyDebug
|
|
72
|
+
* @param debug - Debug message to display
|
|
73
|
+
* @param time - Include or not a timestamp
|
|
74
|
+
*/
|
|
75
|
+
function prettyDebug(debug: string, time = false): void {
|
|
76
|
+
const date = getDate(time);
|
|
77
|
+
console.log(`\x1b[1;97m ⚙ ${date} - ${debug} \x1b[0m`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Show a separator line in console
|
|
82
|
+
* @function logSeparator
|
|
83
|
+
*/
|
|
84
|
+
function logSeparator(): void {
|
|
85
|
+
console.log(`\x1b[1m${'═'.repeat(50)}\x1b[0m`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Display a header with a title in the center
|
|
90
|
+
* @function logHeader
|
|
91
|
+
* @param title - The title of the header
|
|
92
|
+
*/
|
|
93
|
+
function logHeader(title: string): void {
|
|
94
|
+
console.log(`\x1b[1m╔${'═'.repeat(title.length + 2)}╗\x1b[0m`);
|
|
95
|
+
console.log(`\x1b[1m║ ${title} ║\x1b[0m`);
|
|
96
|
+
console.log(`\x1b[1m╚${'═'.repeat(title.length + 2)}╝\x1b[0m`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* Validation utilities
|
|
102
|
+
*
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Check if any type of variable is empty
|
|
107
|
+
* @function isEmpty
|
|
108
|
+
* @param value - The variable to check
|
|
109
|
+
* @returns {boolean} True if the value is empty, false otherwise
|
|
110
|
+
*/
|
|
111
|
+
function isEmpty(value: unknown): boolean {
|
|
112
|
+
if (value === null || value === undefined) return true;
|
|
113
|
+
if (typeof value === "string") return value.trim().length === 0;
|
|
114
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
115
|
+
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Check if the variable is of the chosen type
|
|
121
|
+
* @function isType
|
|
122
|
+
* @param value - The variable to check
|
|
123
|
+
* @param type - The type you want
|
|
124
|
+
* @returns {boolean} True if the value is of the chosen type, false otherwise
|
|
125
|
+
*/
|
|
126
|
+
function isType(value: unknown, type: string): boolean {
|
|
127
|
+
return typeof value === type;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Check if an email is valid
|
|
132
|
+
* @function isEmail
|
|
133
|
+
* @param email - The email to check
|
|
134
|
+
* @returns {boolean} True if the email is valid, false otherwise
|
|
135
|
+
*/
|
|
136
|
+
function isEmail(email: string): boolean {
|
|
137
|
+
const regex = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
138
|
+
return regex.test(email);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Check if a URL is valid
|
|
143
|
+
* @function isURL
|
|
144
|
+
* @param url - The URL to check
|
|
145
|
+
* @returns {boolean} True if the URL is valid, false otherwise
|
|
146
|
+
*/
|
|
147
|
+
function isURL(url: string): boolean {
|
|
148
|
+
try {
|
|
149
|
+
new URL(url);
|
|
150
|
+
return true;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
*
|
|
158
|
+
* Object manipulation
|
|
159
|
+
*
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Create a deep clone of any object or array
|
|
164
|
+
* @function deepClone
|
|
165
|
+
* @param obj - The object or array to clone
|
|
166
|
+
* @returns A deep clone of the object
|
|
167
|
+
*/
|
|
168
|
+
function deepClone<T>(obj: T): T {
|
|
169
|
+
if (obj === null || typeof obj !== "object") return obj;
|
|
170
|
+
|
|
171
|
+
if (Array.isArray(obj)) return obj.map(item => deepClone(item)) as T;
|
|
172
|
+
|
|
173
|
+
const cloned = {} as T;
|
|
174
|
+
for (const key in obj) {
|
|
175
|
+
if (obj.hasOwnProperty(key)) cloned[key] = deepClone(obj[key]);
|
|
176
|
+
}
|
|
177
|
+
return cloned;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Remove duplicate values from an array
|
|
182
|
+
* @function noTwins
|
|
183
|
+
* @param arr - The array to remove duplicates from
|
|
184
|
+
* @returns {unknown[]} A new array with unique values only
|
|
185
|
+
*/
|
|
186
|
+
function noTwins(arr: unknown[]): unknown[] {
|
|
187
|
+
return Array.from(new Set(arr));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
*
|
|
192
|
+
* Transforamtion utilites
|
|
193
|
+
*
|
|
194
|
+
*/
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Transform a string to camelCase format
|
|
198
|
+
* @function camelify
|
|
199
|
+
* @param str - The string to transform
|
|
200
|
+
* @returns {string} The string in camelCase format
|
|
201
|
+
*/
|
|
202
|
+
function camelify(str: string): string {
|
|
203
|
+
let camelCased: string = "";
|
|
204
|
+
if (!isEmpty(str)) {
|
|
205
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
206
|
+
let toCamelify = cleaned.split(" ");
|
|
207
|
+
|
|
208
|
+
camelCased = toCamelify.map((word, index) => {
|
|
209
|
+
if (index === 0) return word.toLowerCase();
|
|
210
|
+
return word[0].toUpperCase() + word.slice(1).toLowerCase();
|
|
211
|
+
})
|
|
212
|
+
.join('');
|
|
213
|
+
}
|
|
214
|
+
return camelCased;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Transform a string to kebab-case format
|
|
219
|
+
* @function kebabify
|
|
220
|
+
* @param str - The string to transform
|
|
221
|
+
* @returns {string} The string in kebab-case format
|
|
222
|
+
*/
|
|
223
|
+
function kebabify(str: string): string {
|
|
224
|
+
let kebabised: string = "";
|
|
225
|
+
if (!isEmpty(str)) {
|
|
226
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
227
|
+
let toKebabify = cleaned.split(" ");
|
|
228
|
+
kebabised = toKebabify.map(word => word.toLowerCase())
|
|
229
|
+
.join("-");
|
|
230
|
+
}
|
|
231
|
+
return kebabised;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Transform a string to snake_case format
|
|
236
|
+
* @function snakify
|
|
237
|
+
* @param str - The string to transform
|
|
238
|
+
* @returns {string} The string in snake_case format
|
|
239
|
+
*/
|
|
240
|
+
function snakify(str: string): string {
|
|
241
|
+
let snaked: string = "";
|
|
242
|
+
if (!isEmpty(str)) {
|
|
243
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
244
|
+
let toSnakify = cleaned.split(" ");
|
|
245
|
+
snaked = toSnakify.map(word => word.toLowerCase())
|
|
246
|
+
.join("_");
|
|
247
|
+
}
|
|
248
|
+
return snaked;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Capitalize the first character of a string
|
|
253
|
+
* @function capitalize
|
|
254
|
+
* @param str - The string to capitalize
|
|
255
|
+
* @returns {string} The string with the first character in uppercase
|
|
256
|
+
*/
|
|
257
|
+
function capitalize(str: string): string {
|
|
258
|
+
if (isEmpty(str)) return str;
|
|
259
|
+
return str[0].toUpperCase() + str.slice(1);
|
|
260
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": [
|
|
6
|
+
"ES2020",
|
|
7
|
+
"DOM"
|
|
8
|
+
],
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"declaration": true,
|
|
16
|
+
"declarationMap": true
|
|
17
|
+
},
|
|
18
|
+
"include": [
|
|
19
|
+
"src/**/*"
|
|
20
|
+
],
|
|
21
|
+
"exclude": [
|
|
22
|
+
"node_modules"
|
|
23
|
+
]
|
|
24
|
+
}
|