forked from restify/node-restify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
49 lines (35 loc) · 1 KB
/
utils.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
// Copyright 2012 Mark Cavage, Inc. All rights reserved.
var assert = require('assert-plus');
/**
* Cleans up sloppy URL paths, like /foo////bar/// to /foo/bar.
*
* @param {String} path the HTTP resource path.
* @return {String} Cleaned up form of path.
*/
function sanitizePath(path) {
assert.ok(path);
// Be nice like apache and strip out any //my//foo//bar///blah
path = path.replace(/\/\/+/g, '/');
// Kill a trailing '/'
if (path.lastIndexOf('/') === (path.length - 1) && path.length > 1)
path = path.substr(0, path.length - 1);
return (path);
}
/**
* Return a shallow copy of the given object;
*/
function shallowCopy(obj) {
if (!obj) {
return (obj);
}
var copy = {};
Object.keys(obj).forEach(function (k) {
copy[k] = obj[k];
});
return (copy);
}
///--- Exports
module.exports = {
sanitizePath: sanitizePath,
shallowCopy: shallowCopy
};