UNPKG

cbi-units-converter

Version:

Forked from units-converter. A simple utility library to measure and convert between units

412 lines (365 loc) 10.8 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var acceleration_js = _interopDefault(require('./units/acceleration.js')); var allUnits_js = _interopDefault(require('./units/allUnits.js')); var angle_js = _interopDefault(require('./units/angle.js')); var apparentPower_js = _interopDefault(require('./units/apparentPower.js')); var area_js = _interopDefault(require('./units/area.js')); var charge_js = _interopDefault(require('./units/charge.js')); var current_js = _interopDefault(require('./units/current.js')); var digital_js = _interopDefault(require('./units/digital.js')); var each_js = _interopDefault(require('./units/each.js')); var energy_js = _interopDefault(require('./units/energy.js')); var force_js = _interopDefault(require('./units/force.js')); var frequency_js = _interopDefault(require('./units/frequency.js')); var illuminance_js = _interopDefault(require('./units/illuminance.js')); var length_js = _interopDefault(require('./units/length.js')); var mass_js = _interopDefault(require('./units/mass.js')); var pace_js = _interopDefault(require('./units/pace.js')); var partsPer_js = _interopDefault(require('./units/partsPer.js')); var power_js = _interopDefault(require('./units/power.js')); var pressure_js = _interopDefault(require('./units/pressure.js')); var reactiveEnergy_js = _interopDefault(require('./units/reactiveEnergy.js')); var reactivePower_js = _interopDefault(require('./units/reactivePower.js')); var salinity_js = _interopDefault(require('./units/salinity.js')); var speed_js = _interopDefault(require('./units/speed.js')); var sound_js = _interopDefault(require('./units/sound.js')); var temperature_js = _interopDefault(require('./units/temperature.js')); var time_js = _interopDefault(require('./units/time.js')); var voltage_js = _interopDefault(require('./units/voltage.js')); var volume_js = _interopDefault(require('./units/volume.js')); var volumeFlowRate_js = _interopDefault(require('./units/volumeFlowRate.js')); const Converter = function (numerator, definitions) { this.definitions = definitions; this.val = numerator; }; Converter.prototype.from = function (from) { if (this.destination) { throw new Error('.from must be called before .to') } this.origin = this.getUnit(from); if (!this.origin) { this.throwUnsupportedUnitError(from); } return this }; Converter.prototype.to = function (to, precision = null) { if (!this.origin) { throw new Error('.to must be called after .from') } this.destination = this.getUnit(to); let result; if (!this.destination) { this.throwUnsupportedUnitError(to); } if (this.origin.abbr === this.destination.abbr) { return Object.assign({ value: this.val }, this.describe(this.destination.abbr)) } result = this.val * this.origin.unit.to_anchor; if (this.origin.unit.anchor_shift) { result -= this.origin.unit.anchor_shift; } if (this.origin.system !== this.destination.system) { result = this.definitions[this.origin.system].transform(result); } if (this.destination.unit.anchor_shift !== undefined) { result += this.destination.unit.anchor_shift; } result = result / this.destination.unit.to_anchor; if (precision) { result = +result.toFixed(precision); // Rounding number to percise value } return Object.assign({ value: result }, this.describe(this.destination.abbr)) }; Converter.prototype.toBestAllSystems = function (options) { if (!this.origin) { throw new Error('.toBestAllSystems must be called after .from') } options = Object.assign({ exclude: [], cutOffNumber: 1, percision: null, system: null }, options); const systems = this.getSystems(); const resultObj = {}; systems.forEach(system => { options.system = system; resultObj[system] = this.toBest(options); }); return resultObj }; Converter.prototype.toBest = function (options) { if (!this.origin) { throw new Error('.toBest must be called after .from') } options = Object.assign({ exclude: [], cutOffNumber: 1, percision: null, system: null }, options); const list = this.list() .filter(item => { if (!options.system) { return !options.exclude.includes(item.unit) && this.describe(item.unit).system === this.origin.system } return !options.exclude.includes(item.unit) && options.system === this.describe(item.unit).system }) .reduce((acc, item) => { const result = this.to(item.unit); if (!acc || (result.value >= options.cutOffNumber && result.value < acc.value)) { return result } else { return acc } }, undefined); if (options.precision) { list.value = +list.value.toFixed(options.precision); // Rounding number to percise value } return list }; Converter.prototype.getUnit = function (abbr) { const systemNames = Object.keys(this.definitions); const found = systemNames.map(systemName => { if (this.definitions[systemName][abbr]) { return { abbr: abbr, system: systemName, unit: this.definitions[systemName][abbr] } } }).filter(item => item !== undefined); return Array.isArray(found) ? found[0] : undefined }; Converter.prototype.list = function () { return this.possibilities().map(abbr => this.describe(abbr)) }; Converter.prototype.throwUnsupportedUnitError = function (what) { throw new Error('Unsupported unit ' + what) }; Converter.prototype.describe = function (abbr) { if (!abbr) { throw new Error('You must select a unit') } const unit = this.getUnit(abbr); return { unit: unit.abbr, system: unit.system, singular: unit.unit.name.singular, plural: unit.unit.name.plural } }; Converter.prototype.possibilities = function () { return Array.prototype.concat(...Object.keys(this.definitions) .map(systemName => { return Object.keys(this.definitions[systemName]).splice(2) })) }; Converter.prototype.getSystems = function () { return Object.keys(this.definitions) }; function converter (definitions) { return (val) => { return new Converter(val, definitions) } } const RATIO = 1; const light = { metric: { baseUnit: 'ntu', transform: (val) => { return val * RATIO }, ntu: { name: { singular: 'Nephelometric Turbidity Ratio Unit (NTRU)', plural: 'Nephelometric Turbidity Ratio Unit (NTRU)' }, to_anchor: 1 }, '.1 ntu': { name: { singular: '.1 ntu', plural: '.1 ntu' }, to_anchor: 1 / 10 } } }; var light$1 = converter(light); const RATIO$1 = 1; const pharos = { metric: { baseUnit: 'Original', transform: (val) => { return val * RATIO$1 }, Original: { name: { singular: 'Original Value', plural: 'Original Values' }, to_anchor: 1 }, psu: { name: { singular: '.00001 psu', plural: '.00001 psu' }, to_anchor: 0.00001 }, MV: { name: { singular: '1 millivolt', plural: '1 millivolts' }, to_anchor: 0.001 }, cal: { name: { singular: '.001 cal/cm^2', plural: '.001 cal/cm^2' }, to_anchor: 0.001 }, calM: { name: { singular: '.001 cal/cm^2/min', plural: '.001 cal/cm^2/min' }, to_anchor: 0.001 }, mcg: { name: { singular: '.01 mcg/L', plural: '.01 mcg/L' }, to_anchor: 0.01 }, percent: { name: { singular: '.1 %', plural: '.1 %' }, to_anchor: 0.1 }, degC: { name: { singular: '.1 degC', plural: '.1 degC' }, to_anchor: 0.1 }, ms: { name: { singular: '.1 m/s', plural: '.1 m/s' }, to_anchor: 0.1 }, mb: { name: { singular: '.1 millibar', plural: '.1 millibars' }, to_anchor: 0.1 }, mm: { name: { singular: '.1 mm', plural: '.1 mm' }, to_anchor: 0.1 }, ntu: { name: { singular: '.1 ntu', plural: '.1 ntu' }, to_anchor: 0.1 }, sec: { name: { singular: '.1 sec', plural: '.1 sec' }, to_anchor: 0.1 }, v: { name: { singular: '.1 volt', plural: '.1 volts' }, to_anchor: 0.1 }, dB: { name: { singular: '.43 dB', plural: '.43 dB' }, to_anchor: 0.43 }, cells: { name: { singular: '10 cells/mL', plural: '10 cells/mL' }, to_anchor: 10 }, cfs: { name: { singular: '10 cfs', plural: '10 cfs' }, to_anchor: 10 }, microhos: { name: { singular: '10 micromhos/cm', plural: '10 micromhos/cm' }, to_anchor: 10 }, microSiemen: { name: { singular: '10 microSiemen/cm', plural: '10 microSiemen/cm' }, to_anchor: 10 }, degfromN: { name: { singular: 'degfromN', plural: 'degfromN' }, to_anchor: 1 }, degrees: { name: { singular: 'degrees', plural: 'degrees' }, to_anchor: 1 } } }; var pharos$1 = converter(pharos); exports.acceleration = acceleration_js; exports.allUnits = allUnits_js; exports.angle = angle_js; exports.apparentPower = apparentPower_js; exports.area = area_js; exports.charge = charge_js; exports.current = current_js; exports.digital = digital_js; exports.each = each_js; exports.energy = energy_js; exports.force = force_js; exports.frequency = frequency_js; exports.illuminance = illuminance_js; exports.length = length_js; exports.mass = mass_js; exports.pace = pace_js; exports.partsPer = partsPer_js; exports.power = power_js; exports.pressure = pressure_js; exports.reactiveEnergy = reactiveEnergy_js; exports.reactivePower = reactivePower_js; exports.salinity = salinity_js; exports.speed = speed_js; exports.sound = sound_js; exports.temperature = temperature_js; exports.time = time_js; exports.voltage = voltage_js; exports.volume = volume_js; exports.volumeFlowRate = volumeFlowRate_js; exports.light = light$1; exports.pharos = pharos$1;