From 8ea4b50e8ff70905d4b3fb639f6b53c722e5f0cf Mon Sep 17 00:00:00 2001 From: acalcutt Date: Sun, 29 Dec 2024 03:15:54 -0500 Subject: [PATCH] cleanup serve_style Co-Authored-By: Andrew Calcutt --- src/serve_style.js | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/serve_style.js b/src/serve_style.js index 15f9250..ec8a0b4 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -12,15 +12,26 @@ import { fixUrl, allowedOptions } from './utils.js'; const httpTester = /^https?:\/\//i; const allowedSpriteFormats = allowedOptions(['png', 'json']); -const allowedSpriteScales = (scale) => { +/** + * Checks and formats sprite scale + * @param {string} scale string containing the scale + * @returns {string} formated string for the scale or empty string if scale is invalid + */ +function allowedSpriteScales(scale) { if (!scale) return ''; // Default to 1 if no scale provided const match = scale.match(/(\d+)x/); // Match one or more digits before 'x' const parsedScale = match ? parseInt(match[1], 10) : 1; // Parse the number, or default to 1 if no match return '@' + Math.min(parsedScale, 3) + 'x'; -}; +} export const serve_style = { - init: (options, repo) => { + /** + * Initializes the serve_style module. + * @param {object} options Configuration options. + * @param {object} repo Repository object. + * @returns {express.Application} The initialized Express application. + */ + init: function (options, repo) { const app = express().disable('x-powered-by'); app.get('/:id/style.json', (req, res, next) => { @@ -93,10 +104,35 @@ export const serve_style = { return app; }, - remove: (repo, id) => { + /** + * Removes an item from the repository. + * @param {object} repo Repository object. + * @param {string} id ID of the item to remove. + * @returns {void} + */ + remove: function (repo, id) { delete repo[id]; }, - add: (options, repo, params, id, publicUrl, reportTiles, reportFont) => { + /** + * Adds a new style to the repository. + * @param {object} options Configuration options. + * @param {object} repo Repository object. + * @param {object} params Parameters object containing style path + * @param {string} id ID of the style. + * @param {string} publicUrl Public URL of the data. + * @param {Function} reportTiles Function for reporting tile sources. + * @param {Function} reportFont Function for reporting font usage + * @returns {boolean} true if add is succesful + */ + add: function ( + options, + repo, + params, + id, + publicUrl, + reportTiles, + reportFont, + ) { const styleFile = path.resolve(options.paths.styles, params.style); let styleFileData;