<commit_before>var config = require('./config');
var api = require('./api');
var i18n = require('i18next-client');

var forms = module.exports = {
    collect: function($form) {
        var data = {};

        $form.find('[name]').each(function() {
            var $field = $(this);
            var value = $field.val();
            var filled = value.length > 0;
            var name = $field.attr('name');
            var ml = typeof $field.data('i18n-field') !== "undefined";

            if (filled && name.indexOf('.') > 0) {
                var parts = name.split('.');
                var target = data;
                var tval, fromarray;
                while (parts.length) {
                    if (parts.length > 1) {
                        tval = target[parts[0]];
                        if (typeof tval === 'undefined') {
                            tval = [];

                        } else if ($.isArray(tval) && !$.isNumeric(parts[1])) {
                            fromarray = tval;
                            tval = {};
                            for (var i=0; i<fromarray.length; i++) {
                                tval[i.toString()] = fromarray[i];
                            }
                        }

                        tval[parts[1]] = value;
                        target[parts[0]] = tval;
                    }
                    parts.splice(0, 1);
                    target = tval;
                }
            }

            if (ml) {
                var cur_lang = config.get('forever.language');
                name = name + '_' + cur_lang;
            }

            if (name.indexOf('.') < 0) {
                data[name] = value;
            }
        });

        return data;
    },

    getField: function($form, name) {
        var $field = $form.find('[name='+name+']');
        return $field;
    },
    getFieldLabel: function($form, name) {
        var $field = $form.find('[name='+name+']');
        var id = $field.attr('id');
        return $form.find('label[for='+ id +']');
    },

    initial: function($form, model) {
        var self = this;
        var data = model.data();
        $.each(data, function(name, value) {
            self.getField($form, name).val(value);
        })
    },

    populateDropdown: function($form, name, collection) {
        var $field = this.getField($form, name);
        var empty_label_key = $field.attr('data-empty-label-key');
        var empty_label = i18n.t(empty_label_key);

        function resetOptions() {
            var value = $field.val();
            var data = collection.data();
            var empty_option = $('<option/>');
            empty_option.text(empty_label);

            $field.html("");
            $field.append(empty_option)

            var $option;
            for (var i=0; i < data.length; i++) {
                $option = $('<option/>');
                $option.attr('value', data[i].url);
                $option.text(data[i].name);
                $field.append($option);<commit_msg>Sort dropdown values before populating them<commit_after>var config = require('./config');
var api = require('./api');
var i18n = require('i18next-client');

var forms = module.exports = {
    collect: function($form) {
        var data = {};

        $form.find('[name]').each(function() {
            var $field = $(this);
            var value = $field.val();
            var filled = value.length > 0;
            var name = $field.attr('name');
            var ml = typeof $field.data('i18n-field') !== "undefined";

            if (filled && name.indexOf('.') > 0) {
                var parts = name.split('.');
                var target = data;
                var tval, fromarray;
                while (parts.length) {
                    if (parts.length > 1) {
                        tval = target[parts[0]];
                        if (typeof tval === 'undefined') {
                            tval = [];

                        } else if ($.isArray(tval) && !$.isNumeric(parts[1])) {
                            fromarray = tval;
                            tval = {};
                            for (var i=0; i<fromarray.length; i++) {
                                tval[i.toString()] = fromarray[i];
                            }
                        }

                        tval[parts[1]] = value;
                        target[parts[0]] = tval;
                    }
                    parts.splice(0, 1);
                    target = tval;
                }
            }

            if (ml) {
                var cur_lang = config.get('forever.language');
                name = name + '_' + cur_lang;
            }

            if (name.indexOf('.') < 0) {
                data[name] = value;
            }
        });

        return data;
    },

    getField: function($form, name) {
        var $field = $form.find('[name='+name+']');
        return $field;
    },
    getFieldLabel: function($form, name) {
        var $field = $form.find('[name='+name+']');
        var id = $field.attr('id');
        return $form.find('label[for='+ id +']');
    },

    initial: function($form, model) {
        var self = this;
        var data = model.data();
        $.each(data, function(name, value) {
            self.getField($form, name).val(value);
        })
    },

    populateDropdown: function($form, name, collection) {
        var $field = this.getField($form, name);
        var empty_label_key = $field.attr('data-empty-label-key');
        var empty_label = i18n.t(empty_label_key);

        function resetOptions() {
            var value = $field.val();
            var data = collection.data();
            var empty_option = $('<option/>');
            empty_option.text(empty_label);

            data.sort(function(a, b){
                var prop = "name";
                return ((a[prop] < b[prop]) ? -1 : ((a[prop] > b[prop]) ? 1 : 0));
            })

            $field.html("");
            $field.append(empty_option)

            var $option;
            for (var i=0; i < data.length; i++) {
                $option = $('<option/>');
                $option.attr('value', data[i].url);
                $option.text(data[i].name);
                $field.append($option);
{'fraction_non_alphanumeric': 0.15591054313099043, 'fraction_numerical': 0.006869009584664537, 'mean_word_length': 1.2883771929824561, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '184508', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1709, 'n_words': 363}