Skip to content
This repository has been archived by the owner on Dec 21, 2020. It is now read-only.

Commit

Permalink
Add relative path parse support and test
Browse files Browse the repository at this point in the history
  • Loading branch information
edward-hsu-1994 committed Dec 5, 2017
1 parent 5830815 commit b986900
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ var uriDataSet = [
pathSegments: ['watch']
}
],
[
'./a/b/c/watch',
{
schema: UriBuilder.relative,
host: '.',
port: undefined,
pathSegments: ['a', 'b', 'c', 'watch']
}
],
[
'https://www.youtube.com/watch?v=TlzfSfc_ymI&%E4%B8%AD%E6%96%87=%E4%B8%AD%E6%96%87',
{
Expand Down Expand Up @@ -200,3 +209,10 @@ console.assert(
emptyBuilder.toString() === 'http://guest@example/home/index#top'
);
console.log(emptyBuilder.toString());

hr();
console.info('Relative Test');
const relativeBuilder = UriBuilder.parse('./home/exam?id=0');
relativeBuilder.query.id = 1;
console.assert(relativeBuilder.toString() === './home/exam?id=1');
console.log(relativeBuilder.toString());
15 changes: 14 additions & 1 deletion src/uriBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IUriModel {
}

export class UriBuilder implements IUriModel {
public static relative = 'relative';
public schema: string;

private _authority: IUriAuthority;
Expand Down Expand Up @@ -63,7 +64,10 @@ export class UriBuilder implements IUriModel {

public static parse(uri: string): UriBuilder {
if (!this.isUriFormat(uri)) {
throw new Error('URI Format ERROR');
uri = `${UriBuilder.relative}://${uri}`;
if (!this.isUriFormat(uri)) {
throw new Error('URI Format ERROR');
}
}

const result = new UriBuilder();
Expand Down Expand Up @@ -126,8 +130,17 @@ export class UriBuilder implements IUriModel {
this._authority.password = password;
}

public isRelative(): boolean {
return this.schema === UriBuilder.relative;
}

public toString(): string {
let result = `${this.schema}://`;

if (this.schema === UriBuilder.relative) {
result = '';
}

if (this.authority && this.authority.user) {
result += this.authority.user;
if (this.authority.password) {
Expand Down
1 change: 1 addition & 0 deletions src/uriSchemaPortList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class UriSchemaPortList {
static http = 80;
static https = 80;
static relative = undefined;

public static getPort(schema: string): number {
return this[schema.toLowerCase()];
Expand Down

0 comments on commit b986900

Please sign in to comment.