Adds pathOf javascript util function e.g. pathOf http://abc.com/d/e/f == /d/e/f
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/64880aa3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/64880aa3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/64880aa3 Branch: refs/heads/master Commit: 64880aa3b8ba82fb1fc60033d46d057348d7cd1c Parents: 016485f Author: Sam Corbett <sam.corb...@cloudsoftcorp.com> Authored: Wed Jul 9 18:10:33 2014 +0100 Committer: Sam Corbett <sam.corb...@cloudsoftcorp.com> Committed: Wed Jul 16 19:19:50 2014 +0100 ---------------------------------------------------------------------- .../src/main/webapp/assets/js/libs/brooklyn-utils.js | 14 +++++++++++++- .../src/test/javascript/specs/brooklyn-utils-spec.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/64880aa3/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js ---------------------------------------------------------------------- diff --git a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js index b6e32ca..4207823 100644 --- a/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js +++ b/usage/jsgui/src/main/webapp/assets/js/libs/brooklyn-utils.js @@ -47,7 +47,9 @@ define([ if (!String.prototype.trim) { // some older javascripts do not support 'trim' (including jasmine spec runner) so let's define it - String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');}; + String.prototype.trim = function(){ + return this.replace(/^\s+|\s+$/g, ''); + }; } // from http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string @@ -67,6 +69,16 @@ define([ window.prompt("To copy to the clipboard, press Ctrl+C then Enter.", text); }; + /** + * Returns the path component of a string URL. e.g. http://example.com/bob/bob --> /bob/bob + */ + Util.pathOf = function(string) { + if (!string) return ""; + var a = document.createElement("a"); + a.href = string; + return a.pathname; + }; + return Util; }); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/64880aa3/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js ---------------------------------------------------------------------- diff --git a/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js b/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js index f7008eb..ba9d32c 100644 --- a/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js +++ b/usage/jsgui/src/test/javascript/specs/brooklyn-utils-spec.js @@ -68,4 +68,19 @@ define([ }); }); + + describe("pathOf", function() { + + it("should extract the path component of a URI", function() { + expect(Util.pathOf("http://www.example.com/path/to/resource#more?a=b&c=d")).toBe("/path/to/resource"); + }); + + it("should return an empty path for an empty URL", function() { + expect(Util.pathOf("")).toBe(""); + }); + + it("should handle input without domain", function() { + expect(Util.pathOf("/a/b/c/d#e")).toBe("/a/b/c/d"); + }) + }); });