提交 2f4f5718 编写于 作者: J Johannes Rieken

perf - less object and validation when creating URI objects

上级 b38e0521
...@@ -152,13 +152,14 @@ export default class URI { ...@@ -152,13 +152,14 @@ export default class URI {
// ---- parse & validate ------------------------ // ---- parse & validate ------------------------
public static parse(value: string): URI { public static parse(value: string): URI {
var ret = URI._parse(value); const ret = new URI();
ret = ret.with(undefined, const data = URI._parseComponents(value);
decodeURIComponent(ret.authority), ret._scheme = data.scheme;
decodeURIComponent(ret.path), ret._authority = decodeURIComponent(data.authority);
decodeURIComponent(ret.query), ret._path = decodeURIComponent(data.path);
decodeURIComponent(ret.fragment)); ret._query = decodeURIComponent(data.query);
ret._fragment = decodeURIComponent(data.fragment);
URI._validate(ret);
return ret; return ret;
} }
...@@ -173,29 +174,39 @@ export default class URI { ...@@ -173,29 +174,39 @@ export default class URI {
? '/' + path ? '/' + path
: path; : path;
var ret = URI._parse(path); const data = URI._parseComponents(path);
if (ret.scheme || ret.fragment || ret.query) { if (data.scheme || data.fragment || data.query) {
throw new Error('Path contains a scheme, fragment or a query. Can not convert it to a file uri.'); throw new Error('Path contains a scheme, fragment or a query. Can not convert it to a file uri.');
} }
ret = ret.with('file', undefined, const ret = new URI();
decodeURIComponent(ret.path[0] === '/' ? ret.path : '/' + ret.path), // path starts with slash ret._scheme = 'file';
undefined, undefined); ret._authority = data.authority;
ret._path = decodeURIComponent(data.path[0] === '/' ? data.path : '/' + data.path); // path starts with slash
ret._query = data.query;
ret._fragment = data.fragment;
URI._validate(ret);
return ret; return ret;
} }
private static _parse(value: string): URI { private static _parseComponents(value: string): UriComponents {
var ret = new URI();
var match = URI._regexp.exec(value); const ret: UriComponents = {
scheme: URI._empty,
authority: URI._empty,
path: URI._empty,
query: URI._empty,
fragment: URI._empty,
};
const match = URI._regexp.exec(value);
if (match) { if (match) {
ret._scheme = match[2] || ret._scheme; ret.scheme = match[2] || ret.scheme;
ret._authority = match[4] || ret._authority; ret.authority = match[4] || ret.authority;
ret._path = match[5] || ret._path; ret.path = match[5] || ret.path;
ret._query = match[7] || ret._query; ret.query = match[7] || ret.query;
ret._fragment = match[9] || ret._fragment; ret.fragment = match[9] || ret.fragment;
} }
URI._validate(ret);
return ret; return ret;
} }
...@@ -284,7 +295,7 @@ export default class URI { ...@@ -284,7 +295,7 @@ export default class URI {
} }
public toJSON(): any { public toJSON(): any {
return <URIComponents> { return <UriState> {
scheme: this.scheme, scheme: this.scheme,
authority: this.authority, authority: this.authority,
path: this.path, path: this.path,
...@@ -298,25 +309,28 @@ export default class URI { ...@@ -298,25 +309,28 @@ export default class URI {
static revive(data: any): URI { static revive(data: any): URI {
let result = new URI(); let result = new URI();
result._scheme = (<URIComponents> data).scheme; result._scheme = (<UriState> data).scheme;
result._authority = (<URIComponents> data).authority; result._authority = (<UriState> data).authority;
result._path = (<URIComponents> data).path; result._path = (<UriState> data).path;
result._query = (<URIComponents> data).query; result._query = (<UriState> data).query;
result._fragment = (<URIComponents> data).fragment; result._fragment = (<UriState> data).fragment;
result._fsPath = (<URIComponents> data).fsPath; result._fsPath = (<UriState> data).fsPath;
result._formatted = (<URIComponents>data).external; result._formatted = (<UriState>data).external;
URI._validate(result); URI._validate(result);
return result; return result;
} }
} }
interface URIComponents { interface UriComponents {
$mid: number;
scheme: string; scheme: string;
authority: string; authority: string;
path: string; path: string;
fsPath: string;
query: string; query: string;
fragment: string; fragment: string;
}
interface UriState extends UriComponents {
$mid: number;
fsPath: string;
external: string; external: string;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册