<commit_before>/**
 * Created by sam on 12.02.2015.
 */
import {get as getData} from '../data';
import {isDataType, IDataType} from '../datatype';
import {GraphNode, isType} from '../graph/graph';
import ActionNode from './ActionNode';
import StateNode from './StateNode';


/**
 * list of categories for actions and objects
 */
export const cat = {
  data: 'data',
  selection: 'selection',
  visual: 'visual',
  layout: 'layout',
  logic: 'logic',
  custom: 'custom',
  annotation: 'annotation'
};

/**
 * list of operations
 */
export const op = {
  create: 'create',
  update: 'update',
  remove: 'remove'
};


/**
 * an object reference is a common description of an object node in the provenance graph
 */
export interface IObjectRef<T> {
  /**
   * name of the object
   */
  readonly name: string;
  /**
   * category one of categories
   */
  readonly category: string;
  /**
   * the value
   */
  readonly v: Promise<T>;

  /**
   * maybe null if not defined
   */
  readonly value: T;

  /**
   * a hash for avoiding false duplicate detection
   */
  readonly hash: string;
}

/**
 * creates an object reference to the given object
 * @param v
 * @param name
 * @param category
 * @param hash
 * @returns {{v: T, name: string, category: string}}
 */
export function ref<T>(v: T, name: string, category = cat.data, hash = name + '_' + category): IObjectRef<T> {
  return {
    v: Promise.resolve(v),
    value: v,
    name: name,
    category: category,
    hash: hash
  };
}

/**
 * tries to persist an object value supporting datatypes and DOM elements having an id
 * @param v
 * @returns {any}
 */
function persistData(v: any): any {
  if (v === undefined || v === null) {
    return null;
  }
  if (v instanceof Element) {
    let e = <Element>v,
      id = e.getAttribute('id');
    return {type: 'element', id: id};
  }
  if (isDataType(v)) {
    let e = <IDataType>v;
    return {type: 'dataset', id: e.desc.id, persist: e.persist()};
  }
  if (typeof v === 'string' || typeof v === 'number') {
    return {type: 'primitive', v: v};
  }
  return null;
}

function restoreData(v: any): any {
  if (!v) {
    return null;
  }
  switch (v.type) {
    case 'element':
      if (v.id) {
        return Promise.resolve(document.getElementById(v.id));
      }
      return null;
    case 'dataset':
      return getData(v.persist);
    case 'primitive':
      return Promise.resolve(v.v);<commit_msg>fix linting<commit_after>/**
 * Created by sam on 12.02.2015.
 */
import {get as getData} from '../data';
import {isDataType, IDataType} from '../datatype';
import {GraphNode, isType} from '../graph/graph';
import ActionNode from './ActionNode';
import StateNode from './StateNode';


/**
 * list of categories for actions and objects
 */
export const cat = {
  data: 'data',
  selection: 'selection',
  visual: 'visual',
  layout: 'layout',
  logic: 'logic',
  custom: 'custom',
  annotation: 'annotation'
};

/**
 * list of operations
 */
export const op = {
  create: 'create',
  update: 'update',
  remove: 'remove'
};


/**
 * an object reference is a common description of an object node in the provenance graph
 */
export interface IObjectRef<T> {
  /**
   * name of the object
   */
  readonly name: string;
  /**
   * category one of categories
   */
  readonly category: string;
  /**
   * the value
   */
  readonly v: Promise<T>;

  /**
   * maybe null if not defined
   */
  readonly value: T;

  /**
   * a hash for avoiding false duplicate detection
   */
  readonly hash: string;
}

/**
 * creates an object reference to the given object
 * @param v
 * @param name
 * @param category
 * @param hash
 * @returns {{v: T, name: string, category: string}}
 */
export function ref<T>(v: T, name: string, category = cat.data, hash = name + '_' + category): IObjectRef<T> {
  return {
    v: Promise.resolve(v),
    value: v,
    name,
    category,
    hash
  };
}

/**
 * tries to persist an object value supporting datatypes and DOM elements having an id
 * @param v
 * @returns {any}
 */
function persistData(v: any): any {
  if (v === undefined || v === null) {
    return null;
  }
  if (v instanceof Element) {
    const e = <Element>v,
      id = e.getAttribute('id');
    return {type: 'element', id};
  }
  if (isDataType(v)) {
    const e = <IDataType>v;
    return {type: 'dataset', id: e.desc.id, persist: e.persist()};
  }
  if (typeof v === 'string' || typeof v === 'number') {
    return {type: 'primitive', v};
  }
  return null;
}

function restoreData(v: any): any {
  if (!v) {
    return null;
  }
  switch (v.type) {
    case 'element':
      if (v.id) {
        return Promise.resolve(document.getElementById(v.id));
      }
      return null;
    case 'dataset':
      return getData(v.persist);
    case 'primitive':
      return Promise.resolve(v.v);
{'fraction_non_alphanumeric': 0.15363769022305607, 'fraction_numerical': 0.003335417969564311, 'mean_word_length': 2.8414731785428344, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7632328', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1553, 'n_words': 519}