UNPKG

unitsnet-js

Version:

A better way to hold unit variables and easily convert to the destination unit

419 lines (418 loc) 18.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElectricImpedance = exports.ElectricImpedanceUnits = void 0; const base_unit_1 = require("../base-unit"); /** ElectricImpedanceUnits enumeration */ var ElectricImpedanceUnits; (function (ElectricImpedanceUnits) { /** */ ElectricImpedanceUnits["Ohms"] = "Ohm"; /** */ ElectricImpedanceUnits["Nanoohms"] = "Nanoohm"; /** */ ElectricImpedanceUnits["Microohms"] = "Microohm"; /** */ ElectricImpedanceUnits["Milliohms"] = "Milliohm"; /** */ ElectricImpedanceUnits["Kiloohms"] = "Kiloohm"; /** */ ElectricImpedanceUnits["Megaohms"] = "Megaohm"; /** */ ElectricImpedanceUnits["Gigaohms"] = "Gigaohm"; /** */ ElectricImpedanceUnits["Teraohms"] = "Teraohm"; })(ElectricImpedanceUnits = exports.ElectricImpedanceUnits || (exports.ElectricImpedanceUnits = {})); /** Electric impedance is the opposition to alternating current presented by the combined effect of resistance and reactance in a circuit. It is defined as the inverse of admittance. The SI unit of impedance is the ohm (symbol Ω). */ class ElectricImpedance extends base_unit_1.BaseUnit { /** * Create a new ElectricImpedance. * @param value The value. * @param fromUnit The ‘ElectricImpedance’ unit to create from. * The default unit is Ohms */ constructor(value, fromUnit = ElectricImpedanceUnits.Ohms) { super(); this.ohmsLazy = null; this.nanoohmsLazy = null; this.microohmsLazy = null; this.milliohmsLazy = null; this.kiloohmsLazy = null; this.megaohmsLazy = null; this.gigaohmsLazy = null; this.teraohmsLazy = null; if (value === undefined || value === null || Number.isNaN(value)) { throw new TypeError('invalid unit value ‘' + value + '’'); } this.value = this.convertToBase(value, fromUnit); } /** * The base value of ElectricImpedance is Ohms. * This accessor used when needs a value for calculations and it's better to use directly the base value */ get BaseValue() { return this.value; } /** Gets the default unit used when creating instances of the unit or its DTO */ get baseUnit() { return ElectricImpedanceUnits.Ohms; } /** */ get Ohms() { if (this.ohmsLazy !== null) { return this.ohmsLazy; } return this.ohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Ohms); } /** */ get Nanoohms() { if (this.nanoohmsLazy !== null) { return this.nanoohmsLazy; } return this.nanoohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Nanoohms); } /** */ get Microohms() { if (this.microohmsLazy !== null) { return this.microohmsLazy; } return this.microohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Microohms); } /** */ get Milliohms() { if (this.milliohmsLazy !== null) { return this.milliohmsLazy; } return this.milliohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Milliohms); } /** */ get Kiloohms() { if (this.kiloohmsLazy !== null) { return this.kiloohmsLazy; } return this.kiloohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Kiloohms); } /** */ get Megaohms() { if (this.megaohmsLazy !== null) { return this.megaohmsLazy; } return this.megaohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Megaohms); } /** */ get Gigaohms() { if (this.gigaohmsLazy !== null) { return this.gigaohmsLazy; } return this.gigaohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Gigaohms); } /** */ get Teraohms() { if (this.teraohmsLazy !== null) { return this.teraohmsLazy; } return this.teraohmsLazy = this.convertFromBase(ElectricImpedanceUnits.Teraohms); } /** * Create a new ElectricImpedance instance from a Ohms * * @param value The unit as Ohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromOhms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Ohms); } /** * Create a new ElectricImpedance instance from a Nanoohms * * @param value The unit as Nanoohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromNanoohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Nanoohms); } /** * Create a new ElectricImpedance instance from a Microohms * * @param value The unit as Microohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromMicroohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Microohms); } /** * Create a new ElectricImpedance instance from a Milliohms * * @param value The unit as Milliohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromMilliohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Milliohms); } /** * Create a new ElectricImpedance instance from a Kiloohms * * @param value The unit as Kiloohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromKiloohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Kiloohms); } /** * Create a new ElectricImpedance instance from a Megaohms * * @param value The unit as Megaohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromMegaohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Megaohms); } /** * Create a new ElectricImpedance instance from a Gigaohms * * @param value The unit as Gigaohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromGigaohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Gigaohms); } /** * Create a new ElectricImpedance instance from a Teraohms * * @param value The unit as Teraohms to create a new ElectricImpedance from. * @returns The new ElectricImpedance instance. */ static FromTeraohms(value) { return new ElectricImpedance(value, ElectricImpedanceUnits.Teraohms); } /** * Gets the base unit enumeration associated with ElectricImpedance * @returns The unit enumeration that can be used to interact with this type */ static getUnitEnum() { return ElectricImpedanceUnits; } /** * Gets the default unit used when creating instances of the unit or its DTO * @returns The unit enumeration value used as a default parameter in constructor and DTO methods */ static getBaseUnit() { return ElectricImpedanceUnits.Ohms; } /** * Create API DTO represent a ElectricImpedance unit. * @param holdInUnit The specific ElectricImpedance unit to be used in the unit representation at the DTO */ toDto(holdInUnit = ElectricImpedanceUnits.Ohms) { return { value: this.convert(holdInUnit), unit: holdInUnit }; } /** * Create a ElectricImpedance unit from an API DTO representation. * @param dtoElectricImpedance The ElectricImpedance API DTO representation */ static FromDto(dtoElectricImpedance) { return new ElectricImpedance(dtoElectricImpedance.value, dtoElectricImpedance.unit); } /** * Convert ElectricImpedance to a specific unit value. * @param toUnit The specific unit to convert to * @returns The value of the specific unit provided. */ convert(toUnit) { switch (toUnit) { case ElectricImpedanceUnits.Ohms: return this.Ohms; case ElectricImpedanceUnits.Nanoohms: return this.Nanoohms; case ElectricImpedanceUnits.Microohms: return this.Microohms; case ElectricImpedanceUnits.Milliohms: return this.Milliohms; case ElectricImpedanceUnits.Kiloohms: return this.Kiloohms; case ElectricImpedanceUnits.Megaohms: return this.Megaohms; case ElectricImpedanceUnits.Gigaohms: return this.Gigaohms; case ElectricImpedanceUnits.Teraohms: return this.Teraohms; default: break; } return Number.NaN; } convertFromBase(toUnit) { if (base_unit_1.areAnyOperatorsOverridden()) switch (toUnit) { case ElectricImpedanceUnits.Ohms: return this.value; case ElectricImpedanceUnits.Nanoohms: return super.internalDivide(this.value, 1e-9); case ElectricImpedanceUnits.Microohms: return super.internalDivide(this.value, 0.000001); case ElectricImpedanceUnits.Milliohms: return super.internalDivide(this.value, 0.001); case ElectricImpedanceUnits.Kiloohms: return super.internalDivide(this.value, 1000); case ElectricImpedanceUnits.Megaohms: return super.internalDivide(this.value, 1000000); case ElectricImpedanceUnits.Gigaohms: return super.internalDivide(this.value, 1000000000); case ElectricImpedanceUnits.Teraohms: return super.internalDivide(this.value, 1000000000000); default: return Number.NaN; } switch (toUnit) { case ElectricImpedanceUnits.Ohms: return this.value; case ElectricImpedanceUnits.Nanoohms: return (this.value) / 1e-9; case ElectricImpedanceUnits.Microohms: return (this.value) / 0.000001; case ElectricImpedanceUnits.Milliohms: return (this.value) / 0.001; case ElectricImpedanceUnits.Kiloohms: return (this.value) / 1000; case ElectricImpedanceUnits.Megaohms: return (this.value) / 1000000; case ElectricImpedanceUnits.Gigaohms: return (this.value) / 1000000000; case ElectricImpedanceUnits.Teraohms: return (this.value) / 1000000000000; default: return Number.NaN; } } convertToBase(value, fromUnit) { if (base_unit_1.areAnyOperatorsOverridden()) switch (fromUnit) { case ElectricImpedanceUnits.Ohms: return value; case ElectricImpedanceUnits.Nanoohms: return super.internalMultiply(value, 1e-9); case ElectricImpedanceUnits.Microohms: return super.internalMultiply(value, 0.000001); case ElectricImpedanceUnits.Milliohms: return super.internalMultiply(value, 0.001); case ElectricImpedanceUnits.Kiloohms: return super.internalMultiply(value, 1000); case ElectricImpedanceUnits.Megaohms: return super.internalMultiply(value, 1000000); case ElectricImpedanceUnits.Gigaohms: return super.internalMultiply(value, 1000000000); case ElectricImpedanceUnits.Teraohms: return super.internalMultiply(value, 1000000000000); default: return Number.NaN; } switch (fromUnit) { case ElectricImpedanceUnits.Ohms: return value; case ElectricImpedanceUnits.Nanoohms: return (value) * 1e-9; case ElectricImpedanceUnits.Microohms: return (value) * 0.000001; case ElectricImpedanceUnits.Milliohms: return (value) * 0.001; case ElectricImpedanceUnits.Kiloohms: return (value) * 1000; case ElectricImpedanceUnits.Megaohms: return (value) * 1000000; case ElectricImpedanceUnits.Gigaohms: return (value) * 1000000000; case ElectricImpedanceUnits.Teraohms: return (value) * 1000000000000; default: return Number.NaN; } } /** * Format the ElectricImpedance to string. * Note! the default format for ElectricImpedance is Ohms. * To specify the unit format set the 'unit' parameter. * @param unit The unit to format the ElectricImpedance. * @param options The ToString options, it also can be the number of fractional digits to keep that deprecated and moved to the options object. support in number will be dropped in the upcoming versions. * @returns The string format of the ElectricImpedance. */ toString(unit = ElectricImpedanceUnits.Ohms, options) { if (typeof options === 'number') { console.warn('The number parameter is deprecated and moved to the options object. support in number will be dropped in the upcoming versions.'); options = { fractionalDigits: options }; } switch (unit) { case ElectricImpedanceUnits.Ohms: return super.truncateFractionDigits(this.Ohms, options) + ` Ω`; case ElectricImpedanceUnits.Nanoohms: return super.truncateFractionDigits(this.Nanoohms, options) + ` nΩ`; case ElectricImpedanceUnits.Microohms: return super.truncateFractionDigits(this.Microohms, options) + ` μΩ`; case ElectricImpedanceUnits.Milliohms: return super.truncateFractionDigits(this.Milliohms, options) + ` mΩ`; case ElectricImpedanceUnits.Kiloohms: return super.truncateFractionDigits(this.Kiloohms, options) + ` kΩ`; case ElectricImpedanceUnits.Megaohms: return super.truncateFractionDigits(this.Megaohms, options) + ` MΩ`; case ElectricImpedanceUnits.Gigaohms: return super.truncateFractionDigits(this.Gigaohms, options) + ` GΩ`; case ElectricImpedanceUnits.Teraohms: return super.truncateFractionDigits(this.Teraohms, options) + ` TΩ`; default: break; } return this.value.toString(); } /** * Get ElectricImpedance unit abbreviation. * Note! the default abbreviation for ElectricImpedance is Ohms. * To specify the unit abbreviation set the 'unitAbbreviation' parameter. * @param unitAbbreviation The unit abbreviation of the ElectricImpedance. * @returns The abbreviation string of ElectricImpedance. */ getUnitAbbreviation(unitAbbreviation = ElectricImpedanceUnits.Ohms) { switch (unitAbbreviation) { case ElectricImpedanceUnits.Ohms: return `Ω`; case ElectricImpedanceUnits.Nanoohms: return `nΩ`; case ElectricImpedanceUnits.Microohms: return `μΩ`; case ElectricImpedanceUnits.Milliohms: return `mΩ`; case ElectricImpedanceUnits.Kiloohms: return `kΩ`; case ElectricImpedanceUnits.Megaohms: return `MΩ`; case ElectricImpedanceUnits.Gigaohms: return `GΩ`; case ElectricImpedanceUnits.Teraohms: return `TΩ`; default: break; } return ''; } /** * Check if the given ElectricImpedance are equals to the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns True if the given ElectricImpedance are equal to the current ElectricImpedance. */ equals(electricImpedance) { return super.internalEquals(this.value, electricImpedance.BaseValue); } /** * Compare the given ElectricImpedance against the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns 0 if they are equal, -1 if the current ElectricImpedance is less then other, 1 if the current ElectricImpedance is greater then other. */ compareTo(electricImpedance) { return super.internalCompareTo(this.value, electricImpedance.BaseValue); } /** * Add the given ElectricImpedance with the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns A new ElectricImpedance instance with the results. */ add(electricImpedance) { return new ElectricImpedance(super.internalAdd(this.value, electricImpedance.BaseValue)); } /** * Subtract the given ElectricImpedance with the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns A new ElectricImpedance instance with the results. */ subtract(electricImpedance) { return new ElectricImpedance(super.internalSubtract(this.value, electricImpedance.BaseValue)); } /** * Multiply the given ElectricImpedance with the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns A new ElectricImpedance instance with the results. */ multiply(electricImpedance) { return new ElectricImpedance(super.internalMultiply(this.value, electricImpedance.BaseValue)); } /** * Divide the given ElectricImpedance with the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns A new ElectricImpedance instance with the results. */ divide(electricImpedance) { return new ElectricImpedance(super.internalDivide(this.value, electricImpedance.BaseValue)); } /** * Modulo the given ElectricImpedance with the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns A new ElectricImpedance instance with the results. */ modulo(electricImpedance) { return new ElectricImpedance(super.internalModulo(this.value, electricImpedance.BaseValue)); } /** * Pow the given ElectricImpedance with the current ElectricImpedance. * @param electricImpedance The other ElectricImpedance. * @returns A new ElectricImpedance instance with the results. */ pow(electricImpedance) { return new ElectricImpedance(super.internalPow(this.value, electricImpedance.BaseValue)); } } exports.ElectricImpedance = ElectricImpedance;