Skip to content
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions packages/react-native/Libraries/Blob/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,43 +107,55 @@ export class URL {
}

get hash(): string {
throw new Error('URL.hash is not implemented');
const hashMatch = this._url.match(/#([^/]*)/);
return hashMatch ? `#${hashMatch[1]}` : '';
}

get host(): string {
throw new Error('URL.host is not implemented');
const hostMatch = this._url.match(/^https?:\/\/(?:[^@]+@)?([^:/?#]+)/);
const portMatch = this._url.match(/:(\d+)(?=[/?#]|$)/);
return hostMatch
? hostMatch[1] + (portMatch ? `:${portMatch[1]}` : '')
: '';
}

get hostname(): string {
throw new Error('URL.hostname is not implemented');
const hostnameMatch = this._url.match(/^https?:\/\/(?:[^@]+@)?([^:/?#]+)/);
return hostnameMatch ? hostnameMatch[1] : '';
}

get href(): string {
return this.toString();
}

get origin(): string {
throw new Error('URL.origin is not implemented');
const matches = this._url.match(/^(https?:\/\/[^/]+)/);
return matches ? matches[1] : '';
}

get password(): string {
throw new Error('URL.password is not implemented');
const passwordMatch = this._url.match(/https?:\/\/.*:(.*)@/);
return passwordMatch ? passwordMatch[1] : '';
}

get pathname(): string {
throw new Error('URL.pathname not implemented');
const pathMatch = this._url.match(/https?:\/\/[^/]+(\/[^?#]*)?/);
return pathMatch ? pathMatch[1] || '/' : '/';
}

get port(): string {
throw new Error('URL.port is not implemented');
const portMatch = this._url.match(/:(\d+)(?=[/?#]|$)/);
return portMatch ? portMatch[1] : '';
}

get protocol(): string {
throw new Error('URL.protocol is not implemented');
const protocolMatch = this._url.match(/^([a-zA-Z][a-zA-Z\d+\-.]*):/);
return protocolMatch ? protocolMatch[1] + ':' : '';
}

get search(): string {
throw new Error('URL.search is not implemented');
const searchMatch = this._url.match(/\?([^#]*)/);
return searchMatch ? `?${searchMatch[1]}` : '';
}

get searchParams(): URLSearchParams {
Expand All @@ -168,6 +180,7 @@ export class URL {
}

get username(): string {
throw new Error('URL.username is not implemented');
const usernameMatch = this._url.match(/^https?:\/\/([^:@]+)(?::[^@]*)?@/);
return usernameMatch ? usernameMatch[1] : '';
}
}
12 changes: 12 additions & 0 deletions packages/react-native/Libraries/Blob/__tests__/URL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@ describe('URL', function () {
// Insert / between Base and Path if missing
const k = new URL('en-US/docs', 'https://developer.mozilla.org');
expect(k.href).toBe('https://developer.mozilla.org/en-US/docs');
//More cases
const url = new URL(
'https://username:[email protected]:8080/docs/path?query=testQuery&key=value#fragment',
);
expect(url.hash).toBe('#fragment');
expect(url.host).toBe('reactnative.dev:8080');
expect(url.hostname).toBe('reactnative.dev');
expect(url.password).toBe('password');
expect(url.username).toBe('username');
expect(url.pathname).toBe('/docs/path');
expect(url.port).toBe('8080');
expect(url.search).toBe('?query=testQuery&key=value');
});
});
Loading