Skip to content

Commit 1fd62f4

Browse files
author
Michael
committed
added transformAsync
1 parent add1263 commit 1fd62f4

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,19 @@ You can read this as follows:
7474
- Run Data.parse on the date value.
7575
- Run each function on all items after mapping and operations.
7676

77-
Run it
77+
Run it synchronously
7878
```javascript
7979
var dataTransform = DataTransform(data, map);
8080
var result = dataTransform.transform();
8181
console.log(result);
8282
```
83+
... or asynchronously
84+
```javascript
85+
var dataTransform = DataTransform(data, map);
86+
var result = dataTransform.transformAsync(function(result){
87+
console.log(result);
88+
});
89+
```
8390

8491
The expected output.
8592
```javascript
@@ -340,6 +347,8 @@ The expected output.
340347
Enjoy!
341348

342349
## Changelog
350+
1.0.18 Introducing transformAsync which returns a promise.
351+
1.0.17 Ensure transform always returns an array
343352
1.0.16 ES5 compatibility
344353
1.0.15 Add support for a context object that is passed through to the operate.run and each functions.
345354
1.0.14 Add support for default values via "defaults" definition. Add support for removing attributes via the "remove" definition.

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ exports.DataTransform = function(data, map){
9090

9191
},
9292

93+
transformAsync : function(context) {
94+
return new Promise(function(resolve, reject) {
95+
try {
96+
resolve(this.transform(context))
97+
} catch (err) {
98+
reject(err);
99+
}
100+
}.bind(this));
101+
},
102+
93103
removeAll: function(data){
94104
if (_.isArray(map.remove)) {
95105
return _.each(data, this.remove)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-json-transform",
3-
"version": "1.0.17",
3+
"version": "1.0.18",
44
"description": "A node module for transforming and performing operations on JSON.",
55
"main": "index.js",
66
"scripts": {

test/nodeDataTransformSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ describe("node-json-transform", function() {
6868

6969
});
7070

71+
it("should transform data asynchronously", function() {
72+
73+
var dataTransform = DataTransform(_.clone(data), map);
74+
dataTransform.transformAsync().then(function(result){
75+
expect(result).toEqual([{
76+
name: "TITLE1",
77+
info: "description1",
78+
text: "This is a blog.",
79+
date: Date.parse('11/4/2013'),
80+
link: "http://goo.cm",
81+
info: "mike"
82+
}]);
83+
});
84+
});
85+
7186
it("should allow you to clear out fields", function() {
7287

7388
// Add a map item to clear out the "clearMe" field.

0 commit comments

Comments
 (0)