ronaldkwandy

Untitled

Jul 10th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Temperature.js
  2. import { Celcius } from './Celcius'
  3. import { Fahrenheit } from './Fahrenheit'
  4. import { Kelvin } from './Kelvin';
  5.  
  6. export class Temperature {
  7.  
  8.     static unit = {
  9.         Celcius: Celcius.order,
  10.         Fahrenheit: Fahrenheit.order,
  11.         Kelvin: Kelvin.order
  12.     };
  13.  
  14.     formula = () => {
  15.         return [
  16.             [this._nothing, Celcius.toFahrenheit, Celcius.toKelvin],
  17.             [Fahrenheit.toCelcius, this._nothing, Fahrenheit.toKelvin],
  18.             [Kelvin.toCelcius, Kelvin.toFahrenheit, this._nothing]
  19.         ]
  20.     }
  21.  
  22.     static convert = (value, from, to) => {
  23.         try {
  24.             const x = new Temperature();
  25.             let formula = x.formula();
  26.             formula = formula[from][to]
  27.             if (!formula)
  28.                 throw 'No formula found!'
  29.             return formula(value);
  30.         } catch (err) {
  31.             throw err
  32.         }
  33.     }
  34.  
  35.     _nothing = (val) => { return val; }
  36. }
Add Comment
Please, Sign In to add comment