<commit_before>import _clone from 'lodash-es/clone';
import _groupBy from 'lodash-es/groupBy';
import _reduce from 'lodash-es/reduce';
import _some from 'lodash-es/some';
import _union from 'lodash-es/union';

import { dispatch as d3_dispatch } from 'd3-dispatch';

import { osmEntity } from '../osm';
import { utilRebind } from '../util/rebind';
import {
	utilQsString,
	utilStringQs
} from '../util';


export function rendererFeatures(context) {
    var traffic_roads = {
        'motorway': true,
        'motorway_link': true,
        'trunk': true,
        'trunk_link': true,
        'primary': true,
        'primary_link': true,
        'secondary': true,
        'secondary_link': true,
        'tertiary': true,
        'tertiary_link': true,
        'residential': true,
        'unclassified': true,
        'living_street': true
    };

    var service_roads = {
        'service': true,
        'road': true,
        'track': true
    };

    var paths = {
        'path': true,
        'footway': true,
        'cycleway': true,
        'bridleway': true,
        'steps': true,
        'pedestrian': true,
        'corridor': true
    };

    var past_futures = {
        'proposed': true,
        'construction': true,
        'abandoned': true,
        'dismantled': true,
        'disused': true,
        'razed': true,
        'demolished': true,
        'obliterated': true
    };

    var dispatch = d3_dispatch('change', 'redraw'),
        _cullFactor = 1,
        _cache = {},
        _features = {},
        _stats = {},
        _keys = [],
        _hidden = [];


    function update() {
        if (!window.mocha) {
            var q = utilStringQs(window.location.hash.substring(1));
            var disabled = features.disabled();
            if (disabled.length) {
                q.disable_features = disabled.join(',');
            } else {
                delete q.disable_features;
            }
            window.location.replace('#' + utilQsString(q, true));
            context.storage('disabled-features', disabled.join(','));
        }
        _hidden = features.hidden();
        dispatch.call('change');
        dispatch.call('redraw');
    }


    function defineFeature(k, filter, max) {
        var isEnabled = true;

        _keys.push(k);
        _features[k] = {
            filter: filter,
            enabled: isEnabled,   // whether the user wants it enabled..
            count: 0,
            currentMax: (max || Infinity),
            defaultMax: (max || Infinity),
            enable: function() { this.enabled = true; this.currentMax = this.defaultMax; },
            disable: function() { this.enabled = false; this.currentMax = 0; },
            hidden: function() { return !context.editable() || this.count > this.currentMax * _cullFactor; },
            autoHidden: function() { return this.hidden() && this.currentMax > 0; }
        };
    }


    defineFeature('points', function isPoint(entity, resolver, geometry) {
        return geometry === 'point';
    }, 200);

    defineFeature('traffic_roads', function isTrafficRoad(entity) {
        return traffic_roads[entity.tags.highway];
    });

    defineFeature('service_roads', function isServiceRoad(entity) {
        return service_roads[entity.tags.highway];
    });

    defineFeature('paths', function isPath(entity) {
        return paths[entity.tags.highway];
    });

    defineFeature('buildings', function isBuilding(entity) {
        return (
            !!entity.tags['building:part'] ||
            (!!entity.tags.building && entity.tags.building !== 'no') ||
            entity.tags.parking === 'multi-storey' ||
            entity.tags.parking === 'sheds' ||
            entity.tags.parking === 'carports' ||<commit_msg>Add the range filter feature<commit_after>import _clone from 'lodash-es/clone';
import _groupBy from 'lodash-es/groupBy';
import _reduce from 'lodash-es/reduce';
import _some from 'lodash-es/some';
import _union from 'lodash-es/union';

import { dispatch as d3_dispatch } from 'd3-dispatch';

import { osmEntity } from '../osm';
import { utilRebind } from '../util/rebind';
import {
	utilQsString,
	utilStringQs
} from '../util';


export function rendererFeatures(context) {
    var traffic_roads = {
        'motorway': true,
        'motorway_link': true,
        'trunk': true,
        'trunk_link': true,
        'primary': true,
        'primary_link': true,
        'secondary': true,
        'secondary_link': true,
        'tertiary': true,
        'tertiary_link': true,
        'residential': true,
        'unclassified': true,
        'living_street': true
    };

    var service_roads = {
        'service': true,
        'road': true,
        'track': true
    };

    var paths = {
        'path': true,
        'footway': true,
        'cycleway': true,
        'bridleway': true,
        'steps': true,
        'pedestrian': true,
        'corridor': true
    };

    var past_futures = {
        'proposed': true,
        'construction': true,
        'abandoned': true,
        'dismantled': true,
        'disused': true,
        'razed': true,
        'demolished': true,
        'obliterated': true
    };

    var dispatch = d3_dispatch('change', 'redraw'),
        _cullFactor = 1,
        _cache = {},
        _features = {},
        _stats = {},
        _keys = [],
        _hidden = [];


    function update() {
        if (!window.mocha) {
            var q = utilStringQs(window.location.hash.substring(1));
            var disabled = features.disabled();
            if (disabled.length) {
                q.disable_features = disabled.join(',');
            } else {
                delete q.disable_features;
            }
            window.location.replace('#' + utilQsString(q, true));
            context.storage('disabled-features', disabled.join(','));
        }
        _hidden = features.hidden();
        dispatch.call('change');
        dispatch.call('redraw');
    }


    function defineFeature(k, filter, max) {
        var isEnabled = true;

        _keys.push(k);
        _features[k] = {
            filter: filter,
            enabled: isEnabled,   // whether the user wants it enabled..
            count: 0,
            currentMax: (max || Infinity),
            defaultMax: (max || Infinity),
            enable: function() { this.enabled = true; this.currentMax = this.defaultMax; },
            disable: function() { this.enabled = false; this.currentMax = 0; },
            hidden: function() { return !context.editable() || this.count > this.currentMax * _cullFactor; },
            autoHidden: function() { return this.hidden() && this.currentMax > 0; }
        };
    }

    defineFeature('date_range', function isWithinRange(entity) {

      // Function to parse time (YYYY-MM-DD) to an INT
      var valueToInt = function(value) {
        return (typeof value === 'string') ?
          parseInt(value.replace(/-/g, ''), 10) :
          0;
      }

      // Convert the date range from the entity to a number
      entity.tags = entity.tags || {};
      var entityRange = {
        'start_date': valueToInt(entity.tags['start_date']),
        'end_date': valueToInt(entity.tags['end_date'])
      };

      // These will be pulled from the on-screen controls
      // Maybe put them in browser storage?
      var selectedRange = {
        'start_date': -Infinity, //valueToInt(entity['start_date']),
        'end_date': Infinity // valueToInt(entity['end_date'])
      }
      if (context.features().dateRange) {
        selectedRange.start_date = context.features().dateRange[0];
        selectedRange.end_date = context.features().dateRange[1];
      }

      // console.log('v---------------------------------------------v');
      // console.log('selectedRange', selectedRange);
      // console.log('entityRange', entityRange);
      // console.log('Return', !((selectedRange['start_date'] <= entityRange['end_date']) && (entityRange['start_date'] <= selectedRange['end_date'])));
      // console.log('^---------------------------------------------^');

      // Within range if:
      //    the entity started before the selected range started 
      //    and also ended after the selected range started
      return !((selectedRange['start_date'] <= entityRange['end_date']) &&
        (entityRange['start_date'] <= selectedRange['end_date']));

    });

    defineFeature('points', function isPoint(entity, resolver, geometry) {
        return geometry === 'point';
    }, 200);

    defineFeature('traffic_roads', function isTrafficRoad(entity) {
        return traffic_roads[entity.tags.highway];
    });

    defineFeature('service_roads', function isServiceRoad(entity) {
        return service_roads[entity.tags.highway];
    });

    defineFeature('paths', function isPath(entity) {
        return paths[entity.tags.highway];
    });

    defineFeature('buildings', function isBuilding(entity) {
        return (
            !!entity.tags['building:part'] ||
            (!!entity.tags.building && entity.tags.building !== 'no') ||
            entity.tags.parking === 'multi-storey' ||
            entity.tags.parking === 'sheds' ||
            entity.tags.parking === 'carports' ||
{'fraction_non_alphanumeric': 0.15338688666956143, 'fraction_numerical': 0.002930959617889709, 'mean_word_length': 2.135806671204901, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7459432', 'n_tokens_mistral': 2706, 'n_tokens_neox': 2609, 'n_words': 584}