File tree Expand file tree Collapse file tree 3 files changed +128
-1
lines changed
src/tools/text-statistics Expand file tree Collapse file tree 3 files changed +128
-1
lines changed Original file line number Diff line number Diff line change @@ -166,6 +166,7 @@ declare module '@vue/runtime-core' {
166
166
RouterLink : typeof import ( 'vue-router' ) [ 'RouterLink' ]
167
167
RouterView : typeof import ( 'vue-router' ) [ 'RouterView' ]
168
168
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' ]
169
170
SlugifyString : typeof import ( './src/tools/slugify-string/slugify-string.vue' ) [ 'default' ]
170
171
SpanCopyable : typeof import ( './src/components/SpanCopyable.vue' ) [ 'default' ]
171
172
SqlPrettify : typeof import ( './src/tools/sql-prettify/sql-prettify.vue' ) [ 'default' ]
Original file line number Diff line number Diff line change 1
1
import { describe , expect , it } from 'vitest' ;
2
- import { getStringSizeInBytes } from './text-statistics.service' ;
2
+ import { getStringSizeInBytes , textStatistics } from './text-statistics.service' ;
3
3
4
4
describe ( 'text-statistics' , ( ) => {
5
5
describe ( 'getStringSizeInBytes' , ( ) => {
@@ -11,4 +11,114 @@ describe('text-statistics', () => {
11
11
expect ( getStringSizeInBytes ( 'aaaaaaaaaa' ) ) . toEqual ( 10 ) ;
12
12
} ) ;
13
13
} ) ;
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
+ } ) ;
14
124
} ) ;
Original file line number Diff line number Diff line change 1
1
export function getStringSizeInBytes ( text : string ) {
2
2
return new TextEncoder ( ) . encode ( text ) . buffer . byteLength ;
3
3
}
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
+ }
You can’t perform that action at this time.
0 commit comments