-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer.js
63 lines (61 loc) · 2.38 KB
/
transformer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
define(['linq'], function (linq) {
'use strict';
var transformer;
/**
* input - The input that each transform operates on
* transforms - The object that contains key value pairs as follows
* {
* transformName: transformFunction(input, callback(transformedResult));
* }
*/
transformer = function (input, transforms, callback) {
var applyTransforms,
transformsArray,
transformName,
results;
// linq.from does not work if the value in the object is a function.
// Therefore, transform the object into an array.
transformsArray = [];
// Iterate over all the properties of the object
for(transformName in transforms) {
if (transforms.hasOwnProperty(transformName)) {
// add the pair to the array.
transformsArray.push({
name: transformName,
fn: transforms[transformName]
});
}
}
// Define the function to transform the input to generate the result.
applyTransforms =
function (input) {
var results;
// Iterate over the transforms array, apply each transform to the input,
// and store the result paired with the transform name in combination
// object that represents the result.
results =
linq.from(transformsArray)
.aggregate({},
function (combination, transform) {
transform.fn(input, function (err, result) {
if (err) {
throw err;
}
combination[transform.name] =
(result instanceof linq) ? result.toArray() : result;
});
return combination;
}
);
return results;
};
try {
results = applyTransforms(input);
callback(null, results);
}
catch (err) {
callback(err, undefined);
}
};
return transformer;
});