Skip to content

Commit 8fc2d72

Browse files
committed
Merge branch 'feat/smart-raw-errors-conv' into chore/all-my-stuffs
# Conflicts: # components.d.ts # src/tools/index.ts
2 parents d0385ea + 7a83b35 commit 8fc2d72

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { tool as passphraseGenerator } from './passphrase-generator';
3434
import { tool as pinCodeGenerator } from './pin-code-generator';
3535
import { tool as punycodeConverter } from './punycode-converter';
3636
import { tool as rsaEcdsaSigning } from './rsa-ecdsa-signing';
37+
import { tool as smartRawConverter } from './smart-raw-converter';
3738

3839
import { tool as cssXpathConverter } from './css-xpath-converter';
3940
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -232,6 +233,7 @@ export const toolsByCategory: ToolCategory[] = [
232233
jsonToGo,
233234
markdownTocGenerator,
234235
rmbNumbers,
236+
smartRawConverter,
235237
],
236238
},
237239
{
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)