X-Git-Url: http://git.squeep.com/?p=squeep-indie-auther;a=blobdiff_plain;f=src%2Ftemplate%2Ftemplate-helper.js;fp=src%2Ftemplate%2Ftemplate-helper.js;h=a33c8b004a35677925ba3e1c60b0ac8034b212c4;hp=0000000000000000000000000000000000000000;hb=b0103b0d496262c438b40bc20304081dbfe41e73;hpb=8ed81748bce7cea7904cac7225b20a60cafdfc16 diff --git a/src/template/template-helper.js b/src/template/template-helper.js new file mode 100644 index 0000000..a33c8b0 --- /dev/null +++ b/src/template/template-helper.js @@ -0,0 +1,53 @@ +'use strict'; + +const { TemplateHelper } = require('@squeep/html-template-helper'); + + +/** + * Escape a string to be suitable as a CSS name. + * @param {String} unsafeName + * @returns {String} + */ +function escapeCSS(unsafeName) { + return unsafeName.replace(/([^0-9a-zA-Z-])/g, '\\$1'); +} + + +/** + * Given a pair of Array tuples containing scope names and scope details, + * return the comparison between the two, for sorting. + * Scopes are sorted such that they are grouped by application, then name. + * Empty applications are sorted ahead of extant applications. + * @param {Array} a + * @param {Array} b + * @returns {Number} + */ +function scopeCompare([aScope, aDetails], [bScope, bDetails]) { + const { application: aApp } = aDetails; + const { application: bApp } = bDetails; + if ((aApp || bApp) && (aApp !== bApp)) { + if (!aApp) { + return -1; + } + if (!bApp) { + return 1; + } + if (aApp > bApp) { + return 1; + } + return -1; + } + + if (aScope > bScope) { + return 1; + } else if (aScope < bScope) { + return -1; + } + return 0; +} + + +module.exports = Object.assign(Object.create(TemplateHelper), { + escapeCSS, + scopeCompare, +}); \ No newline at end of file