UNPKG

1.69 kBJavaScriptView Raw
1var util = require('util'),
2 StringUtil = require('../sprintf/sprintf').StringUtil;
3
4var UserInfo = exports.UserInfo = function(string) {
5 var _email = '', _date = new Date(), _offset = 0, _name = '';
6
7 // Parse the content
8 var match_results = string.match(/^(.*?) <(.*)> (\d+) ([+-])0*(\d+?)$/);
9 // If we don't have a correct match set parse it partially
10 if(!match_results) {
11 if(string.match(/<.+>/)) {
12 var sub_match = string.match(/(.*) <(.+?)>/);
13 _name = sub_match[1];
14 _email = sub_match[2];
15 } else {
16 _name = string;
17 }
18 } else {
19 _name = match_results[1];
20 _email = match_results[2];
21 _date = new Date(parseInt(match_results[3] * 1000));
22 _offset = (match_results[4] == "-" ? -1 : 1) * parseInt(match_results[5]);
23 }
24
25 // Define properties
26 Object.defineProperty(this, "name", { get: function() { return _name; }, enumerable: true});
27 Object.defineProperty(this, "email", { get: function() { return _email; }, enumerable: true});
28 Object.defineProperty(this, "date", { get: function() { return _date; }, enumerable: true});
29 Object.defineProperty(this, "offset", { get: function() { return _offset; }, enumerable: true});
30}
31
32UserInfo.prototype.toString = function() {
33 // Ensure correct formating for the offset
34 var offset_str = this.offset.toString();
35 var add_string = '';
36 if(offset_str.length < 5) {
37 for(var i = 0; i < (5 - offset_str.length); i++) { add_string += '0'; }
38 offset_str = offset_str.substr(0, 1) + add_string + offset_str.substr(1);
39 }
40 // Return the userinfo as a string
41 return "" + this.name + " <" + this.email + "> " + (this.date.getTime()/1000) + " " + offset_str;
42}
\No newline at end of file