Skip to content

Commit 79c2ef9

Browse files
Added new math functions (#142)
* 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
1 parent 11a8a44 commit 79c2ef9

18 files changed

+706
-0
lines changed

function/math/README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<!--
2+
title: MATH
3+
weight: 4601
4+
-->
5+
6+
# Math Functions
7+
This function package exposes common math functions.
8+
9+
## ceil()
10+
Ceil returns the least integer value greater than or equal to input
11+
12+
### Input Args
13+
14+
| Arg | Type | Description |
15+
|:------------|:-------|:-----------------|
16+
| inputNumber | number | The input number |
17+
18+
### Output
19+
20+
| Arg | Type | Description |
21+
|:----------|:-------|:-----------------------------------|
22+
| returnVal | number | The ceil value of the input number |
23+
24+
25+
## floor()
26+
Floor returns the greatest integer value less than or equal to input
27+
28+
### Input Args
29+
30+
| Arg | Type | Description |
31+
|:------------|:-------|:-----------------|
32+
| inputNumber | number | The input number |
33+
34+
### Output
35+
36+
| Arg | Type | Description |
37+
|:----------|:-------|:------------------------------------|
38+
| returnVal | number | The floor value of the input number |
39+
40+
41+
## isNaN()
42+
IsNaN reports whether input is an IEEE 754 "not-a-number" value
43+
44+
### Input Args
45+
46+
| Arg | Type | Description |
47+
|:------|:-------|:------------------|
48+
| input | any | The input to test |
49+
50+
### Output
51+
52+
| Arg | Type | Description |
53+
|:----------|:--------|:--------------------------------------------|
54+
| returnVal | boolean | Is true if input is IEEE 754 "not-a-number" |
55+
56+
57+
## mod()
58+
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
59+
60+
### Input Args
61+
62+
| Arg | Type | Description |
63+
|:----|:-------|:----------------------------|
64+
| x | number | The dividend or first input |
65+
| y | number | The divisor or second input |
66+
67+
### Output
68+
69+
| Arg | Type | Description |
70+
|:----------|:-------|:--------------------------------------------------|
71+
| returnVal | number | The remainder of the Euclidean division of x by y |
72+
73+
74+
## round()
75+
Round returns the nearest integer, rounding half away from zero
76+
77+
### Input Args
78+
79+
| Arg | Type | Description |
80+
|:------------|:-------|:-----------------|
81+
| inputNumber | number | The input number |
82+
83+
### Output
84+
85+
| Arg | Type | Description |
86+
|:----------|:-------|:--------------------------------------------------|
87+
| returnVal | number | The nearest integer, rounding half away from zero |
88+
89+
90+
## roundToEven()
91+
RoundToEven returns the nearest integer, rounding ties to even
92+
93+
### Input Args
94+
95+
| Arg | Type | Description |
96+
|:------------|:-------|:-----------------|
97+
| inputNumber | number | The input number |
98+
99+
### Output
100+
101+
| Arg | Type | Description |
102+
|:----------|:-------|:-------------------------------------------|
103+
| returnVal | number | The nearest integer, rounding ties to even |
104+
105+
106+
## trunc()
107+
Trunc returns the integer value of input
108+
109+
### Input Args
110+
111+
| Arg | Type | Description |
112+
|:----|:-------|:----------------------------|
113+
| inputNumber | number | The input number |
114+
115+
### Output
116+
117+
| Arg | Type | Description |
118+
|:----------|:-------|:-----------------------------------------|
119+
| returnVal | number | The truncated integer value of the input |

function/math/ceil.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package math
2+
3+
import (
4+
"fmt"
5+
"math"
6+
7+
"github.com/project-flogo/core/data"
8+
"github.com/project-flogo/core/data/coerce"
9+
"github.com/project-flogo/core/data/expression/function"
10+
)
11+
12+
func init() {
13+
_ = function.Register(&fnCeil{})
14+
}
15+
16+
type fnCeil struct {
17+
}
18+
19+
// Name returns the name of the function
20+
func (fnCeil) Name() string {
21+
return "ceil"
22+
}
23+
24+
// Sig returns the function signature
25+
func (fnCeil) Sig() (paramTypes []data.Type, isVariadic bool) {
26+
return []data.Type{data.TypeFloat64}, false
27+
}
28+
29+
// Eval executes the function
30+
func (fnCeil) Eval(params ...interface{}) (interface{}, error) {
31+
obj, err := coerce.ToFloat64(params[0])
32+
if err != nil {
33+
return nil, fmt.Errorf("Unable to coerce [%+v] to float64: %s", params[0], err.Error())
34+
}
35+
return math.Ceil(obj), nil
36+
}

function/math/ceil_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package math
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFnCeil(t *testing.T) {
10+
input := 1.49
11+
f := &fnCeil{}
12+
v, err := f.Eval(input)
13+
assert.Nil(t, err)
14+
assert.Equal(t, 2.0, v)
15+
}

function/math/descriptor.json

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"name": "math",
3+
"type": "flogo:function",
4+
"version": "0.1.0",
5+
"title": "Math Functions",
6+
"description": "Common Math Functions",
7+
"homepage": "https://github.com/prject-flogo/contrib/tree/master/function/math",
8+
"functions": [
9+
{
10+
"name": "ceil",
11+
"description": "Ceil returns the least integer value greater than or equal to input",
12+
"example": "math.ceil(1.49) => 2.0",
13+
"args": [
14+
{
15+
"name": "inputNumber",
16+
"type": "number"
17+
}
18+
],
19+
"return": {
20+
"type": "number"
21+
}
22+
},
23+
{
24+
"name": "floor",
25+
"description": "Floor returns the greatest integer value less than or equal to input",
26+
"example": "math.ceil(1.51) => 1.0",
27+
"args": [
28+
{
29+
"name": "inputNumber",
30+
"type": "number"
31+
}
32+
],
33+
"return": {
34+
"type": "number"
35+
}
36+
},
37+
{
38+
"name": "isNaN",
39+
"description": "IsNaN reports whether input is an IEEE 754 \"not-a-number\" value",
40+
"example": "math.isNaN(1.0) => false",
41+
"args": [
42+
{
43+
"name": "input",
44+
"type": "any"
45+
}
46+
],
47+
"return": {
48+
"type": "boolean"
49+
}
50+
},
51+
{
52+
"name": "mod",
53+
"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",
54+
"example": "math.mod(7, 4) => 3.0",
55+
"args": [
56+
{
57+
"name": "x",
58+
"type": "number"
59+
},
60+
{
61+
"name": "y",
62+
"type": "number"
63+
}
64+
],
65+
"return": {
66+
"type": "number"
67+
}
68+
},
69+
{
70+
"name": "round",
71+
"description": "Round returns the nearest integer, rounding half away from zero",
72+
"example": "math.round(1.50) => 2.0",
73+
"args": [
74+
{
75+
"name": "inputNumber",
76+
"type": "number"
77+
}
78+
],
79+
"return": {
80+
"type": "number"
81+
}
82+
},
83+
{
84+
"name": "roundToEven",
85+
"description": "RoundToEven returns the nearest integer, rounding ties to even",
86+
"example": "math.round(12.5) => 12.0",
87+
"args": [
88+
{
89+
"name": "inputNumber",
90+
"type": "number"
91+
}
92+
],
93+
"return": {
94+
"type": "number"
95+
}
96+
},
97+
{
98+
"name": "trunc",
99+
"description": "Trunc returns the integer value of input",
100+
"example": "math.round(3.142) => 3.0",
101+
"args": [
102+
{
103+
"name": "inputNumber",
104+
"type": "number"
105+
}
106+
],
107+
"return": {
108+
"type": "number"
109+
}
110+
}
111+
]
112+
}

