Skip to content

Commit

Permalink
Merge branch 'add-random-number-function' of https://github.com/kaaqu…
Browse files Browse the repository at this point in the history
…ist/feel-scala into kaaquist-add-random-number-function
  • Loading branch information
saig0 committed Oct 6, 2022
2 parents 4258847 + e96dcd1 commit ad33eab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,16 @@ even(5)
even(2)
// true
```

## random number()

Returns number between 0 and 1

- parameters:
- NaN
- result: number

```js
random number()
// 0.9701618132579795
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.camunda.feel.syntaxtree.{
}

import scala.math.BigDecimal.RoundingMode
import scala.util.Random

object NumericBuiltinFunctions {

Expand All @@ -32,7 +33,8 @@ object NumericBuiltinFunctions {
"round up" -> List(roundUpFunction),
"round down" -> List(roundDownFunction),
"round half up" -> List(roundHalfUpFunction),
"round half down" -> List(roundHalfDownFunction)
"round half down" -> List(roundHalfDownFunction),
"random number" -> List(randomNumberFunction)
)

private def decimalFunction = builtinFunction(
Expand Down Expand Up @@ -164,4 +166,10 @@ object NumericBuiltinFunctions {
case List(ValNumber(n), ValNumber(scale)) =>
round(n, scale, RoundingMode.HALF_DOWN)
})

private def randomNumberFunction =
builtinFunction(params = List(), invoke = {
case List() =>
ValNumber(Random.nextDouble())
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.camunda.feel.impl.builtin

import org.camunda.feel.impl.FeelIntegrationTest
import org.camunda.feel.syntaxtree
import org.camunda.feel.syntaxtree._
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
Expand Down Expand Up @@ -259,4 +260,17 @@ class BuiltinNumberFunctionsTest
eval(" round half down(-1.126, 2) ") should be(ValNumber(-1.13))
}


"A random number() function" should "return a number" in {

eval(" random number() ") shouldBe a [ValNumber]
}

it should "return a number between 0.0 and 1.0 " in {

eval(" random number() ") match {
case ValNumber(x) => x should (be >= BigDecimal(0) and be <= BigDecimal(1))
case other => fail()
}
}
}

0 comments on commit ad33eab

Please sign in to comment.