<commit_before>$(function() {
    function setAddress($container) {
        clearTimeout(window._address_timeout);

        window._address_timeout = setTimeout(function(){
            var country = MapasCulturais.pais ? MapasCulturais.pais : 'BR';
            var state = $container.find('.js-rfc-input-En_Estado').val();
            var city = $container.find('.js-rfc-input-En_Municipio').val();
            var street = $container.find('.js-rfc-input-En_Nome_Logradouro').val();
            var neighborhood = $container.find('.js-rfc-input-En_Bairro').val();
            var number = $container.find('.js-rfc-input-En_Num').val();

            if (state && city && street) {
                var address = neighborhood ?
                    street + ' ' + number + ', ' + neighborhood + ', ' + city + ', ' + state + ', ' + country :
                    street + ' ' + number + ', ' + city + ', ' + state + ', ' + country;

                var $scope = $container.scope();
                var topAddr = $scope.entity[$scope.field.fieldName];
                topAddr.endereco = address;
                $scope.saveField($scope.field, topAddr);
            }
        },500);
    }

    function changeCEP($cep, $container, timeout) {
        clearTimeout(window._cep_timeout);

        window._cep_timeout = setTimeout(function() {
            if (/^\d{5}-\d{3}$/.exec($cep.val())) {
                var oldStreet = $container.find('.js-rfc-input-En_Nome_Logradouro').val();
                var oldNeighbourhood = $container.find('.js-rfc-input-En_Bairro').val();
                var oldState = $container.find('.js-rfc-input-En_Estado').val();
                var oldCity = $container.find('.js-rfc-input-En_Municipio').val();
                $container.find('.js-rfc-input-En_Nome_Logradouro').val('').attr('placeholder', 'carregando...');
                $container.find('.js-rfc-input-En_Bairro').val('').attr('placeholder', 'carregando...');
                $container.find('.js-rfc-input-En_Estado').val('').attr('placeholder', 'carregando...');
                $container.find('.js-rfc-input-En_Municipio').val('').attr('placeholder', 'carregando...');

                $.getJSON('/site/address_by_postalcode?postalcode='+$cep.val())
                .success(function(r) {
                    $container.find('.js-rfc-input-En_Nome_Logradouro').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Bairro').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Estado').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Municipio').attr('placeholder', '');
                    if (r.success) {
                        $container.find('.js-rfc-input-_lat').val(r.lat).trigger('change');
                        $container.find('.js-rfc-input-_lon').val(r.lon).trigger('change');

                        $container.find('.js-rfc-input-En_Nome_Logradouro').val(r.streetName).trigger('change');
                        $container.find('.js-rfc-input-En_Bairro').val(r.neighborhood).trigger('change');
                        $container.find('.js-rfc-input-En_Estado').val(r.state.sigla).trigger('change');

                        $container.find('.js-rfc-input-En_Municipio').val(r.city.nome).trigger('change');

                        setAddress($container);
                    }
                })
                .error(function() {
                    $container.find('.js-rfc-input-En_Nome_Logradouro').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Bairro').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Estado').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Municipio').attr('placeholder', '');
                    $container.find('.js-rfc-input-En_Nome_Logradouro').val(oldStreet);
                    $container.find('.js-rfc-input-En_Bairro').val(oldNeighbourhood);
                    $container.find('.js-rfc-input-En_Estado').val(oldState);
                    $container.find('.js-rfc-input-En_Municipio').val(oldCity);
                });
            }
        },timeout);

        setAddress($container);
    }

    $('body').on('change', 'input.js-rfc-input-En_CEP', function() {
        var $cep = $(this);
        var $container = $cep.parents('.js-rfc-location');

        changeCEP($cep, $container, 10);<commit_msg>realiza melhorias na busca por CEP<commit_after>$(function() {
    function setAddress($container) {
        clearTimeout(window._address_timeout);

        window._address_timeout = setTimeout(function(){
            var country = MapasCulturais.pais ? MapasCulturais.pais : 'BR';
            var state = $container.find('.js-rfc-input-En_Estado').val();
            var city = $container.find('.js-rfc-input-En_Municipio').val();
            var street = $container.find('.js-rfc-input-En_Nome_Logradouro').val();
            var neighborhood = $container.find('.js-rfc-input-En_Bairro').val();
            var number = $container.find('.js-rfc-input-En_Num').val();

            if (state && city && street) {
                var address = neighborhood ?
                    street + ' ' + number + ', ' + neighborhood + ', ' + city + ', ' + state + ', ' + country :
                    street + ' ' + number + ', ' + city + ', ' + state + ', ' + country;

                var $scope = $container.scope();
                var topAddr = $scope.entity[$scope.field.fieldName];
                topAddr.endereco = address;
                $scope.saveField($scope.field, topAddr);
            }
        },500);
    }

    function changeCEP($cep, $container, timeout)
    {
        clearTimeout(window._cep_timeout);
        window._cep_timeout = setTimeout(function() {
            if (/^\d{5}-\d{3}$/.exec($cep.val())) {
                const $street = $container.find(".js-rfc-input-En_Nome_Logradouro");
                const $nbhood = $container.find(".js-rfc-input-En_Bairro");
                const $state = $container.find(".js-rfc-input-En_Estado");
                const $city = $container.find(".js-rfc-input-En_Municipio");
                const oldStreet = $street.val();
                const oldNeighbourhood = $nbhood.val();
                const oldState = $state.val();
                const oldCity = $city.val();
                const msgLoading = coalesce(MapasCulturais.gettext.locationPatch.loading, "carregando...");
                $([$street, $nbhood, $state, $city]).val("").attr("placeholder", msgLoading);
                $.getJSON("/site/address_by_postalcode?postalcode=" + $cep.val())
                .done(function(r) {
                    if (r.success) {
                        $container.find(".js-rfc-input-_lat").val(r.lat).trigger("change");
                        $container.find(".js-rfc-input-_lon").val(r.lon).trigger("change");
                        $street.val(r.streetName).trigger("change");
                        $nbhood.val(r.neighborhood).trigger("change");
                        $state.val(r.state.sigla).trigger("change");
                        $city.val(r.city.nome).trigger("change");
                        setAddress($container);
                    } else {
                        $street.val(oldStreet);
                        $nbhood.val(oldNeighbourhood);
                        $state.val(oldState);
                        $city.val(oldCity);
                    }
                    return;
                }).fail(function() {
                    $street.val(oldStreet);
                    $nbhood.val(oldNeighbourhood);
                    $state.val(oldState);
                    $city.val(oldCity);
                    return;
                }).always(function() {
                    $([$street, $nbhood, $state, $city]).attr("placeholder", "");
                    return;
                });
            }
            return;
        }, timeout);
        setAddress($container);
        return;
    }

    $('body').on('change', 'input.js-rfc-input-En_CEP', function() {
        var $cep = $(this);
        var $container = $cep.parents('.js-rfc-location');

        changeCEP($cep, $container, 10);
{'fraction_non_alphanumeric': 0.1730019493177388, 'fraction_numerical': 0.001705653021442495, 'mean_word_length': 1.7988407773610637, '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': '2375008', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2332, 'n_words': 269}