<commit_before>part of calzone.transformers;

final String _MAP_PREFIX = "var \$Map = require('es6-map');";

// ES6-like Maps and Objects to Dart Maps, Arrays to Lists
class CollectionsTransformer implements TypeTransformer {
  final List<String> types = ["Map", "List"];

  final bool _usePolyfill;

  CollectionsTransformer([this._usePolyfill = false]);

  transformToDart(Compiler compiler, StringBuffer output) {
    var mangledNames = compiler.mangledNames;
    var constructor = mangledNames.getClassName("dart.collection", "new LinkedHashMap\$fromIterable");

    output.write("""
      if(Array.isArray(obj)) {
        return obj.map(function(e) {
          return dynamicTo(e);
        });
      }
      if(obj.constructor.name === 'Object') {
        var elms = Object.keys(obj).reduce(function(arr, key) {
        arr.push(key); arr.push(dynamicTo(obj[key]));
          return arr;
        }, []);
        var map = new ${mangledNames.getLibraryObject("dart.collection")}.$constructor(elms);
        map.\$builtinTypeInfo = [${compiler.mangledNames.getLibraryObject("dart.core")}.${compiler.mangledNames.getClassName("dart.core", "String")}, null];
        return map;
      }
    """);
  }

  transformFromDart(Compiler compiler, StringBuffer output) {
    var jsonData = compiler.classes["dart.convert._JsonMap"];
    var linkedData = compiler.classes["_js_helper.JsLinkedHashMap"];

    output.write("""
      if(Array.isArray(obj)) {
        return obj.map(function(e) {
          return dynamicFrom(e);
        });
      }
      var keys;
      var values;
      if(${jsonData.key.renderConditional("obj")}) {
        keys = obj.${jsonData.key.getMangledName("keys")}();
        values = obj.${jsonData.key.getMangledName("values")}();
      }
      if(${linkedData.key.renderConditional("obj")}) {
        keys = obj.${linkedData.key.getMangledName("keys")}();
        values = obj.${linkedData.key.getMangledName("values")}();
      }

      var a = {};
      keys.forEach(function(key, index) {
        a[key] = dynamicFrom(values[index]);
      });

      return a;
    """);
  }
}<commit_msg>Bug fixes pertaining to CollectionsTransformer.<commit_after>part of calzone.transformers;

final String _MAP_PREFIX = "var \$Map = require('es6-map');";

// ES6-like Maps and Objects to Dart Maps, Arrays to Lists
class CollectionsTransformer implements TypeTransformer {
  final List<String> types = ["Map", "List"];

  final bool _usePolyfill;

  CollectionsTransformer([this._usePolyfill = false]);

  transformToDart(Compiler compiler, StringBuffer output) {
    var mangledNames = compiler.mangledNames;
    var constructor = mangledNames.getClassName("dart.collection", "new LinkedHashMap\$fromIterable");

    output.write("""
      if(Array.isArray(obj)) {
        return obj.map(function(e) {
          return dynamicTo(e);
        });
      }
      if(obj.constructor.name === 'Object') {
        var elms = Object.keys(obj).reduce(function(arr, key) {
        arr.push(key); arr.push(dynamicTo(obj[key]));
          return arr;
        }, []);
        var map = new ${mangledNames.getLibraryObject("dart.collection")}.$constructor(elms);
        map.\$builtinTypeInfo = [${compiler.mangledNames.getLibraryObject("dart.core")}.${compiler.mangledNames.getClassName("dart.core", "String")}, null];
        return map;
      }
    """);
  }

  transformFromDart(Compiler compiler, StringBuffer output) {
    var data = compiler.classes["_js_helper.JsLinkedHashMap"];

    output.write("""
      if(Array.isArray(obj)) {
        return obj.map(function(e) {
          return dynamicFrom(e);
        });
      }

      if(obj.${data.key.getMangledName("keys")} && obj.${data.key.getMangledName("values")}) {
        var keys = obj.${data.key.getMangledName("keys")}();
        var values = obj.${data.key.getMangledName("values")}();

        var a = {};
        keys.forEach(function(key, index) {
          a[key] = dynamicFrom(values[index]);
        });

        return a;
      }
    """);
  }
}
{'fraction_non_alphanumeric': 0.15778994524639123, 'fraction_numerical': 0.0009955201592832255, 'mean_word_length': 3.09683995922528, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1066006', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1217, 'n_words': 213}