<commit_before>"use strict";

var m        = require("mithril"),
    paginate = require("paginationator"),
    moment   = require("moment"),
    fuzzy    = require("fuzzysearch"),
    debounce = require("lodash.debounce"),
    slug     = require("sluggo"),
    
    db = require("../../lib/firebase"),
    
    css = require("./listings.css"),

    size = 10;

module.exports = {
    controller : function(options) {
        var ctrl = this;

        ctrl.schema = options.schema;

        ctrl.page = 0;
        
        ctrl.entries = null;
        ctrl.content = null;
        ctrl.results = null;
        
        ctrl.sorting = {
            field : "updated",
            desc  : true
        };

        ctrl.fetch = function() {
            db.child("content/" + ctrl.schema.key).orderByChild(ctrl.sorting.field).on("value", function(snap) {
                var entries = [];

                snap.forEach(function(record) {
                    var data = record.val();

                    data.key = record.key();
                    data._updated = data.updated;
                    data.created = moment.utc(data.created);
                    data.updated = moment.utc(data.updated);
                    data.search  = slug(data.name, { separator : "" });

                    entries.push(data);
                });

                ctrl.entries = entries;
                
                if(ctrl.sorting.desc) {
                    ctrl.entries.reverse();
                }
                
                ctrl._paginate();
                
                m.redraw();
            });
        };
        
        ctrl._paginate = function() {
            ctrl.content = paginate(ctrl.entries, { limit : size });
        };
        
        // Go get initial data
        ctrl.fetch();

        ctrl.change = function(page, e) {
            e.preventDefault();

            ctrl.page = page;
        };

        // m.redraw calls are necessary due to debouncing, this function
        // may not be executing during a planned redraw cycle
        ctrl.filter = debounce(function(input) {
            if(input.length < 2) {
                ctrl.results = false;

                return m.redraw();
            }

            input = slug(input);

            ctrl.results = ctrl.entries.filter(function(content) {
                return fuzzy(input, content.search);
            }).slice(0, size);

            m.redraw();
        }, 100);
        
        ctrl.sort = function(field) {<commit_msg>Don't paginate if nothing to process<commit_after>"use strict";

var m        = require("mithril"),
    paginate = require("paginationator"),
    moment   = require("moment"),
    fuzzy    = require("fuzzysearch"),
    debounce = require("lodash.debounce"),
    slug     = require("sluggo"),
    
    db = require("../../lib/firebase"),
    
    css = require("./listings.css"),

    size = 10;

module.exports = {
    controller : function(options) {
        var ctrl = this;

        ctrl.schema = options.schema;

        ctrl.page = 0;
        
        ctrl.entries = null;
        ctrl.content = null;
        ctrl.results = null;
        
        ctrl.sorting = {
            field : "updated",
            desc  : true
        };

        ctrl.fetch = function() {
            db.child("content/" + ctrl.schema.key).orderByChild(ctrl.sorting.field).on("value", function(snap) {
                var entries = [];

                snap.forEach(function(record) {
                    var data = record.val();

                    data.key = record.key();
                    data._updated = data.updated;
                    data.created = moment.utc(data.created);
                    data.updated = moment.utc(data.updated);
                    data.search  = slug(data.name, { separator : "" });

                    entries.push(data);
                });

                ctrl.entries = entries;
                
                if(ctrl.sorting.desc) {
                    ctrl.entries.reverse();
                }
                
                ctrl._paginate();
                
                m.redraw();
            });
        };
        
        ctrl._paginate = function() {
            if(!ctrl.entries.length) {
                return;
            }

            ctrl.content = paginate(ctrl.entries, { limit : size });
        };
        
        // Go get initial data
        ctrl.fetch();

        ctrl.change = function(page, e) {
            e.preventDefault();

            ctrl.page = page;
        };

        // m.redraw calls are necessary due to debouncing, this function
        // may not be executing during a planned redraw cycle
        ctrl.filter = debounce(function(input) {
            if(input.length < 2) {
                ctrl.results = false;

                return m.redraw();
            }

            input = slug(input);

            ctrl.results = ctrl.entries.filter(function(content) {
                return fuzzy(input, content.search);
            }).slice(0, size);

            m.redraw();
        }, 100);
        
        ctrl.sort = function(field) {
{'fraction_non_alphanumeric': 0.12390243902439024, 'fraction_numerical': 0.0031219512195121953, 'mean_word_length': 1.2531868131868131, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3841511', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1295, 'n_words': 261}