-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cert-v5): New certification configuration workflow for multi cer…
…t UI (#1430)
- Loading branch information
Brett Goulder
committed
Feb 5, 2020
1 parent
630331e
commit 50414f1
Showing
4 changed files
with
278 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
function splitDomains(domains) { | ||
return domains.map((domain) => { | ||
return [domain.substring(0, 1), domain.substring(1)]; | ||
}); | ||
} | ||
|
||
function createMatcherFromSplitDomain([firstChar, rest]) { | ||
const matcherContents = []; | ||
if (firstChar === '*') { | ||
matcherContents.push('^[\\w\\-]+'); | ||
} else { | ||
matcherContents.push(firstChar); | ||
} | ||
|
||
const escapedRest = rest.replace(/\./g, '\\.'); | ||
|
||
matcherContents.push(escapedRest); | ||
|
||
return new RegExp(matcherContents.join('')); | ||
} | ||
|
||
function includesWildcard(domains) { | ||
if (domains.some((domain) => (domain[0] === "*"))) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
module.exports = function (certDomains, appDomains) { | ||
const splitCertDomains = splitDomains(certDomains); | ||
const matchers = splitCertDomains.map((splitDomain) => createMatcherFromSplitDomain(splitDomain)); | ||
|
||
if (includesWildcard(splitCertDomains)) { | ||
const matchedDomains = appDomains.reduce((acc, appDomain) => { | ||
if (matchers.some((matcher) => matcher.test(appDomain))) { | ||
acc.push(appDomain); | ||
} | ||
|
||
return acc; | ||
}, []); | ||
|
||
return matchedDomains; | ||
} else { | ||
return certDomains.filter((domain) => appDomains.includes(domain)); | ||
} | ||
} |
Oops, something went wrong.