varna / measure-ts

Format length or area units to human readable Strings.
MIT License
5 stars 1 forks source link

Idea: MultiMeasure (m/s, kg/ha etc.) #5

Open varna opened 5 years ago

varna commented 5 years ago

Idea: type of Measure, that could handle multi dimensional units of different types.

const distance = new Measure(1, 'km')
const time = new Measure(1, 'h')
const speed = new MultiMeasure(distance, time)
String(speed) // "1 km/h"
speed.to('m/s') // "0.28 m/s"
varna commented 5 years ago
const distance = new Measure(1, 'km')
const time = new Measure(1, 'h')
const speed = distance / time // new MultiMeasure(distance, time)
const distance = new Measure(1, 'km')
const moreDistance = distance * 10 // new Measure(10, 'km')
const area = distance * distance // new Measure(1, "km2")

This would be cool. Don't know if possible, should do a research on how operators work in JS.

varna commented 5 years ago

It seems that JavaScript Operator Overloading is impossible.

varna commented 5 years ago

Workaround with methods:

const distance = new Measure(1, 'km')
const area = distance.multiply(distance)  // new Measure(1, "km2")
const distance = new Measure(1, 'km')
const time = new Measure(1, 'h')
const speed = distance.divide(time) // new Measure(1, "km/h")