Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / misc.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
/*****************************************
 *   Misc functions
 *****************************************
 *   Author:  mikael.lofjard@gmail.com
 *   Website: http://lofjard.se
 *   License: MIT License
 ****************************************/
  
var Misc = (function() {
  
    var url = require('url');
  
    var dateTimeRegex = new RegExp("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}:\\d{2}).+$", '');
    var trimSlashRegEx = new RegExp("[/]+$", 'g');
  
    function doubleDigit(digit) {
        return (digit < 10) ? '0' + digit : digit;
    }
  
    return {
        monthNames: ['unknown', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
          
        dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
          
        getDateTimeNow: function() {
            var now = new Date();
            var year = now.getFullYear();
            var month = doubleDigit(now.getMonth() + 1); //indexed from 0-11 =)
            var day = doubleDigit(now.getDate());
            var hours = doubleDigit(now.getHours());
            var mins = doubleDigit(now.getMinutes());
            var secs = doubleDigit(now.getSeconds());
            var tz = doubleDigit(-now.getTimezoneOffset() / 60); //offset in negative minutes
              
            return year + '-' + month + '-' + day + 'T' + hours + ':' + mins + ':' + secs + '+' + tz + ':00';
        },
  
        prettyDate: function(dateTime) {
            var parsedDateTime = dateTimeRegex.exec(dateTime);
  
            var year = parsedDateTime[1];
            var month = Number(parsedDateTime[2]);
            var day = Number(parsedDateTime[3]);
            var time = parsedDateTime[4];
              
            return this.monthNames[month] + " " + day + ", " + year + " at " + time;
        },
  
        getPathName: function (inUrl) {
            var pathName = url.parse(inUrl).pathname;
  
            var lastSlashPos = pathName.lastIndexOf("/");
            if ((lastSlashPos > 0) && (lastSlashPos === pathName.length - 1)) {
                pathName = pathName.replace(trimSlashRegEx, '');
            }
  
            return pathName;
        },
  
        getGuid: function () {
            var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
                 return v.toString(16);
            });
            return guid;
        }
    };
  
}());
  
typeof(exports) != 'undefined' ? exports.Misc = Misc : null;