<commit_before>import {isNull, isPromise, is, invoke, not, val, pattern, tpl, parse, objectKeys}  from "./common";
import {isDefined, isObject, isString, isFunction, extend, forEach, isArray, identity} from "./common";
import {Resolvable}  from "./resolve";
import {Transition, TransitionRejection}  from "./transition";

function promiseToString(p) {
  if (is(TransitionRejection)(p.reason)) return p.reason.toString();
  return "Promise(" + JSON.stringify(p) + ")";
}

function fnToString(fn) {
  var name = fn.name ? fn.name : "(anonymous)";
  return tpl("function {name}(...)", { name: name });
}

class Trace {
  approximateDigests: number;
  format: Function;

  constructor() {
    this.approximateDigests = 0;
  }

  _trace(string) {
    console.log(string);
  }

  _replacer(key, val) {
    return this.format(val);
  }

  _toJson(o) {
    return JSON.stringify(o, (key, val) => this._replacer(key, val));
  }

  _stringify(o) {
    this.format = pattern([
      [not(isDefined),            val("undefined")],
      [isNull,                    val("null")],
      [isPromise,                 promiseToString],
      [is(Transition),            invoke("toString")],
      [is(Resolvable),            invoke("toString")],
      [isFunction,                fnToString],
      [val(true),                 identity]
    ]);

    return this._toJson(o);
  }

  traceTransitionStart(transition) {
    var tplData = {tid: transition.$id, digest: this.approximateDigests, transition: this._stringify(transition)};
    this._trace(tpl('Transition #{tid} Digest #{digest}: Started {transition}', tplData));
  }

  traceTransitionIgnored(transition) {
    var tplData = {tid: transition.$id, digest: this.approximateDigests, transition: this._stringify(transition)};
    this._trace(tpl('Transition #{tid} Digest #{digest}: Ignored {transition}', tplData));
  }

  traceHookInvocation(step, options) {
    var tplData = {tid: parse("transition.$id")(options), digest: this.approximateDigests, step: step.toString()};
    this._trace(tpl('Transition #{tid} Digest #{digest}:   Hook: {step}', tplData));
  }

  traceHookResult(hookResult, transitionResult, transitionOptions) {
    if (!isDefined(hookResult)) return;
    var tplData = {tid: parse("transition.$id")(transitionOptions), digest: this.approximateDigests, hookResult: this._stringify(hookResult), transitionResult: this._stringify(transitionResult)};
    this._trace(tpl('Transition #{tid} Digest #{digest}:   Hook returned: {hookResult} -> {transitionResult}', tplData));
  }

  traceResolvePath(path, options) {
    var tplData = {
      tid: parse("transition.$id")(options),
      digest: this.approximateDigests,
      path: path && path.toString(),
      policy: options && options.resolvePolicy
    };
    this._trace(tpl('Transition #{tid} Digest #{digest}:         Resolving {path} ({policy})', tplData));
  }

  traceResolvePathElement(pathElement, resolvablePromises, options) {
    var tplData = {
      tid: parse("transition.$id")(options),
      digest: this.approximateDigests,
      resolvablePromises: objectKeys(resolvablePromises).join(", "),
      pathElement: pathElement && pathElement.toString(),
      policy: options && options.resolvePolicy
    };
    this._trace(tpl('Transition #{tid} Digest #{digest}:         Resolve {pathElement} resolvables: [{resolvablePromises}] ({policy})', tplData));
  }

  traceResolveResolvable(resolvable, options) {
    var tplData = {
      tid: parse("transition.$id")(options),
      digest: this.approximateDigests,
      resolvable: resolvable && resolvable.toString(),
      policy: options && options.resolvePolicy
    };
    this._trace(tpl('Transition #{tid} Digest #{digest}:               Resolving {resolvable} ({policy})', tplData));
  }

  tracePathElementInvoke(pathElement, fn, deps, options) {
    var title = "Invoke " + options.when;
    var tplData = {
      tid: parse("transition.$id")(options),
      digest: this.approximateDigests,
      pathElement: pathElement && pathElement.toString(),
      fn: fn.name || "(anonymous)",
      title: title,
      deps: deps,
      policy: options && options.resolvePolicy
    };
    this._trace(tpl('Transition #{tid} Digest #{digest}:         {title}: {fn} {pathElement} requires: [{deps}]', tplData));
  }

  traceError(error, transition) {
    var tplData = {tid: transition.$id, digest: this.approximateDigests, error: error};
    this._trace(tpl('Transition #{tid} Digest #{digest}: Transition Rejected, reason: {error}', tplData));
  }

  traceSuccess(finalState, transition) {
    var tplData = {tid: transition.$id, digest: this.approximateDigests, state: finalState.name};
    this._trace(tpl('Transition #{tid} Digest #{digest}: Transition Success, state: {state}', tplData));
  }
}

export var trace = new Trace();

