Skip to content

Commit

Permalink
feat: url helpers in utils (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney authored Feb 7, 2024
1 parent 39859bd commit 0a90c1b
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/spacecat-shared-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,45 @@ export function dateAfterDays(days: string): Date;

export function sqsEventAdapter(fn: (message: object, context: object) => Promise<Response>):
(request: object, context: object) => Promise<Response>;

/**
* Prepends 'https://' schema to the URL if it's not already present.
* @param url - The URL to modify.
* @returns The URL with 'https://' schema prepended.
*/
declare function prependSchema(url: string): string;

/**
* Strips the port number from the end of the URL.
* @param url - The URL to modify.
* @returns The URL with the port removed.
*/
declare function stripPort(url: string): string;

/**
* Strips the trailing dot from the end of the URL.
* @param url - The URL to modify.
* @returns The URL with the trailing dot removed.
*/
declare function stripTrailingDot(url: string): string;

/**
* Strips the trailing slash from the end of the URL.
* @param url - The URL to modify.
* @returns The URL with the trailing slash removed.
*/
declare function stripTrailingSlash(url: string): string;

/**
* Strips 'www.' from the beginning of the URL if present.
* @param url - The URL to modify.
* @returns The URL with 'www.' removed.
*/
declare function stripWWW(url: string): string;

/**
* Composes a base URL by applying a series of transformations to the given domain.
* @param domain - The domain to compose the base URL from.
* @returns The composed base URL.
*/
declare function composeBaseURL(domain: string): string;
9 changes: 9 additions & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ export {
export { resolveSecretsName } from './helpers.js';

export { sqsEventAdapter } from './sqs.js';

export {
composeBaseURL,
prependSchema,
stripPort,
stripTrailingDot,
stripTrailingSlash,
stripWWW,
} from './url-helpers.js';
81 changes: 81 additions & 0 deletions packages/spacecat-shared-utils/src/url-helpers.js
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,
};
6 changes: 6 additions & 0 deletions packages/spacecat-shared-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('Index Exports', () => {
'resolveSecretsName',
'dateAfterDays',
'sqsEventAdapter',
'composeBaseURL',
'prependSchema',
'stripPort',
'stripTrailingDot',
'stripTrailingSlash',
'stripWWW',
];

it('exports all expected functions', () => {
Expand Down
99 changes: 99 additions & 0 deletions packages/spacecat-shared-utils/test/url-helpers.test.js
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');
});
});
});

0 comments on commit 0a90c1b

Please sign in to comment.