• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geosolutions-it / MapStore2 / 19710972030

26 Nov 2025 03:38PM UTC coverage: 76.665% (-0.3%) from 76.929%
19710972030

Pull #11119

github

web-flow
Fix maven publish (#11739)
Pull Request #11119: Layer Selection Plugin on ArcGIS, WFS & WMS layers

32272 of 50209 branches covered (64.28%)

3 of 3 new or added lines in 2 files covered. (100.0%)

3018 existing lines in 249 files now uncovered.

40157 of 52380 relevant lines covered (76.66%)

37.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/web/client/utils/TemplateUtils.js
1
/**
2
 * Copyright 2017, GeoSolutions Sas.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under the BSD-style license found in the
6
 * LICENSE file in the root directory of this source tree.
7
 */
8

9
import {isString, has, trim, template} from 'lodash';
10

11
/**
12
 * check if a string attribute is inside of a given object
13
 * @param feature {object}
14
 * @param attribute {string} name of attribue with dot notations
15
 * @param start {array} substring start
16
 * @param end {array} substring end
17
 * @return {bool} true if feature contains the attribute
18
 */
19
export const validateStringAttribute = (feature, attribute, start = 0, end = 0) => {
1✔
20
    const path = isString(attribute) && trim(attribute.substring(start, attribute.length - end)) || '';
38!
21
    return has(feature, path);
38✔
22
};
23

24
/**
25
 * returns a valid template
26
 * @param chosenTemplate {string} text with attribute to validate
27
 * @param feature {object} object to match attributes
28
 * @param regex {regex}
29
 * @param start {array} substring start
30
 * @param end {array} substring end
31
 * @return {string} template without invalid attribute and html tag inside attribute, e.g. ${ <p>properties.id</p> } -> ${ properties.id }
32
 */
33
export const getCleanTemplate = (chosenTemplate, feature, regex, start = 0, end = 0, getDefaultMissingProperty = () => "") => {
2!
34
    const matchVariables = isString(chosenTemplate) && chosenTemplate.match(regex);
19✔
35
    const replacedTag = matchVariables && matchVariables.map(temp => {
19!
36
        const varReplaced = temp.replace(/(<([^>]+)>)/ig, '');
32✔
37
        return {
32✔
38
            previous: temp,
39
            next: validateStringAttribute(feature, varReplaced, start, end) ? varReplaced : getDefaultMissingProperty(temp)
32✔
40
        };
41
    }) || null;
42
    return replacedTag && replacedTag.reduce((temp, variable) => temp.replace(variable.previous, variable.next), chosenTemplate) || chosenTemplate || '';
32!
43
};
44

45
/**
46
 * parses a template with attributes defined in ${ ... } and applied to the metadata object
47
 * @param metadataTemplate {string} text with attribute to validate
48
 * @param getDefaultMissingProperty {function} if defined it returns a default value for undefined attributes
49
 * @param metadata {object} metadata object to match attributes
50
 * @return {string} template without invalid attribute and html tag inside attribute, e.g. ${ <p>properties.id</p> } -> ${ properties.id } and a default value for undefined attributes
51
 */
52
export const parseCustomTemplate = (metadataTemplate = "", metadata = {}, getDefaultMissingProperty = (attribute) => `${trim(attribute.substring(2, attribute.length - 1))} Not Available`) => {
2!
53
    return template(getCleanTemplate(metadataTemplate || '', metadata, /\$\{.*?\}/g, 2, 1, getDefaultMissingProperty))(metadata);
11!
54
};
55

56
export const generateTemplateString = (function() {
1✔
57
    var cache = {};
1✔
58

59
    function generateTemplate(chosenTemplate, escapeFn) {
60

61
        var fn = cache[chosenTemplate];
399✔
62
        // if escapeFn is defined, no cache is used
63
        if (!fn || escapeFn) {
399✔
64
            fn = (map) => {
15✔
65

66
                let sanitized = chosenTemplate
399✔
67
                    .replace(/\$\{([\s]*[^;\s\{]+[\s]*)\}/g, (_, match) => {
68
                        const escapeFunction = escapeFn || (a => a);
56✔
69
                        return escapeFunction(match.trim().split(".").reduce((a, b) => {
56✔
70
                            return a && a[b];
107✔
71
                        }, map));
72
                    });
73

74
                return isString(sanitized) && sanitized || '';
399✔
75
            };
76
            if (!escapeFn) {
15✔
77
                cache[chosenTemplate] = fn;
14✔
78
            }
79

80

81
        }
82
        return fn;
399✔
83
    }
84
    return generateTemplate;
1✔
85
})();
86

87
export const parseTemplate = function(temp, callback) {
1✔
88
    import("@babel/standalone").then(({transform}) => {
7✔
89
        let chosenTemplate = typeof temp === "function" ? temp() : temp;
7✔
90
        try {
7✔
91
            const { code } = transform(chosenTemplate, { presets: ["env", "react"] });
7✔
92
            callback(code);
7✔
93
        } catch (e) {
UNCOV
94
            callback(null, e);
×
95
        }
96
    });
97
};
98

99
const TemplateUtils = {
1✔
100
    /**
101
     * generates a template string to use for static replacements.
102
     * It's useful for using a similar syntax for static configured strings to
103
     * use as templates.
104
     */
105
    generateTemplateString,
106
    parseTemplate,
107
    validateStringAttribute,
108
    getCleanTemplate,
109
    parseCustomTemplate
110
};
111

112
export default TemplateUtils;
113

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc