Created
May 24, 2017 11:32
-
-
Save sqs/80f7b4b01671e274d11213347b29afa6 to your computer and use it in GitHub Desktop.
resolves to equal uri typescript vscode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns true iff url == uri-resolve(url, candidate) per | |
* https://tools.ietf.org/html/rfc3986#section-5.2.2. For example, if currentURL is | |
* http://example.com/foo and newURL is /foo, then it returns true. | |
*/ | |
export function resolvesToEqual(url: URI | string, candidate: URI | string): boolean { | |
if (typeof url === 'string') { url = URI.parse(url); } | |
if (typeof candidate === 'string') { candidate = URI.parse(candidate); } | |
const change: { | |
scheme?: string; | |
authority?: string; | |
path?: string; | |
query?: string; | |
fragment?: string; | |
} = {}; | |
if (candidate.scheme) { change.scheme = candidate.scheme; } | |
if (candidate.scheme || candidate.authority) { change.authority = candidate.authority; } | |
if (candidate.scheme || candidate.authority || candidate.path) { | |
if (candidate.path[0] === '/') { | |
change.path = candidate.path; | |
} else { | |
change.path = paths.join(url.path, candidate.path); | |
} | |
} | |
if (candidate.scheme || candidate.authority || candidate.path || candidate.query) { | |
change.query = candidate.query; | |
} | |
if (candidate.scheme || candidate.authority || candidate.path || candidate.query || candidate.fragment) { | |
change.fragment = candidate.fragment; | |
} | |
candidate = url.with(change); | |
return url.toString() === candidate.toString(); | |
} | |
'use strict'; | |
import assert = require('assert'); | |
import { resolvesToEqual } from 'vs/platform/urlRoute/common/urlRoute'; | |
suite('URL Route Service', () => { | |
suite('resolvesToEqual', () => { | |
test('true', () => { | |
[ | |
['http://example.com/a?b#c', 'http://example.com/a?b#c'], | |
['http://example.com/a?b#c', ''], | |
['http://example.com/a?b#c', '/a?b#c'], | |
['http://example.com/a?b#c', '?b#c'], | |
['http://example.com/a?b#c', '#c'], | |
['http://example.com/a?b#c', '../a?b#c'], | |
].forEach(([url, candidate]) => { | |
assert.ok(resolvesToEqual(url, candidate), `expected to resolve to equal: ${url} and ${candidate}`); | |
}); | |
}); | |
test('false', () => { | |
[ | |
['http://example.com/a?b#c', 'http://example.com/x?b#c'], | |
['http://example.com/a?b#c', 'http://example.com/a?x#c'], | |
['http://example.com/a?b#c', 'http://example.com/a?b#x'], | |
['http://example.com/a?b#c', 'http://example.com/x?y#c'], | |
['http://example.com/a?b#c', 'http://example.com/x?b#y'], | |
['http://example.com/a?b#c', 'http://example.com/x?y#z'], | |
['http://example.com/a?b#c', 'http://example.com/a?b'], | |
['http://example.com/a?b#c', 'http://example.com/a#c'], | |
['http://example.com/a?b#c', 'http://example.com/a'], | |
['http://example.com/a?b#c', '/a'], | |
['http://example.com/a?b#c', '/x'], | |
['http://example.com/a?b#c', '?y'], | |
['http://example.com/a?b#c', '#z'], | |
['http://example.com/a?b#c', '?y#z'], | |
['http://example.com/a?b#c', '/x?y'], | |
['http://example.com/a?b#c', '/x#z'], | |
['http://example.com/a?b#c', '/x?y#z'], | |
['http://example.com/a?b#c', '../'], | |
['http://example.com/a?b#c', 'http://example.com'], | |
].forEach(([url, candidate]) => { | |
assert.equal(resolvesToEqual(url, candidate), false, `expected to resolve to not equal: ${url} and ${candidate}`); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment