Skip to content

Commit

Permalink
feat: implement dateTime namespace (fixes #143)
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr committed Sep 7, 2023
1 parent 8c1a9e3 commit 932b2bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/evaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {ExprNode} from '../nodeTypes'
import {
DateTime,
FALSE_VALUE,
fromDateTime,
fromJS,
fromNumber,
fromPath,
Expand Down Expand Up @@ -645,6 +646,12 @@ math.avg = async function (args, scope, execute) {
}
math.avg.arity = 1

const dateTime: FunctionSet = {}
dateTime.now = async function now(args, scope, execute) {
return fromDateTime(new DateTime(scope.context.timestamp))
}
dateTime.now.arity = 0

export const namespaces: NamespaceSet = {
global: _global,
string,
Expand All @@ -654,4 +661,5 @@ export const namespaces: NamespaceSet = {
diff,
sanity,
math,
dateTime,
}
19 changes: 19 additions & 0 deletions test/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ t.test('Functions', async (t) => {
})
})

t.test('dateTime::now()', async (t) => {
t.test('returns the same value as dateTime(now())', async (t) => {
const query = `{"a": dateTime(now()), "b": dateTime::now()}`
const tree = parse(query)
const value = await evaluate(tree)
const data = await value.get()
t.same(data.a, data.b)
})

t.test('is a dateTime object', async (t) => {
// This is not possible with `now()`:
const query = `string(dateTime::now() + 1000)`
const tree = parse(query)
const value = await evaluate(tree)
const data = await value.get()
t.type(data, 'string')
})
})

t.test('upper()', async (t) => {
t.test('uppercases the given string', async (t) => {
const tree = parse('upper("abc")')
Expand Down

0 comments on commit 932b2bc

Please sign in to comment.