Skip to content

Commit

Permalink
feature(fragment-query): add setFragment()
Browse files Browse the repository at this point in the history
closes #338

* Add tests for static method URI.setQuery
  • Loading branch information
fishead authored and rodneyrehm committed Oct 1, 2017
1 parent 61cd727 commit 29ab103
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 15 deletions.
23 changes: 20 additions & 3 deletions src/URI.fragmentQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
// uri.toString() === "http://example.org/#?bar=foo&name=value";
// uri.removeFragment("name");
// uri.toString() === "http://example.org/#?bar=foo";
// uri.setFragment("name", "value1");
// uri.toString() === "http://example.org/#?bar=foo&name=value1";
// uri.setFragment("name", "value2");
// uri.toString() === "http://example.org/#?bar=foo&name=value2";

(function (root, factory) {
'use strict';
Expand Down Expand Up @@ -57,12 +61,12 @@
p.fragment = function(v, build) {
var prefix = this._parts.fragmentPrefix;
var fragment = this._parts.fragment || '';

if (v === true) {
if (fragment.substring(0, prefix.length) !== prefix) {
return {};
}

return URI.parseQuery(fragment.substring(prefix.length));
} else if (v !== undefined && typeof v !== 'string') {
this._parts.fragment = prefix + URI.buildQuery(v);
Expand Down Expand Up @@ -96,9 +100,22 @@
this.build(!build);
return this;
};
p.setFragment = function(name, value, build) {
var prefix = this._parts.fragmentPrefix;
var data = URI.parseQuery((this._parts.fragment || '').substring(prefix.length));
URI.setQuery(data, name, value);
this._parts.fragment = prefix + URI.buildQuery(data);
if (typeof name !== 'string') {
build = value;
}

this.build(!build);
return this;
};
p.addHash = p.addFragment;
p.removeHash = p.removeFragment;
p.setHash = p.setFragment;

// extending existing object rather than defining something new
return URI;
}));
}));
15 changes: 15 additions & 0 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,21 @@
throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
}
};

URI.setQuery = function(data, name, value) {
if (typeof name === 'object') {
for (var key in name) {
if (hasOwn.call(name, key)) {
URI.setQuery(data, key, name[key]);
}
}
} else if (typeof name === 'string') {
data[name] = value === undefined ? null : value;
} else {
throw new TypeError('URI.setQuery() accepts an object, string as the name parameter');
}
};

URI.removeQuery = function(data, name, value) {
var i, length, key;

Expand Down
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,44 @@
result = URI.joinPaths('a', '', '', 'b', '', '').toString();
equal(result, 'a/b/', 'trailing empty segment');
});
test('setQuery', function () {
var o = {foo: 'bar'};

URI.setQuery(o, 'foo', 'bam');
deepEqual(o, {foo: 'bam'}, 'set name, value');

URI.setQuery(o, 'array', ['one', 'two']);
deepEqual(o, {foo: 'bam', array: ['one', 'two']}, 'set name, array');

URI.setQuery(o, 'foo', 'qux');
deepEqual(o, {foo: 'qux', array: ['one', 'two']}, 'override name, value');

o = {foo: 'bar'};
URI.setQuery(o, {baz: 'qux'});
deepEqual(o, {foo: 'bar', baz: 'qux'}, 'set {name: value}');

URI.setQuery(o, {bar: ['1', '2']});
deepEqual(o, {foo: 'bar', bar: ['1', '2'], baz: 'qux'}, 'set {name: array}');

URI.setQuery(o, {foo: 'qux'});
deepEqual(o, {foo: 'qux', bar: ['1', '2'], baz: 'qux'}, 'override {name: value}');

o = {foo: 'bar'};
URI.setQuery(o, {bam: null, baz: ''});
deepEqual(o, {foo: 'bar', bam: null, baz: ''}, 'set {name: null}');

o = {foo: 'bar'};
URI.setQuery(o, 'empty');
deepEqual(o, {foo: 'bar', empty: null}, 'set undefined');

o = {foo: 'bar'};
URI.setQuery(o, 'empty', '');
deepEqual(o, {foo: 'bar', empty: ''}, 'set empty string');

o = {};
URI.setQuery(o, 'some value', 'must be encoded because of = and ? and #');
deepEqual(o, {'some value': 'must be encoded because of = and ? and #'}, 'encoding');
});

module('comparing URLs');
test('equals', function() {
Expand Down
32 changes: 20 additions & 12 deletions test/test_fragmentQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,63 @@
module('URI.fragmentQuery');
test('storing query-data in fragment', function() {
var u = URI('http://example.org');

deepEqual(u.fragment(true), {}, 'empty map for missing fragment');

u = URI('http://example.org/#');
deepEqual(u.fragment(true), {}, 'empty map for empty fragment');

u = URI('http://example.org/#?hello=world');
deepEqual(u.fragment(true), {hello: 'world'}, 'reading data object');

u.fragment({bar: 'foo'});
deepEqual(u.fragment(true), {bar: 'foo'}, 'setting data object');
equal(u.toString(), 'http://example.org/#?bar=foo', 'setting data object serialized');

u.addFragment('name', 'value');
deepEqual(u.fragment(true), {bar: 'foo', name: 'value'}, 'adding value');
equal(u.toString(), 'http://example.org/#?bar=foo&name=value', 'adding value serialized');

u.removeFragment('bar');
deepEqual(u.fragment(true), {name: 'value'}, 'removing value bar');
equal(u.toString(), 'http://example.org/#?name=value', 'removing value bar serialized');

u.removeFragment('name');
deepEqual(u.fragment(true), {}, 'removing value name');
equal(u.toString(), 'http://example.org/#?', 'removing value name serialized');

u.setFragment('name', 'value1');
deepEqual(u.fragment(true), {name: 'value1'}, 'setting name to value1');
equal(u.toString(), 'http://example.org/#?name=value1', 'setting name to value1 serialized');

u.setFragment('name', 'value2');
deepEqual(u.fragment(true), {name: 'value2'}, 'setting name to value2');
equal(u.toString(), 'http://example.org/#?name=value2', 'setting name to value2 serialized');
});
test('fragmentPrefix', function() {
var u;

URI.fragmentPrefix = '!';
u = URI('http://example.org');
equal(u._parts.fragmentPrefix, '!', 'init using global property');

u.fragment('#?hello=world');
equal(u.fragment(), '?hello=world', 'unparsed ?');
deepEqual(u.fragment(true), {}, 'parsing ? prefix');

u.fragment('#!hello=world');
equal(u.fragment(), '!hello=world', 'unparsed !');
deepEqual(u.fragment(true), {hello: 'world'}, 'parsing ! prefix');

u.fragmentPrefix('§');
equal(u.fragment(), '!hello=world', 'unparsed §');
deepEqual(u.fragment(true), {}, 'parsing § prefix');

u.fragment('#§hello=world');
equal(u.fragment(), '§hello=world', 'unparsed §');
deepEqual(u.fragment(true), {hello: 'world'}, 'parsing § prefix');

URI.fragmentPrefix = '?';
});

})();
})();

0 comments on commit 29ab103

Please sign in to comment.