convert spreadsheet | sheet to web | package.json | Search

The getTemplateByUrl function resolves a template for a given URL path by filtering a templates object based on the path segments, whereas the getEntryTemplate function resolves the entry template from the templates object by filtering for keys with a template property. The getTemplateByUrl function is exported as a module for use in other parts of the code.

Run example

npm run import -- "convert sheet helper functions"

convert sheet helper functions


var getTemplateByUrl = (templates, path) => !path || path === '' || path === '/'
    ? getEntryTemplate(templates)
    : path.split('/').filter(segment => templates[segment]
                             && templates[segment].template)[0]
    || path.split('/')[0];

var getEntryTemplate = (templates) => Object.keys(templates)
    .filter(t => templates[t].template
            && templates[t].template.properties
            && templates[t].template.properties.index == 0)[0]
    || Object.keys(templates).filter(t => templates[t].template)[0];

module.exports = getTemplateByUrl;

What the code could have been:

/**
 * Retrieves a template from the provided templates object based on the given path.
 * 
 * If the path is empty or root, returns the entry template. Otherwise, splits the path
 * by '/' and finds the first segment that matches a template in the templates object.
 * If no match is found, returns the first segment of the path.
 * 
 * @param {Object} templates - An object containing template information.
 * @param {string} path - The path to the template.
 * @returns {string|undefined} The path to the template, or undefined if not found.
 */
const getTemplateByUrl = (templates, path) => {
    // Check if path is empty or root, and return the entry template if so
    if (!path || path === '' || path === '/') {
        return getEntryTemplate(templates);
    }

    // Split the path by '/' and filter out segments that do not match a template
    const matchingSegments = path.split('/').filter(segment => templates[segment] && templates[segment].template);

    // If a matching segment is found, return its absolute path
    if (matchingSegments.length > 0) {
        return path.startsWith('/')? '/' + matchingSegments[0] : matchingSegments[0];
    }

    // If no matching segment is found, return the first segment of the path
    return path.split('/')[0];
};

/**
 * Retrieves the entry template from the provided templates object.
 * 
 * @param {Object} templates - An object containing template information.
 * @returns {string|undefined} The path to the entry template, or undefined if not found.
 */
const getEntryTemplate = (templates) => {
    // Find the entry template with index 0
    const entryTemplate = Object.keys(templates).find(t => templates[t].template && templates[t].template.properties && templates[t].template.properties.index === 0);

    // If no entry template is found, return the first template
    return entryTemplate || Object.keys(templates).find(t => templates[t].template);
};

module.exports = getTemplateByUrl;

Code Breakdown

Function: getTemplateByUrl

Purpose

Resolves the template for a given URL path.

Parameters

Behavior

  1. If path is empty or a root URL ('' or '/'), returns the entry template.
  2. Splits the path into segments and filters the templates object for matching segments.
  3. If a matching segment is found, returns the corresponding template.
  4. If no matching segment is found, returns the first segment of the path.

Function: getEntryTemplate

Purpose

Resolves the entry template from a templates object.

Parameters

Behavior

  1. Filters the templates object for keys with a template property.
  2. Further filters the result for keys with a template property and an index property set to 0.
  3. If a matching key is found, returns it. Otherwise, returns the first key with a template property.

Export

The getTemplateByUrl function is exported as a module.