forked from tasict/homebridge-http-accessory
-
Notifications
You must be signed in to change notification settings - Fork 23
/
mappers.js
127 lines (108 loc) · 2.76 KB
/
mappers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var xpath = require("xpath");
var dom = require("xmldom").DOMParser;
var JSONPath = require("JSONPath");
module.exports = {
StaticMapper,
RegexMapper,
XPathMapper,
JPathMapper,
EvalMapper,
}
/**
* Mapper class that can be used as a dictionary for mapping one value to another
*
* @param {Object} parameters The parameters of the mapper
* @constructor
*/
function StaticMapper(parameters) {
var self = this;
self.mapping = parameters.mapping;
self.map = function(value) {
return self.mapping[value] || value;
};
}
/**
* Mapper class that can extract a part of the string using a regex
*
* @param {Object} parameters The parameters of the mapper
* @constructor
*/
function RegexMapper(parameters) {
var self = this;
self.regexp = new RegExp(parameters.regexp);
self.capture = parameters.capture || "1";
self.map = function(value) {
var matches = self.regexp.exec(value);
if (matches !== null && self.capture in matches) {
return matches[self.capture];
}
return value;
};
}
/**
* Mapper class that uses XPath to select the text of a node or the value of an attribute
*
* @param {Object} parameters The parameters of the mapper
* @constructor
*/
function XPathMapper(parameters) {
var self = this;
self.xpath = parameters.xpath;
self.index = parameters.index || 0;
self.map = function(value) {
var document = new dom().parseFromString(value);
var result = xpath.select(this.xpath, document);
if (typeof result == "string") {
return result;
} else if (result instanceof Array && result.length > self.index) {
return result[self.index].data;
}
return value;
};
}
/**
* Mapper class that uses JSONPath to select the text of a node or the value of an attribute
*
* @param {Object} parameters The parameters of the mapper
* @constructor
*/
function JPathMapper(parameters) {
var self = this;
self.jpath = parameters.jpath;
self.index = parameters.index || 0;
self.map = function(value) {
var result = 'inconclusive';
var json;
try {
json = JSON.parse(value);
} catch (e) {
return result;
}
if (typeof json !== 'object') {
return result;
} else {
result = JSONPath({path: self.jpath, json: json});
if (result instanceof Array && result.length > self.index) {
result = result[self.index];
}
if (result instanceof Object) {
result = JSON.stringify(result);
}
}
return result;
}
}
/**
* Mapper class that evaluates the given js string
*
* @param {Object} parameters The parameters of the mapper
* @constructor
*/
function EvalMapper(parameters) {
var self = this;
self.exp = parameters.expression;
self.map = function(value) {
var result = eval(self.exp);
return result;
};
}