function/math/floor.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package math
2+
3+
import (
4+
"fmt"
5+
"math"
6+
7+
"github.com/project-flogo/core/data"
8+
"github.com/project-flogo/core/data/coerce"
9+
"github.com/project-flogo/core/data/expression/function"
10+
)
11+
12+
func init() {
13+
_ = function.Register(&fnFloor{})
14+
}
15+
16+
type fnFloor struct {
17+
}
18+
19+
// Name returns the name of the function
20+
func (fnFloor) Name() string {
21+
return "floor"
22+
}
23+
24+
// Sig returns the function signature
25+
func (fnFloor) Sig() (paramTypes []data.Type, isVariadic bool) {
26+
return []data.Type{data.TypeFloat64}, false
27+
}
28+
29+
// Eval executes the function
30+
func (fnFloor) Eval(params ...interface{}) (interface{}, error) {
31+
obj, err := coerce.ToFloat64(params[0])
32+
if err != nil {
33+
return nil, fmt.Errorf("Unable to coerce [%+v] to float64: %s", params[0], err.Error())
34+
}
35+
return math.Floor(obj), nil
36+
}

function/math/floor_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package math
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFnFloor(t *testing.T) {
10+
input := 1.51
11+
f := &fnFloor{}
12+
v, err := f.Eval(input)
13+
assert.Nil(t, err)
14+
assert.Equal(t, 1.0, v)
15+
}

function/math/go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/project-flogo/contrib/function/math
2+
3+
go 1.16
4+
5+
require (
6+
github.com/project-flogo/core v1.5.0
7+
github.com/stretchr/testify v1.7.0
8+
)

0 commit comments

Comments
 (0)