forked from falconair/nodefix
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.js
More file actions
83 lines (68 loc) · 1.97 KB
/
Message.js
File metadata and controls
83 lines (68 loc) · 1.97 KB
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
var _ = require("underscore");
var map = require("./map");
var SOHCHAR = String.fromCharCode(1);
function Message() {}
Message.prototype.getType = function () {
return this.get("MsgType");
};
Message.prototype.get = function (field) {
var rawValue = this.getKey(field),
arr = [];
if (!rawValue) {
return null;
} else if (typeof rawValue === "string") {
return map.get(field, rawValue);
// Assume it's an array (repeating group)
} else {
rawValue.forEach(function (rawValue) {
arr.push(map.get(field, rawValue));
});
return arr;
}
};
Message.prototype.getKey = function (field) {
// TODO for performance could include logic to quickly return value if known not to be a repeating group field (header, etc)
var arr = _.reduce(this.data, function (memo, item) {
if (item[0] === field) {
memo.push(item[1]);
}
return memo;
}, []);
if (arr.length === 0) {
return null;
} else if (arr.length === 1) {
return arr[0];
} else {
return arr;
}
};
// Second argument can be an array of field names or a single field name
Message.prototype.getRepeating = function (keyField, fields) {
var keys = this.get(keyField),
data = {},
obj = {};
if (typeof fields === "string") {
data = this.get(fields);
} else {
fields.forEach(function (field) {
data[field] = this.get(field);
}.bind(this));
}
keys.forEach(function (key, index) {
obj[key] = {};
for (var field in data) {
if (typeof fields === "string") {
obj[key] = data[index];
} else {
if (typeof data[field][index] !== "undefined") {
obj[key][field] = data[field][index];
}
}
}
});
return obj;
};
Message.prototype.getFIX = function () {
return this.raw;
};
module.exports = Message;