<commit_before>/*
 * Copyright 2015-2016 <NAME>, Inc.
 *
 * 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.
 */

import { SimpleArray } from '../simple-array/simple-array';
import { KeyedArray } from '../keyed-array/keyed-array';
import { immutableEqual } from '../equality/equality';

export interface Nameable {
  name: string;
}

function getName(thing: any): string {
  return thing.name;
}

function noop() {}

export interface SynchronizerOptions<T> {
  key?: (thing: T, index?: number) => string;
  equals?: (thingA: T, thingB: T) => boolean;
  onEnter?: (newThing: T) => void;
  onUpdate?: (newThing: T, oldThing: T) => void;
  onExit?: (oldThing: T) => void;
}

export interface DiffJS {
  before?: any;
  after?: any;
}

export class Diff<T> {
  static inflateFromJS<T>(Class: { fromJS: (js: any) => T }, diffJS: DiffJS): Diff<T> {
    let before: T | null = null;
    let after: T | null = null;
    if (diffJS.before) before = Class.fromJS(diffJS.before);
    if (diffJS.after) after = Class.fromJS(diffJS.after);
    return new Diff(before, after);
  }

  public before?: T;
  public after?: T;

  constructor(before: T | null, after: T | null) {
    this.before = before;
    this.after = after;
  }

  toJS(): DiffJS {
    let js: DiffJS = {};
    if (this.before) js.before = this.before;
    if (this.after) js.after = this.after;
    return js;
  }

  toJSON() {
    return this.toJS();
  }
}

const KEYED_ARRAY = KeyedArray.withKey("name");<commit_msg>also work on arrays<commit_after>/*
 * Copyright 2015-2016 Imply Data, Inc.
 *
 * 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.
 */

import { SimpleArray } from '../simple-array/simple-array';
import { KeyedArray } from '../keyed-array/keyed-array';
import { immutableEqual } from '../equality/equality';

export interface Nameable {
  name: string;
}

function getName(thing: any): string {
  return thing.name;
}

function noop() {}

export interface SynchronizerOptions<T> {
  key?: (thing: T, index?: number) => string;
  equals?: (thingA: T, thingB: T) => boolean;
  onEnter?: (newThing: T) => void;
  onUpdate?: (newThing: T, oldThing: T) => void;
  onExit?: (oldThing: T) => void;
}

export interface DiffJS {
  before?: any;
  after?: any;
}

export class Diff<T> {
  static inflateFromJS<T>(Class: { fromJS: (js: any) => T }, diffJS: DiffJS): Diff<T> {
    let before: T | null = null;
    let after: T | null = null;
    if (diffJS.before) before = Class.fromJS(diffJS.before);
    if (diffJS.after) after = Class.fromJS(diffJS.after);
    return new Diff(before, after);
  }

  static inflateFromJSs<T>(Class: { fromJS: (js: any) => T }, diffJSs: DiffJS[]): Diff<T>[] {
    if (!Array.isArray(diffJSs)) throw new Error('diffs must be an array');
    return diffJSs.map(diffJS => Diff.inflateFromJS<T>(Class, diffJS));
  }

  public before?: T;
  public after?: T;

  constructor(before: T | null, after: T | null) {
    this.before = before;
    this.after = after;
  }

  toJS(): DiffJS {
    let js: DiffJS = {};
    if (this.before) js.before = this.before;
    if (this.after) js.after = this.after;
    return js;
  }

  toJSON() {
    return this.toJS();
  }
}

const KEYED_ARRAY = KeyedArray.withKey("name");
{'fraction_non_alphanumeric': 0.13905395768956502, 'fraction_numerical': 0.0057047777513667694, 'mean_word_length': 3.5198711063372716, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, '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': '946973', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1358, 'n_words': 455}