Name: js-handler/node_modules/restify/lib/utils.js
| 1: | // Copyright 2012 Mark Cavage, Inc. All rights reserved. |
| 2: | |
| 3: | var assert = require('assert-plus'); |
| 4: | |
| 5: | |
| 6: | |
| 7: | /** |
| 8: | * Cleans up sloppy URL paths, like /foo////bar/// to /foo/bar. |
| 9: | * |
| 10: | * @param {String} path the HTTP resource path. |
| 11: | * @return {String} Cleaned up form of path. |
| 12: | */ |
| 13: | function sanitizePath(path) { |
| 14: | assert.ok(path); |
| 15: | |
| 16: | // Be nice like apache and strip out any //my//foo//bar///blah |
| 17: | path = path.replace(/\/\/+/g, '/'); |
| 18: | |
| 19: | // Kill a trailing '/' |
| 20: | if (path.lastIndexOf('/') === (path.length - 1) && path.length > 1) |
| 21: | path = path.substr(0, path.length - 1); |
| 22: | |
| 23: | return (path); |
| 24: | } |
| 25: | |
| 26: | |
| 27: | |
| 28: | /** |
| 29: | * Return a shallow copy of the given object; |
| 30: | */ |
| 31: | function shallowCopy(obj) { |
| 32: | if (!obj) { |
| 33: | return (obj); |
| 34: | } |
| 35: | var copy = {}; |
| 36: | Object.keys(obj).forEach(function (k) { |
| 37: | copy[k] = obj[k]; |
| 38: | }); |
| 39: | return (copy); |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | ///--- Exports |
| 45: | |
| 46: | module.exports = { |
| 47: | sanitizePath: sanitizePath, |
| 48: | shallowCopy: shallowCopy |
| 49: | }; |
