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 1, 2019
2 parents ed3d51d + a6c1fd5 commit 6b3ce0b
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 35 deletions.
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
11 changes: 8 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
* Licensed under the MIT license.
*/

/*
* round-off-value
* https://github.com/lets-fiware/round-off-value-operator
*
* Copyright (c) 2019 Kazuhito Suda
* Licensed under the MIT license.
*/

var ConfigParser = require('wirecloud-config-parser');
var parser = new ConfigParser('src/config.xml');

Expand Down Expand Up @@ -120,9 +128,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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Calculator Operator operator
# Calculator operator

The Calculator 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-operator.svg)](https://opensource.org/licenses/MIT)<br/>
[![Build Status](https://travis-ci.org/lets-fiware/calculator-operator.svg?branch=master)](https://travis-ci.org/lets-fiware/calculator-operator)
[![Coverage Status](https://coveralls.io/repos/github/lets-fiware/calculator-operator/badge.svg?branch=master)](https://coveralls.io/github/lets-fiware/calculator-operator?branch=master)

The Calculator operator is a WireCloud operator that allows to calculate a value of a formula which is set in settings with value 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
21 changes: 20 additions & 1 deletion src/config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<operator xmlns="http://wirecloud.conwet.fi.upm.es/ns/macdescription/1" vendor="FISUDA" name="calculator-operator" version="0.1.0">
<operator xmlns="http://wirecloud.conwet.fi.upm.es/ns/macdescription/1" vendor="FISUDA" name="calculator-operator" version="0.1.1">

<details>
<title>Calculator Operator</title>
Expand All @@ -23,6 +23,25 @@

<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>
Expand Down
6 changes: 5 additions & 1 deletion src/doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.0
## v0.1.1 (2019-12-01)

- Add round-off feature

## v0.1.0 (2019-11-16)

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 Operator
The Calculator operator is a WireCloud operator that allows to calculate a value of a formula which is set in settings with value 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 : Number e.g. 123 or "123"

### Output Endpoints

- Output : Number calculated and rounded off e.g. 123

## Usage

## Reference
Expand Down
66 changes: 52 additions & 14 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,65 @@

"use strict";

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);
}
}

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';
}
return new Function('A', '"use strict";return (' + formula + ')');

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 formula = safeEval(MashupPlatform.prefs.get('formula'));
var calculator = function calculator(value) {
var formula = safeEval();

MashupPlatform.prefs.registerCallback(function (new_preferences) {
formula = safeEval(MashupPlatform.prefs.get('formula'));
}.bind(this));
if (value != null) {
if (!isNaN(value)) {
pushEvent(formula(value));

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

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

/* test-code */
window.calculator = calculator;
/* end-test-code */

})();
Loading

0 comments on commit 6b3ce0b

Please sign in to comment.