Repository navigation
Expand file tree
/
Copy pathcaps-filter.js
More file actions
112 lines (88 loc) · 2.96 KB
/
Copy pathcaps-filter.js
File metadata and controls
112 lines (88 loc) · 2.96 KB
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
// Detects ALL CAPS "screaming" and rewrites it in sentence case.
//
// A message counts as screaming when it has at least MIN_SHOUTED_WORDS words
// written fully in capitals (2+ letters each) and those words make up more
// than half of all words. This lets a single shouted word ("this is NOT ok")
// and acronym-heavy sentences ("My GCP and DEM look off") through.
const MIN_SHOUTED_WORDS = 3;
// Parts of a message that must never be counted or recased: code blocks,
// inline code, URLs, Discord mentions/channels/custom emoji/timestamps and
// :emoji_names:.
const PROTECTED_PATTERN =
'```[\\s\\S]*?```|`[^`\\n]*`|https?:\\/\\/\\S+|<(?:@[!&]?|#|a?:\\w+:)\\d+>|<t:-?\\d+(?::\\w)?>|:[\\w~-]+:';
const WORD_PATTERN = "[\\p{L}\\p{N}]+(?:['’][\\p{L}\\p{N}]+)*";
const TOKEN_REGEX = new RegExp(`(${PROTECTED_PATTERN})|(${WORD_PATTERN})`, 'gu');
// Punctuation ending a sentence, optionally followed by closing quotes or
// brackets, then whitespace. Line breaks also start a new sentence.
const SENTENCE_BREAK = /[.!?]+["'’”)\]]*\s|\n/u;
function countLetters(word) {
return (word.match(/\p{L}/gu) ?? []).length;
}
function isAllCaps(word) {
return /\p{Lu}/u.test(word) && !/\p{Ll}/u.test(word);
}
function isShoutedWord(word) {
return isAllCaps(word) && countLetters(word) >= 2;
}
function* words(text) {
for (const match of text.matchAll(TOKEN_REGEX)) {
if (match[2]) {
yield match[2];
}
}
}
export function isScreaming(text) {
if (!text) {
return false;
}
let total = 0;
let shouted = 0;
for (const word of words(text)) {
if (countLetters(word) === 0) {
continue;
}
total += 1;
if (isShoutedWord(word)) {
shouted += 1;
}
}
return shouted >= MIN_SHOUTED_WORDS && shouted * 2 > total;
}
function capitalize(word) {
return word.replace(/\p{L}/u, (letter) => letter.toUpperCase());
}
// Lowercases every all-caps word and capitalizes the ones that start a
// sentence. Words that already use mixed or lower case are left as written.
export function toSentenceCase(text) {
if (!text) {
return text ?? '';
}
let result = '';
let lastIndex = 0;
let sentenceStart = true;
for (const match of text.matchAll(TOKEN_REGEX)) {
const gap = text.slice(lastIndex, match.index);
const [token, , word] = match;
if (SENTENCE_BREAK.test(gap)) {
sentenceStart = true;
}
result += gap;
lastIndex = match.index + token.length;
if (!word) {
result += token;
sentenceStart = false;
continue;
}
// Single letters such as "I" or "3D" are left alone, except a lone "A".
if (isAllCaps(word) && (countLetters(word) >= 2 || word === 'A')) {
let recased = word.toLowerCase();
// Keep the pronoun capitalized in "I'M", "I'LL", ...
recased = recased.replace(/^i(?=['’])/u, 'I');
result += sentenceStart ? capitalize(recased) : recased;
} else {
result += word;
}
sentenceStart = false;
}
return result + text.slice(lastIndex);
}