提交 2071e2fa 编写于 作者: J Johannes Rieken

expose Uri#with to allow deriving a new Uri from an existing, #6678

上级 26caa263
......@@ -133,12 +133,32 @@ export default class URI {
// ---- modify to new -------------------------
public with(change: { scheme?: string; authority?: string; path?: string; query?: string; fragment?: string }): URI {
var ret = new URI();
ret._scheme = change.scheme || this.scheme;
ret._authority = change.authority || this.authority;
ret._path = change.path || this.path;
ret._query = change.query || this.query;
ret._fragment = change.fragment || this.fragment;
if (!change) {
return this;
}
let scheme = change.scheme || this.scheme;
let authority = change.authority || this.authority;
let path = change.path || this.path;
let query = change.query || this.query;
let fragment = change.fragment || this.fragment;
if (scheme === this.scheme
&& authority === this.authority
&& path === this.path
&& query === this.query
&& fragment === this.fragment) {
return this;
}
const ret = new URI();
ret._scheme = scheme;
ret._authority = authority;
ret._path = path;
ret._query = query;
ret._fragment = fragment;
URI._validate(ret);
return ret;
}
......
......@@ -62,7 +62,21 @@ suite('URI', () => {
assert.equal(uri2.fragment, uri3.fragment);
});
test('with', () => {
test('with, identity', () => {
let uri = URI.parse('foo:bar/path');
let uri2 = uri.with(null);
assert.ok(uri === uri2);
uri2 = uri.with(undefined);
assert.ok(uri === uri2);
uri2 = uri.with({});
assert.ok(uri === uri2);
uri2 = uri.with({ scheme: 'foo', path: 'bar/path' });
assert.ok(uri === uri2);
});
test('with, changes', () => {
assert.equal(URI.parse('before:some/file/path').with({ scheme: 'after' }).toString(), 'after:some/file/path');
assert.equal(URI.create().with({ scheme: 'http', path: '/api/files/test.me', query: 't=1234' }).toString(), 'http:/api/files/test.me?t%3D1234');
assert.equal(URI.create().with({ scheme: 'http', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'http:/api/files/test.me?t%3D1234');
assert.equal(URI.create().with({ scheme: 'https', authority: '', path: '/api/files/test.me', query: 't=1234', fragment: '' }).toString(), 'https:/api/files/test.me?t%3D1234');
......
......@@ -937,6 +937,20 @@ declare namespace vscode {
*/
fsPath: string;
/**
* Derive a new Uri from this Uri.
*
* @param change An object that describes a change.
* @return A new Uri that reflects the changes. Will return `this` Uri if the change
* is not changing anything.
* @sample ```
let file = Uri.parse('before:some/file/path');
let other = file.with({ scheme: 'after' });
assert.ok(other.toString() === 'after:some/file/path');
* ```
*/
with(change: { scheme?: string; authority?: string; path?: string; query?: string; fragment?: string }): Uri;
/**
* Returns a string representation of this Uri. The representation and normalization
* of a URI depends on the scheme. The resulting string can be safely used with
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册