diff --git a/src/dccQuantity.js b/src/dccQuantity.js index 23068d29bdbf0ff9af9212caf5281ffb8b48ca92..8387c51f86a7adcf2964670be500a1d6c3206837 100644 --- a/src/dccQuantity.js +++ b/src/dccQuantity.js @@ -1,28 +1,15 @@ // src/dccQuantity.js import { DSIUnit } from "dsiunits-js/src/dsiUnit.js"; -// Import the unit input component if needed: -// import "dsi-units-js-lib/src/dsiUnitInput.js"; -/** - * Base class for a DCC quantity. - * Holds a pointer to the original JSON data. - */ export class DCCQuantity { constructor(jsonData) { this.jsonData = jsonData; } - /** - * Returns the refType attribute of the quantity. - */ getRefType() { return (this.jsonData.$ && this.jsonData.$.refType) ? this.jsonData.$.refType : ''; } - /** - * Returns the quantity name in the specified language. - * @param {string} language - e.g., 'en', 'de' - */ getName(language) { const nameData = this.jsonData['dcc:name']; if (!nameData) return ''; @@ -30,23 +17,16 @@ export class DCCQuantity { if (Array.isArray(content)) { const match = content.find(item => item.$ && item.$.lang === language) || content[0]; return match._ || match; - } else { - return content._ || content; } + return content._ || content; } } -/** - * Class for quantities represented by <si:realListXMLList>. - */ export class DCCRealListQuantity extends DCCQuantity { constructor(jsonData) { super(jsonData); } - /** - * Extracts and returns an array of numeric values. - */ getValues() { const realList = this.jsonData['si:realListXMLList']; if (realList && realList['si:valueXMLList']) { @@ -58,26 +38,20 @@ export class DCCRealListQuantity extends DCCQuantity { return []; } - /** - * Returns the unit string rendered as HTML using the dsi-units-js library. - */ getUnit() { const realList = this.jsonData['si:realListXMLList']; if (realList && realList['si:unitXMLList']) { const rawUnit = realList['si:unitXMLList'].trim(); const unit = new DSIUnit(rawUnit); - // Render the unit in a one-line format return unit.toHTML({ oneLine: true }); } return ''; } - /** - * Extracts and returns an array of uncertainty values. - */ getUncertainty() { const realList = this.jsonData['si:realListXMLList']; if ( + realList && realList['si:measurementUncertaintyUnivariateXMLList'] && realList['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList'] && realList['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList']['si:valueExpandedMUXMLList'] @@ -89,19 +63,12 @@ export class DCCRealListQuantity extends DCCQuantity { } } -/** - * Class for quantities represented by a single <si:real>. - * (This class is provided for completeness and may be expanded as needed.) - */ export class DCCRealQuantity extends DCCQuantity { constructor(jsonData) { super(jsonData); } - /** - * Returns the numeric value from the <si:real> element. - */ - getValue() { + getValues() { const realData = this.jsonData['si:real']; if (realData && realData['si:value']) { return parseFloat(realData['si:value']); @@ -109,9 +76,6 @@ export class DCCRealQuantity extends DCCQuantity { return null; } - /** - * Returns the unit rendered as HTML. - */ getUnit() { const realData = this.jsonData['si:real']; if (realData && realData['si:unit']) { @@ -121,4 +85,17 @@ export class DCCRealQuantity extends DCCQuantity { } return ''; } -} \ No newline at end of file + getUncertainty() { + const realList = this.jsonData['si:real']; + if ( + realList && + realList['si:measurementUncertaintyUnivariate'] && + realList['si:measurementUncertaintyUnivariate']['si:expandedMU'] && + realList['si:measurementUncertaintyUnivariate']['si:expandedMU']['si:valueExpandedMU'] + ) { + const errStr = realList['si:measurementUncertaintyUnivariate']['si:expandedMU']['si:valueExpandedMU']; + return errStr.trim().split(/\s+/).map(v => parseFloat(v)); + } + return []; + } +}