зеркало из https://github.com/Azure/ms-rest-js.git
Merge pull request #85 from Azure/daschult/FixSubstitution
Make substitution happen across entire URLBuilder, not just individua…
This commit is contained in:
Коммит
4daf131feb
40
lib/url.ts
40
lib/url.ts
|
@ -144,7 +144,7 @@ export class URLBuilder {
|
|||
* Set the scheme/protocol for this URL. If the provided scheme contains other parts of a URL
|
||||
* (such as a host, port, path, or query), those parts will be added to this URL as well.
|
||||
*/
|
||||
public setScheme(scheme: string): void {
|
||||
public setScheme(scheme: string | undefined): void {
|
||||
if (!scheme) {
|
||||
this._scheme = undefined;
|
||||
} else {
|
||||
|
@ -163,7 +163,7 @@ export class URLBuilder {
|
|||
* Set the host for this URL. If the provided host contains other parts of a URL (such as a
|
||||
* port, path, or query), those parts will be added to this URL as well.
|
||||
*/
|
||||
public setHost(host: string): void {
|
||||
public setHost(host: string | undefined): void {
|
||||
if (!host) {
|
||||
this._host = undefined;
|
||||
} else {
|
||||
|
@ -220,16 +220,6 @@ export class URLBuilder {
|
|||
return this._path;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the provided searchValue is found in this URLBuilder's path, then replace it with the
|
||||
* provided replaceValue.
|
||||
*/
|
||||
public pathSubstitution(searchValue: string, replaceValue: string): void {
|
||||
if (this._path && searchValue) {
|
||||
this._path = replaceAll(this._path, searchValue, replaceValue || "");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the query in this URL.
|
||||
*/
|
||||
|
@ -270,16 +260,6 @@ export class URLBuilder {
|
|||
return this._query ? this._query.toString() : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the provided searchValue is found in this URLBuilder's query, then replace it with the
|
||||
* provided replaceValue.
|
||||
*/
|
||||
public querySubstitution(searchValue: string, replaceValue: string): void {
|
||||
if (this._query && searchValue) {
|
||||
this._query = URLQuery.parse(replaceAll(this._query.toString(), searchValue, replaceValue));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parts of this URL by parsing the provided text using the provided startState.
|
||||
*/
|
||||
|
@ -349,6 +329,20 @@ export class URLBuilder {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the provided searchValue is found in this URLBuilder, then replace it with the provided
|
||||
* replaceValue.
|
||||
*/
|
||||
public replaceAll(searchValue: string, replaceValue: string): void {
|
||||
if (searchValue) {
|
||||
this.setScheme(replaceAll(this.getScheme(), searchValue, replaceValue));
|
||||
this.setHost(replaceAll(this.getHost(), searchValue, replaceValue));
|
||||
this.setPort(replaceAll(this.getPort(), searchValue, replaceValue));
|
||||
this.setPath(replaceAll(this.getPath(), searchValue, replaceValue));
|
||||
this.setQuery(replaceAll(this.getQuery(), searchValue, replaceValue));
|
||||
}
|
||||
}
|
||||
|
||||
public static parse(text: string): URLBuilder {
|
||||
const result = new URLBuilder();
|
||||
result.set(text, URLTokenizerState.SCHEME_OR_HOST);
|
||||
|
@ -413,7 +407,7 @@ export function isAlphaNumericCharacter(character: string): boolean {
|
|||
/**
|
||||
* Replace all of the instances of searchValue in value with the provided replaceValue.
|
||||
*/
|
||||
export function replaceAll(value: string, searchValue: string, replaceValue: string): string {
|
||||
export function replaceAll(value: string | undefined, searchValue: string, replaceValue: string): string | undefined {
|
||||
return !value || !searchValue ? value : value.split(searchValue).join(replaceValue || "");
|
||||
}
|
||||
|
||||
|
|
|
@ -707,11 +707,11 @@ describe("URLBuilder", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("pathSubstitution()", () => {
|
||||
describe("replaceAll()", () => {
|
||||
it(`with undefined path, "{arg}" searchValue, and "cats" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setPath(undefined);
|
||||
urlBuilder.pathSubstitution("{arg}", "cats");
|
||||
urlBuilder.replaceAll("{arg}", "cats");
|
||||
assert.strictEqual(urlBuilder.getPath(), undefined);
|
||||
assert.strictEqual(urlBuilder.toString(), "");
|
||||
});
|
||||
|
@ -719,7 +719,7 @@ describe("URLBuilder", () => {
|
|||
it(`with "" path, "{arg}" searchValue, and "cats" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setPath("");
|
||||
urlBuilder.pathSubstitution("{arg}", "cats");
|
||||
urlBuilder.replaceAll("{arg}", "cats");
|
||||
assert.strictEqual(urlBuilder.getPath(), undefined);
|
||||
assert.strictEqual(urlBuilder.toString(), "");
|
||||
});
|
||||
|
@ -727,7 +727,7 @@ describe("URLBuilder", () => {
|
|||
it(`with "my/really/cool/path" path, "" searchValue, and "cats" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setPath("my/really/cool/path");
|
||||
urlBuilder.pathSubstitution("", "cats");
|
||||
urlBuilder.replaceAll("", "cats");
|
||||
assert.strictEqual(urlBuilder.getPath(), "my/really/cool/path");
|
||||
assert.strictEqual(urlBuilder.toString(), "/my/really/cool/path");
|
||||
});
|
||||
|
@ -735,7 +735,7 @@ describe("URLBuilder", () => {
|
|||
it(`with "my/really/cool/path" path, "y" searchValue, and "z" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setPath("my/really/cool/path");
|
||||
urlBuilder.pathSubstitution("y", "z");
|
||||
urlBuilder.replaceAll("y", "z");
|
||||
assert.strictEqual(urlBuilder.getPath(), "mz/reallz/cool/path");
|
||||
assert.strictEqual(urlBuilder.toString(), "/mz/reallz/cool/path");
|
||||
});
|
||||
|
@ -743,17 +743,15 @@ describe("URLBuilder", () => {
|
|||
it(`with "my/really/cool/path" path, "y" searchValue, and "" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setPath("my/really/cool/path");
|
||||
urlBuilder.pathSubstitution("y", "");
|
||||
urlBuilder.replaceAll("y", "");
|
||||
assert.strictEqual(urlBuilder.getPath(), "m/reall/cool/path");
|
||||
assert.strictEqual(urlBuilder.toString(), "/m/reall/cool/path");
|
||||
});
|
||||
});
|
||||
|
||||
describe("querySubstitution()", () => {
|
||||
it(`with undefined query, "A" searchValue, and "Z" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setQuery(undefined);
|
||||
urlBuilder.querySubstitution("A", "Z");
|
||||
urlBuilder.replaceAll("A", "Z");
|
||||
assert.strictEqual(urlBuilder.getQuery(), undefined);
|
||||
assert.strictEqual(urlBuilder.toString(), "");
|
||||
});
|
||||
|
@ -761,7 +759,7 @@ describe("URLBuilder", () => {
|
|||
it(`with "A=B&C=D&E=A" query, "" searchValue, and "Z" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setQuery("A=B&C=D&E=A");
|
||||
urlBuilder.querySubstitution("", "Z");
|
||||
urlBuilder.replaceAll("", "Z");
|
||||
assert.strictEqual(urlBuilder.getQuery(), "A=B&C=D&E=A");
|
||||
assert.strictEqual(urlBuilder.toString(), "?A=B&C=D&E=A");
|
||||
});
|
||||
|
@ -769,7 +767,7 @@ describe("URLBuilder", () => {
|
|||
it(`with "A=B&C=D&E=A" query, "A" searchValue, and "" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setQuery("A=B&C=D&E=A");
|
||||
urlBuilder.querySubstitution("A", "");
|
||||
urlBuilder.replaceAll("A", "");
|
||||
assert.strictEqual(urlBuilder.getQuery(), "C=D&E=");
|
||||
assert.strictEqual(urlBuilder.toString(), "?C=D&E=");
|
||||
});
|
||||
|
@ -777,7 +775,7 @@ describe("URLBuilder", () => {
|
|||
it(`with "A=B&C=D&E=A" query, "A" searchValue, and "Z" replaceValue`, () => {
|
||||
const urlBuilder = new URLBuilder();
|
||||
urlBuilder.setQuery("A=B&C=D&E=A");
|
||||
urlBuilder.querySubstitution("A", "Z");
|
||||
urlBuilder.replaceAll("A", "Z");
|
||||
assert.strictEqual(urlBuilder.getQuery(), "Z=B&C=D&E=Z");
|
||||
assert.strictEqual(urlBuilder.toString(), "?Z=B&C=D&E=Z");
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче