From 2071e2fa5c45e59ff1f022a2549a28e4e0acaed8 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 3 Jun 2016 09:36:29 +0200 Subject: [PATCH] expose Uri#with to allow deriving a new Uri from an existing, #6678 --- src/vs/base/common/uri.ts | 32 +++++++++++++++++++++++------ src/vs/base/test/common/uri.test.ts | 16 ++++++++++++++- src/vs/vscode.d.ts | 14 +++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/vs/base/common/uri.ts b/src/vs/base/common/uri.ts index 14881b4c746..43f5b3bcdcf 100644 --- a/src/vs/base/common/uri.ts +++ b/src/vs/base/common/uri.ts @@ -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; } diff --git a/src/vs/base/test/common/uri.test.ts b/src/vs/base/test/common/uri.test.ts index ad73e49c254..66263c60484 100644 --- a/src/vs/base/test/common/uri.test.ts +++ b/src/vs/base/test/common/uri.test.ts @@ -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'); diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 32867f1b486..66450265731 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -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 -- GitLab