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 // src/dccQuantity.js
import { DSIUnit } from "dsiunits-js/src/dsiUnit.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 { export class DCCQuantity {
constructor(jsonData) { constructor(jsonData) {
this.jsonData = jsonData; this.jsonData = jsonData;
} }
/**
* Returns the refType attribute of the quantity.
*/
getRefType() { getRefType() {
return (this.jsonData.$ && this.jsonData.$.refType) ? this.jsonData.$.refType : ''; 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) { getName(language) {
const nameData = this.jsonData['dcc:name']; const nameData = this.jsonData['dcc:name'];
if (!nameData) return ''; if (!nameData) return '';
...@@ -30,23 +17,16 @@ export class DCCQuantity { ...@@ -30,23 +17,16 @@ export class DCCQuantity {
if (Array.isArray(content)) { if (Array.isArray(content)) {
const match = content.find(item => item.$ && item.$.lang === language) || content[0]; const match = content.find(item => item.$ && item.$.lang === language) || content[0];
return match._ || match; return match._ || match;
} else {
return content._ || content;
} }
return content._ || content;
} }
} }
/**
* Class for quantities represented by <si:realListXMLList>.
*/
export class DCCRealListQuantity extends DCCQuantity { export class DCCRealListQuantity extends DCCQuantity {
constructor(jsonData) { constructor(jsonData) {
super(jsonData); super(jsonData);
} }
/**
* Extracts and returns an array of numeric values.
*/
getValues() { getValues() {
const realList = this.jsonData['si:realListXMLList']; const realList = this.jsonData['si:realListXMLList'];
if (realList && realList['si:valueXMLList']) { if (realList && realList['si:valueXMLList']) {
...@@ -58,26 +38,20 @@ export class DCCRealListQuantity extends DCCQuantity { ...@@ -58,26 +38,20 @@ export class DCCRealListQuantity extends DCCQuantity {
return []; return [];
} }
/**
* Returns the unit string rendered as HTML using the dsi-units-js library.
*/
getUnit() { getUnit() {
const realList = this.jsonData['si:realListXMLList']; const realList = this.jsonData['si:realListXMLList'];
if (realList && realList['si:unitXMLList']) { if (realList && realList['si:unitXMLList']) {
const rawUnit = realList['si:unitXMLList'].trim(); const rawUnit = realList['si:unitXMLList'].trim();
const unit = new DSIUnit(rawUnit); const unit = new DSIUnit(rawUnit);
// Render the unit in a one-line format
return unit.toHTML({ oneLine: true }); return unit.toHTML({ oneLine: true });
} }
return ''; return '';
} }
/**
* Extracts and returns an array of uncertainty values.
*/
getUncertainty() { getUncertainty() {
const realList = this.jsonData['si:realListXMLList']; const realList = this.jsonData['si:realListXMLList'];
if ( if (
realList &&
realList['si:measurementUncertaintyUnivariateXMLList'] && realList['si:measurementUncertaintyUnivariateXMLList'] &&
realList['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList'] && realList['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList'] &&
realList['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList']['si:valueExpandedMUXMLList'] realList['si:measurementUncertaintyUnivariateXMLList']['si:expandedMUXMLList']['si:valueExpandedMUXMLList']
...@@ -89,19 +63,12 @@ export class DCCRealListQuantity extends DCCQuantity { ...@@ -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 { export class DCCRealQuantity extends DCCQuantity {
constructor(jsonData) { constructor(jsonData) {
super(jsonData); super(jsonData);
} }
/** getValues() {
* Returns the numeric value from the <si:real> element.
*/
getValue() {
const realData = this.jsonData['si:real']; const realData = this.jsonData['si:real'];
if (realData && realData['si:value']) { if (realData && realData['si:value']) {
return parseFloat(realData['si:value']); return parseFloat(realData['si:value']);
...@@ -109,9 +76,6 @@ export class DCCRealQuantity extends DCCQuantity { ...@@ -109,9 +76,6 @@ export class DCCRealQuantity extends DCCQuantity {
return null; return null;
} }
/**
* Returns the unit rendered as HTML.
*/
getUnit() { getUnit() {
const realData = this.jsonData['si:real']; const realData = this.jsonData['si:real'];
if (realData && realData['si:unit']) { if (realData && realData['si:unit']) {
...@@ -121,4 +85,17 @@ export class DCCRealQuantity extends DCCQuantity { ...@@ -121,4 +85,17 @@ export class DCCRealQuantity extends DCCQuantity {
} }
return ''; return '';
} }
} getUncertainty() {
\ No newline at end of file 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