Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
13 changes: 10 additions & 3 deletions docs/4_secondary_admin_controls/expressions/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ There are various supported functions, and we are willing to add more. Let us kn

The currently supported functions are:

##### General operations

**length(val)**

Find the length of the item passed in.
* For a strings it will return the number of unicode graphemes
* For arrays, the number of elements
* For JSON or other objects, it will return the number of properties
* For numbers it will return the length of the string representation

##### Numeric operations

**round(val)**
Expand Down Expand Up @@ -110,7 +120,6 @@ Find the index of the first occurrence of a value within the provided string.

Optionally provide an offset to start the search from.


**lastIndexOf(val, find, offset)**

Find the index of the last occurrence of a value within the provided string.
Expand Down Expand Up @@ -141,7 +150,6 @@ eg `encode("Companion","hex")` gives `"436f6d70616e696f6e"`

Decode a string from the requested format ('hex','base64'). If `enc` is missing, `latin1` will be used.


eg `decode("436f6d70616e696f6e","hex")` gives `"Companion"`

**parseVariables(string)**
Expand Down Expand Up @@ -204,7 +212,6 @@ eg `00:10:15` gives 615

You can do the reverse of this with `secondsToTimestamp(str)`


**secondsToTimestamp(seconds, format)**

Convert a number of seconds into a timestamp of format 'HH:mm:ss'.
Expand Down
23 changes: 22 additions & 1 deletion shared-lib/lib/Expression/ExpressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ import { JSONPath } from 'jsonpath-plus'

// Note: when adding new functions, make sure to update the docs!
export const ExpressionFunctions: Record<string, (...args: any[]) => any> = {
// General operations
length: (v) => {
let len = 0
if (v === undefined || v === null) {
len = 0
} else if (Array.isArray(v)) {
len = v.length
} else if (typeof v === 'object') {
len = Object.keys(v).length
} else if (typeof v === 'number') {
len = (v + '').length
} else if (typeof v === 'bigint') {
len = v.toString().length
} else if (v instanceof RegExp) {
len = v.toString().length
} else {
len = v.length
}
return len
},

// Number operations
// TODO: round to fractionals, without fp issues
round: (v) => Math.round(v),
Expand Down Expand Up @@ -74,7 +95,7 @@ export const ExpressionFunctions: Record<string, (...args: any[]) => any> = {
// Bool operations
bool: (v) => !!v && v !== 'false' && v !== '0',

// Object operations
// Object/array operations
jsonpath: (obj, path) => {
const shouldParseInput = typeof obj === 'string'
if (shouldParseInput) {
Expand Down
32 changes: 32 additions & 0 deletions shared-lib/test/expressions-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@
import { ExpressionFunctions } from '../lib/Expression/ExpressionFunctions.js'

describe('functions', () => {
describe('general', () => {
it('length', () => {
expect(ExpressionFunctions.length()).toBe(0)
expect(ExpressionFunctions.length('')).toBe(0)
expect(ExpressionFunctions.length('a')).toBe(1)
expect(ExpressionFunctions.length('abc')).toBe(3)
expect(ExpressionFunctions.length('ä')).toBe(1) // codepoint U+00E4, one grapheme
// expect(ExpressionFunctions.length('̈a')).toBe(1) // codepoints U+0308 U+0061, one grapheme
// expect(ExpressionFunctions.length('́̈a')).toBe(1) // codepoints U+0301 U+0308 U+0061, one grapheme
expect(ExpressionFunctions.length(9)).toBe(1)
expect(ExpressionFunctions.length(99)).toBe(2)
expect(ExpressionFunctions.length(-123)).toBe(4)
expect(ExpressionFunctions.length(3.14)).toBe(4)
expect(ExpressionFunctions.length(BigInt(1024))).toBe(4)
expect(ExpressionFunctions.length(BigInt(9007199254740991))).toBe(16)
expect(ExpressionFunctions.length(new RegExp('ab+c', 'i'))).toBe(7)

Check failure on line 20 in shared-lib/test/expressions-functions.test.ts

View workflow job for this annotation

GitHub Actions / test

shared-lib/test/expressions-functions.test.ts > functions > general > length

AssertionError: expected +0 to be 7 // Object.is equality - Expected + Received - 7 + 0 ❯ shared-lib/test/expressions-functions.test.ts:20:64
expect(ExpressionFunctions.length([])).toBe(0)
expect(ExpressionFunctions.length([9])).toBe(1)
expect(ExpressionFunctions.length([99])).toBe(1)
expect(ExpressionFunctions.length(['abc'])).toBe(1)
expect(ExpressionFunctions.length([9, 'a'])).toBe(2)
expect(ExpressionFunctions.length(['a', 'c'])).toBe(2)
expect(ExpressionFunctions.length(['ab', ''])).toBe(2)
expect(ExpressionFunctions.length([1, , 3])).toBe(3)
expect(ExpressionFunctions.length(['a', 'b', 'c'])).toBe(3)
expect(ExpressionFunctions.length(['a', ['b', 'b'], 'c'])).toBe(3)
expect(ExpressionFunctions.length({ a: 1 })).toBe(1)
expect(ExpressionFunctions.length({ a: 1, b: { c: 5 } })).toBe(2)
expect(ExpressionFunctions.length({ a: ['a', 'c'], b: { c: 5 } })).toBe(2)
})
})

describe('number', () => {
it('round', () => {
expect(ExpressionFunctions.round(9.99)).toBe(10)
Expand Down
Loading