Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addFragment/addHash method #356

Merged
merged 2 commits into from Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/URI.fragmentQuery.js
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mind adding tests for this to test/test.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sorry so the delay, I got really busy there days.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries… I'm leaving for 2 week vacation in 1h, sorry for that delay :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added, happy vacation!

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
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
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 = '?';
});

})();
})();