<commit_before>module System {

  // a string key hastable
  interface IHashTable<T> {
    [key: string]: T;
  }


  export class Reflection {
    // cached module names and types
    private static cachedNameSpaces: IHashTable<Function> = {}; 
    private static cachedTypes: IHashTable<any> = {};

    // does caching of module names
    public static cacheNameSpace(...namespaces: string[]): void {
      for (var i = 0, len_i = namespaces.length; i < len_i; i++) {
        var elem = namespaces[i];
        if (elem in Reflection.cachedNameSpaces)
          continue;
        var objtype = Reflection.checkCallPath(window, elem.split('.'));
        if (objtype)
          Reflection.cachedNameSpaces[elem] = objtype;
      }
    }

    // does caching of types
    public static cacheType(proto: any): string {
      if (!proto)
        return;
      if (typeof proto == "string") {

        // if allready cached
        if (Reflection.cachedTypes[proto])
          return proto;

        var classname = <string>proto.substr(proto.lastIndexOf('.') + 1, proto.length);
        if (!classname)
          return null;

        // not yet cached     
        for (var ns in Reflection.cachedNameSpaces) {
          var ref = Reflection.cachedNameSpaces[ns];
          for (var cls in ref) {
            var tempObj = ref[cls];
            if (tempObj && Reflection.getClassName(tempObj) == classname) {
              var path = ns + '.' + classname;
              Reflection.cachedTypes[path] = tempObj.prototype;
              return path;
            }
          }
        }

      }
      else {
        if (typeof proto != "object")
          return null;
        var classname = Reflection.getClassName(proto);
        if (!classname || classname == "Object")
          return null;
        var path: string = null;

        // allready cached
        for (path in Reflection.cachedTypes)
          if (Reflection.cachedTypes[path] === proto.__proto__) {
            return path;
            break;
          }
        // not yet cached      
        for (var ns in Reflection.cachedNameSpaces) {
          var ref = Reflection.cachedNameSpaces[ns];
          for (var cls in ref) {
            var tempObj = ref[cls];

            if (tempObj && tempObj.prototype == proto.__proto__) {
              var path = ns + '.' + classname;
              Reflection.cachedTypes[path] = proto.__proto__;
              return path;
            }
          }
        }
      }
      return null;
    }

    // removes cached type
    private static decacheType(classpath: string): { classproto: any, typeproto: any } {
      if (!classpath)
        return null;
      if (Reflection.cachedTypes[classpath] != undefined
        || Reflection.cacheType(classpath) == classpath)
        return Reflection.cachedTypes[classpath];
      return null;
    }

    // retruns class name
    public static getClassName(obj: any): string {
      if (!obj)
        return "undefined";
      var str = (obj.prototype ? obj.prototype.constructor : obj.constructor).toString();
      var cname = str.match(/function\s(\w*)/)[1];
      return ["", "anonymous", "Anonymous"].indexOf(cname) > -1 ? "Function" : cname;
    }

    // serializes an object by using JSON
    public static serialize(obj: any, ...namespaces: string[]): string {
      Reflection.cacheNameSpace.apply(this, namespaces);
      return JSON.stringify(Reflection.decycle(obj));
    }
    // does decycling
    private static seq: number = 0;
    private static decycle(value: any, ret: any = null, map: any[] = [], keys: string[] = [], lastname: string = ".", seq: number = 0): any {
      if (value && typeof value == 'object')
        if (value instanceof Date)
          return { $date: value.toJSON() };
        else if (value instanceof RegExp)
          return { $regex: value.toString() };
        else {
          if (ret == null) {
            Reflection.seq = -1;
            if (value instanceof Array)
              ret = [];
            else
              ret = {};
          }

          var i = 0, len_i = map.length;
          for (; i < len_i && map[i] !== value; i++);
          if (i < len_i)
            return { $ref: keys[i] }; 

          if (value instanceof Array) {
            if (value.length > 0) {

              map.push(value);
              keys.push(seq + '.' + lastname);

              for (var i = 0, len_i = <number>value.length; i < len_i; i++) {
                var value_i = value[i];
                if (value_i && typeof value_i == "object")
                  if (value_i instanceof Date)
                    ret[i] = { $date: value_i.toJSON() };
                  else if (value instanceof RegExp)
                    ret[i] = { $regex: value_i.toString() };
                  else
                    ret[i] = Reflection.decycle(value_i, (value_i instanceof Array) ? [] : {}, map, keys, lastname + '.' + i, seq);
                else
                  ret.push(value_i);
              }<commit_msg>Update Reflection.ts<commit_after>module System {

  // a string key hastable
  interface IHashTable<T> {
    [key: string]: T;
  }


  export class Reflection {
    // cached module names and types
    private static cachedNameSpaces: IHashTable<Function> = {}; 
    private static cachedTypes: IHashTable<any> = {};

    // does caching of module names
    public static cacheNameSpace(...namespaces: string[]): void {
      for (var i = 0, len_i = namespaces.length; i < len_i; i++) {
        var elem = namespaces[i];
        if (elem in Reflection.cachedNameSpaces)
          continue;
        var objtype = Reflection.checkCallPath(window, elem.split('.'));
        if (objtype)
          Reflection.cachedNameSpaces[elem] = objtype;
      }
    }

    // does caching of types
    public static cacheType(proto: any): string {
      if (!proto)
        return;
      if (typeof proto == "string") {

        // if allready cached
        if (Reflection.cachedTypes[proto])
          return proto;

        var classname = <string>proto.substr(proto.lastIndexOf('.') + 1, proto.length);
        if (!classname)
          return null;

        // not yet cached     
        for (var ns in Reflection.cachedNameSpaces) {
          var ref = Reflection.cachedNameSpaces[ns];
          for (var cls in ref) {
            var tempObj = ref[cls];
            if (tempObj && Reflection.getClassName(tempObj) == classname) {
              var path = ns + '.' + classname;
              Reflection.cachedTypes[path] = tempObj.prototype;
              return path;
            }
          }
        }

      }
      else {
        if (typeof proto != "object")
          return null;
        var classname = Reflection.getClassName(proto);
        if (!classname || classname == "Object")
          return null;
        var path: string = null;

        // allready cached
        for (path in Reflection.cachedTypes)
          if (Reflection.cachedTypes[path] === proto.__proto__) {
            return path;
            break;
          }
        // not yet cached      
        for (var ns in Reflection.cachedNameSpaces) {
          var ref = Reflection.cachedNameSpaces[ns];
          for (var cls in ref) {
            var tempObj = ref[cls];

            if (tempObj && tempObj.prototype == proto.__proto__) {
              var path = ns + '.' + classname;
              Reflection.cachedTypes[path] = proto.__proto__;
              return path;
            }
          }
        }
      }
      return null;
    }

    // removes cached type
    private static decacheType(classpath: string): { classproto: any, typeproto: any } {
      if (!classpath)
        return null;
      if (Reflection.cachedTypes[classpath] != undefined
        || Reflection.cacheType(classpath) == classpath)
        return Reflection.cachedTypes[classpath];
      return null;
    }

    // retruns class name
    public static getClassName(obj: any): string {
      if (!obj)
        return "undefined";
      var str = (obj.prototype ? obj.prototype.constructor : obj.constructor).toString();
      var cname = str.match(/function\s(\w*)/)[1];
      return ["", "anonymous", "Anonymous"].indexOf(cname) > -1 ? "Function" : cname;
    }

    // serializes an object by using JSON
    public static serialize(obj: any, ...namespaces: string[]): string {
      Reflection.cacheNameSpace.apply(this, namespaces);
      return JSON.stringify(Reflection.decycle(obj));
    }
    // does decycling
    private static seq: number = 0;
    private static decycle(value: any, ret: any = null, map: any[] = [], keys: string[] = [], lastname: string = ".", seq: number = 0): any {
      if (value && typeof value == 'object')
        if (value instanceof Date)
          return { $date: value.toJSON() };
        else if (value instanceof RegExp)
          return { $regex: value.toString() };
        else {
          if (ret == null) {
            Reflection.seq = -1;
            if (value instanceof Array)
              ret = [];
            else
              ret = {};
          }

          var i = 0, len_i = map.length;
          for (; i < len_i && map[i] !== value; i++);
          if (i < len_i)
            return { $ref: keys[i] }; 

          if (value instanceof Array) {
            if (value.length > 0) {

              map.push(value);
              keys.push(seq + '.' + lastname);

              for (var i = 0, len_i = <number>value.length; i < len_i; i++) {
                var value_i = value[i];
                if (value_i && typeof value_i == "object")
                  if (value_i instanceof Date)
                    ret[i] = { $date: value_i.toJSON() };
                  else if (value_i instanceof RegExp)
                    ret[i] = { $regex: value_i.toString() };
                  else
                    ret[i] = Reflection.decycle(value_i, (value_i instanceof Array) ? [] : {}, map, keys, lastname + '.' + i, seq);
                else
                  ret.push(value_i);
              }
{'fraction_non_alphanumeric': 0.1223122312231223, 'fraction_numerical': 0.002000200020002, 'mean_word_length': 1.7464982147761603, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, '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': '3893777', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2761, 'n_words': 773}