Skip to content

Commit 7a83b35

Browse files
committed
feat(new tool): Smart Raw Error Converter
Fix CorentinTh#1197
1 parent 318fb6e commit 7a83b35

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

components.d.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,15 @@ declare module '@vue/runtime-core' {
129129
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
130130
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
131131
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
132-
NCode: typeof import('naive-ui')['NCode']
133132
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
134133
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
135134
NEllipsis: typeof import('naive-ui')['NEllipsis']
136-
NForm: typeof import('naive-ui')['NForm']
137-
NFormItem: typeof import('naive-ui')['NFormItem']
138135
NH1: typeof import('naive-ui')['NH1']
139136
NH3: typeof import('naive-ui')['NH3']
140137
NIcon: typeof import('naive-ui')['NIcon']
141-
NInputNumber: typeof import('naive-ui')['NInputNumber']
142138
NLayout: typeof import('naive-ui')['NLayout']
143139
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
144140
NMenu: typeof import('naive-ui')['NMenu']
145-
NScrollbar: typeof import('naive-ui')['NScrollbar']
146-
NSlider: typeof import('naive-ui')['NSlider']
147-
NSwitch: typeof import('naive-ui')['NSwitch']
148141
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
149142
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
150143
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']
@@ -161,6 +154,7 @@ declare module '@vue/runtime-core' {
161154
RsaKeyPairGenerator: typeof import('./src/tools/rsa-key-pair-generator/rsa-key-pair-generator.vue')['default']
162155
SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default']
163156
SlugifyString: typeof import('./src/tools/slugify-string/slugify-string.vue')['default']
157+
SmartRawConverter: typeof import('./src/tools/smart-raw-converter/smart-raw-converter.vue')['default']
164158
SpanCopyable: typeof import('./src/components/SpanCopyable.vue')['default']
165159
SqlPrettify: typeof import('./src/tools/sql-prettify/sql-prettify.vue')['default']
166160
StringObfuscator: typeof import('./src/tools/string-obfuscator/string-obfuscator.vue')['default']

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
44
import { tool as emailNormalizer } from './email-normalizer';
5+
import { tool as smartRawConverter } from './smart-raw-converter';
56

67
import { tool as asciiTextDrawer } from './ascii-text-drawer';
78

@@ -112,6 +113,7 @@ export const toolsByCategory: ToolCategory[] = [
112113
tomlToYaml,
113114
xmlToJson,
114115
jsonToXml,
116+
smartRawConverter,
115117
],
116118
},
117119
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Disc } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'SMART Raw Converter',
6+
path: '/smart-raw-converter',
7+
description: 'Convert SMART Raw values to human readable',
8+
keywords: ['smart', 'raw', 'converter'],
9+
component: () => import('./smart-raw-converter.vue'),
10+
icon: Disc,
11+
createdAt: new Date('2024-07-14'),
12+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { getSmartValue } from './smart-raw-converter.service';
3+
4+
describe('smart-raw-converter', () => {
5+
describe('getSmartValue', () => {
6+
it('to return correct values', () => {
7+
expect(getSmartValue(8590065666)).to.deep.eq({
8+
errors: 2,
9+
operations: 131074,
10+
});
11+
});
12+
});
13+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function getSmartValue(raw: number) {
2+
const n = raw.toString(2);
3+
let bin = '';
4+
for (let i = 0; i < 64 - n.length; ++i) {
5+
bin += '0';
6+
}
7+
bin += n;
8+
const lo = Number.parseInt(bin.slice(0, 32), 2);
9+
const hi = Number.parseInt(bin.slice(32), 2);
10+
return {
11+
errors: lo,
12+
operations: hi,
13+
};
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<script setup lang="ts">
2+
import { getSmartValue } from './smart-raw-converter.service';
3+
import { useValidation } from '@/composable/validation';
4+
5+
const inputRegex = /^(?:0x(?<hex>[a-f\d]+)|(?<dec>\d+))$/i;
6+
const rawValue = ref('0xFE45E3');
7+
const rawValueValidation = useValidation({
8+
source: rawValue,
9+
rules: [
10+
{
11+
validator: (v) => {
12+
return inputRegex.test(v?.trim());
13+
},
14+
message: 'Smart Raw Value must be decimal or "0x" hexadecimal.',
15+
},
16+
],
17+
});
18+
const smartDecodedValue = computed(() => {
19+
if (!rawValueValidation.isValid) {
20+
return null;
21+
}
22+
const inputMatch = rawValue.value.match(inputRegex);
23+
const rawValueInt = inputMatch?.groups?.hex
24+
? Number.parseInt(inputMatch?.groups?.hex, 16)
25+
: Number.parseInt(inputMatch?.groups?.dec || '0', 10);
26+
return getSmartValue(rawValueInt);
27+
});
28+
</script>
29+
30+
<template>
31+
<div style="max-width: 600px;">
32+
<c-input-text
33+
v-model:value="rawValue"
34+
label="Smart Raw Value"
35+
placeholder="Put your Smart Raw Value (decimal or '0x' hexa)"
36+
:validation="rawValueValidation"
37+
mb-2
38+
/>
39+
40+
<c-card v-if="smartDecodedValue" title="Decoded">
41+
<strong>{{ smartDecodedValue.errors }}</strong> in {{ smartDecodedValue.operations }} operations
42+
</c-card>
43+
</div>
44+
</template>

0 commit comments

Comments
 (0)