Source: index.js

  1. /**
  2. * Extracts the name as a String from a FHIR HumanName. Returns an empty String ("") if any of the parameters is null or undefined.
  3. *
  4. * @example
  5. * <caption>Basic usage</caption>
  6. * import fhirUtil from "molit-fhir-util";
  7. *
  8. * fhirUtil.getStringFromHumanName(patient.name);
  9. *
  10. * @param {Object[]} humanName - the HumanNames Array
  11. * @param {Boolean} lastNameFirst - if true will put the Family-Name first
  12. * @return {String} the human name
  13. */
  14. export function getStringFromHumanName(humanName, lastNameFirst) {
  15. if (!humanName || !humanName[0]) {
  16. return "";
  17. }
  18. let givenFamilyName = "";
  19. humanName.forEach(name => {
  20. if (name.use === "official") {
  21. if (lastNameFirst) {
  22. if (name.prefix) {
  23. givenFamilyName += name.prefix + " ";
  24. }
  25. if (name.family) {
  26. givenFamilyName += name.family + ", ";
  27. }
  28. if (name.given) {
  29. givenFamilyName += name.given.join(" ");
  30. }
  31. } else {
  32. if (name.prefix) {
  33. givenFamilyName += name.prefix + " ";
  34. }
  35. if (name.given) {
  36. givenFamilyName += name.given.join(" ");
  37. }
  38. if (name.family) {
  39. givenFamilyName += " " + name.family;
  40. }
  41. }
  42. }
  43. });
  44. if (givenFamilyName && givenFamilyName !== "") {
  45. return givenFamilyName;
  46. }
  47. let textName = humanName
  48. .map(name => name.text)
  49. .filter(text => text && text !== "" && text !== " ")
  50. .join(", ");
  51. if (textName && textName !== "") {
  52. return textName;
  53. } else {
  54. return "";
  55. }
  56. }
  57. /**
  58. * Returns the value that is annotated with the given loinc code from the given Observation components
  59. * complying to the obs-variant profile (http://hl7.org/fhir/uv/genomics-reporting/StructureDefinition/obs-variant).
  60. *
  61. * @param {Array} components - the components from the obs-variant
  62. * @param {String} loincCode - the loinc code
  63. * @returns - the value or undefined, if the loinc code could not be found within the components
  64. */
  65. export function getValueByLoincCode(components, loincCode) {
  66. if (!components || !components.length) {
  67. return undefined;
  68. }
  69. let component = components.find(component => {
  70. if (!component || !component.code || !component.code.coding || !component.code.coding.length) {
  71. return false;
  72. }
  73. let code = component.code.coding.find(
  74. code => code.system === "http://loinc.org" || code.system === "http://hl7.org/fhir/uv/genomics-reporting/CodeSystem/LOINC-TBD" || code.system === "http://hl7.org/fhir/uv/genomics-reporting/CodeSystem/tbd-codes"
  75. );
  76. if (!code) {
  77. return false;
  78. }
  79. return code.code === loincCode;
  80. });
  81. if (!component) {
  82. return undefined;
  83. }
  84. if (component.valueCodeableConcept && component.valueCodeableConcept.coding && component.valueCodeableConcept.coding.length) {
  85. return component.valueCodeableConcept.coding[0].display;
  86. } else if (component.valueQuantity) {
  87. return component.valueQuantity.value;
  88. } else if (component.valueRange) {
  89. let value = "";
  90. if (component.valueRange.low && component.valueRange.low.value) {
  91. value += component.valueRange.low.value;
  92. } else {
  93. value += "unknown";
  94. }
  95. value += "-";
  96. if (component.valueRange.high && component.valueRange.high.value) {
  97. value += component.valueRange.high.value;
  98. } else {
  99. value += "unknown";
  100. }
  101. return value;
  102. } else {
  103. return undefined;
  104. }
  105. }
  106. /**
  107. * Returns the first identifier with the given system. If you just want to get the identifier value, use {@link getIdentifierValueBySystem}. Returns null if any of the parameters is null or undefined.
  108. *
  109. * @param {Object[]} identifier - the identifier Array
  110. * @param {String} system - the system String
  111. * @return {Object} the identifier
  112. */
  113. export function getIdentifierBySystem(identifier, system) {
  114. if (!identifier || !system) {
  115. return null;
  116. }
  117. return identifier.find(identifier => identifier.system === system);
  118. }
  119. /**
  120. * Returns the value as String of an identifier String inthe format system|value. Returns null if any of the parameters is null or undefined.
  121. *
  122. * @param {String} identifierString
  123. */
  124. export function getIdentifierValueByIdentifierString(identifierString) {
  125. if (!identifierString) {
  126. return null;
  127. }
  128. return identifierString.substring(identifierString.indexOf("|") + 1);
  129. }
  130. /**
  131. * Returns the first identifier <b>value</b> it finds with the given system. If no identifier is found, null is returned. Returns "" if any of the parameters is null or undefined.
  132. *
  133. * @param {Array} identifiers
  134. * @param {String} system
  135. * @return {String|null} the identifier value
  136. */
  137. export function getIdentifierValueBySystem(identifiers, system) {
  138. let identifier = getIdentifierBySystem(identifiers, system);
  139. if (!identifier) {
  140. return null;
  141. }
  142. return identifier.value;
  143. }
  144. /**
  145. * Returns the first identifier it finds with the given system. If no identifier is found, null is returned.
  146. *
  147. * @param {Object} resource - the FHIR resource
  148. * @param {String} system - the system of the identifier
  149. * @return {Object|null} the identifier
  150. */
  151. export function getIdentifierByResourceAndSystem(resource, system) {
  152. if (!resource || !system) {
  153. return null;
  154. }
  155. return getIdentifierBySystem(resource.identifier, system);
  156. }