-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (113 loc) 路 3.56 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
/* ---------------------------------------------
</> with 馃挍 by Vishwa Gaurav
GitHub : https://github.com/VishwaGauravIn
Website : https://itsvg.in
------------------------------------------------ */
function analyzeText(inputText) {
const charCount = inputText.length;
const wordCount = inputText
.split(/\s+/)
.filter((word) => word.length > 0).length;
const sentenceCount = inputText
.split(/[.!?]/)
.filter((sentence) => sentence.length > 0).length;
const newLineCount = (inputText.match(/\n/g) || []).length;
const punctuationCount = inputText
.split(/[\s,.!?]/)
.filter((punctuation) => punctuation.length > 0).length;
const consonantCount = inputText.match(/[bcdfghjklmnpqrstvwxyz]/gi).length;
const vowelCount = inputText.match(/[aeiou]/gi).length;
const spaceConsumedOnDisk = calcDiskUsage(inputText);
const capitalLetters = inputText.match(/[A-Z]/g).length;
const smallLetters = inputText.match(/[a-z]/g).length;
const totalLetters = capitalLetters + smallLetters;
const consonantPercentage = ((consonantCount / totalLetters) * 100).toFixed(
2
);
const vowelPercentage = ((vowelCount / totalLetters) * 100).toFixed(2);
const capitalPercentage = ((capitalLetters / totalLetters) * 100).toFixed(2);
const smallPercentage = ((smallLetters / totalLetters) * 100).toFixed(2);
const isEmail = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i.test(inputText);
const isURL = /https?:\/\/[^\s/$.?#].[^\s]*/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,
};
}
function findQueryFromURL(url) {
const urlObject = new URL(url);
const queryJSON = {};
for (const [key, value] of urlObject.searchParams.entries()) {
queryJSON[key] = value;
}
return queryJSON;
}
function findDomainFromURL(url) {
const urlObject = new URL(url);
return urlObject.hostname;
}
function extractCookie(cookieString) {
const cookiesJSON = {};
cookieString.split(";").forEach((cookie) => {
const parts = cookie.split("=");
if (parts.length === 2) {
const key = parts[0].trim();
const value = parts[1].trim();
cookiesJSON[key] = value;
}
});
return cookiesJSON;
}
function calcDiskUsage(inputText) {
// Assuming UTF-8 encoding, one character can take 1 to 4 bytes.
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;
}
function extractURLs(inputText) {
const urlRegex = /https?:\/\/[^\s/$.?#].[^\s]*/gi;
const urls = inputText.match(urlRegex) || [];
return urls;
}
function extractEmails(inputText) {
const emailRegex = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi;
const emails = inputText.match(emailRegex) || [];
return emails;
}
module.exports = {
analyzeText,
findQueryFromURL,
findDomainFromURL,
extractCookie,
extractEmails,
extractURLs,
};