Skip to content
Snippets Groups Projects
Commit f4e05737 authored by Benedikt's avatar Benedikt
Browse files

improved siReal handling

parent 09fe8d45
Branches
No related tags found
No related merge requests found
// 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 [];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment