Ghost/core/server/web/shared/utils.js
Hannah Wolfe 1a4506dcf0 Moved validation utils into validation/upload
- These two functions have no dependencies and are only used in valiation/upload
- Co-locating the code makes it easier to move
- Exported them with a new module.exports._test pattern - we'll see about whether this is a good idea
- This is one of many similar changes needed to make it easier to refactor to the existing setup
2020-04-22 07:21:41 +01:00

32 lines
854 B
JavaScript

const url = require('url');
const _private = {};
_private.removeDoubleCharacters = (character, string) => {
const stringArray = string.split('');
return stringArray.reduce((newString, currentCharacter, index) => {
if (
currentCharacter === character &&
stringArray[index + 1] === character
) {
return newString;
}
return `${newString}${currentCharacter}`;
}, '');
};
module.exports.removeOpenRedirectFromUrl = function removeOpenRedirectFromUrl(urlString) {
const parsedUrl = url.parse(urlString);
return (
// http://
(parsedUrl.protocol ? parsedUrl.protocol + '//' : '') +
(parsedUrl.auth || '') +
(parsedUrl.host || '') +
_private.removeDoubleCharacters('/', parsedUrl.path) +
(parsedUrl.hash || '')
);
};