-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use object deconstruction, optional chaining and hot path optimizatio…
…ns (#287)
- Loading branch information
1 parent
1affdb8
commit 9a3b014
Showing
9 changed files
with
63 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,15 @@ | ||
export default { | ||
name: 'path', | ||
|
||
lookup(options) { | ||
let found; | ||
if (typeof window !== 'undefined') { | ||
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (language instanceof Array) { | ||
if (typeof options.lookupFromPathIndex === 'number') { | ||
if (typeof language[options.lookupFromPathIndex] !== 'string') { | ||
return undefined; | ||
} | ||
found = language[options.lookupFromPathIndex].replace('/', ''); | ||
} else { | ||
found = language[0].replace('/', ''); | ||
} | ||
} | ||
} | ||
return found; | ||
// Deconstruct the options object and extract the lookupFromPathIndex property | ||
lookup({ lookupFromPathIndex }) { | ||
|
||
if (typeof window === 'undefined') return undefined; | ||
|
||
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (!Array.isArray(language)) return undefined; | ||
|
||
const index = typeof lookupFromPathIndex === 'number' ? lookupFromPathIndex : 0; | ||
return language[index]?.replace('/', ''); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
export default { | ||
name: 'subdomain', | ||
|
||
lookup(options) { | ||
lookup({ lookupFromSubdomainIndex }) { | ||
// If given get the subdomain index else 1 | ||
const lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' | ||
? options.lookupFromSubdomainIndex + 1 | ||
: 1; | ||
const internalLookupFromSubdomainIndex = typeof lookupFromSubdomainIndex === 'number' ? lookupFromSubdomainIndex + 1 : 1; | ||
// get all matches if window.location. is existing | ||
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match | ||
// first item of match is the match itself and the second is the first group match which should be the first subdomain match | ||
// is the hostname no public domain get the or option of localhost | ||
const language = typeof window !== 'undefined' | ||
&& window.location | ||
&& window.location.hostname | ||
&& window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); | ||
const language = typeof window !== 'undefined' && window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); | ||
|
||
// if there is no match (null) return undefined | ||
if (!language) return undefined; | ||
// return the given group match | ||
return language[lookupFromSubdomainIndex]; | ||
return language[internalLookupFromSubdomainIndex]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters