<commit_before>'use strict';

var postcss = require('postcss');
var symbolizers = {
    POLYGON: 'polygon',
    LINE: 'line',
    MARKER: 'marker',
    SHIELD: 'shield',
    TEXT: 'text',
    BUILDING: 'building'
};

module.exports = function (cartoCss) {
    return postcss()
        .use(postcssCartocssMigrator())
        .process(cartoCss)
        .css;
};

var postcssCartocssMigrator = postcss.plugin('postcss-cartocss-migrator', function (options) {
    options = options || {};
    return function (css) {
        css.walkRules(function (rule) {
            setPropertyToDefault(rule, symbolizers.POLYGON, 'clip', true);
            setPropertyToDefault(rule, symbolizers.POLYGON, 'pattern-aligment', 'local');

            setPropertyToDefault(rule, symbolizers.LINE, 'clip', true);

            setPropertyToDefault(rule, symbolizers.MARKER, 'clip', true);

            setPropertyToDefault(rule, symbolizers.SHIELD, 'clip', true);

            setPropertyToDefault(rule, symbolizers.TEXT, 'clip', true);
            setPropertyToDefault(rule, symbolizers.TEXT, 'label-position-tolerance', 0);

            setPropertyToDefault(rule, symbolizers.BUILDING, 'fill', 'white');
        });
    };
});

function setPropertyToDefault(rule, symbolizer, propSuffix, defaultValue) {
    var property = [symbolizer, propSuffix].join('-');

    if (hasDeclWithSymbolyzer(rule, symbolizer) && !hasPropertyDefined(rule, property)) {
        var patternAlignmentDecl = postcss.decl({ prop: property, value: defaultValue });
        rule.append(patternAlignmentDecl);
    }
}

function hasPropertyDefined(rule, property) {
    var hasPropertyDefined = false;

    rule.walkDecls(function (decl) {
        if (decl.prop === property) {
            hasPropertyDefined = true;
        }
    });

    return hasPropertyDefined;
}

function hasDeclWithSymbolyzer(rule, symbolyzer) {
    var hasPropertySymbolyzer = false;

    rule.walkDecls(function (decl) {
        var property = decl.prop;

        if (property.indexOf(symbolyzer) === 0) {
            hasPropertySymbolyzer = true;
        }
    });<commit_msg>Use dictionary to apply default values to cartocss properties<commit_after>'use strict';

var postcss = require('postcss');
var propertyDefaultValues = [{
    symbolizer: 'polygon',
    defaults: [{
        property: 'polygon-clip',
        value: true
    }, {
        property: 'polygon-pattern-aligment',
        value: 'local'
    }]
}, {
    symbolizer: 'line',
    defaults: [{
        property: 'line-clip',
        value: true
    }]
}, {
    symbolizer: 'marker',
    defaults: [{
        property: 'marker-clip',
        value: true
    }]
}, {
    symbolizer: 'shield',
    defaults: [{
        property: 'shield-clip',
        value: true
    }]
}, {
    symbolizer: 'text',
    defaults: [{
        property: 'text-clip',
        value: true
    }, {
        property: 'text-label-position-tolerance',
        value: 0
    }]
}, {
    symbolizer: 'building',
    defaults: [{
        property: 'building-fill',
        value: 'white'
    }]
}];

module.exports = function (cartoCss) {
    return postcss()
        .use(postcssCartocssMigrator())
        .process(cartoCss)
        .css;
};

var postcssCartocssMigrator = postcss.plugin('postcss-cartocss-migrator', function (options) {
    options = options || {};
    return function (css) {
        css.walkRules(function (rule) {
            propertyDefaultValues.forEach(function (property) {
                property.defaults.forEach(function (propertyDefault) {
                    setPropertyToDefault(rule, property.symbolizer, propertyDefault.property, propertyDefault.value);
                });
            });
        });
    };
});

function setPropertyToDefault(rule, symbolizer, property, defaultValue) {
    if (hasDeclWithSymbolyzer(rule, symbolizer) && !hasPropertyDefined(rule, property)) {
        var patternAlignmentDecl = postcss.decl({ prop: property, value: defaultValue });
        rule.append(patternAlignmentDecl);
    }
}

function hasPropertyDefined(rule, property) {
    var hasPropertyDefined = false;

    rule.walkDecls(function (decl) {
        if (decl.prop === property) {
            hasPropertyDefined = true;
        }
    });

    return hasPropertyDefined;
}

function hasDeclWithSymbolyzer(rule, symbolyzer) {
    var hasPropertySymbolyzer = false;

    rule.walkDecls(function (decl) {
        var property = decl.prop;

        if (property.indexOf(symbolyzer) === 0) {
            hasPropertySymbolyzer = true;
        }
    });
{'fraction_non_alphanumeric': 0.12876953554919657, 'fraction_numerical': 0.0008804754567466431, 'mean_word_length': 2.7307060755336616, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804113', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1330, 'n_words': 266}