-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
245 lines (211 loc) · 6.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* ---------------------------------------------
</> with 💛 by Vishwa Gaurav
GitHub : https://github.com/VishwaGauravIn
Website : https://itsvg.in
------------------------------------------------ */
/**
* Analyzes text and returns various statistical information
* @param {string} inputText - The text to analyze
* @returns {Object} Analysis results
*/
function analyzeText(inputText) {
// Handle null, undefined, or non-string inputs
if (!inputText || typeof inputText !== "string") {
return {
charCount: 0,
wordCount: 0,
sentenceCount: 0,
newLineCount: 0,
punctuationCount: 0,
consonantCount: 0,
consonantPercentage: "0.00",
vowelCount: 0,
vowelPercentage: "0.00",
spaceConsumedOnDisk: 0,
capitalLetters: 0,
capitalPercentage: "0.00",
smallLetters: 0,
smallPercentage: "0.00",
isEmail: false,
isURL: false,
alphanumericText: "",
whitespaceCount: 0,
};
}
const charCount = inputText.length;
const wordCount = inputText
.trim()
.split(/\s+/)
.filter((word) => word.length > 0).length;
const sentenceCount = inputText
.split(/[.!?]+/)
.filter((sentence) => sentence.trim().length > 0).length;
const newLineCount = (inputText.match(/\n/g) || []).length;
const punctuationCount = (
inputText.match(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g) || []
).length;
const consonantMatch = inputText.match(/[bcdfghjklmnpqrstvwxyz]/gi) || [];
const consonantCount = consonantMatch.length;
const vowelMatch = inputText.match(/[aeiou]/gi) || [];
const vowelCount = vowelMatch.length;
const spaceConsumedOnDisk = calcDiskUsage(inputText);
const capitalLettersMatch = inputText.match(/[A-Z]/g) || [];
const capitalLetters = capitalLettersMatch.length;
const smallLettersMatch = inputText.match(/[a-z]/g) || [];
const smallLetters = smallLettersMatch.length;
const totalLetters = capitalLetters + smallLetters;
const consonantPercentage = totalLetters
? ((consonantCount / totalLetters) * 100).toFixed(2)
: "0.00";
const vowelPercentage = totalLetters
? ((vowelCount / totalLetters) * 100).toFixed(2)
: "0.00";
const capitalPercentage = totalLetters
? ((capitalLetters / totalLetters) * 100).toFixed(2)
: "0.00";
const smallPercentage = totalLetters
? ((smallLetters / totalLetters) * 100).toFixed(2)
: "0.00";
const isEmail = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(inputText);
const isURL =
/^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/i.test(
inputText
);
const alphanumericText = inputText.replace(/[^0-9a-z]/gi, "");
const whitespaceCount = (inputText.match(/\s/g) || []).length;
return {
charCount,
wordCount,
sentenceCount,
newLineCount,
punctuationCount,
consonantCount,
consonantPercentage,
vowelCount,
vowelPercentage,
spaceConsumedOnDisk,
capitalLetters,
capitalPercentage,
smallLetters,
smallPercentage,
isEmail,
isURL,
alphanumericText,
whitespaceCount,
};
}
/**
* Extracts query parameters from a URL
* @param {string} url - The URL to parse
* @returns {Object} Query parameters as key-value pairs
*/
function findQueryFromURL(url) {
if (!url || typeof url !== "string") {
return {};
}
try {
const urlObject = new URL(url);
const queryJSON = {};
for (const [key, value] of urlObject.searchParams.entries()) {
queryJSON[key] = value;
}
return queryJSON;
} catch (error) {
return {};
}
}
/**
* Extracts domain from a URL
* @param {string} url - The URL to parse
* @returns {string} The domain name
*/
function findDomainFromURL(url) {
if (!url || typeof url !== "string") {
return "";
}
try {
const urlObject = new URL(url);
return urlObject.hostname;
} catch (error) {
return "";
}
}
/**
* Parses cookie string into an object
* @param {string} cookieString - The cookie string to parse
* @returns {Object} Parsed cookies as key-value pairs
*/
function extractCookie(cookieString) {
if (!cookieString || typeof cookieString !== "string") {
return {};
}
const cookiesJSON = {};
cookieString.split(";").forEach((cookie) => {
const parts = cookie.split("=");
if (parts.length === 2) {
const key = parts[0].trim();
const value = parts[1].trim();
if (key) {
cookiesJSON[key] = value;
}
}
});
return cookiesJSON;
}
/**
* Calculates the disk space usage of a string in bytes
* @param {string} inputText - The text to analyze
* @returns {number} Number of bytes the text would occupy
*/
function calcDiskUsage(inputText) {
if (!inputText || typeof inputText !== "string") {
return 0;
}
let totalBytes = 0;
for (let i = 0; i < inputText.length; i++) {
const charCode = inputText.charCodeAt(i);
if (charCode <= 0x7f) {
totalBytes += 1; // 1-byte character
} else if (charCode <= 0x7ff) {
totalBytes += 2; // 2-byte character
} else if (charCode <= 0xffff) {
totalBytes += 3; // 3-byte character
} else {
totalBytes += 4; // 4-byte character
}
}
return totalBytes;
}
/**
* Extracts URLs from text
* @param {string} inputText - The text to analyze
* @returns {string[]} Array of found URLs
*/
function extractURLs(inputText) {
if (!inputText || typeof inputText !== "string") {
return [];
}
const urlRegex =
/https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
return inputText.match(urlRegex) || [];
}
/**
* Extracts email addresses from text
* @param {string} inputText - The text to analyze
* @returns {string[]} Array of found email addresses
*/
function extractEmails(inputText) {
if (!inputText || typeof inputText !== "string") {
return [];
}
const emailRegex = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/gi;
return inputText.match(emailRegex) || [];
}
module.exports = {
analyzeText,
findQueryFromURL,
findDomainFromURL,
extractCookie,
extractEmails,
extractURLs,
};