etraid_formats
v1.0.17
Published
ETRA I+D format helpers
Downloads
6
Readme
ETRA I+D Common Formats
Library for common formatting of dates and measurements accross different applications
Usage
Import
import Formats from 'etraid_formats';
Date-related formatting
Formats.locale('es');
console.log(Formats.date(new Date())); // 25/06/2018
console.log(Formats.time(new Date())); // 13:28:30
console.log(Formats.shorttime(new Date())); // 13:28
console.log(Formats.datetime(new Date())); // 25/06/2018 13:28:30
console.log(Formats.duration(moment.duration(123, 'second'))); // 2m 3s
console.log(Formats.duration(moment.duration(123, 'minute'))); // 2h 3m 0s
console.log(Formats.duration(moment.duration(123, 'hour'))); // 5d 3h 0m 0s
console.log(Formats.duration(moment.duration(123, 'day'))); // 123d 0h 0m 0s
Date-related formats
console.log(Formats.date_format()); // L
console.log(Formats.time_format()); // HH:mm:ss
console.log(Formats.shorttime_format()); // HH:mm
console.log(Formats.datetime_format()); // L HH:mm:ss
console.log(Formats.duration_format()); // d[d] h[h] m[m] s[s]
Measurements formatting
Formats.measurement(value, units, [factor], [decimals])
value: number to be formatted
units: units of the measurement
[factor]: optional; for those cases where the value is not given in the defined units, factor that shall be applied to convert it (see examples below)
[decimals]: optional; maximum number of decimals to round to (fewer can be used)
Defined measurements
| CATEGORY | MAGNITUDE | UNIT | |--------|--------|--------| | Energy | Active power | Formats.SYMBOLS.ENERGY.W | | Energy | Reactive power | Formats.SYMBOLS.ENERGY.var | | Energy | Apparent power | Formats.SYMBOLS.ENERGY.VA | | Energy | Voltage | Formats.SYMBOLS.ENERGY.V | | Energy | Current | Formats.SYMBOLS.ENERGY.A | | Energy | Frequency | Formats.SYMBOLS.ENERGY.Hz | | Energy | Active energy | Formats.SYMBOLS.ENERGY.kWh | | Energy | Reactive energy | Formats.SYMBOLS.ENERGY.kvarh | | Emissions | CO2 | Formats.SYMBOLS.EMISSIONS.CO2 | | Distance | Distance | Formats.SYMBOLS.DISTANCE.m | | Speed | Speed (m/s) | Formats.SYMBOLS.SPEED.m_s | | Speed | Speed (km/h) | Formats.SYMBOLS.SPEED.km_h | | Weight | Weight | Formats.SYMBOLS.WEIGHT.g | | Weather | Temperature (degree Celsius) | Formats.SYMBOLS.WEATHER.degC | | Weather | Temperature (degree Fahrenheit) | Formats.SYMBOLS.WEATHER.degF | | Weather | Temperature (Kelvin) | Formats.SYMBOLS.WEATHER.K | | Weather | Atmospheric pressure (mbar) | Formats.SYMBOLS.WEATHER.mbar | | Currency | Euro (€) | Formats.SYMBOLS.CURRENCY.EUR | | Currency | Pound sterling (£) | Formats.SYMBOLS.CURRENCY.GBP | | Currency | United States dollar ($) | Formats.SYMBOLS.CURRENCY.USD | | General | Percentage | Formats.SYMBOLS.GENERAL.PERCENTAGE | | General | Degree | Formats.SYMBOLS.GENERAL.DEGREE |
Energy-related measurements formatting
console.log(Formats.measurement(20000000, Formats.SYMBOLS.ENERGY.W)); // 20 MW
console.log(Formats.measurement(20000, Formats.SYMBOLS.ENERGY.var)); // 20 kvar
console.log(Formats.measurement(200, Formats.SYMBOLS.ENERGY.VA)); // 200 VA
console.log(Formats.measurement(0.2, Formats.SYMBOLS.ENERGY.W)); // 200 mW
Distance-related measurements formatting
console.log(Formats.measurement(20, Formats.SYMBOLS.DISTANCE.m, 1000)); // 20 km
Speed-related measurements formatting
console.log(Formats.measurement(20, Formats.SYMBOLS.SPEED.m_s)); // 20 m/s
console.log(Formats.measurement(20, Formats.SYMBOLS.SPEED.km_h)); // 20 km/h
Weight-related measurements formatting
console.log(Formats.measurement(20000, Formats.SYMBOLS.WEIGHT.g, 1000)); // 20 t
Weather-related measurements formatting
console.log(Formats.measurement(20, Formats.SYMBOLS.WEATHER.degC)); // 20 °C
console.log(Formats.measurement(20, Formats.SYMBOLS.WEATHER.degF)); // 20 °F
console.log(Formats.measurement(20, Formats.SYMBOLS.WEATHER.K)); // 20 K
console.log(Formats.measurement(20, Formats.SYMBOLS.WEATHER.mbar)); // 20 mbar
Emissions-related measurements formatting
// Please note it returns HTML code
console.log(Formats.measurement(20000, Formats.SYMBOLS.EMISSIONS.CO2, 1000)); // 20 t CO<sub>2</sub>
Currency-related measurements formatting
console.log(Formats.measurement(20, Formats.SYMBOLS.CURRENCY.EUR)); // 20 €
console.log(Formats.measurement(2000000, Formats.SYMBOLS.CURRENCY.EUR)); // 2 M€
console.log(Formats.measurement(20, Formats.SYMBOLS.CURRENCY.GBP)); // £20
console.log(Formats.measurement(2000000, Formats.SYMBOLS.CURRENCY.GBP)); // £2m
console.log(Formats.measurement(20, Formats.SYMBOLS.CURRENCY.USD)); // $20
console.log(Formats.measurement(2000000, Formats.SYMBOLS.CURRENCY.USD)); // $2m
Other measurements formatting
console.log(Formats.measurement(200, Formats.SYMBOLS.GENERAL.PERCENTAGE)); // 200%
console.log(Formats.measurement(200, Formats.SYMBOLS.GENERAL.DEGREE)); // 200°
Decimals and thousands formatting
Formats.locale('es');
console.log(Formats.measurement(20200.200, Formats.SYMBOLS.CURRENCY.EUR)); // 20.200,2 €
console.log(Formats.measurement(20200.200, Formats.SYMBOLS.CURRENCY.GBP)); // £20.200,2
console.log(Formats.measurement(20200.246, Formats.SYMBOLS.DISTANCE.m, 1000)); // 20.200,25 km
Formats.locale('en');
console.log(Formats.measurement(20200.200, Formats.SYMBOLS.CURRENCY.EUR)); // 20,200.2 €
console.log(Formats.measurement(20200.200, Formats.SYMBOLS.CURRENCY.GBP)); // £20,200.2
console.log(Formats.measurement(20200.246, Formats.SYMBOLS.DISTANCE.m, 1000)); // 20,200.25 km