UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

40 lines (39 loc) 1.43 kB
import { $Area } from './area.js'; import { $BaseConverter } from './base.js'; import { UNITS } from './constants.js'; import { $Data } from './data.js'; import { $Length } from './length.js'; import { $Mass } from './mass.js'; import { $Temperature } from './temp.js'; import { $Time } from './time.js'; import { $Volume } from './volume.js'; export function Converter(value, unit) { const category = (() => { if (unit) { for (const [category, values] of Object.entries(UNITS)) { if ([...values].includes(unit)) { return category; } } } })(); switch (category) { case 'area': return new $Area(value, unit); case 'time': return new $Time(value, unit); case 'data': return new $Data(value, unit); case 'length': return new $Length(value, unit); case 'mass': return new $Mass(value, unit); case 'temp': return new $Temperature(value, unit); case 'volume': return new $Volume(value, unit); default: return new $BaseConverter(value, unit); } } export { $Area as AreaConverter, $Data as DataConverter, $Length as LengthConverter, $Mass as MassConverter, $Temperature as TemperatureConverter, $Time as TimeConverter, $Volume as VolumeConverter, Converter as converter, };