How can I use ExpressionEngineFunction expressions in javascript code?

#1

In javascript application logic, is there a way to use expressions defined in ExpressionEngineFunction? Calling a method name of type ExpressionEngineFunction ( i.e. ExpressionEngineFunction.dateTime(someArgs) ) returns “No engine could be found” error.

0 Likes

#2

I was wondering this for a long time as well - I just saw @danielle.youngsmith answer her own post for the answer to this, thanks Danielle!

Expr.eval('dateTime("2018-01-01")')
DateTime {_ms: 1514764800000, _offsetMinutes: undefined, _setMonth: undefined, _pad: undefined, _formatOffset: undefined, …}
4 Likes

#3

nice @scott.kruyswyk! Yes, Expr.eval("expression") is especially useful for quick testing of transformation logic in the static console.

But be wary of the results you get from Expr.eval() – it’s been my experience that it’s not a 1:1 mapping between functions in this API and ExpressionEngineFunctions, and many EEFunctions are not available in Expr.eval and will throw errors (generally a ‘NotImplementedException’) even if they’re proven to work in your transforms.

Depending on the use-case, there may be an alternative Type and API or JS method/library with similar ExpressionEngine functionality. For example, to get a timestamp from a date string like '2018-01-01', you could run both of the following:

DateTime('2018-01-01')  // C3 Type
DateTime {_ms: 1514764800000, _offsetMinutes: undefined, _setMonth: undefined, _pad: undefined, _formatOffset: undefined, …}

or

Date('2018-01-01')  // standard JS object
DateTime {_ms: 1514764800000, _offsetMinutes: undefined, _setMonth: undefined, _pad: undefined, _formatOffset: undefined, …}

On the matter of testing transformation logic more easily, look out for c3Tools Data Management when it launches, which has an on-the-fly transform expression validator, @boaz.stossel is the man for this!

2 Likes

Handling scientific notation in transforms
NotImplementedException: rollup in CompoundMetric
#4

Just for reference, currently in C3 JavaScript runtimes, (Date === DateTime) evaluates to true. DateTime is not a C3 type, but rather a C3 JavaScript Type System helper class.

1 Like

#5

Now that we’re here, is there a quick rule about interoperation between pure JS, JS TS and TS objects? For example, I noticed that TestApi.createEntity seems to accept JSON only, but not a mixture of already created TS objects and JSON; otherwise, it throws unintuitive errors.

0 Likes