angular.module("ui.router").run(function($rootScope) {
  $rootScope.$watch(function() { trace.approximateDigests++; });
});<commit_msg>refactor(trace): convert to use template strings<commit_after>import {isNull, isPromise, is, invoke, not, val, pattern, parse, objectKeys}  from "./common";
import {isDefined, isObject, isString, isFunction, extend, forEach, isArray, identity} from "./common";
import {Resolvable}  from "./resolve";
import {Transition, TransitionRejection}  from "./transition";

function promiseToString(p) {
  if (is(TransitionRejection)(p.reason)) return p.reason.toString();
  return `Promise(${JSON.stringify(p)})`;
}

function fnToString(fn) {
  var name = fn.name ? fn.name : "(anonymous)";
  return `function ${name}(...)`;
}

class Trace {
  approximateDigests: number;
  format: Function;

  constructor() {
    this.approximateDigests = 0;
  }

  _trace(string) {
    console.log(string);
  }

  _replacer(key, val) {
    return this.format(val);
  }

  _toJson(o) {
    return JSON.stringify(o, (key, val) => this._replacer(key, val));
  }

  _stringify(o) {
    this.format = pattern([
      [not(isDefined),            val("undefined")],
      [isNull,                    val("null")],
      [isPromise,                 promiseToString],
      [is(Transition),            invoke("toString")],
      [is(Resolvable),            invoke("toString")],
      [isFunction,                fnToString],
      [val(true),                 identity]
    ]);

    return this._toJson(o);
  }

  traceTransitionStart(transition) {
    var tid = transition.$id,
      digest = this.approximateDigests,
      transitionStr = this._stringify(transition);
    this._trace(`Transition #${tid} Digest #${digest}: Started ${transitionStr}`);
  }

  traceTransitionIgnored(transition) {
    var tid = transition.$id,
      digest = this.approximateDigests,
      transitionStr = this._stringify(transition)
    this._trace(`Transition #${tid} Digest #${digest}: Ignored ${transitionStr}`);
  }

  traceHookInvocation(step, options) {
    var tid = parse("transition.$id")(options),
      digest = this.approximateDigests;
    this._trace(`Transition #${tid} Digest #${digest}:   Hook: ${step.toString()}`);
  }

  traceHookResult(hookResult, transitionResult, transitionOptions) {
    if (!isDefined(hookResult)) return;
    var tid = parse("transition.$id")(transitionOptions),
      digest = this.approximateDigests,
      hookResultStr = this._stringify(hookResult),
      transitionResultStr = this._stringify(transitionResult);
    this._trace(`Transition #${tid} Digest #${digest}:   Hook returned: ${hookResultStr} -> ${transitionResultStr}`);
  }

  traceResolvePath(path, options) {
    var tid = parse("transition.$id")(options),
      digest = this.approximateDigests,
      pathStr = path && path.toString(),
      policyStr = options && options.resolvePolicy;
    this._trace(`Transition #${tid} Digest #${digest}:         Resolving ${pathStr} (${policyStr})`);
  }

  traceResolvePathElement(pathElement, resolvablePromises, options) {
    var tid = parse("transition.$id")(options),
      digest = this.approximateDigests,
      resolvablePromisesStr = objectKeys(resolvablePromises).join(", "),
      pathElementStr = pathElement && pathElement.toString(),
      policyStr = options && options.resolvePolicy;
    this._trace(`Transition #${tid} Digest #${digest}:         Resolve ${pathElementStr} resolvables: [${resolvablePromisesStr}] (${policyStr})`);
  }

  traceResolveResolvable(resolvable, options) {
    var tid = parse("transition.$id")(options),
      digest = this.approximateDigests,
      resolvableStr = resolvable && resolvable.toString(),
      policyStr = options && options.resolvePolicy;
    this._trace(`Transition #${tid} Digest #${digest}:               Resolving ${resolvableStr} (${policyStr})`);
  }

  tracePathElementInvoke(pathElement, fn, deps, options) {
    var title = `Invoke ${options.when}`,
      tid = parse("transition.$id")(options),
      digest = this.approximateDigests,
      pathElementStr = pathElement && pathElement.toString(),
      fnName = fn.name || "(anonymous)",
      policyStr = options && options.resolvePolicy;
    this._trace(`Transition #${tid} Digest #${digest}:         ${title}: ${fnName} ${pathElementStr} requires: [${deps}]`);
  }

  traceError(error, transition) {
    var tid = transition.$id,
      digest = this.approximateDigests;
    this._trace(`Transition #${tid} Digest #${digest}: Transition Rejected, reason: ${error}`);
  }

  traceSuccess(finalState, transition) {
    var tid = transition.$id,
      digest = this.approximateDigests,
      state = finalState.name;
    this._trace(`Transition #${tid} Digest #${digest}: Transition Success, state: ${state}`);
  }
}

export var trace = new Trace();

angular.module("ui.router").run(function($rootScope) {
  $rootScope.$watch(function() { trace.approximateDigests++; });
});
{'fraction_non_alphanumeric': 0.1549599507085644, 'fraction_numerical': 0.00020538098172109262, 'mean_word_length': 3.790457452041318, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7167747', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2887, 'n_words': 618}