Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
@spg-drycooling/field-services
Advanced tools
Convert, calculate and validate fields and values for the SPG Drycooling Asset Database
This is a collection of utils and tools for use with the SPG Asset Database.
npm install -S @spg-drycooling/field-services
or
yarn add @spg-drycooling/field-services
The field services packages exposes a collection of tools that can be imported from the main entry point:
import { FieldCalculcator, IUnit } from '@spg-drycooling/field-services';
The tools included in this package support a wide range of unit types and units:
UnitType | Base unit | Imperial | Metric (SI) | Other |
---|---|---|---|---|
Acceleration | g-force | m/s2, g-force | ||
Angle | deg | deg, rad, grad, arcmin, arcsec | ||
ApparentPower | VA | VA, mVA, kVA, MVA, GVA | ||
Area | m2 | in2, ft2, ac, mi2, yd2 | mm2, cm2, m2, ha, km2 | |
Charge | c | c, mC, μC, nC, pC | ||
Current | A | A, mA, kA | ||
Density | kg/m3 | kg/m3 | ||
Each | ea | / | ea, dz | |
Energy | J | Wh, mWh, kWh, MWh, GWh, J, kJ | ||
Force | N | lbf | N, kN | |
Frequency | Hz | Hz, mHz, kHz, MHz, GHz, THz, rpm, deg/s, rad/s | ||
Generic | / | / | wu, ul, %, kWe, W/m2K, kJ/kg, bar(g)+FV, bool | |
Illuminance | lx | ft-cd | lx | |
Length | m | in, ft-us, ft, mi, yd, nMi | mm, cm, m, km | fathom |
Mass | g | oz, lb, t | mcg, mg, g, kg, mt | |
MassFlowRate | kg/s | lb/s | kg/s | |
Pace | s/m | s/ft, min/mi | s/m, min/km | |
PartsPer | ppm | / | ppm, ppb, ppt, ppq | |
Power | W | W, kW, mW, kM, MW, GW | ||
Pressure | kPa | psi, ksi | Pa, hPa, kPa, MPa, bar, mbar | torr |
ReactiveEnergy | VARh | VARh, mVARh, kVARh, MVARh, GVARh | ||
ReactivePower | VAR | VARh, mVARh, kVARh, MVARh, GVARh | ||
Sound | db(A) | dB(A) | dB(L) | |
Speed | km/h | ft/s, knot | m/s, km/h, m/h | |
Temperature | C | F | C, K | R |
Time | s | ns, mu, ms, s, min, h, d, week, month, year | ||
Voltage | V | V, mV, kV | ||
Volume | l | tsp, Tbs, in3, fl-oz, cup, pnt, qt, gal, ft3, yd3 | mm3, cm3, ml, l, kl, m3, km3, cl, dl | |
VolumeFlowRate | l/s | tsp/s, Tbs/s, in3/s, in3/min, in3/h, fl-oz/s, fl-oz/min, fl-oz/h, cup/s, pnt/s, pnt/min, pnt/h, qt/s, gal/s, gal/min, gal/h, ft3/s, ft3/min, ft3/h, yd3/s, yd3/min, yd3/h | mm3/s, cm3/s, ml/s, cl/s, dl/s, l/s, l/min, l/h, kl/s, kl/min, kl/h, m3/s, m3/min, m3/h, km3/s |
Generic
is a special case, this unit type contains all units that cannot be converted and should be used as is.Fields are calculated and validation from formulas. A formula consists of 3 parts:
The source
and target
can be a new formula as well, allowing the recursive nesting of formulas.
The source and target properties are formula segments. They can contain 1 of 4 things:
Operations are always converted to a formula with a left- and righthand side.
E.g.:
10^3
will be seen as
10 POW 3
These operators are available:
ABS
: calculcate absolute valueADD
: add the target to the sourceDIVIDE
: divide the source by the targetMAX
: return the highest value between the source and the targetMIN
: return the lowest value between the source and the targetMODULUS
: return the remainder after dividing the source by the targetMULTIPLY
: multiply the source with the targetNOPE
: ignore this operation, return the sourcePOW
: exponentiate the source with the targetROUND
: round the source to the number of decimal places as defined by the target (negative values get rounded to the left of the decimal point)SUBTRACT
: subtract the target from the sourceSimple division:
{
"source": {
"value": 10
},
"operator": "DIVIDE",
"target": {
"value": 5
}
}
Would translate to:
10 / 5
Power:
Formula:
{
"source": {
"value": 15
},
"operator": "POW",
"target": {
"value": 2
}
}
Would translate to:
15^2
Nested formula:
{
"source": [
{
"source": {
"value": 15
},
"operator": "POW",
"target": {
"value": 2
}
}
],
"operator": "SUBTRACT",
"target": [
{
"source": [
{
"source": {
"value": 3
},
"operator": "MULTIPLY",
"target": {
"value": 0.9
}
}
],
"operator": "ROUND",
"target": {
"value": 2
}
}
]
}
Would translate to:
15^2 - ROUND(3 * 0.9, 2)
Validation rules follow the same syntax and rules as the calculation formulas. Instead of an operator, they use a comparator.
E.g.:
{
"source": {
"value": 25
},
"comparator": "LTE",
"target": {
"value": 99
}
}
EQ
: resolve if source is equal to targetGT
: resolve if source is greater than targetGTE
: resolve if source is greater than or equal to targetIN
: resolve if source is included in target (list)LT
: resolve if source is less than targetLTE
: resolve if source is less than or equal to targetNE
: resolve if source is not equal to targetNIN
: resolve if source is not included in target (list)The FieldCalculator
is used to calculcate a field value from a provided formula and values map.
It exposes 3 methods:
Provided an operator, return a function to run the operation on a source and target. Used internally to map operators to operation methods.
const operator = FieldCalculator.mapOperation("SUBTRACT");
return operator(2, 5); // return value: 7
Provided a field, formula segment (e.g. source) and fields map, return the resulting value. Used internally in the calculcate method to allow recursive calculcation.
const field = { code: "FIELD", nominalValue: 2.4 };
const segment = {
source: {
value: 4
},
operator: "MULTIPLY",
target: {
fieldId: "TARGET_FIELD"
}
};
const fields: {
TARGET_FIELD: {
currentValue: 0.3
}
};
const value = FieldCalculator.mapValue(field, segment, fields); // return value: 1.2
Provided a field, formula and fields map, return the calculcated value.
const field = { code: "FIELD", nominalValue: 2.4 };
const formula = {
source: [{
source: {
value: 4
},
operator: "MULTIPLY",
target: {
fieldId: "TARGET_FIELD"
}
}],
operator: ROUND,
target: {
value: 2
}
};
const fields: {
TARGET_FIELD: {
currentValue: 0.3
}
};
const value = FieldCalculator.calculate(field, formula, fields); // return value: 1.20
The FieldValidator
is used to validate a field value from a provided validation rule and values map.
It exposes 3 methods:
Provided an comparator, return a function to run the comparison on a source and target. Used internally to map comparators to comparison methods.
const comparator = FieldValidator.mapComparator("EQ");
return comparator(2, 5); // return value: false
Provided a formula segment (e.g. source) and fields map, return the resulting value. Used internally in the validate method to allow recursive validation.
const segment = {
fieldId: "TARGET_FIELD"
};
const fields: {
TARGET_FIELD: {
currentValue: 9
}
};
const value = FieldCalculator.mapValue(segment, fields); // return value: 9
Provided a field, validation rule and fields map, return the resulting valid state.
const field = { code: "FIELD", nominalValue: 2.4 };
const validation = {
source: {
value: 4
},
comparator: "GTE",
target: {
fieldId: "TARGET_FIELD"
}
};
const fields: {
TARGET_FIELD: {
currentValue: 0.3
}
};
const value = FieldValidator.validate(field, validation, fields); // return value: true
The unitConverter is capable of converting a value between the known units of a unit type, as well as returning available unit types and units based on a provided unit.
Each unit type (measure) has defined a conversion rule for each of its units. Based on these conversion rules, values can be converted from one unit to another.
A unit conversion definition can define units for multiple systems (e.g.: SI, US). For each system, a base unit is defined, providing a ratio to be used for conversion between systems.
E.g.:
const length: IUnitConversionDefinition = {
metric: {
mm: {
name: {
singular: 'Millimeter',
plural: 'Millimeters',
},
baseUnitFactor: 1 / 1000,
},
m: {
name: {
singular: 'Meter',
plural: 'Meters',
},
baseUnitFactor: 1,
}
},
imperial: {
ft: {
name: {
singular: 'Foot',
plural: 'Feet',
},
baseUnitFactor: 1,
},
}
baseUnits: {
metric: {
unit: 'm',
ratio: 3.2808399,
},
imperial: {
unit: 'ft',
ratio: 0.3048,
},
}
};
const result = unitConverter.convert(10, { from: 'mm', to: 'ft' });
// return value: 10000g
const result = unitConverter.getUnits();
/**
*
* result = [{
* unitSymbol: 'kg',
* measure: 'mass',
* system: 'metric',
* unit: {
* name: {
* singular: 'Kilogram';
plural: 'Kilograms';
* },
* baseUnitFactor: 1000
* }
* }]
*/
const result = unitConverter.getUnitsByMeasure(UnitType.Mass, MeasurementSystems.Metric);
/**
*
* result = [{
* unitSymbol: 'kg',
* measure: 'mass',
* system: 'metric',
* unit: {
* name: {
* singular: 'Kilogram';
plural: 'Kilograms';
* },
* baseUnitFactor: 1000
* }
* }, ...]
*/
const result = unitConverter.getUnit(Units.kg);
/**
*
* result = {
* unitSymbol: 'kg',
* measure: 'mass',
* system: 'metric',
* unit: {
* name: {
* singular: 'Kilogram';
plural: 'Kilograms';
* },
* baseUnitFactor: 1000
* }
* }
*/
const result = unitConverter.getBaseUnit(Units.kg);
/**
*
* result = {
* unitSymbol: 'g',
* measure: 'mass',
* system: 'metric',
* unit: {
* name: {
* singular: 'Gram';
plural: 'Grams';
* },
* baseUnitFactor: 1
* }
* }
*/
const result = unitConverter.getMeasure(Units.kg);
/**
*
* result = 'Mass'
*/
FAQs
Convert, calculate and validate fields and values for the SPG Drycooling Asset Database
We found that @spg-drycooling/field-services demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.