Expression: Difference between revisions

From ContactsLaw Documentation
No edit summary
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
__FORCETOC__
An '''expression''' refers to either:
An '''expression''' refers to either:
* A literal value, such as a number (e.g. <code>123</code>) or a piece of text (e.g. <code>"Hello world"</code>); or
* A literal value, such as a number (e.g. <code>123</code>) or a piece of text (e.g. <code>"Hello world"</code>); or
* A group of words or symbols that describes how to obtain the above (e.g. <code>Contact.ShortName</code>).
* A group of words or symbols that describes how to obtain a value (e.g. <code>Contact.ShortName</code>, <code>Today()</code>).


[[ContactsLaw]] allows the use of expressions in [[document template|document templates]], automatic descriptions for [[document type|document types]] and [[workgroup|workgroups]], as well as various other areas. This is the basis for generating content that draws upon data from [[contact|contacts]], [[matter|matters]] and so on.
[[ContactsLaw]] allows the use of expressions in [[Document Template|document templates]], automatic descriptions for [[Document Type|document types]] and [[workgroup|workgroups]], as well as various other areas. This is the basis for generating content that draws upon data from [[contact|contacts]], [[matter|matters]] and so on.
 
{{TOC}}


== Types of Expressions ==
== Types of Expressions ==
Line 15: Line 14:
!Remarks
!Remarks
|-
|-
|Literal
|'''Literal'''
|<code>123</code><br /><code>"Hello world"</code>
|<code>123</code><br /><code>"Hello world"</code>
|<code>123</code><br /><code>"Hello world"</code>
|<code>123</code><br /><code>"Hello world"</code>
|To include double-quote characters inside a string literal, prefix with a backslash (i.e. <code>\"</code>).
|To include double-quote characters inside a string literal, prefix with a backslash (i.e. <code>\"</code>).
|-
|-
|Asset
|'''Asset'''
|<code>Contact</code><br /><code>Matter</code><br /><code>Address</code>
|<code>Contact</code><br /><code>Matter</code><br /><code>Address</code>
|
|
|Assets do not produce any result directly, but can be used in conjunction with some functions, e.g. <code>Count(Contact)</code>might evaluate to <code>3</code> if the asset represented three parties on a matter.
|[[Asset|Assets]] do not produce any result directly, but can be used in conjunction with some functions, e.g. <code>Count(Contact)</code>might evaluate to <code>3</code> if the asset represented three parties on a matter.
|-
|-
|Property
|'''Property'''
|<code>Contact.ShortName</code><br /><code>Contact.Gender</code><br /><code>Matter.Code</code><br /><code>Address.Country</code>
|<code>Contact.ShortName</code><br /><code>Contact.Gender</code><br /><code>Matter.Code</code><br /><code>Address.Country</code>
|<code>"John Smith"</code><br /><code>"Male"</code><br /><code>"678-1"</code><br /><code>"Australia"</code>
|<code>"John Smith"</code><br /><code>"Male"</code><br /><code>"678-1"</code><br /><code>"Australia"</code>
|Properties belong to assets. If an asset represents multiple contacts/matters/etc then the expression produces multiple results. Unless you specify otherwise, multiple results appear as a list of values, separated by commas (e.g. <code>"John Smith, Fred Baker, Jane Doe"</code>).
|Properties belong to assets. If an asset resolves to multiple contacts/matters/etc then the expression produces a '''set'''. Unless you specify otherwise, sets appear as a list of values, separated by commas (e.g. <code>"John Smith, Fred Baker, Jane Doe"</code>).
|-
|-
|Operator
|'''Operator'''
|<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code><br />
|<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code><br />
<code>=</code>, <code><></code>, <code><</code>, <code>></code>
<code>=</code>, <code><></code>, <code><</code>, <code>></code>
Line 36: Line 35:
|Arithmetic operators compute numbers. Conditional operators produce either <code>Yes</code> or <code>No</code>.
|Arithmetic operators compute numbers. Conditional operators produce either <code>Yes</code> or <code>No</code>.
|-
|-
|Function
|'''Function'''
|<code>IsNotEmpty(Contact.LastName)</code><br /><code>Plural("word")</code><br /><code>If(Count(Contact) < 3, "friends", "acquaintances")</code>
|<code>IsNotEmpty(Contact.LastName)</code><br /><code>Plural("word")</code><br /><code>If(Count(Contact) < 3, "friends", "acquaintances")</code>
|<code>Yes</code><br /><code>"words"</code><br /><code>"acquaintances"</code>
|<code>Yes</code><br /><code>"words"</code><br /><code>"acquaintances"</code>
|Functions compute a result based on their arguments (inputs). Function calls can be nested within each other.
|[[Function|Functions]] compute a result based on their parameters (inputs). Function calls can be nested within each other.
|-
|-
|Constant
|'''Constant'''
|<code>NewLine</code><br /><code>NewParagraph</code>
|<code>NewLine</code><br /><code>NewParagraph</code>
|<code>"␊"</code><br /><code>"␍"</code>
|<code>"␊"</code><br /><code>"␍"</code>
|Constant expressions provide a short-hand way of inserting frequently-used values, as well as text that cannot normally be entered directly (e.g. tab spaces).
|Constant expressions provide a short-hand way of inserting frequently-used values, as well as text that cannot normally be entered directly (e.g. tab spaces).
|-
|-
|Repetition
|'''Repetition'''
|<code>current.ShortName</code><br /><code>current.Country</code><br /><code>index</code>
|<code>current.ShortName</code><br /><code>current.Country</code><br /><code>index</code>
|<code>"John Smith"</code><br /><code>"Australia"</code><br /><code>3</code>
|<code>"John Smith"</code><br /><code>"Australia"</code><br /><code>3</code>
Line 52: Line 51:
|}
|}


== Function Reference ==
== Desktop App ==
The [[Desktop App]] includes various features to assist in writing and utilising expressions:


=== Text ===
* '''Rich text editing''' - In situations where content may consist of formatted text and expressions, you can edit expressions directly. Text formatted as an expression appears in a monospaced font with a grey background.
'''An'''(''word'')
* '''Syntax highlighting''' - To improve readability, the various components of expressions are colour-coded. Syntax errors are underlined (in the same manner as spelling errors in normal text).
Produces 'a' or 'an' according to grammar rules for a particular word.
* '''Automatic completion''' - Suggestions for assets, functions and other components of expressions appear as you type. You can double-click or press the ''Tab'' key to accept a suggestion.
* '''Inline documentation''' - Hover the mouse over components of an expression to show the relevant documentation.
* '''Previews''' - In many cases, you can toggle between editing an expression and previewing the result (using example data).


* ''<code>word</code>'' - Word.
== Empty Expressions ==
Several operators and functions exhibit different behaviour depending on whether an expression is "empty".


'''Filter'''(''text'', [''allowed'', ...])
The following are all considered empty:
Filters text to allow only specific values.


* ''<code>text</code>'' - Text.
* The <code>Empty</code> constant
* ''<code>allowed</code>'' - Allowed values.
* An empty string literal; i.e. <code>""</code>
* A property of an asset that has not been specified
* A set that does not contain ''any'' values
* A set that contains ''only'' empty values


'''IsAre'''(''value'')
Empty expressions may produce different results depending on how they are used. For example: counting the number of values in a set that does not contain any values will yield zero, while counting the number of values in a set consisting of the <code>Empty</code> constant will yield <code>1</code>.
When there are two or more values, produces 'are'. Otherwise, produces 'is'.


* ''<code>value</code>'' - Number or set of values.
Various [[Function|functions]] are provided to ignore or filter out empty values in situations where they are not desired.


'''Lower'''(''text'')
== Operator Reference ==
Converts text to 'lowercase'.
This section documents each of the operators that can be used in expressions.


* ''<code>text</code>'' - Text.
=== Mathematical ===
These operators work with whole and decimal numbers only. The result is also a number.
{| class="wikitable"
|+
!Operator
!Purpose
|-
|<code>+</code>
|Add
|-
|<code>-</code>
|Subtract
|-
|<code>*</code>
|Multiply
|-
|<code>/</code>
|Divide
|-
|<code>%</code>
|Modulus (remainder)
|}
Numbers formatted as text are treated as numbers. Empty values are treated as zero, except when adding two empty values (the result of which is also empty).


'''Optional'''([''values'', ...])
=== Comparison ===
Joins consecutive values together. If any value is empty, the result is also empty.
These operators work with numbers and date/time values. The result is either ''yes'' or ''no''.
{| class="wikitable"
|+
!Operator
!Synonym(s)
!Purpose
|-
|<code>=</code>
|<code>eq</code>
|Is equal to
|-
|<code><></code>
|<code>!=</code>, <code>neq</code>
|Is not equal to
|-
|<code><</code>
|<code>lt</code>
|Is less than
|-
|<code><=</code>
|<code>lte</code>
|Is less than or equal to
|-
|<code>></code>
|<code>gt</code>
|Is greater than
|-
|<code>>=</code>
|<code>gte</code>
|Is greater than or equal to
|}
Numbers formatted as text are treated as numbers. Date/time values formatted as text are treated as dates including time; if not specified, the time is assumed to be midnight. For all other date/time values, the comparison takes time into account ''only'' if the value on the left-hand side of the operator includes time. The result of comparing an empty value with a non-empty value is always ''no''.


* ''<code>values</code>'' - Any value.
=== Conditional ===
These operators work with yes/no values. The result is also either ''yes'' or ''no''.
{| class="wikitable"
|+
!Operator
!Purpose
|-
|<code>and</code>
|And (''yes'' if both are ''yes'')
|-
|<code>or</code>
|Or (''yes'' if either are ''yes'')
|-
|<code>not</code>
|Not (''no'' if ''yes'', ''yes'' if ''no'')
|}
Other types of values are converted to ''yes'' or ''no'' according to the following rules:


'''Plural'''(''word'')
* '''Text''' - ''No'' if empty, "no", "false" or "0"; otherwise ''yes''.
Converts a word to its plural.
* '''Number''' - ''No'' if zero; otherwise ''yes''.


* ''<code>word</code>'' - Singular form of the word.
=== Text ===
 
These operators work with text. The result is also text.
'''PluralOf'''(''value'', ''word'')
{| class="wikitable"
Converts a word to its plural when there are two or more values.
|+
 
!Operator
* ''<code>value</code>'' - Number or set of values.
!Synonyms
* ''<code>word</code>'' - Singular form of the word.
!Purpose
 
|-
'''Possessive'''(''word'')
|<code>&</code>
Adds a possessive apostrophe to a word according to grammar rules.
|<code>+</code>
 
|Concatenate (join)
* ''<code>word</code>'' - Word.
|}
 
Note: The <code>+</code> operator can be used for both addition and concatenation; if at least one value is a number then addition is performed. Use the <code>&</code> operator to avoid ambiguity.
'''Replace'''(''text'', ''find'', ''replace'')
[[Category:Key Terms]]
Replaces all occurrences of a particular word/phrase in the text.
 
* ''<code>text</code>'' - Subject text.
* ''<code>find</code>'' - Word/phrase to find. Can be a [[wikipedia:Regular_expression|regular expression]].
* ''<code>replace</code>'' - Replacement text. Can include capture groups.
 
'''ReplaceWith'''(''text'', ''find'', ''replace'')
Replaces the entire value if it contains a particular word/phrase.
 
* ''<code>text</code>'' - Subject text.
* ''<code>find</code>'' - Word/phrase to find. Can be a [[wikipedia:Regular_expression|regular expression]].
* ''<code>replace</code>'' - Replacement text. Can include capture groups.
 
'''Sentence'''(''text'')
Converts text to 'Sentence case'.
 
* ''<code>text</code>'' - Text.
 
'''Title'''(''text'')
Converts text to 'Title Case'.
 
* ''<code>text</code>'' - Text.
 
'''Upper'''(''text'')
Converts text to 'UPPERCASE'.
 
* ''<code>text</code>'' - Text.
 
=== Number ===
'''Number'''(''number'', [''thousands'' = Yes])
Formats a whole number as text.
 
* ''<code>number</code>'' - Number.
* ''<code>thousands</code>'' - Use thousands separator (Yes/No).
 
'''Decimal'''(''number'', [''decimals'' = 2], [''thousands'' = Yes])
Formats a decimal number as text.
 
* ''<code>number</code>'' - Number.
* ''<code>decimals</code>'' - Decimal places.
* ''<code>thousands</code>'' - Use thousands separator (Yes/No).
 
'''Currency'''(''number'', [''decimals'' = 2], [''thousands'' = Yes])
Formats a currency value as text.
 
* ''<code>number</code>'' - Number.
* ''<code>decimals</code>'' - Decimal places.
* ''<code>thousands</code>'' - Use thousands separator (Yes/No).
 
'''Min'''(''set'')
Calculates the smallest value in a set.
 
* ''<code>set</code>'' - A set of values.
 
'''Max'''(''set'')
Calculates the largest value in a set.
 
* ''<code>set</code>'' - A set of values.
 
'''Sum'''(''set'')
Calculates the sum of the values in a set.
 
* ''<code>set</code>'' - A set of values.
 
'''Avg'''(''set'')
Calculates the average of a set.
 
* ''<code>set</code>'' - A set of values.
 
'''NumberInWords'''(''number'')
Converts a number to words.
 
* ''<code>number</code>'' - Number.
 
'''CurrencyInWords'''(''number'')
Converts currency to an amount in words.
 
* ''<code>number</code>'' - Number.
 
'''WordsToNumber'''(''text'')
Converts words to a number.
 
* ''<code>text</code>'' - Text.
 
'''WordsToCurrency'''(''text'')
Converts an amount in words to currency.
 
* ''<code>text</code>'' - Text.
 
'''Ordinal'''(''number'')
Adds the ordinal suffix (st/nd/rd/th) to a number.
 
* ''<code>number</code>'' - Number.

