diff --git a/src/datatypes/quantity.ts b/src/datatypes/quantity.ts index 9ce78103..88ec48f2 100644 --- a/src/datatypes/quantity.ts +++ b/src/datatypes/quantity.ts @@ -167,6 +167,10 @@ export class Quantity { } return new Quantity(resultValue, resultUnit); } + + round(precision?: number) { + return new Quantity(this.value.round(precision), this.unit); + } } export function parseQuantity(str: string) { diff --git a/src/elm/arithmetic.ts b/src/elm/arithmetic.ts index dc681843..88bf0d26 100644 --- a/src/elm/arithmetic.ts +++ b/src/elm/arithmetic.ts @@ -292,6 +292,9 @@ export class Round extends Expression { } const dec = this.precision != null ? await this.precision.execute(ctx) : 0; + if (arg instanceof Quantity) { + return arg.round(dec); + } return Decimal.from(arg).round(dec); } } diff --git a/src/elm/string.ts b/src/elm/string.ts index cd855b81..3eb84356 100644 --- a/src/elm/string.ts +++ b/src/elm/string.ts @@ -170,6 +170,26 @@ export class Matches extends Expression { return null; } + // "The Matches operator returns true if the given string matches the given regular expression pattern. + // Regular expressions should [...] use partial matching, use single line mode, and allow Unicode characters. + // The start/end of line markers ^ and $ can be used to match the entire string." + return new RegExp(pattern).test(string); + } +} + +export class MatchesFull extends Expression { + constructor(json: any) { + super(json); + } + + async exec(ctx: Context) { + const [string, pattern] = await this.execArgs(ctx); + if (string == null || pattern == null) { + return null; + } + + // "The MatchesFull operator returns true when the value completely matches the given regular expression + // (implying that the start/end of line markers ^, $ are always surrounding the regex expression provided)." return new RegExp('^' + pattern + '$').test(string); } } diff --git a/test/datatypes/quantity-test.ts b/test/datatypes/quantity-test.ts index 4d555555..50eb21e1 100644 --- a/test/datatypes/quantity-test.ts +++ b/test/datatypes/quantity-test.ts @@ -105,6 +105,21 @@ describe('Quantity', () => { should(quantity1.dividedBy(quantity2)).be.null(); }); + it('should allow rounding', () => { + const quantity = new Quantity(3.456, 'mg'); + let result = quantity.round(); + result.unit.should.equal('mg'); + result.value.should.equalDecimal(3); + + result = quantity.round(0); + result.unit.should.equal('mg'); + result.value.should.equalDecimal(3); + + result = quantity.round(1); + result.unit.should.equal('mg'); + result.value.should.equalDecimal(3.5); + }); + it('should convert units when possible to perform arithmetic', () => { const divide = new Quantity(8, 'm').dividedBy(new Quantity(50, 'cm')); divide.equals(new Quantity(16, '1')).should.be.true(); diff --git a/test/elm/arithmetic/arithmetic-test.ts b/test/elm/arithmetic/arithmetic-test.ts index 86089cd5..4bb46ee2 100644 --- a/test/elm/arithmetic/arithmetic-test.ts +++ b/test/elm/arithmetic/arithmetic-test.ts @@ -821,6 +821,17 @@ describe('Round', () => { (await this.roundNegativeHalfNullPrecision.exec(this.ctx)).should.equalDecimal(-2.0); (await this.roundNegativeHalfZeroPrecision.exec(this.ctx)).should.equalDecimal(-2.0); }); + + it('should support rounding Quantity values', async function () { + const quantityRound = await this.quantityRound.exec(this.ctx); // Round(2.54 'cm') --> 3 'cm' + validateQuantity(quantityRound, 3, 'cm'); + + const quantityRoundZero = await this.quantityRoundZero.exec(this.ctx); // Round(2.54 'cm', 0) --> 3 'cm' + validateQuantity(quantityRoundZero, 3, 'cm'); + + const quantityRoundOne = await this.quantityRoundOne.exec(this.ctx); // Round(2.54 'cm', 1) --> 2.5 'cm' + validateQuantity(quantityRoundOne, 2.5, 'cm'); + }); }); describe('Successor', () => { diff --git a/test/elm/arithmetic/data.cql b/test/elm/arithmetic/data.cql index 7a82bb03..28f236e8 100644 --- a/test/elm/arithmetic/data.cql +++ b/test/elm/arithmetic/data.cql @@ -219,6 +219,9 @@ define RoundPositiveHalfZeroPrecision: Round(1.5, 0) define RoundNegativeHalfOmittedPrecision: Round(-1.5) define RoundNegativeHalfNullPrecision: Round(-1.5, null as Integer) define RoundNegativeHalfZeroPrecision: Round(-1.5, 0) +define QuantityRound: Round(2.54 'cm') // 3 'cm' +define QuantityRoundZero: Round(2.54 'cm', 0) // 3 'cm' +define QuantityRoundOne: Round(2.54 'cm', 1) // 2.5 'cm' // @Test: Ln define ln: Ln(4) diff --git a/test/elm/arithmetic/data.js b/test/elm/arithmetic/data.js index 6a92c3a8..fc59154c 100644 --- a/test/elm/arithmetic/data.js +++ b/test/elm/arithmetic/data.js @@ -13328,6 +13328,9 @@ define RoundPositiveHalfZeroPrecision: Round(1.5, 0) define RoundNegativeHalfOmittedPrecision: Round(-1.5) define RoundNegativeHalfNullPrecision: Round(-1.5, null as Integer) define RoundNegativeHalfZeroPrecision: Round(-1.5, 0) +define QuantityRound: Round(2.54 'cm') // 3 'cm' +define QuantityRoundZero: Round(2.54 'cm', 0) // 3 'cm' +define QuantityRoundOne: Round(2.54 'cm', 1) // 2.5 'cm' */ module.exports['Round'] = { @@ -13342,7 +13345,7 @@ module.exports['Round'] = { "type" : "Annotation", "t" : [ ], "s" : { - "r" : "340", + "r" : "375", "s" : [ { "value" : [ "", "library TestSnippet version '1'" ] } ] @@ -14144,6 +14147,149 @@ module.exports['Round'] = { "annotation" : [ ] } } + }, { + "localId" : "354", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Decimal", + "name" : "QuantityRound", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "354", + "s" : [ { + "value" : [ "", "define ", "QuantityRound", ": " ] + }, { + "r" : "359", + "s" : [ { + "r" : "355", + "value" : [ "Round", "(", "2.54", ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "Round", + "localId" : "359", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Quantity", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "360", + "name" : "{urn:hl7-org:elm-types:r1}Quantity", + "annotation" : [ ] + } ], + "operand" : { + "type": "Quantity", + "value": 2.54, + "unit": "cm" + } + } + }, { + "localId" : "363", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Quantity", + "name" : "QuantityRoundZero", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "363", + "s" : [ { + "value" : [ "// 3 'cm'\n", "define ", "QuantityRoundZero", ": " ] + }, { + "r" : "370", + "s" : [ { + "r" : "364", + "value" : [ "Round", "(", "2.54", ", ", "0", ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "Round", + "localId" : "370", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Quantity", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "371", + "name" : "{urn:hl7-org:elm-types:r1}Quantity", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "372", + "name" : "{urn:hl7-org:elm-types:r1}Integer", + "annotation" : [ ] + } ], + "operand" : { + "type": "Quantity", + "value": 2.54, + "unit": "cm" + }, + "precision" : { + "type" : "Literal", + "localId" : "365", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Integer", + "valueType" : "{urn:hl7-org:elm-types:r1}Integer", + "value" : "0", + "annotation" : [ ] + } + } + }, { + "localId" : "375", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Quantity", + "name" : "QuantityRoundOne", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "375", + "s" : [ { + "value" : [ "// 3 'cm'\n", "define ", "QuantityRoundOne", ": " ] + }, { + "r" : "382", + "s" : [ { + "r" : "376", + "value" : [ "Round", "(", "2.54", " , ", "1", ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "Round", + "localId" : "382", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Quantity", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "383", + "name" : "{urn:hl7-org:elm-types:r1}Quantity", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "384", + "name" : "{urn:hl7-org:elm-types:r1}Integer", + "annotation" : [ ] + } ], + "operand" : { + "type": "Quantity", + "value": 2.54, + "unit": "cm" + }, + "precision" : { + "type" : "Literal", + "localId" : "377", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Integer", + "valueType" : "{urn:hl7-org:elm-types:r1}Integer", + "value" : "1", + "annotation" : [ ] + } + } } ] } } diff --git a/test/elm/string/data.cql b/test/elm/string/data.cql index 0715a225..765ea922 100644 --- a/test/elm/string/data.cql +++ b/test/elm/string/data.cql @@ -60,10 +60,19 @@ define NullIndex: 'HelloWorld'[null] // @Test: Matches define MatchesTrue: Matches('1,2three', '\\d,\\d\\w+') define MatchesFalse: Matches('three', '\\d') +define MatchesPartial: Matches('12three', '\\d') define MatchesIsNullFirst: Matches(null, '\\w+') define MatchesIsNullSecond: Matches('12three', null) define MatchesAllNull: Matches(null, null) +// @Test: MatchesFull +define MatchesTrue: MatchesFull('1,2three', '\\d,\\d\\w+') +define MatchesFalse: MatchesFull('three', '\\d') +define MatchesPartial: MatchesFull('12three', '\\d') +define MatchesIsNullFirst: MatchesFull(null, '\\w+') +define MatchesIsNullSecond: MatchesFull('12three', null) +define MatchesAllNull: MatchesFull(null, null) + // @Test: PositionOf define Found: PositionOf('cde', 'abcdefg') define NotFound: PositionOf('fgh', 'abcdefg') diff --git a/test/elm/string/data.js b/test/elm/string/data.js index cc6beffe..eac0d598 100644 --- a/test/elm/string/data.js +++ b/test/elm/string/data.js @@ -5493,6 +5493,7 @@ using Simple version '1.0.0' context Patient define MatchesTrue: Matches('1,2three', '\\d,\\d\\w+') define MatchesFalse: Matches('three', '\\d') +define MatchesPartial: Matches('12three', '\\d') define MatchesIsNullFirst: Matches(null, '\\w+') define MatchesIsNullSecond: Matches('12three', null) define MatchesAllNull: Matches(null, null) @@ -5510,7 +5511,7 @@ module.exports['Matches'] = { "type" : "Annotation", "t" : [ ], "s" : { - "r" : "272", + "r" : "286", "s" : [ { "value" : [ "", "library TestSnippet version '1'" ] } ] @@ -5719,7 +5720,7 @@ module.exports['Matches'] = { }, { "localId" : "242", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", - "name" : "MatchesIsNullFirst", + "name" : "MatchesPartial", "context" : "Patient", "accessLevel" : "Public", "annotation" : [ { @@ -5728,16 +5729,22 @@ module.exports['Matches'] = { "s" : { "r" : "242", "s" : [ { - "value" : [ "", "define ", "MatchesIsNullFirst", ": " ] + "value" : [ "", "define ", "MatchesPartial", ": " ] }, { "r" : "251", "s" : [ { + "value" : [ "Matches", "(" ] + }, { "r" : "243", - "value" : [ "Matches", "(", "null", ", " ] + "s" : [ { + "value" : [ "'12three'" ] + } ] }, { - "r" : "244", + "value" : [ ", " ] + }, { + "r" : "245", "s" : [ { - "value" : [ "'\\\\w+'" ] + "value" : [ "'\\\\d'" ] } ] }, { "value" : [ ")" ] @@ -5751,31 +5758,92 @@ module.exports['Matches'] = { "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", "annotation" : [ ], "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "252", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { "type" : "NamedTypeSpecifier", "localId" : "253", "name" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ] + } ], + "operand" : [ { + "type" : "Literal", + "localId" : "243", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "12three", + "annotation" : [ ] + }, { + "type" : "Literal", + "localId" : "245", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "\\d", + "annotation" : [ ] + } ] + } + }, { + "localId" : "256", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesIsNullFirst", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "256", + "s" : [ { + "value" : [ "", "define ", "MatchesIsNullFirst", ": " ] + }, { + "r" : "265", + "s" : [ { + "r" : "257", + "value" : [ "Matches", "(", "null", ", " ] + }, { + "r" : "258", + "s" : [ { + "value" : [ "'\\\\w+'" ] + } ] + }, { + "value" : [ ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "Matches", + "localId" : "265", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "267", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] }, { "type" : "NamedTypeSpecifier", - "localId" : "254", + "localId" : "268", "name" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ] } ], "operand" : [ { "type" : "As", - "localId" : "252", + "localId" : "266", "asType" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ], "signature" : [ ], "operand" : { "type" : "Null", - "localId" : "243", + "localId" : "257", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", "annotation" : [ ] } }, { "type" : "Literal", - "localId" : "244", + "localId" : "258", "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", "valueType" : "{urn:hl7-org:elm-types:r1}String", "value" : "\\w+", @@ -5783,7 +5851,7 @@ module.exports['Matches'] = { } ] } }, { - "localId" : "257", + "localId" : "271", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", "name" : "MatchesIsNullSecond", "context" : "Patient", @@ -5792,20 +5860,20 @@ module.exports['Matches'] = { "type" : "Annotation", "t" : [ ], "s" : { - "r" : "257", + "r" : "271", "s" : [ { "value" : [ "", "define ", "MatchesIsNullSecond", ": " ] }, { - "r" : "266", + "r" : "280", "s" : [ { "value" : [ "Matches", "(" ] }, { - "r" : "258", + "r" : "272", "s" : [ { "value" : [ "'12three'" ] } ] }, { - "r" : "260", + "r" : "274", "value" : [ ", ", "null", ")" ] } ] } ] @@ -5813,43 +5881,43 @@ module.exports['Matches'] = { } ], "expression" : { "type" : "Matches", - "localId" : "266", + "localId" : "280", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", "annotation" : [ ], "signature" : [ { "type" : "NamedTypeSpecifier", - "localId" : "268", + "localId" : "282", "name" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ] }, { "type" : "NamedTypeSpecifier", - "localId" : "269", + "localId" : "283", "name" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ] } ], "operand" : [ { "type" : "Literal", - "localId" : "258", + "localId" : "272", "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", "valueType" : "{urn:hl7-org:elm-types:r1}String", "value" : "12three", "annotation" : [ ] }, { "type" : "As", - "localId" : "267", + "localId" : "281", "asType" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ], "signature" : [ ], "operand" : { "type" : "Null", - "localId" : "260", + "localId" : "274", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", "annotation" : [ ] } } ] } }, { - "localId" : "272", + "localId" : "286", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", "name" : "MatchesAllNull", "context" : "Patient", @@ -5858,13 +5926,13 @@ module.exports['Matches'] = { "type" : "Annotation", "t" : [ ], "s" : { - "r" : "272", + "r" : "286", "s" : [ { "value" : [ "", "define ", "MatchesAllNull", ": " ] }, { - "r" : "281", + "r" : "295", "s" : [ { - "r" : "273", + "r" : "287", "value" : [ "Matches", "(", "null", ", ", "null", ")" ] } ] } ] @@ -5872,41 +5940,539 @@ module.exports['Matches'] = { } ], "expression" : { "type" : "Matches", - "localId" : "281", + "localId" : "295", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", "annotation" : [ ], "signature" : [ { "type" : "NamedTypeSpecifier", - "localId" : "284", + "localId" : "298", "name" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ] }, { "type" : "NamedTypeSpecifier", - "localId" : "285", + "localId" : "299", "name" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ] } ], "operand" : [ { "type" : "As", - "localId" : "282", + "localId" : "296", "asType" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ], "signature" : [ ], "operand" : { "type" : "Null", - "localId" : "273", + "localId" : "287", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", "annotation" : [ ] } }, { "type" : "As", - "localId" : "283", + "localId" : "297", "asType" : "{urn:hl7-org:elm-types:r1}String", "annotation" : [ ], "signature" : [ ], "operand" : { "type" : "Null", - "localId" : "274", + "localId" : "288", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", + "annotation" : [ ] + } + } ] + } + } ] + } + } +} + +/* MatchesFull +library TestSnippet version '1' +using Simple version '1.0.0' +context Patient +define MatchesTrue: MatchesFull('1,2three', '\\d,\\d\\w+') +define MatchesFalse: MatchesFull('three', '\\d') +define MatchesPartial: MatchesFull('12three', '\\d') +define MatchesIsNullFirst: MatchesFull(null, '\\w+') +define MatchesIsNullSecond: MatchesFull('12three', null) +define MatchesAllNull: MatchesFull(null, null) +*/ + +module.exports['MatchesFull'] = { + "library" : { + "localId" : "0", + "annotation" : [ { + "type" : "CqlToElmInfo", + "translatorVersion" : "4.2.0", + "translatorOptions" : "EnableDateRangeOptimization,EnableAnnotations,EnableResultTypes", + "signatureLevel" : "All" + }, { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "286", + "s" : [ { + "value" : [ "", "library TestSnippet version '1'" ] + } ] + } + } ], + "identifier" : { + "id" : "TestSnippet", + "version" : "1" + }, + "schemaIdentifier" : { + "id" : "urn:hl7-org:elm", + "version" : "r1" + }, + "usings" : { + "def" : [ { + "localId" : "1", + "localIdentifier" : "System", + "uri" : "urn:hl7-org:elm-types:r1", + "annotation" : [ ] + }, { + "localId" : "206", + "localIdentifier" : "Simple", + "uri" : "https://github.com/cqframework/cql-execution/simple", + "version" : "1.0.0", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "206", + "s" : [ { + "value" : [ "", "using " ] + }, { + "s" : [ { + "value" : [ "Simple" ] + } ] + }, { + "value" : [ " version '1.0.0'" ] + } ] + } + } ] + } ] + }, + "contexts" : { + "def" : [ { + "localId" : "211", + "name" : "Patient", + "annotation" : [ ] + } ] + }, + "statements" : { + "def" : [ { + "localId" : "209", + "name" : "Patient", + "context" : "Patient", + "annotation" : [ ], + "expression" : { + "type" : "SingletonFrom", + "localId" : "210", + "annotation" : [ ], + "signature" : [ ], + "operand" : { + "type" : "Retrieve", + "localId" : "208", + "dataType" : "{https://github.com/cqframework/cql-execution/simple}Patient", + "annotation" : [ ], + "include" : [ ], + "codeFilter" : [ ], + "dateFilter" : [ ], + "otherFilter" : [ ] + } + } + }, { + "localId" : "214", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesTrue", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "214", + "s" : [ { + "value" : [ "", "define ", "MatchesTrue", ": " ] + }, { + "r" : "223", + "s" : [ { + "value" : [ "Matches", "(" ] + }, { + "r" : "215", + "s" : [ { + "value" : [ "'1,2three'" ] + } ] + }, { + "value" : [ ", " ] + }, { + "r" : "217", + "s" : [ { + "value" : [ "'\\\\d,\\\\d\\\\w+'" ] + } ] + }, { + "value" : [ ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "MatchesFull", + "localId" : "223", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "224", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "225", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + } ], + "operand" : [ { + "type" : "Literal", + "localId" : "215", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "1,2three", + "annotation" : [ ] + }, { + "type" : "Literal", + "localId" : "217", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "\\d,\\d\\w+", + "annotation" : [ ] + } ] + } + }, { + "localId" : "228", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesFalse", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "228", + "s" : [ { + "value" : [ "", "define ", "MatchesFalse", ": " ] + }, { + "r" : "237", + "s" : [ { + "value" : [ "Matches", "(" ] + }, { + "r" : "229", + "s" : [ { + "value" : [ "'three'" ] + } ] + }, { + "value" : [ ", " ] + }, { + "r" : "231", + "s" : [ { + "value" : [ "'\\\\d'" ] + } ] + }, { + "value" : [ ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "MatchesFull", + "localId" : "237", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "238", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "239", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + } ], + "operand" : [ { + "type" : "Literal", + "localId" : "229", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "three", + "annotation" : [ ] + }, { + "type" : "Literal", + "localId" : "231", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "\\d", + "annotation" : [ ] + } ] + } + }, { + "localId" : "242", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesPartial", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "242", + "s" : [ { + "value" : [ "", "define ", "MatchesPartial", ": " ] + }, { + "r" : "251", + "s" : [ { + "value" : [ "Matches", "(" ] + }, { + "r" : "243", + "s" : [ { + "value" : [ "'12three'" ] + } ] + }, { + "value" : [ ", " ] + }, { + "r" : "245", + "s" : [ { + "value" : [ "'\\\\d'" ] + } ] + }, { + "value" : [ ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "MatchesFull", + "localId" : "251", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "252", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "253", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + } ], + "operand" : [ { + "type" : "Literal", + "localId" : "243", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "12three", + "annotation" : [ ] + }, { + "type" : "Literal", + "localId" : "245", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "\\d", + "annotation" : [ ] + } ] + } + }, { + "localId" : "256", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesIsNullFirst", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "256", + "s" : [ { + "value" : [ "", "define ", "MatchesIsNullFirst", ": " ] + }, { + "r" : "265", + "s" : [ { + "r" : "257", + "value" : [ "Matches", "(", "null", ", " ] + }, { + "r" : "258", + "s" : [ { + "value" : [ "'\\\\w+'" ] + } ] + }, { + "value" : [ ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "MatchesFull", + "localId" : "265", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "267", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "268", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + } ], + "operand" : [ { + "type" : "As", + "localId" : "266", + "asType" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ], + "signature" : [ ], + "operand" : { + "type" : "Null", + "localId" : "257", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", + "annotation" : [ ] + } + }, { + "type" : "Literal", + "localId" : "258", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "\\w+", + "annotation" : [ ] + } ] + } + }, { + "localId" : "271", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesIsNullSecond", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "271", + "s" : [ { + "value" : [ "", "define ", "MatchesIsNullSecond", ": " ] + }, { + "r" : "280", + "s" : [ { + "value" : [ "Matches", "(" ] + }, { + "r" : "272", + "s" : [ { + "value" : [ "'12three'" ] + } ] + }, { + "r" : "274", + "value" : [ ", ", "null", ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "MatchesFull", + "localId" : "280", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "282", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "283", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + } ], + "operand" : [ { + "type" : "Literal", + "localId" : "272", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}String", + "valueType" : "{urn:hl7-org:elm-types:r1}String", + "value" : "12three", + "annotation" : [ ] + }, { + "type" : "As", + "localId" : "281", + "asType" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ], + "signature" : [ ], + "operand" : { + "type" : "Null", + "localId" : "274", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", + "annotation" : [ ] + } + } ] + } + }, { + "localId" : "286", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "name" : "MatchesAllNull", + "context" : "Patient", + "accessLevel" : "Public", + "annotation" : [ { + "type" : "Annotation", + "t" : [ ], + "s" : { + "r" : "286", + "s" : [ { + "value" : [ "", "define ", "MatchesAllNull", ": " ] + }, { + "r" : "295", + "s" : [ { + "r" : "287", + "value" : [ "Matches", "(", "null", ", ", "null", ")" ] + } ] + } ] + } + } ], + "expression" : { + "type" : "MatchesFull", + "localId" : "295", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Boolean", + "annotation" : [ ], + "signature" : [ { + "type" : "NamedTypeSpecifier", + "localId" : "298", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + }, { + "type" : "NamedTypeSpecifier", + "localId" : "299", + "name" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ] + } ], + "operand" : [ { + "type" : "As", + "localId" : "296", + "asType" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ], + "signature" : [ ], + "operand" : { + "type" : "Null", + "localId" : "287", + "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", + "annotation" : [ ] + } + }, { + "type" : "As", + "localId" : "297", + "asType" : "{urn:hl7-org:elm-types:r1}String", + "annotation" : [ ], + "signature" : [ ], + "operand" : { + "type" : "Null", + "localId" : "288", "resultTypeName" : "{urn:hl7-org:elm-types:r1}Any", "annotation" : [ ] } diff --git a/test/elm/string/string-test.ts b/test/elm/string/string-test.ts index 42eeeebc..c64ac4e4 100644 --- a/test/elm/string/string-test.ts +++ b/test/elm/string/string-test.ts @@ -155,11 +155,45 @@ describe('Matches', () => { setup(this, data); }); - it('should match and return true', async function () { + it('should return true for entire string matching regex', async function () { (await this.matchesTrue.exec(this.ctx)).should.be.true(); }); - it('should not match and return false', async function () { + it('should return true for partial string matching regex', async function () { + (await this.matchesPartial.exec(this.ctx)).should.be.true(); + }); + + it('should return false for nonmatching string', async function () { + (await this.matchesFalse.exec(this.ctx)).should.be.false(); + }); + + it('should not match when string to match is null and return null', async function () { + should(await this.matchesIsNullFirst.exec(this.ctx)).be.null(); + }); + + it('should not match and return null', async function () { + should(await this.matchesIsNullSecond.exec(this.ctx)).be.null(); + }); + + it('should return null if both arguments are null', async function () { + should(await this.matchesAllNull.exec(this.ctx)).be.null(); + }); +}); + +describe('MatchesFull', () => { + beforeEach(function () { + setup(this, data); + }); + + it('should return true for entire string matching regex', async function () { + (await this.matchesTrue.exec(this.ctx)).should.be.true(); + }); + + it('should return false for partial string matching regex', async function () { + (await this.matchesPartial.exec(this.ctx)).should.be.false(); + }); + + it('should return false for nonmatching string', async function () { (await this.matchesFalse.exec(this.ctx)).should.be.false(); });