<commit_before>'use strict';

import * as Rx from 'rx';
import * as wx from 'webrx';

import BaseRoutableViewModel from '../../React/BaseRoutableViewModel';

export interface ISearchRoutingState {
  filter: string;
}

export class SearchViewModel extends BaseRoutableViewModel<ISearchRoutingState> {
  public static displayName = 'SearchViewModel';

  constructor(public isLiveSearchEnabled = false, private liveSearchTimeout = 250, private isCaseInsensitive = true, isRoutingEnabled = false) {
    super(isRoutingEnabled);
  }

  public filter = wx.property('');
  public regex = this.filter.changed
    .select(x => this.createRegex(x))
    .distinctUntilChanged()
    .toProperty();

  public search = wx.asyncCommand((x: RegExp) => {
    this.notifyChanged();

    return Rx.Observable.return(x);
  });

  protected createRegex(filter: string) {
    let regex: RegExp = null;

    if (String.isNullOrEmpty(filter) === false) {
      try {
        regex = new RegExp(filter, this.isCaseInsensitive ? 'i' : null);
      } catch (e) {
        filter = filter.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
        regex = new RegExp(filter, this.isCaseInsensitive ? 'i' : null);
      }
    }

    return regex;
  }

  initialize() {
    super.initialize();

    this.subscribe(this.search.results
      .invokeCommand(this.routingStateChanged));

    if (this.isLiveSearchEnabled) {
      this.subscribe(this.regex.changed
        .debounce(this.liveSearchTimeout)
        .invokeCommand(this.search)
      );
    }
  }

  getRoutingState(context?: any) {
    return this.createRoutingState(state => {
      if (String.isNullOrEmpty(this.filter()) === false) {
        state.filter = this.filter();
      }
    });
  }

  setRoutingState(state: ISearchRoutingState) {
    this.handleRoutingState(state, state => {
      this.filter(state.filter || '');
    });
  }
}

export default SearchViewModel;<commit_msg>updating search component to debounce the input instead of the generated regexes<commit_after>'use strict';

import * as Rx from 'rx';
import * as wx from 'webrx';

import BaseRoutableViewModel from '../../React/BaseRoutableViewModel';

export interface ISearchRoutingState {
  filter: string;
}

export class SearchViewModel extends BaseRoutableViewModel<ISearchRoutingState> {
  public static displayName = 'SearchViewModel';

  constructor(public isLiveSearchEnabled = false, private liveSearchTimeout = 250, private isCaseInsensitive = true, isRoutingEnabled = false) {
    super(isRoutingEnabled);
  }

  public filter = wx.property('');
  public regex = this.filter.changed
    .debounce(this.isLiveSearchEnabled ? this.liveSearchTimeout : 0)
    .distinctUntilChanged()
    .select(x => this.createRegex(x))
    .toProperty();

  public search = wx.asyncCommand((x: RegExp) => {
    this.notifyChanged();

    return Rx.Observable.return(x);
  });

  protected createRegex(filter: string) {
    let regex: RegExp = null;

    if (String.isNullOrEmpty(filter) === false) {
      try {
        regex = new RegExp(filter, this.isCaseInsensitive ? 'i' : null);
      } catch (e) {
        filter = filter.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
        regex = new RegExp(filter, this.isCaseInsensitive ? 'i' : null);
      }
    }

    return regex;
  }

  initialize() {
    super.initialize();

    this.subscribe(this.search.results
      .invokeCommand(this.routingStateChanged));

    if (this.isLiveSearchEnabled) {
      this.subscribe(this.regex.changed
        .invokeCommand(this.search)
      );
    }
  }

  getRoutingState(context?: any) {
    return this.createRoutingState(state => {
      if (String.isNullOrEmpty(this.filter()) === false) {
        state.filter = this.filter();
      }
    });
  }

  setRoutingState(state: ISearchRoutingState) {
    this.handleRoutingState(state, state => {
      this.filter(state.filter || '');
    });
  }
}

export default SearchViewModel;
{'fraction_non_alphanumeric': 0.14448958862366684, 'fraction_numerical': 0.0017775520568816658, 'mean_word_length': 3.194888178913738, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4547427', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1192, 'n_words': 240}