-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.js
More file actions
91 lines (78 loc) · 2.35 KB
/
timestamp.js
File metadata and controls
91 lines (78 loc) · 2.35 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
84
85
86
87
88
89
90
91
var express = require('express');
var app = express();
var convert = require('./convert.js');
var timestamp = {
//Unchanging null object for response
nullres: {unix: null, natural: null},
//Blank object to change
res: {unix: null, natural: null},
//Regular expression for a 10 digit number assumed to be a unix format timestamp
unixregex: /\D|\s/gi,
naturalregex: /\D\s/gi,
//Variable to tell whether the current value is unix format
isunixvar: false,
//Checks the unix value for unix format
isunix: function(){
if(this.unixregex.test(this.res.unix) == false){
this.isunixvar = true;
if(this.isnaturalvar == false){
this.res.natural = convert.unixtonatural(this.res.unix);
this.isnatural();
}
}
else{
this.isunixvar = false;
}
},
//States whether current natural value is natural
isnaturalvar: false,
//Checks natural value for natural properties
isnatural: function(){
if(this.naturalregex.test(this.res.natural) == true){
this.isnaturalvar = true;
if(this.isunixvar == false){
this.res.unix = convert.naturaltounix(this.res.natural);
this.isunix();
}
}
else{
this.isnaturalvar = false;
}
},
//Controls response from server
response: function(){
console.log("Flags: unix: " + this.isunixvar + " natural: " + this.isnaturalvar);
if(this.isunixvar == true && this.isnaturalvar == true){
console.log("Output: " + JSON.stringify(this.res));
return this.res;
}
else{
console.log("Output: " + JSON.stringify(this.nullres));
return this.nullres;
}
},
//Sets values to be checked from inout and cinitiates method calls
dateparse: function(date){
this.res.unix = date;
this.res.natural = date;
this.isunix();
//redundant check to be sure
this.isnatural();
console.log("Input: " + date);
return this.response();
},
//Resets the variables for the next call
reset: function(){
this.isunixvar = false;
this.isnaturalvar = false;
this.res = Object.create(this.nullres);
}
};
//Express server creation
app.get('/:date', function(req, res){
res.json(timestamp.dateparse(req.params.date));
timestamp.reset();
res.end();
});
//Initiates server on specified port
app.listen(80);