Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support wildcard string prefixes #8942

Merged
merged 7 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/native/src/types.tsx
Expand Up @@ -29,14 +29,12 @@ export type LinkingOptions = {
* Only applicable on Android and iOS.
*
* @example
* {
* prefixes: [
* "https://example.com", // Exact
* /^[^.s]+.example.com/g // Match with all subdomain and scheme,
* ],
* }
* "https://*.example.com" // Match with any subdomain
* ]
*/
prefixes: (string | RegExp)[];
prefixes: string[];
/**
* Config to fine-tune how to parse the path.
*
Expand Down
20 changes: 3 additions & 17 deletions packages/native/src/useLinking.native.tsx
Expand Up @@ -58,8 +58,9 @@ export default function useLinking(

const extractPathFromURL = React.useCallback((url: string) => {
for (const prefix of prefixesRef.current) {
if (checkIsPrefixMatch(url, prefix)) {
return url.replace(prefix, '');
const prefixRegex = new RegExp(`^${prefix.replace(/\*/g, '[^/]+')}`);
hosseinmd marked this conversation as resolved.
Show resolved Hide resolved
if (url.match(prefixRegex)) {
hosseinmd marked this conversation as resolved.
Show resolved Hide resolved
return url.replace(prefixRegex, '');
}
}

Expand Down Expand Up @@ -122,18 +123,3 @@ export default function useLinking(
getInitialState,
};
}

function checkIsPrefixMatch(
url: string,
prefix: string | RegExp
): string | RegExp | false {
if (typeof prefix === 'string') {
if (url.startsWith(prefix)) {
return prefix;
}
} else if (url.match(prefix)) {
return prefix;
}

return false;
}