Bookmark this: tool.io/convert/length/source-code
Convert Length Source Code
Browse the implementation files for Convert Length.tool.ts typescript
import {
convertLength as convertPortableLength,
lengthFactors,
lengthReferences
} from '@tool.io/tools/convert/length';
import { measureFactorOptions } from '../tool';
import LengthResult from './LengthResult.svelte';
// Tool: convert/length
export const convertLength = (
fromValue: number,
from: string,
to: string
) => {
const result = convertPortableLength({
fromUnit: from,
toUnit: to,
value: fromValue
});
return result.ok ? result.output.value : '';
};
const lengthOptions = (
selectedUnit: string
): TypeSelectOption[] => [
...measureFactorOptions(lengthFactors, selectedUnit).map(
(option) => ({ ...option, group: 'Length units' })
),
...Object.values(lengthReferences)
.sort(
(first, second) =>
first.group.localeCompare(second.group, 'en-US') ||
first.name.localeCompare(second.name, 'en-US')
)
.map((reference) => ({
group: reference.group,
selected: `reference:${reference.id}` === selectedUnit,
text: reference.name,
value: `reference:${reference.id}`
}))
];
export const Tool: TypeTool = {
name: 'Convert Length',
slug: 'convert/length',
tool: convertLength,
intro:
'Convert standard length units and compare distances with spacecraft, fictional starships, and animals.',
i: [
{
example: '10',
id: 'value',
label: 'Value',
placeholder: '10',
type: 'number',
valueType: { format: 'length', type: 'number' }
},
{
example: 'ft',
id: 'fromUnit',
label: 'From',
options: lengthOptions('ft'),
placeholder: 'Select the source length unit',
type: 'select',
valueType: { format: 'length-unit', type: 'string' },
value: 'ft'
},
{
example: 'm',
id: 'toUnit',
label: 'To',
options: lengthOptions('m'),
placeholder: 'Select the destination length unit',
type: 'select',
valueType: { format: 'length-unit', type: 'string' },
value: 'm'
}
],
o: [
{
example: '3.048',
id: 'value',
label: 'Length',
placeholder: 'Converted length',
render: 'custom',
renderas: LengthResult,
type: 'text',
valueType: { format: 'length', type: 'number' }
}
],
swap: {
inputs: [1, 2],
name: 'Swap length units',
state: false
},
alternative: [
{
title: 'Convert area',
url: '/convert/area'
}
],
cite: [
{
title: 'Length - Wikipedia',
url: 'https://en.wikipedia.org/wiki/Length'
}
]
};
export default Tool;
Description
Browse the implementation files for Convert Length.