Skip to content

Instantly share code, notes, and snippets.

@roziscoding
Created March 12, 2019 20:00
Show Gist options
  • Save roziscoding/8af132d56fdb47a0d1dc7186e4231e8b to your computer and use it in GitHub Desktop.
Save roziscoding/8af132d56fdb47a0d1dc7186e4231e8b to your computer and use it in GitHub Desktop.
Unit converting with pure functions and a factory
export const fromCToK = (value: number) => value * 1.8 + 32
export const fromCToF = (value: number) => value + 273.15
export const fromFToC = (value: number) => (value - 32) / 1.8
export const fromFToK = (value: number) => fromFToK(value) + 273.15
export const fromKToC = (value: number) => value - 273.15
export const fromKToF = (value: number) => (value * 1.8) - 459.67
export const convert = (value: number) => ({
from: {
c: {
to: {
k: () => fromCToK(value),
f: () => fromCToF(value),
}
},
f: {
to: {
c: () => fromFToC(value),
k: () => fromFToK(value)
}
},
k: {
c: () => fromKToC(value),
f: () => fromKToF(value)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment