Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ const operators = require('./operators.js')(isTruthy)
const lexical = require('./lexical.js')
const assert = require('../src/util/assert.js')

/**
* Suffix unary operators that can be used in {% if %} expressions.
*
* These mirror the same-named filters in docx-templater/expressions.js so
* that template authors can use identical syntax in both DOCX template tags
* and Liquid computation text (common_cf_text).
*
* Semantics:
* isNotPresent – true when the variable is null, undefined, or false
* isPresent – true when the variable is NOT null, undefined, or false
*
* Usage in templates:
* {% if sd_thermal_risk_type isNotPresent %}
* {% if sd_some_field isPresent %}
*/
var SUFFIX_UNARY_OPERATORS = {
isNotPresent: function (v) { return v === false || v === undefined || v === null },
isPresent: function (v) { return v !== false && v !== undefined && v !== null }
}

function evalExp (exp, scope) {
assert(scope, 'unable to evalExp: scope undefined')
var operatorREs = lexical.operators
Expand All @@ -27,6 +47,17 @@ function evalExp (exp, scope) {
return range
}

// Handle suffix unary operators: isNotPresent, isPresent
// e.g. "sd_thermal_risk_type isNotPresent"
var trimmedExp = exp.trim()
for (var opName in SUFFIX_UNARY_OPERATORS) {
if (trimmedExp.endsWith(' ' + opName)) {
var operandStr = trimmedExp.slice(0, -(opName.length + 1)).trim()
var val = evalValue(operandStr, scope)
return SUFFIX_UNARY_OPERATORS[opName](val)
}
}

return evalValue(exp, scope)
}

Expand Down Expand Up @@ -63,6 +94,18 @@ function validateExpression(exp, scope, errors = []) {
return errors;
}
}

// Handle suffix unary operators in validation
var trimmedExp = exp.trim()
for (var opName in SUFFIX_UNARY_OPERATORS) {
if (trimmedExp.endsWith(' ' + opName)) {
var operandStr = trimmedExp.slice(0, -(opName.length + 1)).trim()
var valError = validateValue(operandStr, scope)
if (valError) errors.push(valError)
return errors;
}
}

if((error = validateValue(exp, scope))) {
errors.push(error);
}
Expand Down
52 changes: 52 additions & 0 deletions test/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,55 @@ describe('expression', function () {
})
})
})

describe('suffix unary operators', function () {
var scope

beforeEach(function () {
scope = Scope.factory({
present_val: 'Common Area',
falsy_val: false,
empty_str: ''
})
})

describe('isNotPresent', function () {
it('should return true when variable is null', function () {
var s = Scope.factory({ v: null })
expect(evalExp('v isNotPresent', s)).to.equal(true)
})
it('should return true when variable is undefined', function () {
var s = Scope.factory({})
expect(evalExp('v isNotPresent', s)).to.equal(true)
})
it('should return true when variable is false', function () {
expect(evalExp('falsy_val isNotPresent', scope)).to.equal(true)
})
it('should return false when variable has a string value', function () {
expect(evalExp('present_val isNotPresent', scope)).to.equal(false)
})
it('should return false when variable is empty string', function () {
expect(evalExp('empty_str isNotPresent', scope)).to.equal(false)
})
})

describe('isPresent', function () {
it('should return false when variable is null', function () {
var s = Scope.factory({ v: null })
expect(evalExp('v isPresent', s)).to.equal(false)
})
it('should return false when variable is undefined', function () {
var s = Scope.factory({})
expect(evalExp('v isPresent', s)).to.equal(false)
})
it('should return false when variable is false', function () {
expect(evalExp('falsy_val isPresent', scope)).to.equal(false)
})
it('should return true when variable has a string value', function () {
expect(evalExp('present_val isPresent', scope)).to.equal(true)
})
it('should return true when variable is empty string', function () {
expect(evalExp('empty_str isPresent', scope)).to.equal(true)
})
})
})