Skip to content

Commit 7f2bf11

Browse files
committed
feat: refactor text stats to service
1 parent b7debec commit 7f2bf11

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ declare module '@vue/runtime-core' {
166166
RouterLink: typeof import('vue-router')['RouterLink']
167167
RouterView: typeof import('vue-router')['RouterView']
168168
RsaKeyPairGenerator: typeof import('./src/tools/rsa-key-pair-generator/rsa-key-pair-generator.vue')['default']
169+
SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default']
169170
SlugifyString: typeof import('./src/tools/slugify-string/slugify-string.vue')['default']
170171
SpanCopyable: typeof import('./src/components/SpanCopyable.vue')['default']
171172
SqlPrettify: typeof import('./src/tools/sql-prettify/sql-prettify.vue')['default']

src/tools/text-statistics/text-statistics.service.test.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { getStringSizeInBytes } from './text-statistics.service';
2+
import { getStringSizeInBytes, textStatistics } from './text-statistics.service';
33

44
describe('text-statistics', () => {
55
describe('getStringSizeInBytes', () => {
@@ -11,4 +11,114 @@ describe('text-statistics', () => {
1111
expect(getStringSizeInBytes('aaaaaaaaaa')).toEqual(10);
1212
});
1313
});
14+
15+
describe('textStatistics', () => {
16+
it('should return text statistics', () => {
17+
expect(textStatistics('a')).toEqual({
18+
chars: 1,
19+
chars_digits: 0,
20+
chars_lower: 1,
21+
chars_no_spaces: 1,
22+
chars_puncts: 0,
23+
chars_spaces: 0,
24+
chars_upper: 0,
25+
lines: 1,
26+
sentences: 1,
27+
words: 1,
28+
words_no_puncs: 1,
29+
});
30+
expect(textStatistics('A')).toEqual({
31+
chars: 1,
32+
chars_digits: 0,
33+
chars_lower: 0,
34+
chars_no_spaces: 1,
35+
chars_puncts: 0,
36+
chars_spaces: 0,
37+
chars_upper: 1,
38+
lines: 1,
39+
sentences: 1,
40+
words: 1,
41+
words_no_puncs: 1,
42+
});
43+
expect(textStatistics('a a')).toEqual({
44+
chars: 3,
45+
chars_digits: 0,
46+
chars_lower: 2,
47+
chars_no_spaces: 2,
48+
chars_puncts: 0,
49+
chars_spaces: 1,
50+
chars_upper: 0,
51+
lines: 1,
52+
sentences: 1,
53+
words: 2,
54+
words_no_puncs: 2,
55+
});
56+
expect(textStatistics('A a ; 1')).toEqual({
57+
chars: 7,
58+
chars_digits: 1,
59+
chars_lower: 1,
60+
chars_no_spaces: 4,
61+
chars_puncts: 1,
62+
chars_spaces: 3,
63+
chars_upper: 1,
64+
lines: 1,
65+
sentences: 1,
66+
words: 4,
67+
words_no_puncs: 3,
68+
});
69+
expect(textStatistics('Some sentence! Une autre phrase ? « et avec des chiffres 1234 ! »')).toEqual({
70+
chars: 65,
71+
chars_digits: 4,
72+
chars_lower: 41,
73+
chars_no_spaces: 52,
74+
chars_puncts: 5,
75+
chars_spaces: 13,
76+
chars_upper: 2,
77+
lines: 1,
78+
sentences: 3,
79+
words: 14,
80+
words_no_puncs: 10,
81+
});
82+
expect(textStatistics(`Some sentence! Une autre phrase ?
83+
« et avec des chiffres 1234 ! »`)).toEqual({
84+
chars: 72,
85+
chars_digits: 4,
86+
chars_lower: 41,
87+
chars_no_spaces: 52,
88+
chars_puncts: 5,
89+
chars_spaces: 20,
90+
chars_upper: 2,
91+
lines: 2,
92+
sentences: 3,
93+
words: 14,
94+
words_no_puncs: 10,
95+
});
96+
expect(textStatistics('12 35')).toEqual({
97+
chars: 5,
98+
chars_digits: 4,
99+
chars_lower: 0,
100+
chars_no_spaces: 4,
101+
chars_puncts: 0,
102+
chars_spaces: 1,
103+
chars_upper: 0,
104+
lines: 1,
105+
sentences: 1,
106+
words: 2,
107+
words_no_puncs: 2,
108+
});
109+
expect(textStatistics(' 1 2 3. Other ')).toEqual({
110+
chars: 14,
111+
chars_digits: 3,
112+
chars_lower: 4,
113+
chars_no_spaces: 9,
114+
chars_puncts: 1,
115+
chars_spaces: 5,
116+
chars_upper: 1,
117+
lines: 1,
118+
sentences: 2,
119+
words: 4,
120+
words_no_puncs: 4,
121+
});
122+
});
123+
});
14124
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
export function getStringSizeInBytes(text: string) {
22
return new TextEncoder().encode(text).buffer.byteLength;
33
}
4+
5+
export function textStatistics(text: string) {
6+
return {
7+
chars: text.length,
8+
chars_no_spaces: text.replace(/\s+/ug, '').length,
9+
chars_upper: text.replace(/[^\p{Lu}]/ug, '').length,
10+
chars_lower: text.replace(/[^\p{Ll}]/ug, '').length,
11+
chars_digits: text.replace(/\D+/ug, '').length,
12+
chars_puncts: text.replace(/[^\p{P}]/ug, '').length,
13+
chars_spaces: text.replace(/\S/ug, '').length,
14+
words: text.trim().split(/\s+/).length,
15+
words_no_puncs: text.replace(/\p{P}/ug, '').trim().split(/\s+/).length,
16+
sentences: (`${text} `).split(/\w\s*[\.!\?][\s\p{P}]*\s/u).filter(s => s && s?.length > 0).length,
17+
lines: text.split(/\r\n|\r|\n/).length,
18+
};
19+
}

0 commit comments

Comments
 (0)