<commit_before>import { Injectable } from "@angular/core";
import { Router, Event, NavigationEnd } from "@angular/router";

import { Subscription } from "rxjs/Rx";

import { Session, SessionFactory } from "./session";

type NavigateOptions = {
  extraParams?: string,
  defaultUrl ?: string
};

@Injectable()
export class LoginReferrerService {
  private static DEFAULT_URL = '/newsfeed';
  private url: string;
  private exceptions: string[] = [];

  private session: Session = SessionFactory.build();

  constructor(private router: Router) { }

  private _routerListener: Subscription;
  listen(): this {
    this._routerListener = this.router.events.subscribe((event: Event) => {
      if (event instanceof NavigationEnd) {
        this.register(event.urlAfterRedirects || event.url);
      }
    });

    this.session.isLoggedIn(loggedIn => {
      if (!loggedIn) {
        this.unregister();
      }
    });

    return this;
  }

  unlisten(): this {
    this._routerListener.unsubscribe();

    return this;
  }

  register(url: string): this {
    if (!url || this.shouldBeAvoided(url)) {
      return this;
    }

    this.url = this._trim(url);
    return this;
  }

  unregister(): this {
    this.url = void 0;
    return this;
  }

  navigate(options: NavigateOptions = {}): Promise<boolean> {
    let url = this.url || options.defaultUrl || LoginReferrerService.DEFAULT_URL;

    if (options.extraParams) {
      url += `${~url.indexOf('?') ? '&' : '?'}${options.extraParams}`;
    }

    return this.router.navigateByUrl(url, { replaceUrl: true });
  }

  avoid(urls: string[]): this {
    this.exceptions = urls.map(url => this._trim(url));

    return this;
  }

  shouldBeAvoided(url: string): boolean {
    let cleanUrl = this._trim(url);

    if (~cleanUrl.indexOf(';')) {
      cleanUrl = cleanUrl.split(';')[0];
    }

    return !!~this.exceptions.indexOf(cleanUrl);
  }

  static _(router: Router) {
    return new LoginReferrerService(router);
  }

  // based on: https://stackoverflow.com/a/36391166
  private _trim(s): string {
    const mask = ' /?';

    while (~mask.indexOf(s[0])) {
      s = s.slice(1);
    }
    while (~mask.indexOf(s[s.length - 1])) {
      s = s.slice(0, -1);
    }
    return s;
  }
}<commit_msg>(fix): Don't trim ? on login referrer service (#194)<commit_after>import { Injectable } from "@angular/core";
import { Router, Event, NavigationEnd } from "@angular/router";

import { Subscription } from "rxjs/Rx";

import { Session, SessionFactory } from "./session";

type NavigateOptions = {
  extraParams?: string,
  defaultUrl ?: string
};

@Injectable()
export class LoginReferrerService {
  private static DEFAULT_URL = '/newsfeed';
  private url: string;
  private exceptions: string[] = [];

  private session: Session = SessionFactory.build();

  constructor(private router: Router) { }

  private _routerListener: Subscription;
  listen(): this {
    this._routerListener = this.router.events.subscribe((event: Event) => {
      if (event instanceof NavigationEnd) {
        this.register(event.urlAfterRedirects || event.url);
      }
    });

    this.session.isLoggedIn(loggedIn => {
      if (!loggedIn) {
        this.unregister();
      }
    });

    return this;
  }

  unlisten(): this {
    this._routerListener.unsubscribe();

    return this;
  }

  register(url: string): this {
    if (!url || this.shouldBeAvoided(url)) {
      return this;
    }

    this.url = this._trim(url);
    return this;
  }

  unregister(): this {
    this.url = void 0;
    return this;
  }

  navigate(options: NavigateOptions = {}): Promise<boolean> {
    let url = this.url || options.defaultUrl || LoginReferrerService.DEFAULT_URL;

    if (options.extraParams) {
      url += `${~url.indexOf('?') ? '&' : '?'}${options.extraParams}`;
    }

    return this.router.navigateByUrl(url, { replaceUrl: true });
  }

  avoid(urls: string[]): this {
    this.exceptions = urls.map(url => this._trim(url));

    return this;
  }

  shouldBeAvoided(url: string): boolean {
    let cleanUrl = this._trim(url);

    if (~cleanUrl.indexOf(';')) {
      cleanUrl = cleanUrl.split(';')[0];
    }

    return !!~this.exceptions.indexOf(cleanUrl);
  }

  static _(router: Router) {
    return new LoginReferrerService(router);
  }

  // based on: https://stackoverflow.com/a/36391166
  private _trim(s): string {
    const mask = ' /';

    while (~mask.indexOf(s[0])) {
      s = s.slice(1);
    }
    while (~mask.indexOf(s[s.length - 1])) {
      s = s.slice(0, -1);
    }
    return s;
  }
}
{'fraction_non_alphanumeric': 0.15711767298369325, 'fraction_numerical': 0.007271925958572058, 'mean_word_length': 3.020372010628875, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '3094681', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1474, 'n_words': 313}