All files url-builder.js

100% Statements 50/50
95.45% Branches 21/22
100% Functions 8/8
100% Lines 50/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                51x                   34x   34x 34x 34x   34x   34x 9x     34x 57x 57x   57x 19x 19x   38x               34x 9x             9x     34x                         9x 9x 9x   9x         9x 10x 10x   10x       8x 8x   2x   2x             9x   9x   9x                     57x   57x   57x 4x 2x       57x 51x     57x                     47x   47x   47x   47x 45x     2x   2x       2x      
/**
 * © 2014 Liferay, Inc. <https://liferay.com>
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
 
/**
 *
 */
export default class URLBuilder {
	/**
	 * Creates an instance of URLBuilder class
	 * @constructor
	 * @param {Config} config
	 */
	constructor(config) {
		this._config = config;
	}
 
	/**
	 * Returns a list of URLs from provided list of modules.
	 * @param {array} moduleNames list of modules for which URLs should be
	 * 								created
	 * @return {array} list of URLs
	 */
	build(moduleNames) {
		const config = this._config;
 
		const bufferURL = [];
		const modulesURL = [];
		let result = [];
 
		let basePath = config.basePath;
 
		if (basePath.length && basePath.charAt(basePath.length - 1) !== '/') {
			basePath += '/';
		}
 
		moduleNames.forEach(moduleName => {
			const module = config.getModule(moduleName);
			const path = this._getModulePath(module);
 
			if (config.combine) {
				bufferURL.push(path);
				modulesURL.push(module.name);
			} else {
				result.push({
					modules: [module.name],
					url: this._getURLWithParams(config.url + basePath + path),
				});
			}
		});
 
		// Add to the result all modules, which have to be combined.
		if (bufferURL.length) {
			result = result.concat(
				this._generateBufferURLs(modulesURL, bufferURL, {
					basePath,
					url: config.url,
					urlMaxLength: config.urlMaxLength,
				})
			);
			bufferURL.length = 0;
		}
 
		return result;
	}
 
	/**
	 * Generate the appropriate set of URLs based on the list of
	 * required modules and the maximum allowed URL length
	 * @param {Array<String>} modules Array of module names
	 * @param {Array<String>} urls Array of module URLs
	 * @param {Object} config Configuration object containing URL, basePath and
	 *     						urlMaxLength
	 * @return {Array<Object>} Resulting array of {modules, url} objects
	 */
	_generateBufferURLs(modules, urls, config) {
		const basePath = config.basePath;
		const result = [];
		const urlMaxLength = config.urlMaxLength;
 
		let urlResult = {
			modules: [modules[0]],
			url: config.url + basePath + urls[0],
		};
 
		for (let i = 1; i < urls.length; i++) {
			const module = modules[i];
			const path = urls[i];
 
			if (
				urlResult.url.length + basePath.length + path.length + 1 <
				urlMaxLength
			) {
				urlResult.modules.push(module);
				urlResult.url += '&' + basePath + path;
			} else {
				result.push(urlResult);
 
				urlResult = {
					modules: [module],
					url: config.url + basePath + path,
				};
			}
		}
 
		urlResult.url = this._getURLWithParams(urlResult.url);
 
		result.push(urlResult);
 
		return result;
	}
 
	/**
	 * Returns the path for a module. If module has property path, it will be
	 * returned directly. Otherwise, the name of module will be used and
	 * extension .js will be added to module name if omitted.
	 * @param {object} module The module which path should be returned.
	 * @return {string} Module path.
	 */
	_getModulePath(module) {
		const paths = this._config.paths;
 
		let path = module.name;
 
		Object.keys(paths).forEach(function(item) {
			if (path === item || path.indexOf(item + '/') === 0) {
				path = paths[item] + path.substring(item.length);
			}
		});
 
		if (path.lastIndexOf('.js') !== path.length - 3) {
			path += '.js';
		}
 
		return path;
	}
 
	/**
	 * Returns an url with parameters defined in config.defaultURLParams. If
	 * config.defaultURLParams is not defined or is an empty map, the url will
	 * be returned unmodified.
	 * @param {string} url The url to be returned with parameters.
	 * @return {string} url The url with parameters.
	 */
	_getURLWithParams(url) {
		const config = this._config;
 
		const defaultURLParams = config.defaultURLParams || {};
 
		const keys = Object.keys(defaultURLParams);
 
		if (!keys.length) {
			return url;
		}
 
		const queryString = keys
			.map(function(key) {
				return key + '=' + defaultURLParams[key];
			})
			.join('&');
 
		return url + (url.indexOf('?') > -1 ? '&' : '?') + queryString;
	}
}