Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisuke Baba committed Dec 13, 2016
2 parents 4bd6ef0 + 7dde5b1 commit e50668f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can use existing `split` node and `function` node in conjunction with the ab
### Installation

```
cd ~\.node-red
cd ~/.node-red
npm install node-red-contrib-sequence-functions
```

Expand Down Expand Up @@ -316,5 +316,8 @@ You can try the following flow for testing the node behaviors after installing t
```

# Revision History
* 1.1.0
- Add a new property `Convert to String` into `map` node, which enables to convert the map function result into String

* 1.0.0
- Initial release
3 changes: 3 additions & 0 deletions lib/locales/en-US/sequence-functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<a target="_blank" href="https://github.com/silentmatt/expr-eval#expression-syntax">
Silent Matt's expr-eval module</a>.</p>
<p>The variable <code>x</code> represents a current value to be evaluated.</p>

<p>You can convert the <code>Map function</code> result into <code>String</code>
rather than <code>Number</code> by checking <code>Convert to String</code> item.</p>
</script>

<script type="text/x-red" data-help-name="reduce">
Expand Down
1 change: 1 addition & 0 deletions lib/locales/en-US/sequence-functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"topic" : "Topic",
"valueProperty" : "Value (x) Property",
"mapFunctionExpr" : "Map function expression",
"mapToString" : "Convert to String",
"reduceFunctionExpr" : "Reduce function expression",
"reduceRight" : "Reduce right",
"readFromProperty" : "Read value from property",
Expand Down
8 changes: 7 additions & 1 deletion lib/sequence-functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
</label>
<input type='text' id='node-input-mapFunctionExpr' style='width: 70%;' data-i18n='[placeholder]sequence-functions.placeholder.mapFunctionExpr'>
</div>
<div class='form-row' id='node-mapToString'>
<label>&nbsp;</label>
<input type='checkbox' id='node-input-mapToString' style='display: inline-block; width: auto; vertical-align: top;'>
<label for='node-input-mapToString' style='width: 70%;' data-i18n='sequence-functions.label.mapToString'></label>
</div>
</script>

<script type='text/javascript'>
Expand All @@ -125,7 +130,8 @@
topic: { value : '', required: false },
valueProperty: { value: 'payload', required: false },
readFromProperty: { value: false, required: false },
mapFunctionExpr: { value: '', required: false }
mapFunctionExpr: { value: '', required: false },
mapToString: { value: false, required: false }
},
color: 'Gold',
inputs: 1,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-sequence-functions",
"version": "1.0.0",
"version": "1.1.0",
"description": "Node-RED nodes for sequence processing functions",
"license": "MIT",
"repository": {
Expand Down
14 changes: 12 additions & 2 deletions src/sequence-functions.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,30 @@ export default function(RED) {
this.topic = n.topic;
this.valueProperty = n.valueProperty || 'payload';
this.readFromProperty = n.readFromProperty;
this.mapToString = n.mapToString;
if (n.mapFunctionExpr) {
this.parsedMapFunction = Parser.parse(n.mapFunctionExpr);
try {
this.parsedMapFunction.evaluate({ x: 0 });
this.mapFunction = (val) => {
return this.parsedMapFunction.evaluate({ x: val });
let result = this.parsedMapFunction.evaluate({ x: val });
if (this.mapToString) {
return String(result);
} else {
result;
}
};
} catch (e) {
RED.log.error(RED._('sequence-functions.errors.parserError', { error: e }));
}
}
if (!this.mapFunction) {
this.mapFunction = (val) => {
return val;
if (this.mapToString) {
return String(val);
} else {
return val;
}
};
}
let node = this;
Expand Down
18 changes: 17 additions & 1 deletion tests/sequence-functions.test.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,26 @@ describe('sequence-functions module', () => {
name: 'my-name',
topic: '123',
valueProperty: 'payload',
mapFunctionExpr: 'x'
mapFunctionExpr: 'x',
mapToString: true
});
assert.equal('my-name', node.name);
assert.isDefined(node.mapFunction);
assert.isString(node.mapFunction(1));
});
it('should be able to create a new MapNode with mapToString being false', () => {
eventProcessing(RED);
assert.isDefined(MapNode);
let node = new MapNode({
name: 'my-name',
topic: '123',
valueProperty: 'payload',
mapFunctionExpr: 'x',
mapToString: false
});
assert.equal('my-name', node.name);
assert.isDefined(node.mapFunction);
assert.isNotString(node.mapFunction(1));
});
});
});
Expand Down

0 comments on commit e50668f

Please sign in to comment.