forked from CorentinTh/it-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new tool): Smart Raw Error Converter
Fix CorentinTh#1197
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Disc } from '@vicons/tabler'; | ||
import { defineTool } from '../tool'; | ||
|
||
export const tool = defineTool({ | ||
name: 'SMART Raw Converter', | ||
path: '/smart-raw-converter', | ||
description: 'Convert SMART Raw values to human readable', | ||
keywords: ['smart', 'raw', 'converter'], | ||
component: () => import('./smart-raw-converter.vue'), | ||
icon: Disc, | ||
createdAt: new Date('2024-07-14'), | ||
}); |
13 changes: 13 additions & 0 deletions
13
src/tools/smart-raw-converter/smart-raw-converter.service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { getSmartValue } from './smart-raw-converter.service'; | ||
|
||
describe('smart-raw-converter', () => { | ||
describe('getSmartValue', () => { | ||
it('to return correct values', () => { | ||
expect(getSmartValue(8590065666)).to.deep.eq({ | ||
errors: 2, | ||
operations: 131074, | ||
}); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
src/tools/smart-raw-converter/smart-raw-converter.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function getSmartValue(raw: number) { | ||
const n = raw.toString(2); | ||
let bin = ''; | ||
for (let i = 0; i < 64 - n.length; ++i) { | ||
bin += '0'; | ||
} | ||
bin += n; | ||
const lo = Number.parseInt(bin.slice(0, 32), 2); | ||
const hi = Number.parseInt(bin.slice(32), 2); | ||
return { | ||
errors: lo, | ||
operations: hi, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<script setup lang="ts"> | ||
import { getSmartValue } from './smart-raw-converter.service'; | ||
import { useValidation } from '@/composable/validation'; | ||
const inputRegex = /^(?:0x(?<hex>[a-f\d])|(?<dec>\d+))$/i; | ||
const rawValue = ref('0xFE45E3'); | ||
const rawValueValidation = useValidation({ | ||
source: rawValue, | ||
rules: [ | ||
{ | ||
validator: (v) => { | ||
return inputRegex.test(v); | ||
}, | ||
message: 'Smart Raw Value must be decimal or "0x" hexadecimal.', | ||
}, | ||
], | ||
}); | ||
const smartDecodedValue = computed(() => { | ||
if (!rawValueValidation.isValid) { | ||
return null; | ||
} | ||
const inputMatch = rawValue.value.match(inputRegex); | ||
const rawValueInt = inputMatch?.groups?.hex | ||
? Number.parseInt(inputMatch?.groups?.hex, 16) | ||
: Number.parseInt(inputMatch?.groups?.dec || '0', 10); | ||
return getSmartValue(rawValueInt); | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div style="max-width: 600px;"> | ||
<c-input-text | ||
v-model:value="rawValue" | ||
label="Smart Raw Value" | ||
placeholder="Put your Smart Raw Value (decimal or '0x' hexa)" | ||
:validation="rawValueValidation" | ||
mb-2 | ||
/> | ||
|
||
<c-card v-if="smartDecodedValue" title="Decoded"> | ||
<strong>{{ smartDecodedValue.errors }}</strong> in {{ smartDecodedValue.operations }} operations | ||
</c-card> | ||
</div> | ||
</template> |