Guest User

Untitled

a guest
Nov 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. // Licensed as MIT
  2.  
  3. /** Filter and normalize (partly relative) path. The resulting path does not contain multiple slashes and will always be absolute (starting with a slash). Intermediate ../ are expanded (resolved). If a path specifies more ../ than possible an exception is thrown.
  4. * @param {String} path... one path or multiple path fragments (as multipe arguments)
  5. * @return {String} normalized and filtered, absolute path with no trailing slash
  6. */
  7. function normalizePath(str){
  8. var path = "", i;
  9.  
  10. if (arguments.length > 1) {
  11. for (i = 0; i < arguments.length; i++) {
  12. path += "/" + arguments[i];
  13. }
  14. return normalizePath(path);
  15. }
  16.  
  17. path = "/" + str;
  18. path = path.replace(/\/+$/g, "");
  19. path = path.replace(/\/+/g, "/");
  20. while (path.indexOf("..") > -1 && path.indexOf("/..") != 0) {
  21. path = path.replace(/[^\.\/]+\/\.\.\//g, "");
  22. }
  23. if (path.indexOf("/..") === 0) {
  24. throw new Error("Malformed path '"+ str +"' (does not resolve to absolute path: '"+ path +"')");
  25. }
  26. path = path.replace(/\.\//g, "");
  27. return path;
  28. }
Add Comment
Please, Sign In to add comment