<commit_before>/*
    Copyright 2012 UW Information Technology, University of Washington

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

    Changes
    =================================================================

    <EMAIL>: removed resetFilters() since it's the same
      as clear_filter() in search.js.
    <EMAIL>: fix obvious JSHint bugs.
    <EMAIL>: formatting fixes plus hide non-global
      variables/functions.
*/

var requests = [];

// H = Handlebars, $ = jQuery, GM = google.maps
(function (H, $, GM) {
    H.registerHelper('carouselimages', function(spacedata) {
        var space_id = spacedata.id;
        var elements = [];

        var image_url, div_string;
        if (spacedata.images.length > 0) {
            for (var i=0; i < spacedata.images.length; i++) {
                var image_id = spacedata.images[i].id;
                image_url = "background:url(/space/" + space_id + "/image/" + image_id + "/thumb/constrain/width:500)";
                div_string = "<div class='carousel-inner-image item'><div class='carousel-inner-image-inner' style='" + image_url + "'>&nbsp;</div></div>";
                elements.push(div_string);
            }
        } else {
            image_url = "background:url(/static/img/placeholder_noImage_bw.png)";
            div_string = "<div class='carousel-inner-image item'><div class='carousel-inner-image-inner' style='" + image_url + "; background-size: 500px'>&nbsp;</div></div>";
            elements.push(div_string);
        }
        return new H.SafeString(elements.join('\n'));
    });

    H.registerHelper('compare', function(lvalue, rvalue, options) {
        if (arguments.length < 3)
            throw new Error("Handlerbars Helper 'compare' needs 2 parameters");

        var operator = options.hash.operator || "==";

        var operators = {
            '==':       function(l,r) { return l == r; },
            '===':      function(l,r) { return l === r; },
            '!=':       function(l,r) { return l != r; },
            '<':        function(l,r) { return l < r; },
            '>':        function(l,r) { return l > r; },
            '<=':       function(l,r) { return l <= r; },
            '>=':       function(l,r) { return l >= r; },
            'typeof':   function(l,r) { return typeof l == r; }
        };

        if (!operators[operator])
            throw new Error("Handlerbars Helper 'compare' doesn't know the operator "+operator);

        var result = operators[operator](lvalue,rvalue);

        if (result) {
            return options.fn(this);
        } else {
            return options.inverse(this);
        }
    });

    H.registerHelper('addition', function(lvalue, rvalue) {
        if (arguments.length < 2)
            throw new Error("Handlerbars Helper 'compare' needs 2 parameters");

        return new H.SafeString(Number(lvalue) + Number(rvalue));
    });

    H.registerHelper('formatHours', function(hours) {
        //tomorrow_starts_at_midnight = true;
        //tomorrow_is_24_hours =
        //if (start_time[0] == 0 && start_time[1] == 0 && end_time[0] == 23 && end_time[1] == 59 && tomorrow_starts_at_midnight && !tomorrow_is_24_hours && tomorrows_hour > 3) {
            //dsf
        //}
        var formatted = {};
        $.each(hours, function(day) {
            if (hours[day].length > 0) {
                $.each(hours[day], function() {
                    this[0] = this[0].replace(/^0+/, '');
                    this[1] = this[1].replace(/^0+/, '');
                });
                var dayMarker = day.charAt(0).toUpperCase();
                // Show two characters for Th, Sa, Su
                if (dayMarker == 'T' && day.charAt(1) == 'h' || dayMarker == 'S' && day.charAt(1) == 'a' || dayMarker == 'S' && day.charAt(1) == 'u') {
                    dayMarker += day.charAt(1);
                }

                formatted[dayMarker] = to12Hour(hours[day]).join(", ");
            }
        });
        formatted = _sortDays(formatted);
        formatted = _groupHours(formatted);
        return new H.SafeString(formatted.join("<br/>"));
    });

    H.registerHelper('alphaOptGroupsHTML', function(list) {
        list.sort();
        var firstletter = null;
        var out = [];
        for (var i=0; i < list.length; i++) {
            if (list[i][0] == firstletter) {
                out.push('<option value="'+list[i]+'">'+list[i]+'</option>');
            } else {
                // select multiple with optgroups will crash mobile Safari
                if (firstletter !== null || !isMobile) {
                    out.push('</optgroup>');
                }
                firstletter = list[i][0];
                if (!isMobile) {
                    out.push('<optgroup label="'+firstletter+'">');
                }
                out.push('<option value="'+list[i]+'">'+list[i]+'</option>');
            }
        }
        if (!isMobile) {
            out.push('</optgroup>');
        }
        return new H.SafeString(out.join(''));
    });

    function _groupHours(days) {
        var hours_info = [];

        var day2IntMap = {
            M: 0,
            T: 1,
            W: 2,
            Th: 3,
            F: 4,
            Sa: 5,
            Su: 6
        };

        // Break the sorted days array into its hour ranges by
        // day.
        for (var i = 0; i < days.length; i++) {
            var split = days[i].split(": ");

            var day = split[0];
            var day_hours = split[1].split(", ");

            for (var j = 0; j < day_hours.length; j++) {
                var hour_range = day_hours[j];

                hours_info.push({
                    day: day,
                    day_idx: day2IntMap[day],
                    hour_range: hour_range
                });
            }
        }

        // Do collesing for consecutive midnight ranges
        for (var i = 0; i < hours_info.length; i++) {
            var info = hours_info[i];
            if (!info) {
                // Can happen if we've collesed this range already
                continue;
            }

            var range = info.hour_range.split(" - ");

            if (range[1] == "Midnight") {
                // Look for a hour range in the next day that begins at midnight.
                // If we find one, then collese it into this day and remove it.
                for (var j = i + 1; (j - i) < hours_info.length; j++) {
                    var next_info = hours_info[j % hours_info.length];
                    if (!next_info) {
                        continue;
                    }

                    var next_range = next_info.hour_range.split(" - ");
                    var day_diff = (next_info.day_idx - info.day_idx) % 7;

                    if (day_diff > 1) {
                        // Search beyond the next day
                        break;
                    } else if (day_diff == 1 && next_range[0] == "Midnight") {
                        // Found a day in the next range that begins as midnight.
                        range[1] = next_range[1];
                        hours_info[j % hours_info.length] = null;
                        break;
                    }
                }
                info.hour_range = range.join(" - ");
            }
        }

        // Do collesing for same ranges
        for (var i = 0; i < hours_info.length; i++) {
            var info = hours_info[i];<commit_msg>Fix bug with collesing Su to M introduced in rewrite<commit_after>/*
    Copyright 2012 UW Information Technology, University of Washington

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

    Changes
    =================================================================

    <EMAIL>: removed resetFilters() since it's the same
      as clear_filter() in search.js.
    <EMAIL>: fix obvious JSHint bugs.
    <EMAIL>: formatting fixes plus hide non-global
      variables/functions.
*/

var requests = [];

// H = Handlebars, $ = jQuery, GM = google.maps
(function (H, $, GM) {
    H.registerHelper('carouselimages', function(spacedata) {
        var space_id = spacedata.id;
        var elements = [];

        var image_url, div_string;
        if (spacedata.images.length > 0) {
            for (var i=0; i < spacedata.images.length; i++) {
                var image_id = spacedata.images[i].id;
                image_url = "background:url(/space/" + space_id + "/image/" + image_id + "/thumb/constrain/width:500)";
                div_string = "<div class='carousel-inner-image item'><div class='carousel-inner-image-inner' style='" + image_url + "'>&nbsp;</div></div>";
                elements.push(div_string);
            }
        } else {
            image_url = "background:url(/static/img/placeholder_noImage_bw.png)";
            div_string = "<div class='carousel-inner-image item'><div class='carousel-inner-image-inner' style='" + image_url + "; background-size: 500px'>&nbsp;</div></div>";
            elements.push(div_string);
        }
        return new H.SafeString(elements.join('\n'));
    });

    H.registerHelper('compare', function(lvalue, rvalue, options) {
        if (arguments.length < 3)
            throw new Error("Handlerbars Helper 'compare' needs 2 parameters");

        var operator = options.hash.operator || "==";

        var operators = {
            '==':       function(l,r) { return l == r; },
            '===':      function(l,r) { return l === r; },
            '!=':       function(l,r) { return l != r; },
            '<':        function(l,r) { return l < r; },
            '>':        function(l,r) { return l > r; },
            '<=':       function(l,r) { return l <= r; },
            '>=':       function(l,r) { return l >= r; },
            'typeof':   function(l,r) { return typeof l == r; }
        };

        if (!operators[operator])
            throw new Error("Handlerbars Helper 'compare' doesn't know the operator "+operator);

        var result = operators[operator](lvalue,rvalue);

        if (result) {
            return options.fn(this);
        } else {
            return options.inverse(this);
        }
    });

    H.registerHelper('addition', function(lvalue, rvalue) {
        if (arguments.length < 2)
            throw new Error("Handlerbars Helper 'compare' needs 2 parameters");

        return new H.SafeString(Number(lvalue) + Number(rvalue));
    });

    H.registerHelper('formatHours', function(hours) {
        //tomorrow_starts_at_midnight = true;
        //tomorrow_is_24_hours =
        //if (start_time[0] == 0 && start_time[1] == 0 && end_time[0] == 23 && end_time[1] == 59 && tomorrow_starts_at_midnight && !tomorrow_is_24_hours && tomorrows_hour > 3) {
            //dsf
        //}
        var formatted = {};
        $.each(hours, function(day) {
            if (hours[day].length > 0) {
                $.each(hours[day], function() {
                    this[0] = this[0].replace(/^0+/, '');
                    this[1] = this[1].replace(/^0+/, '');
                });
                var dayMarker = day.charAt(0).toUpperCase();
                // Show two characters for Th, Sa, Su
                if (dayMarker == 'T' && day.charAt(1) == 'h' || dayMarker == 'S' && day.charAt(1) == 'a' || dayMarker == 'S' && day.charAt(1) == 'u') {
                    dayMarker += day.charAt(1);
                }

                formatted[dayMarker] = to12Hour(hours[day]).join(", ");
            }
        });
        formatted = _sortDays(formatted);
        formatted = _groupHours(formatted);
        return new H.SafeString(formatted.join("<br/>"));
    });

    H.registerHelper('alphaOptGroupsHTML', function(list) {
        list.sort();
        var firstletter = null;
        var out = [];
        for (var i=0; i < list.length; i++) {
            if (list[i][0] == firstletter) {
                out.push('<option value="'+list[i]+'">'+list[i]+'</option>');
            } else {
                // select multiple with optgroups will crash mobile Safari
                if (firstletter !== null || !isMobile) {
                    out.push('</optgroup>');
                }
                firstletter = list[i][0];
                if (!isMobile) {
                    out.push('<optgroup label="'+firstletter+'">');
                }
                out.push('<option value="'+list[i]+'">'+list[i]+'</option>');
            }
        }
        if (!isMobile) {
            out.push('</optgroup>');
        }
        return new H.SafeString(out.join(''));
    });

    function _groupHours(days) {
        var hours_info = [];

        var day2IntMap = {
            M: 0,
            T: 1,
            W: 2,
            Th: 3,
            F: 4,
            Sa: 5,
            Su: 6
        };

        // Break the sorted days array into its hour ranges by
        // day.
        for (var i = 0; i < days.length; i++) {
            var split = days[i].split(": ");

            var day = split[0];
            var day_hours = split[1].split(", ");

            for (var j = 0; j < day_hours.length; j++) {
                var hour_range = day_hours[j];

                hours_info.push({
                    day: day,
                    day_idx: day2IntMap[day],
                    hour_range: hour_range
                });
            }
        }

        // Do collesing for consecutive midnight ranges
        for (var i = 0; i < hours_info.length; i++) {
            var info = hours_info[i];
            if (!info) {
                // Can happen if we've collesed this range already
                continue;
            }

            var range = info.hour_range.split(" - ");

            if (range[1] == "Midnight") {
                // Look for a hour range in the next day that begins at midnight.
                // If we find one, then collese it into this day and remove it.
                for (var j = i + 1; (j - i) < hours_info.length; j++) {
                    var next_info = hours_info[j % hours_info.length];
                    if (!next_info) {
                        continue;
                    }
                    if (next_info.day_idx == info.day_idx) {
                        continue;
                    }

                    var next_range = next_info.hour_range.split(" - ");
                    var is_next_day = (info.day_idx + 1) % 7 == next_info.day_idx;

                    if (!is_next_day) {
                        // Search beyond the next day
                        break;
                    } else if (next_range[0] == "Midnight") {
                        // Found a day in the next range that begins as midnight.
                        range[1] = next_range[1];
                        hours_info[j % hours_info.length] = null;
                        break;
                    }
                }
                info.hour_range = range.join(" - ");
            }
        }

        // Do collesing for same ranges
        for (var i = 0; i < hours_info.length; i++) {
            var info = hours_info[i];
{'fraction_non_alphanumeric': 0.1451582693388275, 'fraction_numerical': 0.00930261597053131, 'mean_word_length': 1.6070963541666667, 'pattern_counts': {'":': 2, '<': 67, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '876588', 'n_tokens_mistral': 4715, 'n_tokens_neox': 4421, 'n_words': 1164}