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.
npm run import -- "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;
/**
* 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;
getTemplateByUrl
Resolves the template for a given URL path.
templates
: An object containing template information.path
: The URL path to resolve.path
is empty or a root URL (''
or '/'
), returns the entry template.path
into segments and filters the templates
object for matching segments.path
.getEntryTemplate
Resolves the entry template from a templates
object.
templates
: An object containing template information.templates
object for keys with a template
property.template
property and an index
property set to 0.template
property.The getTemplateByUrl
function is exported as a module.