Sha256: 2ba0dc9c08dccd9ca0228cf0137843dea9b1444e6ec5895b5e3b0a3c2d4dc274

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

(function ($) {

    return $.fn.serializeObject = function () {

        var json, patterns, push_counters, _this = this;
        json = {};
        push_counters = {};
        patterns = {
            validate: /^[_a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
            key: /[a-zA-Z0-9_]+|(?=\[\])/g,
            push: /^$/,
            fixed: /^\d+$/,
            named: /^[a-zA-Z0-9_]+$/
        };

        this.build = function (base, key, value) {
            base[key] = value;
            return base;
        };

        this.push_counter = function (key) {
            if (push_counters[key] === void 0) {
                push_counters[key] = 0;
            }
            return push_counters[key]++;
        };

        $.each($(this).serializeArray(), function (i, elem) {
            var k, keys, merge, re, reverse_key;
            if (!patterns.validate.test(elem.name)) {
                return;
            }
            keys = elem.name.match(patterns.key);
            merge = elem.value;
            reverse_key = elem.name;
            while ((k = keys.pop()) !== void 0) {
                if (patterns.push.test(k)) {
                    re = new RegExp("\\[" + k + "\\]$");
                    reverse_key = reverse_key.replace(re, '');
                    merge = _this.build([], _this.push_counter(reverse_key), merge);
                } else if (patterns.fixed.test(k)) {
                    merge = _this.build([], k, merge);
                } else if (patterns.named.test(k)) {
                    merge = _this.build({}, k, merge);
                }
            }
            return json = $.extend(true, json, merge);
        });

        return json;

    };

})(jQuery);

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rademade_admin-0.0.1 app/assets/javascripts/rademade_admin/form/library/jquery.form-serialize.js