Skip to content

Commit

Permalink
Added new math functions (#142)
Browse files Browse the repository at this point in the history
* added new json functions

* added bew url functions

* updated the json function description to support array

* updated test case for url encode to include path escaping as well

* added new url function: escapedPath, fixed names of existing functions

* updated: json function descriptions and output data types

* updated: json function descriptions

* added: new math functions
  • Loading branch information
awakchau-tibco committed Sep 23, 2021
1 parent 11a8a44 commit 79c2ef9
Show file tree
Hide file tree
Showing 18 changed files with 706 additions and 0 deletions.
119 changes: 119 additions & 0 deletions function/math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!--
title: MATH
weight: 4601
-->

# Math Functions
This function package exposes common math functions.

## ceil()
Ceil returns the least integer value greater than or equal to input

### Input Args

| Arg | Type | Description |
|:------------|:-------|:-----------------|
| inputNumber | number | The input number |

### Output

| Arg | Type | Description |
|:----------|:-------|:-----------------------------------|
| returnVal | number | The ceil value of the input number |


## floor()
Floor returns the greatest integer value less than or equal to input

### Input Args

| Arg | Type | Description |
|:------------|:-------|:-----------------|
| inputNumber | number | The input number |

### Output

| Arg | Type | Description |
|:----------|:-------|:------------------------------------|
| returnVal | number | The floor value of the input number |


## isNaN()
IsNaN reports whether input is an IEEE 754 "not-a-number" value

### Input Args

| Arg | Type | Description |
|:------|:-------|:------------------|
| input | any | The input to test |

### Output

| Arg | Type | Description |
|:----------|:--------|:--------------------------------------------|
| returnVal | boolean | Is true if input is IEEE 754 "not-a-number" |


## mod()
Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x

### Input Args

| Arg | Type | Description |
|:----|:-------|:----------------------------|
| x | number | The dividend or first input |
| y | number | The divisor or second input |

### Output

| Arg | Type | Description |
|:----------|:-------|:--------------------------------------------------|
| returnVal | number | The remainder of the Euclidean division of x by y |


## round()
Round returns the nearest integer, rounding half away from zero

### Input Args

| Arg | Type | Description |
|:------------|:-------|:-----------------|
| inputNumber | number | The input number |

### Output

| Arg | Type | Description |
|:----------|:-------|:--------------------------------------------------|
| returnVal | number | The nearest integer, rounding half away from zero |


## roundToEven()
RoundToEven returns the nearest integer, rounding ties to even

### Input Args

| Arg | Type | Description |
|:------------|:-------|:-----------------|
| inputNumber | number | The input number |

### Output

| Arg | Type | Description |
|:----------|:-------|:-------------------------------------------|
| returnVal | number | The nearest integer, rounding ties to even |


## trunc()
Trunc returns the integer value of input

### Input Args

| Arg | Type | Description |
|:----|:-------|:----------------------------|
| inputNumber | number | The input number |

### Output

| Arg | Type | Description |
|:----------|:-------|:-----------------------------------------|
| returnVal | number | The truncated integer value of the input |
36 changes: 36 additions & 0 deletions function/math/ceil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package math

import (
"fmt"
"math"

"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
)

func init() {
_ = function.Register(&fnCeil{})
}

type fnCeil struct {
}

// Name returns the name of the function
func (fnCeil) Name() string {
return "ceil"
}

// Sig returns the function signature
func (fnCeil) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeFloat64}, false
}

// Eval executes the function
func (fnCeil) Eval(params ...interface{}) (interface{}, error) {
obj, err := coerce.ToFloat64(params[0])
if err != nil {
return nil, fmt.Errorf("Unable to coerce [%+v] to float64: %s", params[0], err.Error())
}
return math.Ceil(obj), nil
}
15 changes: 15 additions & 0 deletions function/math/ceil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package math

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFnCeil(t *testing.T) {
input := 1.49
f := &fnCeil{}
v, err := f.Eval(input)
assert.Nil(t, err)
assert.Equal(t, 2.0, v)
}
112 changes: 112 additions & 0 deletions function/math/descriptor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"name": "math",
"type": "flogo:function",
"version": "0.1.0",
"title": "Math Functions",
"description": "Common Math Functions",
"homepage": "https://github.com/prject-flogo/contrib/tree/master/function/math",
"functions": [
{
"name": "ceil",
"description": "Ceil returns the least integer value greater than or equal to input",
"example": "math.ceil(1.49) => 2.0",
"args": [
{
"name": "inputNumber",
"type": "number"
}
],
"return": {
"type": "number"
}
},
{
"name": "floor",
"description": "Floor returns the greatest integer value less than or equal to input",
"example": "math.ceil(1.51) => 1.0",
"args": [
{
"name": "inputNumber",
"type": "number"
}
],
"return": {
"type": "number"
}
},
{
"name": "isNaN",
"description": "IsNaN reports whether input is an IEEE 754 \"not-a-number\" value",
"example": "math.isNaN(1.0) => false",
"args": [
{
"name": "input",
"type": "any"
}
],
"return": {
"type": "boolean"
}
},
{
"name": "mod",
"description": "Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x",
"example": "math.mod(7, 4) => 3.0",
"args": [
{
"name": "x",
"type": "number"
},
{
"name": "y",
"type": "number"
}
],
"return": {
"type": "number"
}
},
{
"name": "round",
"description": "Round returns the nearest integer, rounding half away from zero",
"example": "math.round(1.50) => 2.0",
"args": [
{
"name": "inputNumber",
"type": "number"
}
],
"return": {
"type": "number"
}
},
{
"name": "roundToEven",
"description": "RoundToEven returns the nearest integer, rounding ties to even",
"example": "math.round(12.5) => 12.0",
"args": [
{
"name": "inputNumber",
"type": "number"
}
],
"return": {
"type": "number"
}
},
{
"name": "trunc",
"description": "Trunc returns the integer value of input",
"example": "math.round(3.142) => 3.0",
"args": [
{
"name": "inputNumber",
"type": "number"
}
],
"return": {
"type": "number"
}
}
]
}
36 changes: 36 additions & 0 deletions function/math/floor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package math

import (
"fmt"
"math"

"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
)

func init() {
_ = function.Register(&fnFloor{})
}

type fnFloor struct {
}

// Name returns the name of the function
func (fnFloor) Name() string {
return "floor"
}

// Sig returns the function signature
func (fnFloor) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeFloat64}, false
}

// Eval executes the function
func (fnFloor) Eval(params ...interface{}) (interface{}, error) {
obj, err := coerce.ToFloat64(params[0])
if err != nil {
return nil, fmt.Errorf("Unable to coerce [%+v] to float64: %s", params[0], err.Error())
}
return math.Floor(obj), nil
}
15 changes: 15 additions & 0 deletions function/math/floor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package math

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFnFloor(t *testing.T) {
input := 1.51
f := &fnFloor{}
v, err := f.Eval(input)
assert.Nil(t, err)
assert.Equal(t, 1.0, v)
}
8 changes: 8 additions & 0 deletions function/math/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/project-flogo/contrib/function/math

go 1.16

require (
github.com/project-flogo/core v1.5.0
github.com/stretchr/testify v1.7.0
)

0 comments on commit 79c2ef9

Please sign in to comment.