Skip to content

Commit

Permalink
Merge pull request #17 from movableink/mn/cd-params
Browse files Browse the repository at this point in the history
Add CD.params() that returns all query params
  • Loading branch information
mnutt authored Sep 27, 2017
2 parents 40cffd1 + 26dce12 commit 180d52a
Show file tree
Hide file tree
Showing 3 changed files with 1,727 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/cropduster.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ window.CD = {
},

param: function(name) {
return CD.params()[name];
},

params: function(name) {
if(typeof(CD._urlParams) == "undefined") {
CD._urlParams = {};
var match,
search = /([^&=]+)=?([^&]*)/g,
query = window.location.search.substring(1);
var match;
var search = /([^&=]+)=?([^&]*)/g;
var query = CD._searchString();
while (match = search.exec(query))
CD._urlParams[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
}
return CD._urlParams[name];
if (name) {
return CD._urlParams[name];
} else {
return CD._urlParams;
}
},

_searchString: function() {
return window.location.search.substring(1);
},

autofill: function() {
Expand Down
18 changes: 17 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ var container = $("<div style='position: absolute; left: -5000px;'></div>").appe
// Disable debugging output
CD.log = function() {};

// Stub out search string
CD._searchString = function() {
return 'foo=bar&baz%20test=quux%20value';
};

var imageSuccessStub = function() {
var self = this;
setTimeout(function() {
Expand All @@ -25,14 +30,25 @@ QUnit.test("CD.param when parameter not found", function() {
equal(CD.param('not_found'), null, "returns null");
});

QUnit.test("CD.param when parameter is found", function() {
equal(CD.param('foo'), 'bar', "returns value");
});

QUnit.test("CD.params returns all query params", function() {
deepEqual(CD.params(), {'baz test': 'quux value', foo: 'bar'}, "returns the url params");
});

QUnit.test("CD.params with argument returns that query param", function() {
equal(CD.params('baz test'), 'quux value', "returns the url param");
});

// can't test CD.param returning query params from here, unfortunately...

QUnit.test("CD.autofill", function() {
container.html('');
var el = $("<div id='autofill_foo'></div>");
container.append(el);

CD._urlParams = {foo: 'bar'};
CD.autofill();
equal(el.html(), 'bar', "auto-fills the query param into the element");
});
Expand Down
Loading

0 comments on commit 180d52a

Please sign in to comment.