@arkhn/fhir.ts
Advanced tools
Comparing version 3.2.0 to 3.3.0
@@ -5,2 +5,3 @@ import { AttributeDefinition, ResourceDefinition } from './types'; | ||
children: Attribute[]; | ||
choices: Attribute[]; | ||
slices: Attribute[]; | ||
@@ -26,4 +27,5 @@ items: Attribute[]; | ||
static from(serialized: any): Attribute; | ||
spreadTypes(): Attribute[]; | ||
spreadTypes(): void; | ||
addChild(child: Attribute): void; | ||
addChoice(choice: Attribute): void; | ||
addSlice(slice: Attribute): void; | ||
@@ -30,0 +32,0 @@ addItem(index?: number, attr?: Attribute): Attribute; |
@@ -59,2 +59,3 @@ "use strict"; | ||
this.children = []; | ||
this.choices = []; | ||
this.slices = []; | ||
@@ -138,3 +139,3 @@ this.items = []; | ||
// from rebuilds an Attribute from an other one or from a serialized version of it. | ||
// It recursively browse all the slices, children and items in order to deep copy the provided attribute. | ||
// It recursively browse all the slices, choices, children and items in order to deep copy the provided attribute. | ||
// This function is especially useful when we want to rebuild an | ||
@@ -147,2 +148,5 @@ // Attribute tree from a cached version of a StructureDefinition. | ||
}); | ||
serialized.choices.forEach(function (choice) { | ||
return attr.addChoice(Attribute.from(choice)); | ||
}); | ||
serialized.slices.forEach(function (slice) { | ||
@@ -159,11 +163,6 @@ return attr.addSlice(Attribute.from(slice)); | ||
var _this = this; | ||
if (this.types.length > 1) { | ||
return this.types.map(function (type) { | ||
var _a; | ||
var attr = new Attribute(__assign(__assign({}, _this.definition), { type: [{ code: type }], id: _this.definition.id.replace('[x]', toCamelCase(type)), path: _this.definition.path.replace('[x]', toCamelCase(type)) })); | ||
(_a = _this.parent) === null || _a === void 0 ? void 0 : _a.addChild(attr); | ||
return attr; | ||
}); | ||
} | ||
return [this]; | ||
this.types.forEach(function (type) { | ||
var attr = new Attribute(__assign(__assign({}, _this.definition), { type: [{ code: type }], id: _this.definition.id.replace('[x]', toCamelCase(type)), path: _this.definition.path.replace('[x]', toCamelCase(type)) })); | ||
_this.addChoice(attr); | ||
}); | ||
}; | ||
@@ -175,10 +174,11 @@ // addChild adds a child attribute to this and sets the child's parent to this. | ||
}; | ||
// addChoice adds a choice type attribute to this and update its parent with the current parent. | ||
// Note that if this attribute is an item of an array, the slice must be an item as well (and we pass along the current index) | ||
Attribute.prototype.addChoice = function (choice) { | ||
choice.parent = this.parent; | ||
this.choices.push(choice); | ||
}; | ||
// addSlice adds a slice attribute to this and update the parent of the slice with the current parent. | ||
// Note that if this attribute is an item of an array, the slice must be an item as well (and we pass along the current index) | ||
Attribute.prototype.addSlice = function (slice) { | ||
slice.parent = this.parent; | ||
if (this.isItem) { | ||
slice.isItem = true; | ||
slice.index = this.index; | ||
} | ||
slice.parent = this; | ||
this.slices.push(slice); | ||
@@ -213,5 +213,5 @@ }; | ||
item.index = computeIndex(); | ||
item.slices.forEach(function (slice) { | ||
slice.isItem = true; | ||
slice.index = item.index; | ||
item.choices.forEach(function (choice) { | ||
choice.isItem = true; | ||
choice.index = item.index; | ||
}); | ||
@@ -218,0 +218,0 @@ item.extensions = this.extensions; |
import { Definition, AttributeDefinition } from './types'; | ||
export declare const isChoiceOf: (choice: AttributeDefinition, attr: AttributeDefinition) => boolean; | ||
export declare const isSliceOf: (slice: AttributeDefinition, attr: AttributeDefinition) => boolean; | ||
@@ -3,0 +4,0 @@ export declare const isChildOf: (child: AttributeDefinition, parent: AttributeDefinition) => boolean; |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -35,3 +28,6 @@ var attribute_1 = require("./attribute"); | ||
var allowedAttributes = ['extension']; | ||
exports.isSliceOf = function (slice, attr) { return slice.path === attr.path && !!slice.sliceName; }; | ||
exports.isChoiceOf = function (choice, attr) { | ||
return choice.path === attr.path && !!choice.sliceName && choice.path.endsWith('[x]'); | ||
}; | ||
exports.isSliceOf = function (slice, attr) { return slice.path === attr.path && !!slice.sliceName && !attr.sliceName; }; | ||
exports.isChildOf = function (child, parent) { | ||
@@ -94,2 +90,13 @@ return child.path.substring(0, child.path.lastIndexOf('.')) === parent.path; | ||
if (previous) { | ||
if (exports.isChoiceOf(current, previous.definition)) { | ||
var type = new attribute_1.Attribute(current); | ||
previous.addChoice(type); | ||
// keep iterating on the snapshot elements using the type attribute as previous | ||
return recBuildAttributes(rest, res, type); | ||
} | ||
if (previous.path.endsWith('[x]') && previous.choices.length === 0) { | ||
// If previous has several types but choices are not constrained, we spread them | ||
// to fill choices | ||
previous.spreadTypes(); | ||
} | ||
if (exports.isSliceOf(current, previous.definition)) { | ||
@@ -109,3 +116,3 @@ var slice = new attribute_1.Attribute(current); | ||
// if current is not a children of previous, try with the parent of previous | ||
return recBuildAttributes(__spreadArrays([current], rest), res, previous.parent); | ||
return recBuildAttributes(attributes, res, previous.parent); | ||
} | ||
@@ -112,0 +119,0 @@ // if there is no previous attribute, append the attribute to the attributes list |
{ | ||
"name": "@arkhn/fhir.ts", | ||
"private": false, | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "Open source typescript library providing FHIR utilities", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -105,2 +105,3 @@ import { Attribute } from 'attribute' | ||
"children": Array [], | ||
"choices": Array [], | ||
"definition": Object { | ||
@@ -196,11 +197,2 @@ "base": Object { | ||
}) | ||
it('handles slice array item', () => { | ||
const array = new Attribute(observationIdentifierDefinition) | ||
const slice = new Attribute(observationCategorySliceDefinition) | ||
const item = array.addItem() | ||
item.addSlice(slice) | ||
expect(slice.tail).toEqual('category[0]') | ||
}) | ||
}) | ||
@@ -250,5 +242,5 @@ | ||
const multiTypeAttr = new Attribute(multitypeAttributeDefinition) | ||
const generated = multiTypeAttr.spreadTypes() | ||
expect(generated).toHaveLength(4) | ||
expect(generated.map(({ path }) => path)).toEqual([ | ||
multiTypeAttr.spreadTypes() | ||
expect(multiTypeAttr.choices).toHaveLength(4) | ||
expect(multiTypeAttr.choices.map(({ path }) => path)).toEqual([ | ||
'valueQuantity', | ||
@@ -272,2 +264,17 @@ 'valueCodeableConcept', | ||
describe('addChoice', () => { | ||
it('adds a choice and update the parent', () => { | ||
const parent = new Attribute(observationCodeDefinition) | ||
const attr = new Attribute(observationIdDefinition) | ||
const choice = new Attribute(observationValueSliceDefinition) | ||
parent.addChild(attr) | ||
attr.addChoice(choice) | ||
expect(choice.parent).toEqual(parent) | ||
expect(attr.choices).toEqual([choice]) | ||
expect(choice.isItem).toBe(false) | ||
expect(choice.index).not.toBeDefined() | ||
}) | ||
}) | ||
describe('addSlice', () => { | ||
@@ -281,3 +288,3 @@ it('adds a slice and update the parent', () => { | ||
expect(slice.parent).toEqual(parent) | ||
expect(slice.parent).toEqual(attr) | ||
expect(attr.slices).toEqual([slice]) | ||
@@ -287,14 +294,2 @@ expect(slice.isItem).toBe(false) | ||
}) | ||
it('handles slice items', () => { | ||
const array = new Attribute(observationIdentifierDefinition) | ||
const slice = new Attribute(observationValueSliceDefinition) | ||
const item = array.addItem() | ||
item.addSlice(slice) | ||
expect(slice.parent).not.toBeDefined() | ||
expect(item.slices).toEqual([slice]) | ||
expect(slice.isItem).toBe(true) | ||
expect(slice.index).toEqual(item.index) | ||
}) | ||
}) | ||
@@ -319,17 +314,2 @@ | ||
it('forwards the index to the slices if any', () => { | ||
const array = new Attribute(observationIdentifierDefinition) | ||
const slice1 = new Attribute(observationValueSliceDefinition) | ||
const slice2 = new Attribute(observationValueSliceDefinition) | ||
const item = array.addItem() | ||
item.addSlice(slice1) | ||
item.addSlice(slice2) | ||
expect(slice1.isItem).toBe(true) | ||
expect(slice1.index).toEqual(item.index) | ||
expect(slice2.isItem).toBe(true) | ||
expect(slice2.index).toEqual(item.index) | ||
}) | ||
it('accepts an optional index', () => { | ||
@@ -336,0 +316,0 @@ const array = new Attribute(observationIdentifierDefinition) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
502336
5626