Latest revision as of 13:12, 20 August 2025

An expression refers to either:

  • A literal value, such as a number (e.g. 123) or a piece of text (e.g. "Hello world"); or
  • A group of words or symbols that describes how to obtain a value (e.g. Contact.ShortName, Today()).

ContactsLaw allows the use of expressions in document templates, automatic descriptions for document types and workgroups, as well as various other areas. This is the basis for generating content that draws upon data from contacts, matters and so on.

Types of Expressions

Type Example Expressions Example Results Remarks
Literal 123
"Hello world"
123
"Hello world"
To include double-quote characters inside a string literal, prefix with a backslash (i.e. \").
Asset Contact
Matter
Address
Assets do not produce any result directly, but can be used in conjunction with some functions, e.g. Count(Contact)might evaluate to 3 if the asset represented three parties on a matter.
Property Contact.ShortName
Contact.Gender
Matter.Code
Address.Country
"John Smith"
"Male"
"678-1"
"Australia"
Properties belong to assets. If an asset resolves to multiple contacts/matters/etc then the expression produces a set. Unless you specify otherwise, sets appear as a list of values, separated by commas (e.g. "John Smith, Fred Baker, Jane Doe").
Operator +, -, *, /

=, <>, <, >

Arithmetic operators compute numbers. Conditional operators produce either Yes or No.
Function IsNotEmpty(Contact.LastName)
Plural("word")
If(Count(Contact) < 3, "friends", "acquaintances")
Yes
"words"
"acquaintances"
Functions compute a result based on their parameters (inputs). Function calls can be nested within each other.
Constant NewLine
NewParagraph
"␊"
"␍"
Constant expressions provide a short-hand way of inserting frequently-used values, as well as text that cannot normally be entered directly (e.g. tab spaces).
Repetition current.ShortName
current.Country
index
"John Smith"
"Australia"
3
These expressions can only be used inside a repetition block, where they correspond to either the current instance of the repeated asset or a number indicating how many times the block has been repeated so far.

Desktop App

The Desktop App includes various features to assist in writing and utilising expressions:

  • Rich text editing - In situations where content may consist of formatted text and expressions, you can edit expressions directly. Text formatted as an expression appears in a monospaced font with a grey background.
  • Syntax highlighting - To improve readability, the various components of expressions are colour-coded. Syntax errors are underlined (in the same manner as spelling errors in normal text).
  • Automatic completion - Suggestions for assets, functions and other components of expressions appear as you type. You can double-click or press the Tab key to accept a suggestion.
  • Inline documentation - Hover the mouse over components of an expression to show the relevant documentation.
  • Previews - In many cases, you can toggle between editing an expression and previewing the result (using example data).

Empty Expressions

Several operators and functions exhibit different behaviour depending on whether an expression is "empty".

The following are all considered empty:

  • The Empty constant
  • An empty string literal; i.e. ""
  • A property of an asset that has not been specified
  • A set that does not contain any values
  • A set that contains only empty values

Empty expressions may produce different results depending on how they are used. For example: counting the number of values in a set that does not contain any values will yield zero, while counting the number of values in a set consisting of the Empty constant will yield 1.

Various functions are provided to ignore or filter out empty values in situations where they are not desired.

Operator Reference

This section documents each of the operators that can be used in expressions.

Mathematical

These operators work with whole and decimal numbers only. The result is also a number.

Operator Purpose
+ Add
- Subtract
* Multiply
/ Divide
% Modulus (remainder)

Numbers formatted as text are treated as numbers. Empty values are treated as zero, except when adding two empty values (the result of which is also empty).

Comparison

These operators work with numbers and date/time values. The result is either yes or no.

Operator Synonym(s) Purpose
= eq Is equal to
<> !=, neq Is not equal to
< lt Is less than
<= lte Is less than or equal to
> gt Is greater than
>= gte Is greater than or equal to

Numbers formatted as text are treated as numbers. Date/time values formatted as text are treated as dates including time; if not specified, the time is assumed to be midnight. For all other date/time values, the comparison takes time into account only if the value on the left-hand side of the operator includes time. The result of comparing an empty value with a non-empty value is always no.

Conditional

These operators work with yes/no values. The result is also either yes or no.

Operator Purpose
and And (yes if both are yes)
or Or (yes if either are yes)
not Not (no if yes, yes if no)

Other types of values are converted to yes or no according to the following rules:

  • Text - No if empty, "no", "false" or "0"; otherwise yes.
  • Number - No if zero; otherwise yes.

Text

These operators work with text. The result is also text.

Operator Synonyms Purpose
& + Concatenate (join)

Note: The + operator can be used for both addition and concatenation; if at least one value is a number then addition is performed. Use the & operator to avoid ambiguity.