mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-24 04:25:34 +08:00
feat: support string array for uriPrefix (#66)
This commit is contained in:
@@ -24,7 +24,7 @@ it('urlToPathAndParams with params', () => {
|
||||
expect(params).toEqual({ asdf: '1', dude: 'foo' });
|
||||
});
|
||||
|
||||
it('urlToPathAndParams with custom delimeter', () => {
|
||||
it('urlToPathAndParams with custom delimeter string', () => {
|
||||
const { path, params } = urlToPathAndParams(
|
||||
'https://example.com/foo/bar?asdf=1',
|
||||
'https://example.com/'
|
||||
@@ -32,3 +32,39 @@ it('urlToPathAndParams with custom delimeter', () => {
|
||||
expect(path).toBe('foo/bar');
|
||||
expect(params).toEqual({ asdf: '1' });
|
||||
});
|
||||
|
||||
it('urlToPathAndParams with custom delimeter RegExp', () => {
|
||||
const { path, params } = urlToPathAndParams(
|
||||
'https://example.com/foo/bar?asdf=1',
|
||||
new RegExp('https://example.com/')
|
||||
);
|
||||
expect(path).toBe('foo/bar');
|
||||
expect(params).toEqual({ asdf: '1' });
|
||||
});
|
||||
|
||||
it('urlToPathAndParams with duplicate prefix in query parameters', () => {
|
||||
const { path, params } = urlToPathAndParams(
|
||||
'example://whatever?related=example://something',
|
||||
'example://'
|
||||
);
|
||||
expect(path).toBe('whatever');
|
||||
expect(params).toEqual({ related: 'example://something' });
|
||||
});
|
||||
|
||||
it('urlToPathAndParams with array of custom delimiters, should use first match', () => {
|
||||
const { path, params } = urlToPathAndParams(
|
||||
'https://example.com/foo/bar?asdf=1',
|
||||
['baz', 'https://example.com/', 'https://example.com/foo']
|
||||
);
|
||||
expect(path).toBe('foo/bar');
|
||||
expect(params).toEqual({ asdf: '1' });
|
||||
});
|
||||
|
||||
it('urlToPathAndParams with array of custom delimiters where none match, should resort to default delimiter', () => {
|
||||
const { path, params } = urlToPathAndParams('foo://foo/bar?asdf=1', [
|
||||
'baz',
|
||||
'bazzlefraz',
|
||||
]);
|
||||
expect(path).toBe('foo/bar');
|
||||
expect(params).toEqual({ asdf: '1' });
|
||||
});
|
||||
|
||||
@@ -40,14 +40,25 @@ const getRestOfPath = (pathMatch, pathMatchKeys) => {
|
||||
return rest;
|
||||
};
|
||||
|
||||
const determineDelimiter = (uri, uriPrefix) => {
|
||||
if (Array.isArray(uriPrefix)) {
|
||||
if (uriPrefix.length === 1) return uriPrefix[0];
|
||||
for (let prefix of uriPrefix) {
|
||||
if (uri.startsWith(prefix)) return prefix;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return uriPrefix;
|
||||
};
|
||||
|
||||
export const urlToPathAndParams = (url, uriPrefix) => {
|
||||
const searchMatch = url.match(/^(.*)\?(.*)$/);
|
||||
const params = searchMatch ? queryString.parse(searchMatch[2]) : {};
|
||||
const urlWithoutSearch = searchMatch ? searchMatch[1] : url;
|
||||
const delimiter = uriPrefix || '://';
|
||||
let path = urlWithoutSearch.split(delimiter)[1];
|
||||
const [, urlWithoutQuery, query] = searchMatch || [null, url, {}];
|
||||
const params = queryString.parse(query);
|
||||
const delimiter = determineDelimiter(urlWithoutQuery, uriPrefix) || '://';
|
||||
let path = urlWithoutQuery.split(delimiter)[1];
|
||||
if (path === undefined) {
|
||||
path = urlWithoutSearch;
|
||||
path = urlWithoutQuery;
|
||||
}
|
||||
if (path === '/') {
|
||||
path = '';
|
||||
|
||||
Reference in New Issue
Block a user