Skip to content

Commit

Permalink
feat(core): add parseDomainName to Fn class (aws#10465)
Browse files Browse the repository at this point in the history
Add function to Fn class to parse the domain name given an URL.

Fixes aws#5433

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
BryanPan342 authored Sep 22, 2020
1 parent 2e0824b commit 799da48
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/core/lib/cfn-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ export class Fn {
return Token.asList(new FnCidr(ipBlock, count, sizeMask));
}

/**
* Given an url, parse the domain name
* @param url the url to parse
*/
public static parseDomainName(url: string): string {
const noHttps = Fn.select(1, Fn.split('//', url));
return Fn.select(0, Fn.split('/', noHttps));
}

/**
* The intrinsic function ``Fn::GetAZs`` returns an array that lists
* Availability Zones for a specified region. Because customers have access to
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/core/test/test.fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ export = nodeunit.testCase({
test.done();
},
},
'FnParseDomainName': {
'parse domain name from resolved url'(test: nodeunit.Test) {
test.deepEqual(Fn.parseDomainName('https://test.com/'), 'test.com');
test.done();
},
'parse domain name on token'(test: nodeunit.Test) {
const stack = new Stack();
const url = Fn.join('//', [
'https:',
Fn.join('/', [
'test.com',
'graphql',
]),
]);
test.deepEqual(Fn.parseDomainName(stack.resolve(url)), 'test.com');
test.done();
},
},
'FnJoin': {
'rejects empty list of arguments to join'(test: nodeunit.Test) {
test.throws(() => Fn.join('.', []));
Expand Down

0 comments on commit 799da48

Please sign in to comment.