Skip to content

Commit

Permalink
style(lint): fix all eslint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 authored Apr 18, 2024
1 parent 4151309 commit 61b5ef6
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 406 deletions.
58 changes: 33 additions & 25 deletions src/app/converter/bases/_components/select-base.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from "~/shared/components/select";
import { Label } from "~/shared/components/label";
import { BASE_LABELS, Base } from '../_lib/convert-bases';
import {
Select,
SelectTrigger,
SelectContent,
SelectItem,
SelectValue
} from '~/shared/components/select'
import { Label } from '~/shared/components/label'
import { BASE_LABELS, Base } from '../_lib/convert-bases'

interface SelectBaseProps {
label: string,
value: Base,
onChange: (value: Base) => void,
label: string
value: Base
onChange: (value: Base) => void
}

export function SelectBase({ label, value, onChange }: SelectBaseProps) {
function handleSelectBase(value: string): void {
onChange(value as Base)
}

function handleSelectBase(value: string): void {
onChange(value as Base)
}

return (<>
<Label>{label}</Label>
<Select
value={value}
onValueChange={handleSelectBase}
>
<SelectTrigger>
<SelectValue placeholder={label} />
</SelectTrigger>
<SelectContent>
{ BASE_LABELS.map((base, i) => <SelectItem value={base.type} key={i} >{base.label}</SelectItem>)}
</SelectContent>
</Select>
</>)
}
return (
<>
<Label>{label}</Label>
<Select value={value} onValueChange={handleSelectBase}>
<SelectTrigger>
<SelectValue placeholder={label} />
</SelectTrigger>
<SelectContent>
{BASE_LABELS.map((base, i) => (
<SelectItem value={base.type} key={i}>
{base.label}
</SelectItem>
))}
</SelectContent>
</Select>
</>
)
}
144 changes: 72 additions & 72 deletions src/app/converter/bases/_lib/convert-bases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,75 @@ import { describe, it, expect } from 'bun:test'
import { convertBases } from './convert-bases'

describe('convert bases', () => {
it('convert binary to binary', () => {
let input = '010101'
let expected = '010101'
let output = convertBases(input, 'BIN', 'BIN')

expect(output).toEqual(expected)
})

it('convert binary to hex', () => {
let input = '010101'
let expected = '15'
let output = convertBases(input, 'BIN', 'HEX')

expect(output).toEqual(expected)
})

it('convert binary to decimal', () => {
let input = '010101'
let expected = '21'
let output = convertBases(input, 'BIN', 'DEC')

expect(output).toEqual(expected)
})

it('convert hex to binary', () => {
let input = '6B'
let expected = '01101011'
let output = convertBases(input, 'HEX', 'BIN')

expect(output).toEqual(expected)
})

it('convert hex to hex', () => {
let input = '6B'
let expected = '6B'
let output = convertBases(input, 'HEX', 'HEX')

expect(output).toEqual(expected)
})

it('convert hex to decimal', () => {
let input = '6B'
let expected = '107'
let output = convertBases(input, 'HEX', 'DEC')

expect(output).toEqual(expected)
})

it('convert decimal to binary', () => {
let input = '105'
let expected = '1101001'
let output = convertBases(input, 'DEC', 'BIN')

expect(output).toEqual(expected)
})

it('convert decimal to hex', () => {
let input = '105'
let expected = '69'
let output = convertBases(input, 'DEC', 'HEX')

expect(output).toEqual(expected)
})

it('convert decimal to decimal', () => {
let input = '105'
let expected = '105'
let output = convertBases(input, 'DEC', 'DEC')

expect(output).toEqual(expected)
})
})
it('convert binary to binary', () => {
let input = '010101'
let expected = '010101'
let output = convertBases(input, 'BIN', 'BIN')

expect(output).toEqual(expected)
})

it('convert binary to hex', () => {
let input = '010101'
let expected = '15'
let output = convertBases(input, 'BIN', 'HEX')

expect(output).toEqual(expected)
})

it('convert binary to decimal', () => {
let input = '010101'
let expected = '21'
let output = convertBases(input, 'BIN', 'DEC')

expect(output).toEqual(expected)
})

it('convert hex to binary', () => {
let input = '6B'
let expected = '01101011'
let output = convertBases(input, 'HEX', 'BIN')

expect(output).toEqual(expected)
})

it('convert hex to hex', () => {
let input = '6B'
let expected = '6B'
let output = convertBases(input, 'HEX', 'HEX')

expect(output).toEqual(expected)
})

it('convert hex to decimal', () => {
let input = '6B'
let expected = '107'
let output = convertBases(input, 'HEX', 'DEC')

expect(output).toEqual(expected)
})

it('convert decimal to binary', () => {
let input = '105'
let expected = '1101001'
let output = convertBases(input, 'DEC', 'BIN')

expect(output).toEqual(expected)
})

it('convert decimal to hex', () => {
let input = '105'
let expected = '69'
let output = convertBases(input, 'DEC', 'HEX')

expect(output).toEqual(expected)
})

it('convert decimal to decimal', () => {
let input = '105'
let expected = '105'
let output = convertBases(input, 'DEC', 'DEC')

expect(output).toEqual(expected)
})
})
51 changes: 25 additions & 26 deletions src/app/converter/bases/_lib/convert-bases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@ const onlyBinaryRegex = /^[01]+$/
const onlyHexRegex = /^(0x)?[0-9A-Fa-f]+$/
const onlyDecimalRegex = /^[0-9]+$/

export const BASE_LABELS: Array<{ type: Base, label: string }> = [
{ type: 'BIN', label: 'Binary' },
{ type: 'HEX', label: 'Hex' },
{ type: 'DEC', label: 'Decimal' },
export const BASE_LABELS: Array<{ type: Base; label: string }> = [
{ type: 'BIN', label: 'Binary' },
{ type: 'HEX', label: 'Hex' },
{ type: 'DEC', label: 'Decimal' }
]

function convertBaseToRadix(base: Base): number {
if (base === 'BIN') return 2
if (base === 'DEC') return 10
if (base === 'HEX') return 16
if (base === 'BIN') return 2
if (base === 'DEC') return 10
if (base === 'HEX') return 16

return 10
return 10
}

export function convertBases(input: string, from: Base, to: Base): string {
if (from === 'BIN' && !onlyBinaryRegex.test(input)) {
throw new Error('The input value must be a binary.')
}

if (from === 'BIN' && !onlyBinaryRegex.test(input)) {
throw new Error("The input value must be a binary.")
}
if (from === 'HEX' && !onlyHexRegex.test(input)) {
throw new Error('The input value must be a hex.')
}

if (from === 'HEX' && !onlyHexRegex.test(input)) {
throw new Error("The input value must be a hex.")
}
if (from === 'DEC' && !onlyDecimalRegex.test(input)) {
throw new Error('The input value must be a decimal.')
}

if (from === 'DEC' && !onlyDecimalRegex.test(input)) {
throw new Error("The input value must be a decimal.")
}
if (from === to) return input

if (from === to) return input
let converted = parseInt(input, convertBaseToRadix(from))
.toString(convertBaseToRadix(to))
.toUpperCase()

let converted = parseInt(input, convertBaseToRadix(from))
.toString(convertBaseToRadix(to))
.toUpperCase()
if (from === 'HEX' && to === 'BIN') {
converted = converted.padStart(8, '0')
}

if (from === 'HEX' && to === 'BIN') {
converted = converted.padStart(8, '0')
}

return converted
return converted
}
6 changes: 5 additions & 1 deletion src/app/converter/bases/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const metadata: Metadata = {
title: 'Convert Bases'
}

export default function ConverterBasesLayout({ children }: { children: ReactNode }) {
export default function ConverterBasesLayout({
children
}: {
children: ReactNode
}) {
return children
}
Loading

0 comments on commit 61b5ef6

Please sign in to comment.