-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatternToDates.js
103 lines (76 loc) · 2.96 KB
/
patternToDates.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
var fs = require('fs');
var outputFilename = 'dates.txt';
// Process arguments: pattern file, [start date].
if(process.argv.length < 3) ex('need a pattern file as 1st argument.');
var filename = process.argv[2];
if(process.argv.length ==4){
var startDate = new Date(process.argv[3]);
if(!isValidDate(startDate)) ex('invalid start date as 2nd argument.');
}
// Read in, process, write out.
fs.readFile(filename, {encoding: 'ascii'}, function fileReadDone(err, data){
if(err) ex('can\'t read pattern file.');
data = arrayify(data);
data = buildDates(data);
fs.writeFile(outputFilename, data, function fileWriteDone(err){
if(err) ex('can\'t write output to %s.', outputFilename);
});
console.log('OK: Written to %s.', outputFilename);
});
function ex(msg){
console.error('Error: ' + msg);
process.exit(1);
}
function isValidDate(d) {
if(Object.prototype.toString.call(d) !== "[object Date]") return false;
return !isNaN(d.getTime());
}
// Convert pattern file contents to 2D array.
function arrayify(data){
a = data.replace(/\r/g, '').split('\n');
if(a[a.length - 1] == '') a.pop(); //Remove possible last empty line.
if(a.length != 7) ex('the pattern file should have exactly 7 lines.')
var len = a[0].length; //All rows should have equal length.
a = a.map(function lineToCells(str){
if(str.length != len) ex('all lines in the pattern file should have equal length. (Stuff with spaces).');
return str.split('');
});
return a;
}
function getLatestSaturday(){
var d = new Date(),
dayWkd = d.getDay(),
day00 = d.getDate() + (dayWkd < 6? -1 - dayWkd: 0);
return new Date(d.setDate(day00));
}
function dateOffSet(date, addDays){
var d = new Date(date); //Make copy so setDate() doesn't modify argument "date"'s content.
d.setDate(d.getDate() + addDays);
return d;
}
// Convert the pattern's tiles to dates.
// For more intense color, add multiple lines for the same day but with a different time.
function buildDates(arr){
var firstSunday = startDate || dateOffSet(getLatestSaturday(), - arr[0].length * 7 + 1 ),
w = arr[0].length,
date, str, num, i, j, k,
output = [];
for(j=0; j<w; j++){ //Switch i-j loops to output dates in chronological order.
for(i=0; i<7; i++){
date = dateOffSet(firstSunday, i + j*7);
str = '' + date.getDate();
if(str.length == 1) str = '0' + str;
str += ' ' + date.toLocaleString('en-us', { month: 'short' }) + ' ' + date.getFullYear();
num = arr[i][j];
if (num>='0' && num<='9') num = parseInt(num);
else if(num>='A' && num<='Z') num = num.charCodeAt(0) - 55; // Chars A-Z => 10-35.
else if(num>='a' && num<='x') num = num.charCodeAt(0) - 61; // Chars a-x => 36-59.
else num = 0; //Also space char is equivalent to a zero.
for(k=1; k<=num; k++){
output.push(str + ' 12:' + (k<10?'0':'') + k);
}
}
}
output.push(''); //Make it end with a newline.
return output.join('\r\n');
}