-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
237 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Prepends 'https://' schema to the URL if it's not already present. | ||
* @param {string} url - The URL to modify. | ||
* @returns {string} - The URL with 'https://' schema prepended. | ||
*/ | ||
function prependSchema(url) { | ||
return url.startsWith('http://') || url.startsWith('https://') ? url : `https://${url}`; | ||
} | ||
|
||
/** | ||
* Strips the port number from the end of the URL. | ||
* @param {string} url - The URL to modify. | ||
* @returns {string} - The URL with the port removed. | ||
*/ | ||
function stripPort(url) { | ||
return url.replace(/:\d{1,5}(\/|$)/, ''); | ||
} | ||
|
||
/** | ||
* Strips the trailing dot from the end of the URL. | ||
* @param {string} url - The URL to modify. | ||
* @returns {string} - The URL with the trailing dot removed. | ||
*/ | ||
function stripTrailingDot(url) { | ||
return url.endsWith('.') ? url.slice(0, -1) : url; | ||
} | ||
|
||
/** | ||
* Strips the trailing slash from the end of the URL. | ||
* @param {string} url - The URL to modify. | ||
* @returns {string} - The URL with the trailing slash removed. | ||
*/ | ||
function stripTrailingSlash(url) { | ||
return url.endsWith('/') ? url.slice(0, -1) : url; | ||
} | ||
|
||
/** | ||
* Strips 'www.' from the beginning of the URL if present. | ||
* @param {string} url - The URL to modify. | ||
* @returns {string} - The URL with 'www.' removed. | ||
*/ | ||
function stripWWW(url) { | ||
const regex = /^(https?:\/\/)?(www\.)?/; | ||
// Replace "www." with an empty string, preserving the schema if present | ||
return url.replace(regex, (match, schema) => (schema || '')); | ||
} | ||
|
||
/** | ||
* Composes a base URL by applying a series of transformations to the given domain. | ||
* @param {string} domain - The domain to compose the base URL from. | ||
* @returns {string} - The composed base URL. | ||
*/ | ||
function composeBaseURL(domain) { | ||
let baseURL = stripTrailingDot(domain); | ||
baseURL = stripPort(baseURL); | ||
baseURL = stripTrailingSlash(baseURL); | ||
baseURL = stripWWW(baseURL); | ||
baseURL = prependSchema(baseURL); | ||
return baseURL; | ||
} | ||
|
||
export { | ||
composeBaseURL, | ||
prependSchema, | ||
stripPort, | ||
stripTrailingDot, | ||
stripTrailingSlash, | ||
stripWWW, | ||
}; |
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,99 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'chai'; | ||
import { | ||
composeBaseURL, | ||
prependSchema, | ||
stripPort, | ||
stripTrailingDot, | ||
stripTrailingSlash, | ||
stripWWW, | ||
} from '../src/url-helpers.js'; | ||
|
||
describe('URL Utility Functions', () => { | ||
describe('prependSchema', () => { | ||
it('should prepend "https://" schema if not present', () => { | ||
expect(prependSchema('example.com')).to.equal('https://example.com'); | ||
}); | ||
|
||
it('should not modify the URL if schema is already present', () => { | ||
expect(prependSchema('https://example.com')).to.equal('https://example.com'); | ||
expect(prependSchema('http://example.com')).to.equal('http://example.com'); | ||
}); | ||
}); | ||
|
||
describe('stripPort', () => { | ||
it('should remove port number from the end of the URL', () => { | ||
expect(stripPort('example.com:80')).to.equal('example.com'); | ||
expect(stripPort('example.com:8080')).to.equal('example.com'); | ||
}); | ||
|
||
it('should not modify the URL if no port number present', () => { | ||
expect(stripPort('example.com')).to.equal('example.com'); | ||
}); | ||
}); | ||
|
||
describe('stripTrailingDot', () => { | ||
it('should remove trailing dot from the end of the URL', () => { | ||
expect(stripTrailingDot('example.com.')).to.equal('example.com'); | ||
expect(stripTrailingDot('.example.com')).to.equal('.example.com'); | ||
expect(stripTrailingDot('example.com.abc')).to.equal('example.com.abc'); | ||
}); | ||
|
||
it('should not modify the URL if no trailing dot present', () => { | ||
expect(stripTrailingDot('example.com')).to.equal('example.com'); | ||
}); | ||
}); | ||
|
||
describe('stripTrailingSlash', () => { | ||
it('should remove trailing slash from the end of the URL', () => { | ||
expect(stripTrailingSlash('example.com/')).to.equal('example.com'); | ||
expect(stripTrailingSlash('example.com//')).to.equal('example.com/'); | ||
expect(stripTrailingSlash('/example.com/')).to.equal('/example.com'); | ||
expect(stripTrailingSlash('example.com/abc/')).to.equal('example.com/abc'); | ||
}); | ||
|
||
it('should not modify the URL if no trailing slash present', () => { | ||
expect(stripTrailingSlash('example.com')).to.equal('example.com'); | ||
}); | ||
}); | ||
|
||
describe('stripWWW', () => { | ||
it('should remove "www." from the beginning of the URL', () => { | ||
expect(stripWWW('www.example.com')).to.equal('example.com'); | ||
}); | ||
|
||
it('should not modify the URL if "www." is not present', () => { | ||
expect(stripWWW('example.com')).to.equal('example.com'); | ||
}); | ||
|
||
it('should remove "www." even if schema is present', () => { | ||
expect(stripWWW('https://www.example.com')).to.equal('https://example.com'); | ||
expect(stripWWW('http://www.example.com')).to.equal('http://example.com'); | ||
}); | ||
}); | ||
|
||
describe('composeBaseURL', () => { | ||
it('should compose base URL by applying all transformations', () => { | ||
expect(composeBaseURL('https://www.example.com:8080/')).to.equal('https://example.com'); | ||
expect(composeBaseURL('http://www.example.com:8080/')).to.equal('http://example.com'); | ||
expect(composeBaseURL('www.example.com:8080/')).to.equal('https://example.com'); | ||
expect(composeBaseURL('example.com:8080/')).to.equal('https://example.com'); | ||
expect(composeBaseURL('https://example.com/')).to.equal('https://example.com'); | ||
expect(composeBaseURL('http://example.com/')).to.equal('http://example.com'); | ||
expect(composeBaseURL('example.com/')).to.equal('https://example.com'); | ||
}); | ||
}); | ||
}); |