Skip to content

Commit

Permalink
Merge pull request #1 from fisuda/update
Browse files Browse the repository at this point in the history
Add round-off feature
  • Loading branch information
fisuda committed Dec 5, 2019
2 parents a2948af + df18757 commit 632a4dd
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"env": {
"browser": true
"browser": true,
"es6": true
},
"globals": {
"MashupPlatform": false,
Expand Down
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js
node_js:
- "lts/*"
sudo: false
addons:
chrome: stable
firefox: latest-esr

before_install:
- npm install grunt-cli -g

script:
- xvfb-run grunt ci

branches:
only:
- master
3 changes: 0 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ module.exports = function (grunt) {
'src/js/*.js',
'tests/js/*Spec.js'
],
exclude: [
'src/js/main.js',
],
frameworks: ['jasmine'],
reporters: ['progress', 'coverage'],
browsers: ['Chrome', 'Firefox'],
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Calculator list operator operator

The Calculator list operator operator is a WireCloud operator that provides ...
[![](https://nexus.lab.fiware.org/repository/raw/public/badges/chapters/visualization.svg)](https://www.fiware.org/developers/catalogue/)
[![License: MIT](https://img.shields.io/github/license/lets-fiware/calculator-list-operator.svg)](https://opensource.org/licenses/MIT)<br/>
[![Build Status](https://travis-ci.org/lets-fiware/calculator-list-operator.svg?branch=master)](https://travis-ci.org/lets-fiware/calculator-list-operator)
[![Coverage Status](https://coveralls.io/repos/github/lets-fiware/calculator-list-operator/badge.svg?branch=master)](https://coveralls.io/github/lets-fiware/calculator-list-operator?branch=master)

The Calculator list operator operator is a WireCloud operator that allows to calculate a value of a formula which is set in settings with value list which comes from input endpoint.

## Build dependencies

Expand Down
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
"description": "This package.json file is only used for installing npm dependencies. But this is not an installable node package, but a WireCloud operator. Take a look into src/config.xml for more details about this operator",
"devDependencies": {
"grunt": "^1.0.4",
"mock-applicationmashup": "^1.0.0-e",
"gruntify-eslint": "^5.0.0",
"jasmine-core": "^3.5.0",
"grunt-contrib-clean": "^2.0.0",
"grunt-contrib-compress": "^1.6.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-coveralls": "^2.0.0",
"grunt-karma": "^3.0.2",
"grunt-strip-code": "^1.0.6",
"grunt-text-replace": "~0.4.0",
"grunt-wirecloud": "^0.9.7",
"gruntify-eslint": "^5.0.0",
"jasmine-core": "^3.5.0",
"karma": "^4.4.1",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.1",
"karma-firefox-launcher": "^1.2.0",
"karma-jasmine": "^2.0.1",
"karma-junit-reporter": "^2.0.1",
"grunt-contrib-clean": "^2.0.0",
"grunt-contrib-compress": "^1.6.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-strip-code": "^1.0.6",
"grunt-text-replace": "~0.4.0",
"grunt-wirecloud": "^0.9.7",
"mock-applicationmashup": "^1.0.0-e",
"wirecloud-config-parser": "^0.2.2"
},
"private": true
Expand Down
22 changes: 20 additions & 2 deletions src/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@

<preferences>
<preference name="formula" type="text" label="formula" description="formula" default="A * 100"/>
<preference name="mode" type="list" label="When invalid value" description="when error occur" default="exception">
<option label="Throw exception" value="exception"/>
<option label="Remove value" value="remove"/>
<option label="Pass through" value="pass"/>
</preference>
<preference name="math" type="list" label="Math" description="Math" default="round">
<option label="no operation" value="none"/>
<option label="round" value="round"/>
<option label="floor" value="floor"/>
<option label="ceil" value="ceil"/>
<option label="trunc" value="trunc"/>
</preference>
<preference name="point" type="list" label="Decimal point" description="after the decimal point" default="integer">
<option label="integer" value="integer"/>
<option label="first decimal place" value="first"/>
<option label="second decimal place" value="second"/>
<option label="third decimal place" value="third"/>
</preference>
<preference name="send_nulls" type="boolean" label="Send Nulls" description="Enable this option to propagate null values, leave it disable to filter null events" default="true" />
</preferences>

<wiring>
<inputendpoint name="input" type="text" label="input" description="input list" friendcode="list" />
<outputendpoint name="output" type="text" label="output" description="output list" friendcode="list" />
<inputendpoint name="input" type="text" label="input" description="A"/>
<outputendpoint name="output" type="text" label="output" description="output"/>
</wiring>

<scripts>
Expand Down
2 changes: 1 addition & 1 deletion src/doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## v0.1.0
## v0.1.0 (2019-12-05)

Initial version
12 changes: 11 additions & 1 deletion src/doc/userguide.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
## Introduction

Calculator list operator
The Calculator list operator is a WireCloud operator that allows to calculate a value of a formula which is set in settings with value list which comes from input endpoint.

## Settings

- **formula:** Set a formula. It's able to use arithmetic operators, parentheses and 'A' for the input value. e.g. (100*A)+1
- **Action when an invalid value:** Action when an invalid value received. Throw exception, remove value or pass through.
- **Math:** Math function for roundfing off. No operation, Math.round, Math.floor, Math.ceil or Math.trunc.
- **Decimal point:** Rounding digits, Integer, first decimal place, second decimal place or third decimal place.
- **Send Nulls:** Enable this option to propagate null values, leave it disable to filter null events.

## Wiring

### Input Endpoints

- Input : List of Number e.g. [123, "567"]

### Output Endpoints

- Output : List of Number calculated and rounded off e.g. [1230, 5670]

## Usage

## Reference
Expand Down
67 changes: 48 additions & 19 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

"use strict";

MashupPlatform.prefs.registerCallback(function (new_preferences) {

}.bind(this));

var parseInputEndpointData = function parseInputEndpointData(data) {
if (typeof data === "string") {
try {
Expand All @@ -30,39 +26,72 @@
return data;
};

var safeEval = function safeEval(formula) {
for (var i = 0; i < formula.length; i++) {
if (' 0123456789.()+-*/A'.indexOf(formula.substr(i, 1)) === -1) {
return null;
}
var pushEvent = function pushEvent(data) {
if (MashupPlatform.operator.outputs.output.connected) {
MashupPlatform.wiring.pushEvent("output", data);
}
return new Function('A', '"use strict";return (' + formula + ')');
}

var formula = safeEval(MashupPlatform.prefs.get('formula'));
var mathTable = {"none": "", "round": "Math.round", "floor": "Math.floor", "ceil": "Math.ceil", "trunc": "Math.trunc" };
var shiftTable = {"integer": "1", "first": "10", "second": "100", "third": "1000" };

var safeEval = function safeEval() {
var formula = MashupPlatform.prefs.get('formula');
if (formula != "") {
for (var i = 0; i < formula.length; i++) {
if (' 0123456789.()+-*/A'.indexOf(formula.substr(i, 1)) === -1) {
throw new MashupPlatform.wiring.EndpointTypeError();
}
}
} else {
formula = 'A';
}

MashupPlatform.prefs.registerCallback(function (new_preferences) {
formula = safeEval(MashupPlatform.prefs.get('formula'));
}.bind(this));
var round = mathTable[MashupPlatform.prefs.get('math')];
var shift = shiftTable[MashupPlatform.prefs.get('point')];
round = round + ((round === "" || shift === "1") ? '(A)' : '(A*' + shift + ')/' + shift);

return new Function('A', '"use strict";A=parseFloat(A);A=(' + formula + ');return (' + round + ');');
}

var calculatorList = function calculatorList(list) {
var formula = safeEval();

MashupPlatform.wiring.registerCallback("input", function (list) {
list = parseInputEndpointData(list);

if (list != null) {
var newList = list.map(function (value) {
if (!isNaN(value) && formula != null) {
if (!isNaN(value)) {
return formula(value);
} else {
throw new MashupPlatform.wiring.EndpointTypeError();
var mode = MashupPlatform.prefs.get('mode');
if (mode === "exception") {
throw new MashupPlatform.wiring.EndpointTypeError();
} else if (mode === "pass") {
return value;
} // remove
}
return null;
});
MashupPlatform.wiring.pushEvent("output", newList);

newList = newList.filter(v => v);
pushEvent(newList);

} else {
if (MashupPlatform.prefs.get("send_nulls")) {
MashupPlatform.wiring.pushEvent("output", list);
}
}
});
}

/* TODO
* this if is required for testing, but we have to search a cleaner way
*/
if (window.MashupPlatform != null) {
MashupPlatform.wiring.registerCallback("input", calculatorList);
}

/* test-code */
window.calculatorList = calculatorList;
/* end-test-code */
})();

0 comments on commit 632a4dd

Please sign in to comment.