Compare commits

..
Author SHA1 Message Date
sharevb e6cb108ae4 Merge branch 'main' into feat/text-stats-enh 2024-08-25 22:11:10 +02:00
ShareVB 1cf54a84a4 feat: add unique words count and read time
Fix #1214
2024-08-25 22:06:55 +02:00
ShareVB af1013da36 feat(Text Statistics): add more stats
Sentences, Words with punctuations, Chars by type (upper, lower, digits, puncts...)
2024-04-28 14:33:14 +02:00
sharevb 2d87aaf484 feat: refactor text stats to service 2024-04-28 14:05:08 +02:00
9 changed files with 228 additions and 121 deletions

No files matched your search

+8 -2
View File
@@ -132,15 +132,22 @@ declare module '@vue/runtime-core' {
NCode: typeof import('naive-ui')['NCode']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NForm: typeof import('naive-ui')['NForm']
NFormItem: typeof import('naive-ui')['NFormItem']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSpace: typeof import('naive-ui')['NSpace']
NSpin: typeof import('naive-ui')['NSpin']
NStatistic: typeof import('naive-ui')['NStatistic']
NSlider: typeof import('naive-ui')['NSlider']
NSwitch: typeof import('naive-ui')['NSwitch']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
@@ -148,7 +155,6 @@ declare module '@vue/runtime-core' {
PdfSignatureDetails: typeof import('./src/tools/pdf-signature-checker/components/pdf-signature-details.vue')['default']
PercentageCalculator: typeof import('./src/tools/percentage-calculator/percentage-calculator.vue')['default']
PhoneParserAndFormatter: typeof import('./src/tools/phone-parser-and-formatter/phone-parser-and-formatter.vue')['default']
PunycodeConverter: typeof import('./src/tools/punycode-converter/punycode-converter.vue')['default']
QrCodeGenerator: typeof import('./src/tools/qr-code-generator/qr-code-generator.vue')['default']
RandomPortGenerator: typeof import('./src/tools/random-port-generator/random-port-generator.vue')['default']
ResultRow: typeof import('./src/tools/ipv4-range-expander/result-row.vue')['default']
-2
View File
@@ -42,7 +42,6 @@
"@tiptap/starter-kit": "2.1.6",
"@tiptap/vue-3": "2.0.3",
"@types/figlet": "^1.5.8",
"@types/punycode": "^2.1.4",
"@vicons/material": "^0.12.0",
"@vicons/tabler": "^0.12.0",
"@vueuse/core": "^10.3.0",
@@ -82,7 +81,6 @@
"pdf-signature-reader": "^1.4.2",
"pinia": "^2.0.34",
"plausible-tracker": "^0.3.8",
"punycode": "^2.3.1",
"qrcode": "^1.5.1",
"sql-formatter": "^13.0.0",
"ua-parser-js": "^1.0.35",
+16 -20
View File
@@ -26,9 +26,6 @@ dependencies:
'@types/figlet':
specifier: ^1.5.8
version: 1.5.8
'@types/punycode':
specifier: ^2.1.4
version: 2.1.4
'@vicons/material':
specifier: ^0.12.0
version: 0.12.0
@@ -146,9 +143,6 @@ dependencies:
plausible-tracker:
specifier: ^0.3.8
version: 0.3.8
punycode:
specifier: ^2.3.1
version: 2.3.1
qrcode:
specifier: ^1.5.1
version: 1.5.1
@@ -3038,10 +3032,6 @@ packages:
resolution: {integrity: sha512-ZREFYlpUmPQJ0esjxoG1fMvB2HNaD3z+mjqdSosZvd3RalncI9NEur73P8ZJz4YQdL64CmV1w0RuqoRUlhQRBw==}
dev: true
/@types/punycode@2.1.4:
resolution: {integrity: sha512-trzh6NzBnq8yw5e35f8xe8VTYjqM3NE7bohBtvDVf/dtUer3zYTLK1Ka3DG3p7bdtoaOHZucma6FfVKlQ134pQ==}
dev: false
/@types/qrcode@1.5.0:
resolution: {integrity: sha512-x5ilHXRxUPIMfjtM+1vf/GPTRWZ81nqscursm5gMznJeK9M0YnZ1c3bEvRLQ0zSSgedLx1J6MGL231ObQGGhaA==}
dependencies:
@@ -3370,7 +3360,7 @@ packages:
dependencies:
'@unhead/dom': 0.5.1
'@unhead/schema': 0.5.1
'@vueuse/shared': 11.0.3(vue@3.3.4)
'@vueuse/shared': 10.11.1(vue@3.3.4)
unhead: 0.5.1
vue: 3.3.4
transitivePeerDependencies:
@@ -4003,19 +3993,19 @@ packages:
- vue
dev: false
/@vueuse/shared@10.3.0(vue@3.3.4):
resolution: {integrity: sha512-kGqCTEuFPMK4+fNWy6dUOiYmxGcUbtznMwBZLC1PubidF4VZY05B+Oht7Jh7/6x4VOWGpvu3R37WHi81cKpiqg==}
/@vueuse/shared@10.11.1(vue@3.3.4):
resolution: {integrity: sha512-LHpC8711VFZlDaYUXEBbFBCQ7GS3dVU9mjOhhMhXP6txTV4EhYQg/KGnQuvt/sPAtoUKq7VVUnL6mVtFoL42sA==}
dependencies:
vue-demi: 0.14.5(vue@3.3.4)
vue-demi: 0.14.10(vue@3.3.4)
transitivePeerDependencies:
- '@vue/composition-api'
- vue
dev: false
/@vueuse/shared@11.0.3(vue@3.3.4):
resolution: {integrity: sha512-0rY2m6HS5t27n/Vp5cTDsKTlNnimCqsbh/fmT2LgE+aaU42EMfXo8+bNX91W9I7DDmxfuACXMmrd7d79JxkqWA==}
/@vueuse/shared@10.3.0(vue@3.3.4):
resolution: {integrity: sha512-kGqCTEuFPMK4+fNWy6dUOiYmxGcUbtznMwBZLC1PubidF4VZY05B+Oht7Jh7/6x4VOWGpvu3R37WHi81cKpiqg==}
dependencies:
vue-demi: 0.14.10(vue@3.3.4)
vue-demi: 0.14.5(vue@3.3.4)
transitivePeerDependencies:
- '@vue/composition-api'
- vue
@@ -7692,9 +7682,15 @@ packages:
resolution: {integrity: sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==}
dev: true
/punycode@2.3.0:
resolution: {integrity: sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==}
engines: {node: '>=6'}
dev: true
/punycode@2.3.1:
resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==}
engines: {node: '>=6'}
dev: true
/qrcode@1.5.1:
resolution: {integrity: sha512-nS8NJ1Z3md8uTjKtP+SGGhfqmTCs5flU/xR623oI0JX+Wepz9R8UrRVCTBTJm3qGw3rH6jJ6MUHjkDx15cxSSg==}
@@ -8484,7 +8480,7 @@ packages:
engines: {node: '>=6'}
dependencies:
psl: 1.9.0
punycode: 2.3.1
punycode: 2.3.0
universalify: 0.2.0
url-parse: 1.5.10
dev: true
@@ -8499,7 +8495,7 @@ packages:
resolution: {integrity: sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==}
engines: {node: '>=14'}
dependencies:
punycode: 2.3.1
punycode: 2.3.0
dev: true
/treemate@0.3.11:
@@ -8939,7 +8935,7 @@ packages:
/uri-js@4.4.1:
resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
dependencies:
punycode: 2.3.1
punycode: 2.3.0
dev: true
/url-parse@1.5.10:
+1 -11
View File
@@ -2,7 +2,6 @@ import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as emailNormalizer } from './email-normalizer';
import { tool as punycodeConverter } from './punycode-converter';
import { tool as asciiTextDrawer } from './ascii-text-drawer';
@@ -159,16 +158,7 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Network',
components: [
ipv4SubnetCalculator,
ipv4AddressConverter,
// ipv6AddressConverter,
ipv4RangeExpander,
macAddressLookup,
macAddressGenerator,
ipv6UlaGenerator,
punycodeConverter,
],
components: [ipv4SubnetCalculator, ipv4AddressConverter, ipv4RangeExpander, macAddressLookup, macAddressGenerator, ipv6UlaGenerator],
},
{
name: 'Math',
-12
View File
@@ -1,12 +0,0 @@
import { World } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Punycode Converter',
path: '/punycode-converter',
description: 'Convert international unicode domain names or emails from/to ASCII Punycode version',
keywords: ['punycode', 'converter', 'rfc3492', 'bootstring', 'domain', 'dns'],
component: () => import('./punycode-converter.vue'),
icon: World,
createdAt: new Date('2024-08-15'),
});
@@ -1,66 +0,0 @@
<script setup lang="ts">
import { toASCII, toUnicode } from 'punycode/';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
const punycodeInput = ref('');
const intlOutput = computed(
() => {
try {
return toUnicode(punycodeInput.value?.trim());
}
catch (e: any) {
return e.toString();
}
},
);
const intlInput = ref('');
const punycodeOutput = computed(
() => {
try {
return toASCII(intlInput.value?.trim());
}
catch (e: any) {
return e.toString();
}
},
);
</script>
<template>
<div max-w-600>
<c-card title="Punycode to International">
<c-input-text
v-model:value="punycodeInput"
placeholder="Put your punycode domain name or email to decode..."
label="Punycode Domain name/Email"
raw-text
/>
<n-divider />
<TextareaCopyable
label="Decoded Domain name/Email"
:value="intlOutput"
readonly
/>
</c-card>
<c-card title="International to Punycode" mt-5>
<c-input-text
v-model:value="intlInput"
placeholder="Put your international domain or email name here..."
label="Domain name or email to encode"
raw-text
/>
<n-divider />
<TextareaCopyable
label="Punycode Domain name/Email"
:value="punycodeOutput"
readonly
/>
</c-card>
</div>
</template>
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { getStringSizeInBytes } from './text-statistics.service';
import { getStringSizeInBytes, textStatistics } from './text-statistics.service';
describe('text-statistics', () => {
describe('getStringSizeInBytes', () => {
@@ -11,4 +11,155 @@ describe('text-statistics', () => {
expect(getStringSizeInBytes('aaaaaaaaaa')).toEqual(10);
});
});
describe('textStatistics', () => {
it('should return text statistics', () => {
expect(textStatistics('a')).to.deep.eq({
chars: 1,
chars_digits: 0,
chars_lower: 1,
chars_no_spaces: 1,
chars_puncts: 0,
chars_spaces: 0,
chars_upper: 0,
lines: 1,
sentences: 1,
words: 1,
words_no_puncs: 1,
read_time: 0.3,
words_uniques: 1,
words_uniques_ci: 1,
});
expect(textStatistics('A')).to.deep.eq({
chars: 1,
chars_digits: 0,
chars_lower: 0,
chars_no_spaces: 1,
chars_puncts: 0,
chars_spaces: 0,
chars_upper: 1,
lines: 1,
sentences: 1,
words: 1,
words_no_puncs: 1,
read_time: 0.3,
words_uniques: 1,
words_uniques_ci: 1,
});
expect(textStatistics('a a')).to.deep.eq({
chars: 3,
chars_digits: 0,
chars_lower: 2,
chars_no_spaces: 2,
chars_puncts: 0,
chars_spaces: 1,
chars_upper: 0,
lines: 1,
sentences: 1,
words: 2,
words_no_puncs: 2,
read_time: 0.6,
words_uniques: 1,
words_uniques_ci: 1,
});
expect(textStatistics('A a ; 1')).to.deep.eq({
chars: 7,
chars_digits: 1,
chars_lower: 1,
chars_no_spaces: 4,
chars_puncts: 1,
chars_spaces: 3,
chars_upper: 1,
lines: 1,
sentences: 1,
words: 4,
words_no_puncs: 3,
read_time: 0.8999999999999999,
words_uniques: 3,
words_uniques_ci: 2,
});
expect(textStatistics('Some sentence! Une autre phrase ? « et avec des chiffres 1234 ! »')).to.deep.eq({
chars: 65,
chars_digits: 4,
chars_lower: 41,
chars_no_spaces: 52,
chars_puncts: 5,
chars_spaces: 13,
chars_upper: 2,
lines: 1,
sentences: 3,
words: 14,
words_no_puncs: 10,
read_time: 3,
words_uniques: 10,
words_uniques_ci: 10,
});
expect(textStatistics(`Some sentence! Une autre phrase ?
« et avec des chiffres 1234 ! »`)).to.deep.eq({
chars: 72,
chars_digits: 4,
chars_lower: 41,
chars_no_spaces: 52,
chars_puncts: 5,
chars_spaces: 20,
chars_upper: 2,
lines: 2,
sentences: 3,
words: 14,
words_no_puncs: 10,
read_time: 3,
words_uniques: 10,
words_uniques_ci: 10,
});
expect(textStatistics('12 35')).to.deep.eq({
chars: 5,
chars_digits: 4,
chars_lower: 0,
chars_no_spaces: 4,
chars_puncts: 0,
chars_spaces: 1,
chars_upper: 0,
lines: 1,
sentences: 1,
words: 2,
words_no_puncs: 2,
read_time: 0.6,
words_uniques: 2,
words_uniques_ci: 2,
});
expect(textStatistics(' 1 2 3. Other ')).to.deep.eq({
chars: 14,
chars_digits: 3,
chars_lower: 4,
chars_no_spaces: 9,
chars_puncts: 1,
chars_spaces: 5,
chars_upper: 1,
lines: 1,
sentences: 2,
words: 4,
words_no_puncs: 4,
read_time: 1.2,
words_uniques: 4,
words_uniques_ci: 4,
});
expect(textStatistics('Az az er')).to.deep.eq({
chars: 8,
chars_digits: 0,
chars_lower: 5,
chars_no_spaces: 6,
chars_puncts: 0,
chars_spaces: 2,
chars_upper: 1,
lines: 1,
read_time: 0.8999999999999999,
sentences: 1,
words: 3,
words_no_puncs: 3,
words_uniques: 3,
words_uniques_ci: 2,
});
});
});
});
@@ -1,3 +1,24 @@
export function getStringSizeInBytes(text: string) {
return new TextEncoder().encode(text).buffer.byteLength;
}
export function textStatistics(text: string) {
const words_no_puncts = text.replace(/\p{P}/ug, '').trim().split(/\s+/);
const read_word_per_minutes = 200;
return {
chars: text.length,
chars_no_spaces: text.replace(/\s+/ug, '').length,
chars_upper: text.replace(/[^\p{Lu}]/ug, '').length,
chars_lower: text.replace(/[^\p{Ll}]/ug, '').length,
chars_digits: text.replace(/\D+/ug, '').length,
chars_puncts: text.replace(/[^\p{P}]/ug, '').length,
chars_spaces: text.replace(/\S/ug, '').length,
words: text.trim().split(/\s+/).length,
read_time: words_no_puncts.length / read_word_per_minutes * 60,
words_no_puncs: words_no_puncts.length,
words_uniques: (new Set(words_no_puncts)).size,
words_uniques_ci: (new Set(words_no_puncts.map(s => s.toLowerCase()))).size,
sentences: (`${text} `).split(/\w\s*[\.!\?][\s\p{P}]*\s/u).filter(s => s && s?.length > 0).length,
lines: text.split(/\r\n|\r|\n/).length,
};
}
+30 -7
View File
@@ -1,19 +1,42 @@
<script setup lang="ts">
import { getStringSizeInBytes } from './text-statistics.service';
import { formatMsDuration } from '../eta-calculator/eta-calculator.service';
import { getStringSizeInBytes, textStatistics } from './text-statistics.service';
import { formatBytes } from '@/utils/convert';
const text = ref('');
const stats = computed(() => textStatistics(text.value));
</script>
<template>
<c-card>
<c-input-text v-model:value="text" multiline placeholder="Your text..." rows="5" />
<div mt-5 flex>
<n-statistic label="Character count" :value="text.length" flex-1 />
<n-statistic label="Word count" :value="text === '' ? 0 : text.split(/\s+/).length" flex-1 />
<n-statistic label="Line count" :value="text === '' ? 0 : text.split(/\r\n|\r|\n/).length" flex-1 />
<n-statistic label="Byte size" :value="formatBytes(getStringSizeInBytes(text))" flex-1 />
</div>
<n-space mt-3>
<n-statistic label="Character count" :value="stats.chars" />
<n-statistic label="Word count" :value="stats.words" />
<n-statistic label="Sentences count" :value="stats.sentences" />
<n-statistic label="Line count" :value="stats.lines" />
<n-statistic label="Byte size" :value="formatBytes(getStringSizeInBytes(text))" />
</n-space>
<n-divider />
<n-space mt-3>
<n-statistic label="Unique Word count" :value="stats.words_uniques" />
<n-statistic label="Unique Word count (case insensitive)" :value="stats.words_uniques_ci" />
<n-statistic label="Read Time" :value="formatMsDuration(stats.read_time * 1000)" />
</n-space>
<n-divider />
<n-space>
<n-statistic label="Chars (no spaces)" :value="stats.chars_no_spaces" />
<n-statistic label="Uppercase chars" :value="stats.chars_upper" />
<n-statistic label="Lowercase chars" :value="stats.chars_lower" />
<n-statistic label="Digit chars" :value="stats.chars_digits" />
<n-statistic label="Punctuations" :value="stats.chars_puncts" />
<n-statistic label="Spaces chars" :value="stats.chars_spaces" />
<n-statistic label="Word count (no punct)" :value="stats.words_no_puncs" />
</n-space>
</c-card>
</template>