« Mustache » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Ligne 3 : Ligne 3 :
* [https://mustache.github.io/#demo Demo]
* [https://mustache.github.io/#demo Demo]
* [https://github.com/janl/mustache.js/ Doc]
* [https://github.com/janl/mustache.js/ Doc]
{{warn | Doesn't handle {{boxx|.}} in property name [https://github.com/janl/mustache.js/issues/343 ]}}
 
= [https://github.com/janl/mustache.js/issues/343 Doesn't handle dots in property name] =
<kode lang='js'>
// a solution could be to replace the dots in property names (it uses lodash)
function replaceDotWithUnderscore(obj) {
    _.forOwn(obj, (value, key) => {
        if (_.includes(key, '.')) {
            const cleanKey = _.replace(key, /\./g, '_');
            obj[cleanKey] = value;
            delete obj[key];
        }
 
        // recursive call if the value is an object or an array
        if (_.isObject(value)) {
            return replaceDotWithUnderscore(value);
        }
    });
    return obj;
}
</kode>

Version du 28 janvier 2021 à 17:21

Links

Doesn't handle dots in property name

Js.svg
// a solution could be to replace the dots in property names (it uses lodash)
function replaceDotWithUnderscore(obj) {
    _.forOwn(obj, (value, key) => {
        if (_.includes(key, '.')) {
            const cleanKey = _.replace(key, /\./g, '_');
            obj[cleanKey] = value;
            delete obj[key];
        }

        // recursive call if the value is an object or an array
        if (_.isObject(value)) {
            return replaceDotWithUnderscore(value);
        }
    });
    return obj;
}