-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathutil.js
More file actions
131 lines (109 loc) · 2.92 KB
/
util.js
File metadata and controls
131 lines (109 loc) · 2.92 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var pad = function(str, length, padChar) {
var result = str;
while(result.length < length) {
result = padChar + result;
}
return result;
}
/**
* Convert timezone offset to time-numoffset.
*
* Example:
* util.convertOffsetToTimezone(-540) === '+0900'
*
* @param {number} offset
* @api public
*/
exports.convertOffsetToTimezone = function(offset) {
var sign = offset > 0 ? '-' : '+';
var hourmin = Math.abs((offset / 6) * 10).toString();
var paddedHourmin = pad(hourmin, 4, '0');
return sign + paddedHourmin;
}
/**
* Parse timezone.
*
* Example:
* timezone = util.pasreTimezone('+0900');
* timezone.sign === '+';
* timezone.hour === '09';
* timezone.min === '00';
*
* @param {number} timezone
* @api public
*/
exports.parseTimezone = function(timezone) {
// See ISO 8601 Collected ABNF -- http://www.ietf.org/rfc/rfc3339.txt
// time-hour = 2DIGIT ; 00-24
// time-minute = 2DIGIT ; 00-59
// time-numoffset = ("+" / "-") time-hour [[":"] time-minute]
var parsed, hour, min, isValid;
parsed = timezone.match(/(\+|\-)([0-2][0-9]):?([0-6][0-9])$/);
if (!parsed || parsed.length < 4) {
throw new Error('invalid timezone');
}
hour = parseInt(parsed[2]);
min = parseInt(parsed[3]);
isValid =
(hour >= 0 || hour <= 24) &&
(min >= 0 || min <= 60);
if (!isValid) {
throw new Error('invalid timezone');
}
return {
sign: parsed[1],
hour: parsed[2],
min: parsed[3]
};
}
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last == '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// path.normalize(path)
// posix version
exports.normalize = function(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.slice(-1) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
// posix version
exports.join = function() {
var paths = Array.prototype.slice.call(arguments, 0);
return exports.normalize(paths.filter(function(p, index) {
return p && typeof p === 'string';
}).join('/'));
};