expr-js-story Pub

These are tested separately, for performance testing reasons

The diesel object:

send::  msg ctx.set  (res5<-js{{ diesel.engineId() }})

expect::  (res5 not empty)

send::  msg ctx.set  (res139<-js{{ diesel.env() }})

expect::  (res139 not empty)

The wix object:

send::  msg ctx.set  (res144<-js{{ wix.diesel.env }})

expect::  (res144 not empty)

Javascript and regex

These are not used: send::  msg ctx.set  (a:Number<-1, b:Number<-2)

The input parameters overwrite context above send::  msg expr.js.sum1  (a:Number<-1, b:Number<-2)

expect::  (res39 is 3)

send::  msg expr.js.sum2  (a:Number<-1, b:Number<-2)

expect::  (res42 is 3.0)

send::  msg expr.js.name1  (email<-"jane@doe.com")

expect::  (res40 is "jane")

/$send ctx.setVal (name="cart", value={})

send::  msg expr.js.validateEmail  (email<-"jane@doe.com")

expect::  (ok25 is true)

send::  msg ctx.set  (ok25:Boolean<-false)

send::  msg expr.js.validateEmail  (email<-"janedoe.com")

expect::  (ok25 is false)

Javascript expressions

Simple javascript - script is independent (no arguments):

send::  msg ctx.set  (a225<-js{{ 1+4 }})

expect::  (a225 is 5)

Escaping quotes with javascript regex (notice a230 available inside the js expression):

val a230<-"{"a":32}" send::  msg ctx.set  (a231<-js{{ a230.replace(/"/g, "\"") }})

expect::  (a231 is "{&quot;a&quot;:32}")

More JS examples:

send::  msg ctx.set  (now<-js{{ java.lang.System.currentTimeMillis() }})

send::  msg ctx.set  (later<-js{{ var d = new Date(); d.setSeconds(d.getSeconds() + 10); return d.toISOString(); }})

send::  msg ctx.set  (later<-js{{ var d = new Date(); d.setSeconds(d.getSeconds() + 10); d.toISOString(); }})

send::  msg ctx.set  (noweh<-js{{ var d = new Date(); d.toISOString(); }}, later<-js{{ var d = new Date(); d.setSeconds(d.getSeconds() + 10); d.toISOString(); }})

expect::  (now is number)

JS as lambdas

send::  msg ctx.set  (res57<-"1 2 3 ")

Convert to string and use a JS expression for filter lambda send::  msg ctx.set  (a298:Array<-(([1,2,3] filter ( x->(x > 1)() )) map ( x->(x + "")() )))

send::  msg ctx.echo  (res299<-(res57[="lines"] filter e->js{{ !a298.contains(e) }}()))

expect::  (res299 is ["1"])


Was this useful?    

By: Razie | 2020-01-10 | Tags: story , dsl


Viewed 52 times ( | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2020, all rights reserved.