<commit_before>define([], function() {
  'use strict';
  function Partition(arrayOfCells) {
    var domain = [];
    var images = {};
    var indexSizes = {};

    (function() {
      (function() {
        for (var i = 0; i < arrayOfCells.length; i++) {
          var cell = arrayOfCells[i];
          if (cell.length === 0) {
            throw new Error('All cells in ' + arrayOfCells +
                ' must be nonempty');
          }
          for (var j = 0; j < cell.length; j++) {
            var x = cell[j];
            if (domain.indexOf(x) != -1) {
              throw new Error(x + ' has been seen twice, ' +
                  arrayOfCells + ' is not disjoint');
            }
            domain.push(x);
          }
        }
      })();

      domain.sort();

      (function() {
        var index = 0;
        for (var i = 0; i < arrayOfCells.length; i++) {
          var cell = arrayOfCells[i];
          for (var j = 0; j < cell.length; j++) {
            var x = cell[j];
            images[x] = domain[index];
          }
          indexSizes[index] = cell.length;
          index += cell.length;
        }
      })();
    })();

    this.image = function(x) {
      return images[x];
    };

    this.domain = function() {
      var keys = Object.keys(images);
      keys.sort();
      return keys;
    };

    this.indexes = function() {
      console.log(indexSizes);
      var keys = Object.keys(indexSizes).map(function(i) {
        return +i;
      });
      keys.sort();
      return keys;
    };

    this.indexSize = function(index) {
      if (index in indexSizes) {
        return indexSizes[index];
      }
      throw new Error(index + ' is not a key in ' + indexSizes);
    };
  }

  return {
    Partition: Partition
  };
});
<commit_msg>Remove a conosle.log<commit_after>define([], function() {
  'use strict';
  function Partition(arrayOfCells) {
    var domain = [];
    var images = {};
    var indexSizes = {};

    (function() {
      (function() {
        for (var i = 0; i < arrayOfCells.length; i++) {
          var cell = arrayOfCells[i];
          if (cell.length === 0) {
            throw new Error('All cells in ' + arrayOfCells +
                ' must be nonempty');
          }
          for (var j = 0; j < cell.length; j++) {
            var x = cell[j];
            if (domain.indexOf(x) != -1) {
              throw new Error(x + ' has been seen twice, ' +
                  arrayOfCells + ' is not disjoint');
            }
            domain.push(x);
          }
        }
      })();

      domain.sort();

      (function() {
        var index = 0;
        for (var i = 0; i < arrayOfCells.length; i++) {
          var cell = arrayOfCells[i];
          for (var j = 0; j < cell.length; j++) {
            var x = cell[j];
            images[x] = domain[index];
          }
          indexSizes[index] = cell.length;
          index += cell.length;
        }
      })();
    })();

    this.image = function(x) {
      return images[x];
    };

    this.domain = function() {
      var keys = Object.keys(images);
      keys.sort();
      return keys;
    };

    this.indexes = function() {
      var keys = Object.keys(indexSizes).map(function(i) {
        return +i;
      });
      keys.sort();
      return keys;
    };

    this.indexSize = function(index) {
      if (index in indexSizes) {
        return indexSizes[index];
      }
      throw new Error(index + ' is not a key in ' + indexSizes);
    };
  }

  return {
    Partition: Partition
  };
});
<|endoftext|>


<commit_before>/**
 * Created by thankia on 16/09/2016.
 */
/**
 * Useful when dealing with a subtree and deletion is present because of  absence member, replaces with it 'I' to ignore
 * @param ref_cigar_string reference cigar string
 * @returns {*}
 */
function checkCigar() {
    var cigar_list = [];
    var cigar_id = [];
    var cigars_json = {};

    var member = syntenic_data.cigar

    var reverse_cigar = []
    jQuery.map(syntenic_data.member, function (obj) {
        if (syntenic_data.ref.strand != obj.strand) {
            jQuery.map(obj.Transcript, function (transcript) {
                if(transcript.Translation){
                    reverse_cigar.push(transcript.Translation.id)
                }
            })
        }
    });

    for (var id in syntenic_data.cigar) {

        if (member.hasOwnProperty(id)) {

            var cigar_string = expandCigar(member[id])

            if (reverse_cigar.indexOf(id) >= 0) {
                cigar_string.split("").reverse().join()
            }

            cigar_list.push(cigar_string);
            cigar_id.push(id)
        }
    }
    var pos = [];
    for (var i = 0; i < cigar_list[0].length; i++) {
        if (cigar_list[0][i] == 'D') {
            for (var j = 1; j < cigar_list.length; j++) {
                if (cigar_list[j][i] == 'M') {
                    break;
                }
                if (j == cigar_list.length - 1) {
                    cigar_list[0] = replaceAt(cigar_list[0], i, "I")
                    pos.push(i)
                }
            }
        }
    }

    // to clean all cigars...
    for (var i = 0; i < cigar_list.length; i++) {
        cigar_list[i] = cigar_list[i].split("")

        for (var j = pos.length - 1; j >= 0; j--) {
            cigar_list[i].splice(pos[j], 1);
        }

        cigar_string = cigar_list[i].join("")

        if (reverse_cigar.indexOf(cigar_id[i]) >= 0) {
            cigar_string.split("").reverse().join()
        }

        var new_cigar = compressCigar(cigar_string)

        cigars_json[cigar_id[i]] = new_cigar

    }

    return cigars_json
}

function expandCigar(cigar, toNucleotide) {
    var cigar_string = "";
    var cigar = cigar.replace(/([SIXMND])/g, ":$1,");
    var cigars_array = cigar.split(',');

    var multiply = 1;
    if(toNucleotide){
        multiply = 3
    }

    for (var j = 0; j < cigars_array.length - 1; j++) {
        var cigar = cigars_array[j].split(":");
        var key = cigar[1];
        var length = cigar[0] * multiply;
        if (!length) {
            length = multiply
        }
        while (length--) {
            cigar_string += key;
        }

        cigar_string += "";
    }

    return cigar_string
}

function compressCigar(cigar_string) {
    cigar_string = cigar_string.replace(/(MD)/g, "M,D");
    cigar_string = cigar_string.replace(/(DM)/g, "D,M");
    var cigar_array = cigar_string.split(",")
    var new_cigar = ""

    for (var a = 0; a < cigar_array.length; a++) {
        var key = cigar_array[a].charAt(0)
        var length = cigar_array[a].length;

        if (length > 1) {
            new_cigar += length
        }
        new_cigar += key
    }

    return new_cigar;
}

/**
 * formats hit cigar to match with reference cigar for drawing on genes
 * @param ref_exons list of reference exons
 * @param hit_cigar hit cigar string
 * @param colours colour array
 * @param ref_cigar reference cigar string
 * @param reverse hit strand is reverse or not
 * @param ref_strand reference strand
 * @returns {string} formated cigar
 */
function format_ref_cigar() {
    var i = null;
    jQuery.map(syntenic_data.member[syntenic_data.ref].Transcript, function (obj) {
        if (obj.Translation && obj.Translation.id == protein_member_id) {
            i = syntenic_data.member[syntenic_data.ref].Transcript.indexOf(obj)
        }
    });

    var ref_exons = syntenic_data.member[syntenic_data.ref].Transcript[i].Exon

    var ref_cigar = syntenic_data.cigar[protein_member_id]

    var no_of_exons = ref_exons.length;

    var ref_strand = syntenic_data.member[syntenic_data.ref].strand;

    var ref_exon_array = [];

    while (i < no_of_exons) {
        var length = ref_exons[i].length

        var ref_exon = length
        if (parseInt(length) >= 0) {
            ref_exon_array.push(ref_exon)
        }
        i++;
    }

    var cigar_string = expandCigar(ref_cigar, "true")

    var i = 0
    var total_len = 0;
    var flag = false;
    var cigar_string_match = cigar_string.replace(/D/g, '');

    // if cigar is shorter than CDS than last CDSs becomes 0

    while (i < ref_exon_array.length) {
        if (flag == false) {
            if (parseInt(total_len) + parseInt(ref_exon_array[i]) < cigar_string_match.length) {
                total_len += ref_exon_array[i];
            }
            else {
                ref_exon_array[i] = cigar_string_match.length - total_len;
                total_len = cigar_string_match.length;
                flag = true;
            }
        } else {
            ref_exon_array[i] = 0;
        }
        i++;
    }

    var ref_cigar_count = 0;

    var hit_position = 0;

    var ref_exon_number = 0;
    var count_match = 0;

    var formated_ref_cigar = []

    var last_pos = 0;
    // dividing reference cigar into chunks based on exon length (ignoring deletions)
    while (ref_cigar_count < cigar_string.length) {

        if (cigar_string.charAt(ref_cigar_count) == 'M') {
            if (count_match == ref_exon_array[ref_exon_number]) {
                ref_exon_number++;
                formated_ref_cigar.push(cigar_string.substr(last_pos, hit_position));
                count_match = 0;
                last_pos += hit_position;
                hit_position = 0;
            }
            count_match++;
        }
        hit_position++;
        ref_cigar_count++;
    }

    formated_ref_cigar.push(cigar_string.substr(last_pos, hit_position));
    var i = 0;

    return formated_ref_cigar;

}

/**
 * formats hit cigar to match with reference cigar for drawing on genes
 * @param ref_exons list of reference exons
 * @param hit_cigar hit cigar string
 * @param colours colour array
 * @param ref_cigar reference cigar string
 * @param reverse hit strand is reverse or not
 * @param ref_strand reference strand
 * @returns {string} formated cigar
 */
function formatCigar(ref_exons, hit_cigar, colours, ref_cigar, hit_strand, ref_strand) {

    var no_of_exons = ref_exons.length
    var hit_cigar_arr = [];
    var ref_exon_array = [];
    var last_pos = 0;
    var i = 0
    var j = 0;

    var ref_cigar_array = ref_data.formated_cigar;
    var cigar_string = expandCigar(ref_cigar, "true")
    hit_cigar = expandCigar(hit_cigar, "true")

    while (i < ref_cigar_array.length) {
        ref_exon_array.push(ref_cigar_array[i].replace(/D/g, "").length)
        // ref_exons[i] = ref_exons[i].length;
        i++;
    }

    var i = 0
    while (i < ref_exons.length) {
        // ref_exon_array.push(ref_cigar_array[i].replace(/D/g, "").length)
        ref_exons[i] = ref_exons[i].length;
        i++;
    }

    // if (hit_strand != ref_strand) {
    //     hit_cigar = hit_cigar.split("").reverse().join("");
    // }

    if (hit_strand != ref_strand && hit_strand == 1) { //working fine
        cigar_string = cigar_string.split("").reverse().join("");
        hit_cigar = hit_cigar.split("").reverse().join("");
    }
    else if (hit_strand != ref_strand && ref_strand == 1) {//working fine
        cigar_string = cigar_string.split("").reverse().join("");
        hit_cigar = hit_cigar.split("").reverse().join("");
        ref_exons = ref_exons.reverse()
    }
    else if(hit_strand == ref_strand && ref_strand == -1){ //working fine
        cigar_string = cigar_string.split("").reverse().join("");
        hit_cigar = hit_cigar.split("").reverse().join("");

    }

    // if cigar string is D in all sequences (because of subset) that that part get removed
    while (j < cigar_string.length) {
        if (cigar_string.charAt(j) == 'D') {
            if (hit_cigar.charAt(j) == 'M') {
                hit_cigar = replaceAt(hit_cigar, j, "_");
            }
            else if (hit_cigar.charAt(j) == 'D') {
                hit_cigar = replaceAt(hit_cigar, j, "I");
            }
        }
        j++;
    }

    var ref_cigar_count = 0;

    var hit_position = 0;

    var ref_exon_number = 0;
    var count_match = 0;

    var temp_array = [];
    // dividing reference cigar into chunks based on exon length (ignoring deletions)
    while (ref_cigar_count < cigar_string.length) {

        if (cigar_string.charAt(ref_cigar_count) == 'M' || cigar_string.charAt(ref_cigar_count) == '_') {

            if (count_match == ref_exons[ref_exon_number]) {
                ref_exon_number++;
                hit_cigar_arr.push(hit_cigar.substr(last_pos, hit_position));
                temp_array.push(hit_position + " : " + ref_exon_number)

                count_match = 0;
                last_pos += hit_position;
                hit_position = 0;
                if (hit_strand != ref_strand) {
                }

            }
            count_match++;
        }

        hit_position++;
        ref_cigar_count++;
    }
    hit_cigar_arr.push(hit_cigar.substr(last_pos, hit_position));
    if (hit_strand != ref_strand && hit_strand == 1) {
        hit_cigar_arr.reverse()
    }

    return hit_cigar_arr.join("-");

}
<commit_msg>adds format_cigar function for ref when in tabular view<commit_after>/**
 * Created by thankia on 16/09/2016.
 */
/**
 * Useful when dealing with a subtree and deletion is present because of  absence member, replaces with it 'I' to ignore
 * @param ref_cigar_string reference cigar string
 * @returns {*}
 */
function checkCigar() {
    var cigar_list = [];
    var cigar_id = [];
    var cigars_json = {};

    var member = syntenic_data.cigar

    var reverse_cigar = []
    jQuery.map(syntenic_data.member, function (obj) {
        if (syntenic_data.ref.strand != obj.strand) {
            jQuery.map(obj.Transcript, function (transcript) {
                if(transcript.Translation){
                    reverse_cigar.push(transcript.Translation.id)
                }
            })
        }
    });

    for (var id in syntenic_data.cigar) {

        if (member.hasOwnProperty(id)) {

            var cigar_string = expandCigar(member[id])

            if (reverse_cigar.indexOf(id) >= 0) {
                cigar_string.split("").reverse().join()
            }

            cigar_list.push(cigar_string);
            cigar_id.push(id)
        }
    }
    var pos = [];
    for (var i = 0; i < cigar_list[0].length; i++) {
        if (cigar_list[0][i] == 'D') {
            for (var j = 1; j < cigar_list.length; j++) {
                if (cigar_list[j][i] == 'M') {
                    break;
                }
                if (j == cigar_list.length - 1) {
                    cigar_list[0] = replaceAt(cigar_list[0], i, "I")
                    pos.push(i)
                }
            }
        }
    }

    // to clean all cigars...
    for (var i = 0; i < cigar_list.length; i++) {
        cigar_list[i] = cigar_list[i].split("")

        for (var j = pos.length - 1; j >= 0; j--) {
            cigar_list[i].splice(pos[j], 1);
        }

        cigar_string = cigar_list[i].join("")

        if (reverse_cigar.indexOf(cigar_id[i]) >= 0) {
            cigar_string.split("").reverse().join()
        }

        var new_cigar = compressCigar(cigar_string)

        cigars_json[cigar_id[i]] = new_cigar

    }

    return cigars_json
}

function expandCigar(cigar, toNucleotide) {
    var cigar_string = "";
    var cigar = cigar.replace(/([SIXMND])/g, ":$1,");
    var cigars_array = cigar.split(',');

    var multiply = 1;
    if(toNucleotide){
        multiply = 3
    }

    for (var j = 0; j < cigars_array.length - 1; j++) {
        var cigar = cigars_array[j].split(":");
        var key = cigar[1];
        var length = cigar[0] * multiply;
        if (!length) {
            length = multiply
        }
        while (length--) {
            cigar_string += key;
        }

        cigar_string += "";
    }

    return cigar_string
}

function compressCigar(cigar_string) {
    cigar_string = cigar_string.replace(/(MD)/g, "M,D");
    cigar_string = cigar_string.replace(/(DM)/g, "D,M");
    var cigar_array = cigar_string.split(",")
    var new_cigar = ""

    for (var a = 0; a < cigar_array.length; a++) {
        var key = cigar_array[a].charAt(0)
        var length = cigar_array[a].length;

        if (length > 1) {
            new_cigar += length
        }
        new_cigar += key
    }

    return new_cigar;
}

/**
 * formats hit cigar to match with reference cigar for drawing on genes
 * @param ref_exons list of reference exons
 * @param hit_cigar hit cigar string
 * @param colours colour array
 * @param ref_cigar reference cigar string
 * @param reverse hit strand is reverse or not
 * @param ref_strand reference strand
 * @returns {string} formated cigar
 */
function format_ref_cigar() {
    var i = null;
    jQuery.map(syntenic_data.member[syntenic_data.ref].Transcript, function (obj) {
        if (obj.Translation && obj.Translation.id == protein_member_id) {
            i = syntenic_data.member[syntenic_data.ref].Transcript.indexOf(obj)
        }
    });

    var ref_exons = syntenic_data.member[syntenic_data.ref].Transcript[i].Exon

    var ref_cigar = syntenic_data.cigar[protein_member_id]

    var no_of_exons = ref_exons.length;

    var ref_strand = syntenic_data.member[syntenic_data.ref].strand;

    var ref_exon_array = [];
    i = 0;
    while (i < no_of_exons) {
        var length = ref_exons[i].length

        var ref_exon = length
        if (parseInt(length) >= 0) {
            ref_exon_array.push(ref_exon)
        }
        i++;
    }

    var cigar_string = expandCigar(ref_cigar, "true")

    var i = 0
    var total_len = 0;
    var flag = false;
    var cigar_string_match = cigar_string.replace(/D/g, '');

    // if cigar is shorter than CDS than last CDSs becomes 0

    while (i < ref_exon_array.length) {
        if (flag == false) {
            if (parseInt(total_len) + parseInt(ref_exon_array[i]) < cigar_string_match.length) {
                total_len += ref_exon_array[i];
            }
            else {
                ref_exon_array[i] = cigar_string_match.length - total_len;
                total_len = cigar_string_match.length;
                flag = true;
            }
        } else {
            ref_exon_array[i] = 0;
        }
        i++;
    }

    var ref_cigar_count = 0;

    var hit_position = 0;

    var ref_exon_number = 0;
    var count_match = 0;

    var formated_ref_cigar = []

    var last_pos = 0;
    // dividing reference cigar into chunks based on exon length (ignoring deletions)
    while (ref_cigar_count < cigar_string.length) {

        if (cigar_string.charAt(ref_cigar_count) == 'M') {
            if (count_match == ref_exon_array[ref_exon_number]) {
                ref_exon_number++;
                formated_ref_cigar.push(cigar_string.substr(last_pos, hit_position));
                count_match = 0;
                last_pos += hit_position;
                hit_position = 0;
            }
            count_match++;
        }
        hit_position++;
        ref_cigar_count++;
    }

    formated_ref_cigar.push(cigar_string.substr(last_pos, hit_position));
    var i = 0;

    return formated_ref_cigar;

}

function format_pairwise_ref_cigar(ref_exons, ref_cigar, ref_strand) {
    i = 0;
    var no_of_exons = ref_exons.length;

    var ref_exon_array = [];

    while (i < no_of_exons) {
        var length = ref_exons[i].length

        var ref_exon = length
        if (parseInt(length) >= 0) {
            ref_exon_array.push(ref_exon)
        }
        i++;
    }

    var cigar_string = expandCigar(ref_cigar, "true")

    var i = 0
    var total_len = 0;
    var flag = false;
    var cigar_string_match = cigar_string.replace(/D/g, '');

    // if cigar is shorter than CDS than last CDSs becomes 0
    while (i < ref_exon_array.length) {
        if (flag == false) {
            if (parseInt(total_len) + parseInt(ref_exon_array[i]) < cigar_string_match.length) {
                total_len += ref_exon_array[i];
            }
            else {
                ref_exon_array[i] = cigar_string_match.length - total_len;
                total_len = cigar_string_match.length;
                flag = true;
            }
        } else {
            ref_exon_array[i] = 0;
        }
        i++;
    }

    var ref_cigar_count = 0;

    var hit_position = 0;

    var ref_exon_number = 0;
    var count_match = 0;

    var formated_ref_cigar = []

    var last_pos = 0;

    // dividing reference cigar into chunks based on exon length (ignoring deletions)
    while (ref_cigar_count < cigar_string.length) {

        if (cigar_string.charAt(ref_cigar_count) == 'M') {
            if (count_match == ref_exon_array[ref_exon_number]) {
                ref_exon_number++;
                formated_ref_cigar.push(cigar_string.substr(last_pos, hit_position));
                count_match = 0;
                last_pos += hit_position;
                hit_position = 0;
            }
            count_match++;
        }
        hit_position++;
        ref_cigar_count++;
    }
    formated_ref_cigar.push(cigar_string.substr(last_pos, hit_position));
    var i = 0;

    return formated_ref_cigar;

}

/**
 * formats hit cigar to match with reference cigar for drawing on genes
 * @param ref_exons list of reference exons
 * @param hit_cigar hit cigar string
 * @param colours colour array
 * @param ref_cigar reference cigar string
 * @param reverse hit strand is reverse or not
 * @param ref_strand reference strand
 * @returns {string} formated cigar
 */
function formatCigar(ref_exons, hit_cigar, colours, ref_cigar, hit_strand, ref_strand) {
    var no_of_exons = ref_exons.length
    var hit_cigar_arr = [];
    var ref_exon_array = [];
    var last_pos = 0;
    var i = 0
    var j = 0;

    var ref_cigar_array = ref_data.formated_cigar ? ref_data.formated_cigar : format_pairwise_ref_cigar(ref_exons, ref_cigar, ref_strand);

    var cigar_string = expandCigar(ref_cigar, "true")
    hit_cigar = expandCigar(hit_cigar, "true")

    while (i < ref_cigar_array.length) {
        ref_exon_array.push(ref_cigar_array[i].replace(/D/g, "").length)
        // ref_exons[i] = ref_exons[i].length;
        i++;
    }

    var i = 0
    while (i < ref_exons.length) {
        // ref_exon_array.push(ref_cigar_array[i].replace(/D/g, "").length)
        ref_exons[i] = ref_exons[i].length;
        i++;
    }

    if (hit_strand != ref_strand && hit_strand == 1) { //working fine
        cigar_string = cigar_string.split("").reverse().join("");
        hit_cigar = hit_cigar.split("").reverse().join("");
    }
    else if (hit_strand != ref_strand && ref_strand == 1) {//working fine
        cigar_string = cigar_string.split("").reverse().join("");
        hit_cigar = hit_cigar.split("").reverse().join("");
        ref_exons = ref_exons.reverse()
    }
    else if(hit_strand == ref_strand && ref_strand == -1){ //working fine
        cigar_string = cigar_string.split("").reverse().join("");
        hit_cigar = hit_cigar.split("").reverse().join("");

    }

    // if cigar string is D in all sequences (because of subset) that that part get removed
    while (j < cigar_string.length) {
        if (cigar_string.charAt(j) == 'D') {
            if (hit_cigar.charAt(j) == 'M') {
                hit_cigar = replaceAt(hit_cigar, j, "_");
            }
            else if (hit_cigar.charAt(j) == 'D') {
                hit_cigar = replaceAt(hit_cigar, j, "I");
            }
        }
        j++;
    }

    var ref_cigar_count = 0;

    var hit_position = 0;

    var ref_exon_number = 0;
    var count_match = 0;

    var temp_array = [];
    // dividing reference cigar into chunks based on exon length (ignoring deletions)
    while (ref_cigar_count < cigar_string.length) {

        if (cigar_string.charAt(ref_cigar_count) == 'M' || cigar_string.charAt(ref_cigar_count) == '_') {

            if (count_match == ref_exons[ref_exon_number]) {
                ref_exon_number++;
                hit_cigar_arr.push(hit_cigar.substr(last_pos, hit_position));
                temp_array.push(hit_position + " : " + ref_exon_number)

                count_match = 0;
                last_pos += hit_position;
                hit_position = 0;
                if (hit_strand != ref_strand) {
                }

            }
            count_match++;
        }

        hit_position++;
        ref_cigar_count++;
    }
    hit_cigar_arr.push(hit_cigar.substr(last_pos, hit_position));
    if (hit_strand != ref_strand && hit_strand == 1) {
        hit_cigar_arr.reverse()
    }

    return hit_cigar_arr.join("-");

}
<|endoftext|>


<commit_before>/**
 * Base on: https://github.com/vuejs/vue/blob/dev/scripts/verify-commit-msg.js
 */
const chalk = require('chalk')
const msgPath = process.env.GIT_PARAMS
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()

const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: (.{1,50})/

const errMsg =
  `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
  chalk.red(
    `  Proper commit message format is required for automated changelog generation. Examples:\n\n`
  ) +
  `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
  `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` +
  chalk.red(`  See guides/COMMIT_CONVENTION.md for more details.\n`) +
  chalk.red(
    `  You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`
  )

if (!commitRE.test(msg)) {
  console.log()
  console.error(errMsg)
  process.exit(1)
} else {
  if (checkMsgCase(msg) === false) {
    process.exit(1)
  }
}

function checkMsgCase (msg) {
  const startWithUpperCase = s => /^\s*[A-Z]/.test(s)

  const match = msg.match(commitRE)
  const scope = match[3].replace(/^\(|\)$/g, '') || 'scope'
  const subject = match[4]
  const messages = []

  if (startWithUpperCase(scope)) {
    messages.push(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid scope:`)} ${scope}`
    )
  }

  if (startWithUpperCase(subject)) {
    messages.push(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid subject:`)} ${subject}`
    )
  }

  if (messages.length) {
    console.log()
    messages.forEach(msg => console.error(msg))

    console.error(
      chalk.red(
        `\n  Either scope or subject should START with LOWER CASE letters.\n` +
          `  Proper commit message format is required for automated changelog generation. Examples:\n`
      ) +
        `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
        `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n`
    )
    return false
  }

  return true
}
<commit_msg>chore(git-hooks): fix error when scope of commit message undefined<commit_after>/**
 * Base on: https://github.com/vuejs/vue/blob/dev/scripts/verify-commit-msg.js
 */
const chalk = require('chalk')
const msgPath = process.env.GIT_PARAMS
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()

const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: (.{1,50})/

const errMsg =
  `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
  chalk.red(
    `  Proper commit message format is required for automated changelog generation. Examples:\n\n`
  ) +
  `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
  `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` +
  chalk.red(`  See guides/COMMIT_CONVENTION.md for more details.\n`) +
  chalk.red(
    `  You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`
  )

if (!commitRE.test(msg)) {
  console.log()
  console.error(errMsg)
  process.exit(1)
} else {
  if (checkMsgCase(msg) === false) {
    process.exit(1)
  }
}

function checkMsgCase (msg) {
  const startWithUpperCase = s => /^\s*[A-Z]/.test(s)

  const match = msg.match(commitRE)
  const scope = (match[3] || 'scope').replace(/^\(|\)$/g, '')
  const subject = match[4]
  const messages = []

  if (startWithUpperCase(scope)) {
    messages.push(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid scope:`)} ${scope}`
    )
  }

  if (startWithUpperCase(subject)) {
    messages.push(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid subject:`)} ${subject}`
    )
  }

  if (messages.length) {
    console.log()
    messages.forEach(msg => console.error(msg))

    console.error(
      chalk.red(
        `\n  Either scope or subject should START with LOWER CASE letters.\n` +
          `  Proper commit message format is required for automated changelog generation. Examples:\n`
      ) +
        `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
        `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n`
    )
    return false
  }

  return true
}
<|endoftext|>


<commit_before>/**
 * SEE https://github.com/vuejs/vue/blob/dev/scripts/verify-commit-msg.js
 */
const chalk = require('chalk')
const msgPath = process.env.GIT_PARAMS
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()

const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
  console.log()
  console.error(
    `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
      chalk.red(
        `  Proper commit message format is required for automated changelog generation. Examples:\n\n`
      ) +
      `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
      `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` +
      chalk.red(`  See guides/COMMIT_CONVENTION.md for more details.\n`) +
      chalk.red(
        `  You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`
      )
  )
  process.exit(1)
}
<commit_msg>chore(git-hooks): validate scope/subject of commit message<commit_after>/**
 * Base on: https://github.com/vuejs/vue/blob/dev/scripts/verify-commit-msg.js
 */
const chalk = require('chalk')
const msgPath = process.env.GIT_PARAMS
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()

const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types)(\(.+\))?: (.{1,50})/

const errMsg =
  `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
  chalk.red(
    `  Proper commit message format is required for automated changelog generation. Examples:\n\n`
  ) +
  `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
  `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` +
  chalk.red(`  See guides/COMMIT_CONVENTION.md for more details.\n`) +
  chalk.red(
    `  You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`
  )

if (!commitRE.test(msg)) {
  console.log()
  console.error(errMsg)
  process.exit(1)
} else {
  if (checkMsgCase(msg) === false) {
    process.exit(1)
  }
}

function checkMsgCase (msg) {
  const startWithUpperCase = s => /^\s*[A-Z]/.test(s)

  const match = msg.match(commitRE)
  const scope = match[3].replace(/^\(|\)$/g, '') || 'scope'
  const subject = match[4]
  const messages = []

  if (startWithUpperCase(scope)) {
    messages.push(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid scope:`)} ${scope}`
    )
  }

  if (startWithUpperCase(subject)) {
    messages.push(
      `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid subject:`)} ${subject}`
    )
  }

  if (messages.length) {
    console.log()
    messages.forEach(msg => console.error(msg))

    console.error(
      chalk.red(
        `\n  Either scope or subject should START with LOWER CASE letters.\n` +
          `  Proper commit message format is required for automated changelog generation. Examples:\n`
      ) +
        `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
        `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n`
    )
    return false
  }

  return true
}
<|endoftext|>


<commit_before>/**
 * An abstract model class
 *
 * @author Erik E. Lorenz <erik@tuvero.de>
 * @license MIT License
 * @see LICENSE
 */
define(["lib/extend", "core/emitter", "core/type"], function (extend, Emitter, Type) {
  function getClassName(instance) {
    return instance.constructor.toString().replace(
        /^function (\S+)\((.+|\s+)*$/g, "$1");
  }

  /**
   * match the type of data against referenceType. Also allows to match
   * equal-depth multidimensional arrays with a single contained data type
   *
   * @param data
   * @param referenceType
   * @return true if the types match, false otherwise
   */
  function verifyType(data, referenceType) {
    if (Type.isArray(referenceType)) {
      if (Type.isArray(data)) {
        if (referenceType.length === 1) {
          return data.every(function (dataElement) {
            if (verifyType(dataElement, referenceType[0])) {
              return true;
            }

            console.error("restore(): Array element does not match type");
            return false;
          });
        }
        console.error("SAVEFORMAT array does not contain exactly 1 Type!");
      }
      return false;
    }

    if (Type.is(data, referenceType)) {
      return true;
    }

    console.error(getClassName(this) + ".restore(): missing Key of type "
        + referenceType);
    return false;
  }

  /**
   * Constructor for setting an initial state.
   *
   * Please provide additional functions in order to allow state modifications.
   * Use this.emit() to signal state changes to registered views
   */
  function Model() {
    Model.superconstructor.call(this);
  }
  extend(Model, Emitter);

  Model.prototype.clone = function () {
    var clone = new this.constructor();
    if (!clone.restore(this.save())) {
      throw "Cannot clone object " + this;
    }
    return clone;
  };

  /**
   * save the state of this object, so it can later be restored using the
   * restore() function. Subclasses are supposed to call superclass.save()
   * instead of instantiating their own data object.
   *
   * @return a data object
   */
  Model.prototype.save = function () {
    // TODO auto-verify the format
    return {};
  };

  /**
   * restore the state from a saved state, as written by save(); Subclasses are
   * supposed to call superclass.save() before restoring their own state.
   *
   * @param data
   *          a data object as written by save()
   * @return true on success, false or undefined otherwise
   */
  Model.prototype.restore = function (data) {
    // TODO warn about additional keys
    // TODO allow for the verification of sub-Models
    if (!Type.isObject(data)) {
      console.error("restore(): data is not an object");
      return false;
    }

    if (!Type.isObject(this.SAVEFORMAT)) {
      console.error("restore(): SAVEFORMAT is not an object");
      return false;
    }

    return Object.keys(this.SAVEFORMAT).every(function (key) {
      if (verifyType.call(this, data[key], this.SAVEFORMAT[key])) {
        return true;
      }
      console.error("Missing key or wrong format: " + key);
      console.log(data);
      return false;
    }, this);
  };

  Model.prototype.SAVEFORMAT = {};

  return Model;
});
<commit_msg>add Model.clone() function, mostly for debugging<commit_after>/**
 * An abstract model class
 *
 * @author Erik E. Lorenz <erik@tuvero.de>
 * @license MIT License
 * @see LICENSE
 */
define(["lib/extend", "core/emitter", "core/type"], function (extend, Emitter, Type) {
  function getClassName(instance) {
    return instance.constructor.toString().replace(
      /^function (\S+)\((.+|\s+)*$/g, "$1");
  }

  /**
   * match the type of data against referenceType. Also allows to match
   * equal-depth multidimensional arrays with a single contained data type
   *
   * @param data
   * @param referenceType
   * @return true if the types match, false otherwise
   */
  function verifyType(data, referenceType) {
    if (Type.isArray(referenceType)) {
      if (Type.isArray(data)) {
        if (referenceType.length === 1) {
          return data.every(function (dataElement) {
            if (verifyType(dataElement, referenceType[0])) {
              return true;
            }

            console.error("restore(): Array element does not match type");
            return false;
          });
        }
        console.error("SAVEFORMAT array does not contain exactly 1 Type!");
      }
      return false;
    }

    if (Type.is(data, referenceType)) {
      return true;
    }

    console.error(getClassName(this) + ".restore(): missing Key of type " +
      referenceType);
    return false;
  }

  /**
   * Constructor for setting an initial state.
   *
   * Please provide additional functions in order to allow state modifications.
   * Use this.emit() to signal state changes to registered views
   */
  function Model() {
    Model.superconstructor.call(this);
  }
  extend(Model, Emitter);

  Model.prototype.clone = function () {
    var clone = new this.constructor();
    if (!clone.restore(this.save())) {
      throw "Cannot clone object " + this;
    }
    return clone;
  };

  Model.prototype.clone = function (source) {
    this.restore(source.save());
  };

  /**
   * save the state of this object, so it can later be restored using the
   * restore() function. Subclasses are supposed to call superclass.save()
   * instead of instantiating their own data object.
   *
   * @return a data object
   */
  Model.prototype.save = function () {
    // TODO auto-verify the format
    return {};
  };

  /**
   * restore the state from a saved state, as written by save(); Subclasses are
   * supposed to call superclass.save() before restoring their own state.
   *
   * @param data
   *          a data object as written by save()
   * @return true on success, false or undefined otherwise
   */
  Model.prototype.restore = function (data) {
    // TODO warn about additional keys
    // TODO allow for the verification of sub-Models
    if (!Type.isObject(data)) {
      console.error("restore(): data is not an object");
      return false;
    }

    if (!Type.isObject(this.SAVEFORMAT)) {
      console.error("restore(): SAVEFORMAT is not an object");
      return false;
    }

    return Object.keys(this.SAVEFORMAT).every(function (key) {
      if (verifyType.call(this, data[key], this.SAVEFORMAT[key])) {
        return true;
      }
      console.error("Missing key or wrong format: " + key);
      console.log(data);
      return false;
    }, this);
  };

  Model.prototype.SAVEFORMAT = {};

  return Model;
});<|endoftext|>


<commit_before>define(
  ['api-php', 'pouchdb', 'jquery'],
  function (API, Pouch, $) {
    "use strict";
    var data = {
      getModel: function (model, options) {
        var done, fail, jqXHR, fetch, dbType, createDocument, retrieveDocument, doc;

        done = function (data, status, xhr) {
          options.success(data);
        };

        fail = function (xhr, status, error) {
          if (options.error) {
            options.error(model, xhr, options);
          }
        };

        fetch = function () {
          switch (model.get("BICtype")) {
          case "Interaction":
            jqXHR = API.getInteraction(model.get('siteName'), model.get('_id'), model.get('args')).done(done).fail(fail);
            break;
          case "AnswerSpace":
            jqXHR = API.getAnswerSpace(model.get('siteName')).done(done).fail(fail);
            break;
          case "DataSuitcase":
            jqXHR = API.getDataSuitcase(model.get('siteName'), model.get("_id")).done(done).fail(fail);
            break;
          case "Form":
            jqXHR = API.getForm(model.get('siteName'), model.get("_id")).done(done).fail(fail);
            break;
          default:
            options.error(model, null, options);
            jqXHR = null;
            break;
          }
          if (jqXHR) {
            jqXHR.then(function (data, textStatus, jqXHR) {
              options.dfrd.resolve(data, textStatus, jqXHR);
            }, function (jqXHR, textStatus, errorThrown) {
              options.dfrd.reject(jqXHR, textStatus, errorThrown);
            });
          } else {
            options.dfrd.reject(null, '404', 'Invalid Model Type');
            jqXHR = $.Deferred().reject('Invalid Model Type');
          }

        };

        if (window.NativeApp === true && Pouch.adapters.websql) {
          dbType = 'websql://';
        } else {
          if (Pouch.adapters.idb) {
            dbType = 'idb://';
          } else {
            dbType = false;
          }
        }

        createDocument = function (jqXHR, revision) {
          jqXHR.done(function (data, textStatus, jqXHR) {
            var db = new Pouch(dbType + model.get('siteName') +  '-' + model.get('BICtype'), function (err, db) {
              if (!err) {
                var d = new Date();

                if (revision) {
                  data._rev = revision;
                }

                db.put(data, function (err, response) {});
              }
            });
          });
        };

        retrieveDocument = function () {
          var docdfrd = $.Deferred(), db;
          db = new Pouch(dbType + model.get('siteName') +  '-' + model.get('BICtype'), function (err, db) {
            var d = new Date();
            if (err) {
              docdfrd.reject(err);
            } else {
              if (!model.has("_id")) {
                docdfrd.reject();
              } else {
                db.get(model.get('_id'), function (err, doc) {
                  if (err) {
                    docdfrd.reject();
                  } else {
                    if (doc.deviceCacheTime === 0) {
                      docdfrd.resolve(doc);
                    } else if (model.has("args")) {
                      docdfrd.reject('Interaction has arguments', doc);
                    } else if ((d.getTime() - doc.fetchTime) < (doc.deviceCacheTime * 1000)) {
                      docdfrd.resolve(doc);
                    } else {
                      docdfrd.reject('Interaction too old', doc);
                    }
                  }
                });
              }
            }
          });
          return docdfrd.promise();
        };

        if (dbType !== false) {
          retrieveDocument().then(function (doc) {
            options.dfrd.resolve(doc);
            options.success(model, doc, options);
          }, function (err, doc) {
            fetch();

            var revision;
            if (doc) {
              revision = doc._rev;
            }
            createDocument(jqXHR, revision);
          });
        } else {
          fetch();
        }
      }
    };
    return data;
  }
);
<commit_msg>Tidied the code so that all functions are declared together<commit_after>define(
  ['api-php', 'pouchdb', 'jquery'],
  function (API, Pouch, $) {
    "use strict";
    var data = {
      getModel: function (model, options) {
        var done, fail, jqXHR, fetch, dbType, createDocument, retrieveDocument, doc;

        done = function (data, status, xhr) {
          options.success(data);
        };

        fail = function (xhr, status, error) {
          if (options.error) {
            options.error(model, xhr, options);
          }
        };

        fetch = function () {
          switch (model.get("BICtype")) {
          case "Interaction":
            jqXHR = API.getInteraction(model.get('siteName'), model.get('_id'), model.get('args')).done(done).fail(fail);
            break;
          case "AnswerSpace":
            jqXHR = API.getAnswerSpace(model.get('siteName')).done(done).fail(fail);
            break;
          case "DataSuitcase":
            jqXHR = API.getDataSuitcase(model.get('siteName'), model.get("_id")).done(done).fail(fail);
            break;
          case "Form":
            jqXHR = API.getForm(model.get('siteName'), model.get("_id")).done(done).fail(fail);
            break;
          default:
            options.error(model, null, options);
            jqXHR = null;
            break;
          }
          if (jqXHR) {
            jqXHR.then(function (data, textStatus, jqXHR) {
              options.dfrd.resolve(data, textStatus, jqXHR);
            }, function (jqXHR, textStatus, errorThrown) {
              options.dfrd.reject(jqXHR, textStatus, errorThrown);
            });
          } else {
            options.dfrd.reject(null, '404', 'Invalid Model Type');
            jqXHR = $.Deferred().reject('Invalid Model Type');
          }
        };

        createDocument = function (jqXHR, revision) {
          jqXHR.done(function (data, textStatus, jqXHR) {
            var db = new Pouch(dbType + model.get('siteName') +  '-' + model.get('BICtype'), function (err, db) {
              if (!err) {
                var d = new Date();

                if (revision) {
                  data._rev = revision;
                }

                db.put(data, function (err, response) {});
              }
            });
          });
        };

        retrieveDocument = function () {
          var docdfrd = $.Deferred(), db;
          db = new Pouch(dbType + model.get('siteName') +  '-' + model.get('BICtype'), function (err, db) {
            var d = new Date();
            if (err) {
              docdfrd.reject(err);
            } else {
              if (!model.has("_id")) {
                docdfrd.reject();
              } else {
                db.get(model.get('_id'), function (err, doc) {
                  if (err) {
                    docdfrd.reject();
                  } else {
                    if (doc.deviceCacheTime === 0) {
                      docdfrd.resolve(doc);
                    } else if (model.has("args")) {
                      docdfrd.reject('Interaction has arguments', doc);
                    } else if ((d.getTime() - doc.fetchTime) < (doc.deviceCacheTime * 1000)) {
                      docdfrd.resolve(doc);
                    } else {
                      docdfrd.reject('Interaction too old', doc);
                    }
                  }
                });
              }
            }
          });
          return docdfrd.promise();
        };

        if (window.NativeApp === true && Pouch.adapters.websql) {
          dbType = 'websql://';
        } else {
          if (Pouch.adapters.idb) {
            dbType = 'idb://';
          } else {
            dbType = false;
          }
        }

        if (dbType !== false) {
          retrieveDocument().then(function (doc) {
            options.dfrd.resolve(doc);
            options.success(model, doc, options);
          }, function (err, doc) {
            fetch();

            var revision;
            if (doc) {
              revision = doc._rev;
            }
            createDocument(jqXHR, revision);
          });
        } else {
          fetch();
        }
      }
    };
    return data;
  }
);
<|endoftext|>


<commit_before>$(document).ready(
	function() {
		var lang_tools = ace.require('ace/ext/language_tools');

		var day_editor = ace.edit('day-editor');
		day_editor.$blockScrolling = Infinity;
		day_editor.setTheme('ace/theme/twilight');
		day_editor.getSession().setMode('ace/mode/wizard_diary');
		day_editor.setShowInvisibles(true);
		day_editor.setShowPrintMargin(false);

		day_editor.setOptions(
			{
				enableBasicAutocompletion: true,
				enableLiveAutocompletion: true
			}
		);
		lang_tools.addCompleter(
			{
				getCompletions: function(
					editor,
					session,
					position,
					prefix,
					callback
				) {
					var line_prefix =
						session
						.getLine(position.row)
						.substr(0, position.column);
					var line_prefix_parts =
						line_prefix
						.split(',')
						.map(
							function(part) {
								return part.trim();
							}
						);

					var alternatives = [];
					switch (line_prefix_parts.length) {
						case 1:
							alternatives = Object.keys(
								POINT_HIERARCHY.hierarchy
							);

							break;
						case 2:
							var level_1 = line_prefix_parts[0];
							if (
								POINT_HIERARCHY
									.hierarchy
									.hasOwnProperty(level_1)
							) {
								alternatives =
									POINT_HIERARCHY
									.hierarchy[level_1];
							}

							break;
						default:
							if (
								POINT_HIERARCHY
									.hierarchy
									.hasOwnProperty(line_prefix_parts[0])
								&& $.inArray(
									line_prefix_parts[1],
									POINT_HIERARCHY
										.hierarchy[line_prefix_parts[0]]
								) !== -1
							) {
								var tails = Object.keys(POINT_HIERARCHY.tails);
								for (var i = 0; i < tails.length; i++) {
									var tail = tails[i];
									var counter = POINT_HIERARCHY.tails[tail];
									alternatives.push(
										{
											value: tail,
											score: counter
										}
									);
								}
							}
					}
					alternatives = alternatives.map(
						function(alternative) {
							if (typeof alternative === 'string') {
								alternative = {value: alternative};
							}
							alternative.meta = 'global';

							return alternative;
						}
					);

					callback(null, alternatives);
				}
			}
		);

		var day_mobile_editor = $('#day-mobile-editor');
		var previous_mobile_editor_content = day_mobile_editor.val();

		var FormatPoints = function(points, cursor_position) {
			points = points.map(
				function(point, index) {
					if (
						typeof cursor_position == 'undefined'
						|| cursor_position.row != index
					) {
						return point
							.replace(/((?!\s).)\s{2,}(?=\S)/g, '$1 ')
							.replace(/\s+$/, '');
					} else {
						return point;
					}
				}
			);

			while (
				points.length
				&& points[0].trim().length == 0
				&& (typeof cursor_position == 'undefined'
				|| cursor_position.row > 0)
			) {
				points.shift();
				if (typeof cursor_position != 'undefined') {
					cursor_position.row--;
				}
			}

			var new_points = [];
			var previous_point_length = 0;
			for (var i = 0; i < points.length; i++) {
				var point = points[i];
				var point_length = point.trim().length;
				if (
					point_length > 0
					|| previous_point_length > 0
					|| (typeof cursor_position != 'undefined'
					&& cursor_position.row == i)
				) {
					new_points.push(point);
				} else if (typeof cursor_position != 'undefined') {
					if (cursor_position.row > i) {
						cursor_position.row--;
					}
				}

				previous_point_length = point_length;
			}
			points = new_points;

			while (
				points.length
				&& points.slice(-1)[0].trim().length == 0
				&& (typeof cursor_position == 'undefined'
				|| cursor_position.row + 1 < points.length)
			) {
				points.pop();
			}
			if (points.length && points.slice(-1)[0].length != 0) {
				points.push('');
			}

			if (typeof cursor_position != 'undefined') {
				return {
					points: points,
					cursor_position: cursor_position
				};
			} else {
				return points;
			}
		};
		var FormatPointsDescription = function(
			points_description,
			cursor_position
		) {
			var points = points_description.split('\n');
			var result = FormatPoints(points, cursor_position);
			if (typeof cursor_position != 'undefined') {
				points_description = result.points.join('\n');
			} else {
				points_description = result.join('\n');
			}

			return {
				points_description: points_description,
				cursor_position: result.cursor_position
			};
		};
		day_editor.formatContent = function() {
			var points_description = this.getValue();
			var cursor_position = this.getCursorPosition();
			var result = FormatPointsDescription(
				points_description,
				cursor_position
			);

			this.setValue(result.points_description, -1);
			if (typeof result.cursor_position != 'undefined') {
				this.moveCursorToPosition(result.cursor_position);
			}
		};
		day_mobile_editor.formatContent = function() {
			var points_description = this.val();
			var result = FormatPointsDescription(points_description);
			this.val(result.points_description);
			previous_mobile_editor_content = result.points_description;
		};

		var saved_flag_container = $('.saved-flag');
		var saved_flag_icon = $('span', saved_flag_container);
		var is_saved = true;
		var SetSavedFlag = function(saved) {
			is_saved = saved;

			if (saved) {
				saved_flag_container
					.addClass('label-success')
					.removeClass('label-danger');
				saved_flag_icon
					.addClass('glyphicon-floppy-saved')
					.removeClass('glyphicon-floppy-remove');

				var last_symbol = document.title.slice(-1);
				if (last_symbol.length && last_symbol == '*') {
					document.title = document.title.slice(0, -1);
				}
			} else {
				saved_flag_container
					.addClass('label-danger')
					.removeClass('label-success');
				saved_flag_icon
					.addClass('glyphicon-floppy-remove')
					.removeClass('glyphicon-floppy-saved');

				var last_symbol = document.title.slice(-1);
				if (last_symbol.length && last_symbol != '*') {
					document.title += '*';
				}
			}
		};

		var number_of_points_view = $('.number-of-points-view');
		var SetNumberOfPoints = function(points_description) {
			var points =
				points_description
				.split('\n')
				.filter(
					function(line) {
						return line.trim().length != 0;
					}
				);

			var number_of_points = points.length;
			number_of_points_view.text(
				number_of_points.toString()
				+ ' '
				+ GetPointUnit(number_of_points)
			);
		};

		day_editor.on(
			'change',
			function() {
				SetSavedFlag(false);

				var points_description = day_editor.getValue();
				SetNumberOfPoints(points_description);
			}
		);
		day_mobile_editor.on(
			'keyup',
			function() {
				var points_description = day_mobile_editor.val();
				if (points_description == previous_mobile_editor_content) {
					return;
				}

				SetSavedFlag(false);
				SetNumberOfPoints(points_description);

				previous_mobile_editor_content = points_description;
			}
		);
		$('a[data-toggle="tab"]').on(
			'show.bs.tab',
			function(event) {
				var backupped_saved_flag = is_saved;

				var target = $(event.target).attr('href').slice(1);
				switch (target) {
					case 'default':
						var points_description = day_mobile_editor.val();
						day_editor.setValue(points_description, -1);

						break;
					case 'mobile':
						var points_description = day_editor.getValue();
						day_mobile_editor.val(points_description);
						previous_mobile_editor_content = points_description;

						break;
				}

				SetSavedFlag(backupped_saved_flag);
			}
		);

		var save_button = $('.save-day-button');
		var save_url = save_button.data('save-url');
		var processing_animation_image = $('img', save_button);
		var save_icon = $('span', save_button);
		var day_editor_container = $(day_editor.container);
		var GetActiveEditor = function() {
			return $('.tab-pane.active').attr('id');
		};
		var FinishAnimation = function() {
			save_button.prop('disabled', false);
			processing_animation_image.hide();
			save_icon.show();

			var active_editor = GetActiveEditor();
			switch (active_editor) {
				case 'default':
					day_editor_container.removeClass('wait');
					break;
				case 'mobile':
					day_mobile_editor.removeClass('wait');
					break;
			}
		};
		var SaveViaAjax = function(callback) {
			save_button.prop('disabled', true);
			processing_animation_image.show();
			save_icon.hide();

			var points_description = '';
			var active_editor = GetActiveEditor();
			switch (active_editor) {
				case 'default':
					day_editor_container.addClass('wait');
					day_editor.formatContent();
					points_description = day_editor.getValue();

					break;
				case 'mobile':
					day_mobile_editor.addClass('wait');
					day_mobile_editor.formatContent();
					points_description = day_mobile_editor.val();

					break;
			}

			var data = $.extend(
				{'points_description': points_description},
				CSRF_TOKEN
			);
			$.post(
				save_url,
				data,
				function() {
					SetSavedFlag(true);
					FinishAnimation();

					if (typeof callback !== 'undefined') {
						callback();
					}
				}
			).fail(
				function(xhr, text_status) {
					FinishAnimation();
					AjaxErrorDialog.handler(xhr, text_status);
				}
			);
		};
		save_button.click(
			function() {
				SaveViaAjax();
			}
		);
		$(window).keydown(
			function(event) {
				if (
					(event.ctrlKey || event.metaKey)
					&& String.fromCharCode(event.which).toLowerCase() == 's'
				) {
					event.preventDefault();
					SaveViaAjax();
				}
			}
		);

		var close_button = $('.close-button');
		var day_date = close_button.data('date');
		var day_my_date = close_button.data('my-date');
		var view_url = close_button.data('view-url');
		var CloseDayEditor = function() {
			location.href = view_url;
		};
		close_button.click(
			function() {
				if (!is_saved) {
					CloseDialog.show(
						day_my_date,
						day_date,
						function() {
							CloseDialog.hide();
							SaveViaAjax(CloseDayEditor);
						},
						CloseDayEditor
					);
				} else {
					CloseDayEditor();
				}
			}
		);

		$('.day-form').submit(
			function(event) {
				event.preventDefault();
				event.stopPropagation();
				return false;
			}
		);

		var detector = new MobileDetect(navigator.userAgent);
		if (detector.mobile()) {
			$('a[href="#mobile"]').tab('show');
		}
	}
);
<commit_msg>Issue #135: refactoring.<commit_after>$(document).ready(
	function() {
		var lang_tools = ace.require('ace/ext/language_tools');

		var day_editor = ace.edit('day-editor');
		day_editor.$blockScrolling = Infinity;
		day_editor.setTheme('ace/theme/twilight');
		day_editor.getSession().setMode('ace/mode/wizard_diary');
		day_editor.setShowInvisibles(true);
		day_editor.setShowPrintMargin(false);
		day_editor.setOptions(
			{
				enableBasicAutocompletion: true,
				enableLiveAutocompletion: true
			}
		);

		var GetLinePrefixParts = function(session, position) {
			var line_prefix =
				session
				.getLine(position.row)
				.substr(0, position.column);
			var line_prefix_parts =
				line_prefix
				.split(',')
				.map(
					function(part) {
						return part.trim();
					}
				);

			return line_prefix_parts;
		};
		var GetProperty = function(object, property) {
			return object.hasOwnProperty(property) ? object[property] : [];
		};
		var GetAlternatives = function(line_prefix_parts) {
			var alternatives = [];
			switch (line_prefix_parts.length) {
				case 1:
					alternatives = Object.keys(POINT_HIERARCHY.hierarchy);
					break;
				case 2:
					alternatives = GetProperty(
						POINT_HIERARCHY.hierarchy,
						line_prefix_parts[0]
					);

					break;
				default:
					alternatives = $.map(
						POINT_HIERARCHY.tails,
						function(counter, tail) {
							return {value: tail, score: counter};
						}
					);
			}
			alternatives = alternatives.map(
				function(alternative) {
					if (typeof alternative === 'string') {
						alternative = {value: alternative};
					}

					return $.extend(alternative, {meta: 'global'});
				}
			);

			return alternatives;
		};
		lang_tools.addCompleter(
			{
				getCompletions: function(
					editor,
					session,
					position,
					prefix,
					callback
				) {
					var line_prefix_parts = GetLinePrefixParts(
						session,
						position
					);
					var alternatives = GetAlternatives(line_prefix_parts);
					callback(null, alternatives);
				}
			}
		);

		var day_mobile_editor = $('#day-mobile-editor');
		var previous_mobile_editor_content = day_mobile_editor.val();

		var FormatPoints = function(points, cursor_position) {
			points = points.map(
				function(point, index) {
					if (
						typeof cursor_position == 'undefined'
						|| cursor_position.row != index
					) {
						return point
							.replace(/((?!\s).)\s{2,}(?=\S)/g, '$1 ')
							.replace(/\s+$/, '');
					} else {
						return point;
					}
				}
			);

			while (
				points.length
				&& points[0].trim().length == 0
				&& (typeof cursor_position == 'undefined'
				|| cursor_position.row > 0)
			) {
				points.shift();
				if (typeof cursor_position != 'undefined') {
					cursor_position.row--;
				}
			}

			var new_points = [];
			var previous_point_length = 0;
			for (var i = 0; i < points.length; i++) {
				var point = points[i];
				var point_length = point.trim().length;
				if (
					point_length > 0
					|| previous_point_length > 0
					|| (typeof cursor_position != 'undefined'
					&& cursor_position.row == i)
				) {
					new_points.push(point);
				} else if (typeof cursor_position != 'undefined') {
					if (cursor_position.row > i) {
						cursor_position.row--;
					}
				}

				previous_point_length = point_length;
			}
			points = new_points;

			while (
				points.length
				&& points.slice(-1)[0].trim().length == 0
				&& (typeof cursor_position == 'undefined'
				|| cursor_position.row + 1 < points.length)
			) {
				points.pop();
			}
			if (points.length && points.slice(-1)[0].length != 0) {
				points.push('');
			}

			if (typeof cursor_position != 'undefined') {
				return {
					points: points,
					cursor_position: cursor_position
				};
			} else {
				return points;
			}
		};
		var FormatPointsDescription = function(
			points_description,
			cursor_position
		) {
			var points = points_description.split('\n');
			var result = FormatPoints(points, cursor_position);
			if (typeof cursor_position != 'undefined') {
				points_description = result.points.join('\n');
			} else {
				points_description = result.join('\n');
			}

			return {
				points_description: points_description,
				cursor_position: result.cursor_position
			};
		};
		day_editor.formatContent = function() {
			var points_description = this.getValue();
			var cursor_position = this.getCursorPosition();
			var result = FormatPointsDescription(
				points_description,
				cursor_position
			);

			this.setValue(result.points_description, -1);
			if (typeof result.cursor_position != 'undefined') {
				this.moveCursorToPosition(result.cursor_position);
			}
		};
		day_mobile_editor.formatContent = function() {
			var points_description = this.val();
			var result = FormatPointsDescription(points_description);
			this.val(result.points_description);
			previous_mobile_editor_content = result.points_description;
		};

		var saved_flag_container = $('.saved-flag');
		var saved_flag_icon = $('span', saved_flag_container);
		var is_saved = true;
		var SetSavedFlag = function(saved) {
			is_saved = saved;

			if (saved) {
				saved_flag_container
					.addClass('label-success')
					.removeClass('label-danger');
				saved_flag_icon
					.addClass('glyphicon-floppy-saved')
					.removeClass('glyphicon-floppy-remove');

				var last_symbol = document.title.slice(-1);
				if (last_symbol.length && last_symbol == '*') {
					document.title = document.title.slice(0, -1);
				}
			} else {
				saved_flag_container
					.addClass('label-danger')
					.removeClass('label-success');
				saved_flag_icon
					.addClass('glyphicon-floppy-remove')
					.removeClass('glyphicon-floppy-saved');

				var last_symbol = document.title.slice(-1);
				if (last_symbol.length && last_symbol != '*') {
					document.title += '*';
				}
			}
		};

		var number_of_points_view = $('.number-of-points-view');
		var SetNumberOfPoints = function(points_description) {
			var points =
				points_description
				.split('\n')
				.filter(
					function(line) {
						return line.trim().length != 0;
					}
				);

			var number_of_points = points.length;
			number_of_points_view.text(
				number_of_points.toString()
				+ ' '
				+ GetPointUnit(number_of_points)
			);
		};

		day_editor.on(
			'change',
			function() {
				SetSavedFlag(false);

				var points_description = day_editor.getValue();
				SetNumberOfPoints(points_description);
			}
		);
		day_mobile_editor.on(
			'keyup',
			function() {
				var points_description = day_mobile_editor.val();
				if (points_description == previous_mobile_editor_content) {
					return;
				}

				SetSavedFlag(false);
				SetNumberOfPoints(points_description);

				previous_mobile_editor_content = points_description;
			}
		);
		$('a[data-toggle="tab"]').on(
			'show.bs.tab',
			function(event) {
				var backupped_saved_flag = is_saved;

				var target = $(event.target).attr('href').slice(1);
				switch (target) {
					case 'default':
						var points_description = day_mobile_editor.val();
						day_editor.setValue(points_description, -1);

						break;
					case 'mobile':
						var points_description = day_editor.getValue();
						day_mobile_editor.val(points_description);
						previous_mobile_editor_content = points_description;

						break;
				}

				SetSavedFlag(backupped_saved_flag);
			}
		);

		var save_button = $('.save-day-button');
		var save_url = save_button.data('save-url');
		var processing_animation_image = $('img', save_button);
		var save_icon = $('span', save_button);
		var day_editor_container = $(day_editor.container);
		var GetActiveEditor = function() {
			return $('.tab-pane.active').attr('id');
		};
		var FinishAnimation = function() {
			save_button.prop('disabled', false);
			processing_animation_image.hide();
			save_icon.show();

			var active_editor = GetActiveEditor();
			switch (active_editor) {
				case 'default':
					day_editor_container.removeClass('wait');
					break;
				case 'mobile':
					day_mobile_editor.removeClass('wait');
					break;
			}
		};
		var SaveViaAjax = function(callback) {
			save_button.prop('disabled', true);
			processing_animation_image.show();
			save_icon.hide();

			var points_description = '';
			var active_editor = GetActiveEditor();
			switch (active_editor) {
				case 'default':
					day_editor_container.addClass('wait');
					day_editor.formatContent();
					points_description = day_editor.getValue();

					break;
				case 'mobile':
					day_mobile_editor.addClass('wait');
					day_mobile_editor.formatContent();
					points_description = day_mobile_editor.val();

					break;
			}

			var data = $.extend(
				{'points_description': points_description},
				CSRF_TOKEN
			);
			$.post(
				save_url,
				data,
				function() {
					SetSavedFlag(true);
					FinishAnimation();

					if (typeof callback !== 'undefined') {
						callback();
					}
				}
			).fail(
				function(xhr, text_status) {
					FinishAnimation();
					AjaxErrorDialog.handler(xhr, text_status);
				}
			);
		};
		save_button.click(
			function() {
				SaveViaAjax();
			}
		);
		$(window).keydown(
			function(event) {
				if (
					(event.ctrlKey || event.metaKey)
					&& String.fromCharCode(event.which).toLowerCase() == 's'
				) {
					event.preventDefault();
					SaveViaAjax();
				}
			}
		);

		var close_button = $('.close-button');
		var day_date = close_button.data('date');
		var day_my_date = close_button.data('my-date');
		var view_url = close_button.data('view-url');
		var CloseDayEditor = function() {
			location.href = view_url;
		};
		close_button.click(
			function() {
				if (!is_saved) {
					CloseDialog.show(
						day_my_date,
						day_date,
						function() {
							CloseDialog.hide();
							SaveViaAjax(CloseDayEditor);
						},
						CloseDayEditor
					);
				} else {
					CloseDayEditor();
				}
			}
		);

		$('.day-form').submit(
			function(event) {
				event.preventDefault();
				event.stopPropagation();
				return false;
			}
		);

		var detector = new MobileDetect(navigator.userAgent);
		if (detector.mobile()) {
			$('a[href="#mobile"]').tab('show');
		}
	}
);
<|endoftext|>


<commit_before>'use strict';

const jetpack = require('fs-jetpack'); // filesystem
const marked = require('marked'); // markdown parser
const { DOCS, PACKAGES, README, ASSETS, CSS_PATH } = require('./constants.js');

// Stop someone from running the script from it's directory
const ROOT = process.cwd();
if (ROOT === __dirname) {
  throw `Oops! It looks like you might not be running this from the project's
  root directory.`;
}

/**
 * Returns a fs-jetpack file pointer to the packages directory.
 */
function atPackagesDirectory() {
  return jetpack.cwd(ROOT).dir(PACKAGES);
}

function atDocsDirectory() {
  return jetpack.cwd(ROOT).dir(DOCS);
}

/**
 * Returns the text of a README at a specific package
 */
function getReadmeText(pkg) {
  const pkgDir = atPackagesDirectory().dir(pkg);
  const generatedDoc = pkgDir.read('./docs/docs.md');
  if (generatedDoc) pkgDir.write('Readme.md', generatedDoc);
  const text = pkgDir.read(README);
  if (text) return text;
  else return ''; // don't return "undefined"
}

/**
 * From a package directory, get the html from the markdown
 * @return {pkg, html}
 */
function getDocObject(dir, info) {
  const markdown = getReadmeText(dir);
  const html = marked(markdown);
  const cleanedHTML = prepareHTML(html, info);
  return { pkg: dir, html: cleanedHTML };
}

/**
 * Wraps HTML content in a proper  and
 * adds css.
 */
function prepareHTML(html, info) {
  const cssPath = `../doc_styles.css`;
  const nav = info.map(p => {
    return `<li><a href="/static/docs/${p.name.replace('@vx/', 'vx-')}.html">${p.name}</a></li>`;
  });
  return `

      <link rel="stylesheet" type="text/css" href="${cssPath}">

            Home

            Docs

            Guides

            Gallery

          Star

Packages

        ${nav.join('')}

      ${html}

  `;
}

function readDocs() {
  const dirs = atPackagesDirectory().list(); // Get all the pkg directories
  const info = dirs
    .map(d => {
      return atPackagesDirectory()
        .dir(d)
        .read('./package.json');
    })
    .map(JSON.parse);
  const docs = [];
  for (let pkg of dirs) {
    docs.push(getDocObject(pkg, info));
  }

  return docs;
}

function writeDocs(docs) {
  const filePointer = atDocsDirectory();

  docs.forEach(({ pkg, html }, i) => {
    const fileName = `${pkg}.html`;
    filePointer.write(fileName, html);
  });
}

const docs = readDocs();
writeDocs(docs);
<commit_msg>lint(scripts/docs/index)<commit_after>'use strict';

const jetpack = require('fs-jetpack'); // filesystem
const marked = require('marked'); // markdown parser
const { DOCS, PACKAGES, README } = require('./constants.js');

// Stop someone from running the script from it's directory
const ROOT = process.cwd();
if (ROOT === __dirname) {
  throw new Error(`Oops! It looks like you might not be running this from the project's
  root directory.`);
}

/**
 * Returns a fs-jetpack file pointer to the packages directory.
 */
function atPackagesDirectory() {
  return jetpack.cwd(ROOT).dir(PACKAGES);
}

function atDocsDirectory() {
  return jetpack.cwd(ROOT).dir(DOCS);
}

/**
 * Returns the text of a README at a specific package
 */
function getReadmeText(pkg) {
  const pkgDir = atPackagesDirectory().dir(pkg);
  const generatedDoc = pkgDir.read('./docs/docs.md');
  if (generatedDoc) pkgDir.write('Readme.md', generatedDoc);
  const text = pkgDir.read(README);
  if (text) return text;
  return ''; // don't return "undefined"
}

/**
 * From a package directory, get the html from the markdown
 * @return {pkg, html}
 */
function getDocObject(dir, info) {
  const markdown = getReadmeText(dir);
  const html = marked(markdown);
  const cleanedHTML = prepareHTML(html, info);
  return { pkg: dir, html: cleanedHTML };
}

/**
 * Wraps HTML content in a proper  and
 * adds css.
 */
function prepareHTML(html, info) {
  const cssPath = `../doc_styles.css`;
  const nav = info.map(p => {
    return `<li><a href="/static/docs/${p.name.replace('@vx/', 'vx-')}.html">${p.name}</a></li>`;
  });
  return `

      <link rel="stylesheet" type="text/css" href="${cssPath}">

            Home

            Docs

            Guides

            Gallery

          Star

Packages

        ${nav.join('')}

      ${html}

  `;
}

function readDocs() {
  const dirs = atPackagesDirectory().list(); // Get all the pkg directories
  const info = dirs
    .map(d => {
      return atPackagesDirectory()
        .dir(d)
        .read('./package.json');
    })
    .map(JSON.parse);
  const docs = [];
  for (let pkg of dirs) {
    docs.push(getDocObject(pkg, info));
  }

  return docs;
}

function writeDocs(docs) {
  const filePointer = atDocsDirectory();

  docs.forEach(({ pkg, html }) => {
    const fileName = `${pkg}.html`;
    filePointer.write(fileName, html);
  });
}

const docs = readDocs();
writeDocs(docs);
<|endoftext|>


<commit_before>
/**
 * Convert the IANA definitions from CSV to local.
 */

global.Promise = global.Promise || loadBluebird()

var co = require('co')
var getRawBody = require('raw-body')
var cogent = require('cogent')
var parser = require('csv-parse')
var toArray = require('stream-to-array')
var writedb = require('./lib/write-db')

var extensionsRegExp = /^\s*(?:\d\.\s+)?File extension(?:\(s\)|s|)\s?:\s+(?:\*\.|\.|)([0-9a-z_-]+)\s*(?:\(|$)/im
var extensionsQuotedRegExp = /^\s*(?:\d\.\s+)?File extension(?:\(s\)|s|)\s?:(?:[^'"\r\n]+)(?:"\.?([0-9a-z_-]+)"|'\.?([0-9a-z_-]+)')/im
var leadingSpacesRegExp = /^\s+/
var listColonRegExp = /:(?:\s|$)/m
var nameWithNotesRegExp = /^(\S+)(?: - (.*)$| \((.*)\)$|)/
var mimeTypeLineRegExp = /^(?:\s*|[^:\s-]*\s+)(?:MIME type(?: name)?|MIME media type(?: name)?|Media type(?: name)?|Type name)\s?:\s+(.*)$/im
var mimeSubtypeLineRegExp = /^[^:\s-]*\s*(?:MIME |Media )?subtype(?: name)?\s?:\s+(?:[a-z]+ Tree\s+(?:- ?)?|(?:[a-z]+ )+- )?([^([\r\n]*).*$/im
var mimeSubtypesLineRegExp = /^[^:\s-]*\s*(?:MIME |Media )?subtype(?: names)?\s?:\s+(?:[a-z]+ Tree\s+(?:- ?)?)?(.*)$/im
var rfcReferenceRegExp = /\[(RFC[0-9]{4})]/gi
var slurpModeRegExp = /^\s{0,3}(?:[1-4]\. )?[a-z]{4,}(?: [a-z]{4,})+(?:s|\(s\))?\s*:\s*/i
var symbolRegExp = /[._-]/g
var trimQuotesRegExp = /^"|"$/gm
var urlReferenceRegExp = /\[(https?:\/\/[^\]]+)]/gi

co(function * () {
  var gens = yield [
    get('application'),
    get('audio'),
    get('font', { extensions: true }),
    get('image'),
    get('message', { extensions: true }),
    get('model'),
    get('multipart'),
    get('text'),
    get('video')
  ]

  // flatten generators
  gens = gens.reduce(concat, [])

  // get results in groups
  var results = []
  while (gens.length !== 0) {
    results.push(yield gens.splice(0, 10))
  }

  // flatten results
  results = results.reduce(concat, [])

  var json = Object.create(null)
  results.forEach(function (result) {
    var mime = result.mime

    if (mime in json) {
      throw new Error('duplicate entry for ' + mime)
    }

    json[mime] = {
      extensions: result.extensions,
      notes: result.notes,
      sources: result.sources
    }
  })

  writedb('src/iana-types.json', json)
}).then()

function addTemplateData (data, options) {
  var opts = options || {}

  if (!data.template) {
    return data
  }

  return function * get () {
    var res = yield * cogent('http://www.iana.org/assignments/media-types/' + data.template, { retries: 3 })
    var ref = data.type + '/' + data.name
    var rfc = getRfcReferences(data.reference)[0]

    if (res.statusCode === 404 && data.template !== ref) {
      console.log('template ' + data.template + ' not found, retry as ' + ref)
      data.template = ref
      res = yield * cogent('http://www.iana.org/assignments/media-types/' + ref, { retries: 3 })

      // replace the guessed mime
      if (res.statusCode === 200) {
        data.mime = data.template
      }
    }

    if (res.statusCode === 404 && rfc !== undefined) {
      console.log('template ' + data.template + ' not found, fetch ' + rfc)
      res = yield * cogent('http://tools.ietf.org/rfc/' + rfc.toLowerCase() + '.txt')
    }

    if (res.statusCode === 404) {
      console.log('template ' + data.template + ' not found')
      return data
    }

    if (res.statusCode !== 200) {
      throw new Error('got status code ' + res.statusCode + ' from template ' + data.template)
    }

    var body = yield getTemplateBody(res)
    var href = res.urls[0].href
    var mime = extractTemplateMime(body)

    // add the template as a source
    addSource(data, href)

    if (mimeEql(mime, data.mime)) {
      // use extracted mime
      data.mime = mime

      // use extracted extensions
      if (opts.extensions) {
        data.extensions = extractTemplateExtensions(body)
      }
    }

    return data
  }
}

function extractTemplateMime (body) {
  var type = mimeTypeLineRegExp.exec(body)
  var subtype = mimeSubtypeLineRegExp.exec(body)

  if (!subtype && (subtype = mimeSubtypesLineRegExp.exec(body)) && !/^[A-Za-z0-9]+$/.test(subtype[1])) {
    return
  }

  if (!type || !subtype) {
    return
  }

  type = type[1].trim().replace(trimQuotesRegExp, '')
  subtype = subtype[1].trim().replace(trimQuotesRegExp, '')

  if (!subtype) {
    return
  }

  if (subtype.substr(0, type.length + 1) === type + '/') {
    // strip type from subtype
    subtype = subtype.substr(type.length + 1)
  }

  return (type + '/' + subtype).toLowerCase()
}

function extractTemplateExtensions (body) {
  var match = extensionsRegExp.exec(body) || extensionsQuotedRegExp.exec(body)

  if (!match) {
    return
  }

  var ext = (match[1] || match[2]).toLowerCase()

  if (ext !== 'none' && ext !== 'undefined') {
    return [ext]
  }
}

function * get (type, options) {
  var res = yield * cogent('http://www.iana.org/assignments/media-types/' + encodeURIComponent(type) + '.csv', { retries: 3 })

  if (res.statusCode !== 200) {
    throw new Error('got status code ' + res.statusCode + ' from ' + type)
  }

  var mimes = yield toArray(res.pipe(parser()))
  var headers = mimes.shift().map(normalizeHeader)
  var reduceRows = generateRowMapper(headers)
  var templates = Object.create(null)

  return mimes.map(function (row) {
    var data = row.reduce(reduceRows, {type: type})

    if (data.template) {
      if (data.template === type + '/example') {
        return
      }

      if (templates[data.template]) {
        // duplicate entry
        return
      }

      templates[data.template] = true
    }

    // guess mime type
    data.mime = (data.template || (type + '/' + data.name)).toLowerCase()

    // extract notes from name
    var nameMatch = nameWithNotesRegExp.exec(data.name)
    data.name = nameMatch[1]
    data.notes = nameMatch[2] || nameMatch[3]

    // add reference sources
    parseReferences(data.reference).forEach(function (url) {
      addSource(data, url)
    })

    return addTemplateData(data, options)
  })
}

function * getTemplateBody (res) {
  var body = yield getRawBody(res, {encoding: 'ascii'})
  var lines = body.split(/\r?\n/)
  var slurp = false

  return lines.reduce(function (lines, line) {
    line = line.replace(/=20$/, ' ')

    var prev = (lines[lines.length - 1] || '')
    var match = leadingSpacesRegExp.exec(line)

    if (slurp && line.trim().length === 0 && !/:\s*$/.test(prev)) {
      slurp = false
      return lines
    }

    if (slurpModeRegExp.test(line)) {
      slurp = false
      lines.push(line)
    } else if (slurp) {
      lines[lines.length - 1] = appendToLine(prev, line)
    } else if (match && match[0].length >= 3 && match[0].trim() !== 0 && prev.trim().length !== 0 && !listColonRegExp.test(line)) {
      lines[lines.length - 1] = appendToLine(prev, line)
    } else {
      lines.push(line)
    }

    // turn on slurp mode
    slurp = slurp || slurpModeRegExp.test(line)

    return lines
  }, []).join('\n')
}

function addSource (data, url) {
  var sources = data.sources || (data.sources = [])

  if (sources.indexOf(url) === -1) {
    sources.push(url)
  }
}

function appendToLine (line, str) {
  var trimmed = line.trimRight()
  var append = trimmed.substr(-1) === '-'
    ? str.trimLeft()
    : ' ' + str.trimLeft()
  return trimmed + append
}

function concat (a, b) {
  return a.concat(b.filter(Boolean))
}

function generateRowMapper (headers) {
  return function reduceRows (obj, val, index) {
    if (val !== '') {
      obj[headers[index]] = val
    }

    return obj
  }
}

function getRfcReferences (reference) {
  var match = null
  var rfcs = []

  rfcReferenceRegExp.index = 0

  while ((match = rfcReferenceRegExp.exec(reference))) {
    rfcs.push(match[1].toUpperCase())
  }

  return rfcs
}

function getUrlReferences (reference) {
  var match = null
  var urls = []

  urlReferenceRegExp.index = 0

  while ((match = urlReferenceRegExp.exec(reference))) {
    urls.push(match[1])
  }

  return urls
}

function loadBluebird () {
  var Promise = require('bluebird')

  // Silence all warnings
  Promise.config({
    warnings: false
  })

  return Promise
}

function mimeEql (mime1, mime2) {
  return mime1 && mime2 &&
    mime1.replace(symbolRegExp, '-') === mime2.replace(symbolRegExp, '-')
}

function normalizeHeader (val) {
  return val.substr(0, 1).toLowerCase() + val.substr(1).replace(/ (.)/, function (s, c) {
    return c.toUpperCase()
  })
}

function parseReferences (reference) {
  return getUrlReferences(reference).concat(getRfcReferences(reference).map(function (rfc) {
    return 'http://tools.ietf.org/rfc/' + rfc.toLowerCase() + '.txt'
  }))
}
<commit_msg>build: only pull in iana extensions for common usage<commit_after>
/**
 * Convert the IANA definitions from CSV to local.
 */

global.Promise = global.Promise || loadBluebird()

var co = require('co')
var getRawBody = require('raw-body')
var cogent = require('cogent')
var parser = require('csv-parse')
var toArray = require('stream-to-array')
var writedb = require('./lib/write-db')

var extensionsRegExp = /^\s*(?:\d\.\s+)?File extension(?:\(s\)|s|)\s?:\s+(?:\*\.|\.|)([0-9a-z_-]+)\s*(?:\(|$)/im
var extensionsQuotedRegExp = /^\s*(?:\d\.\s+)?File extension(?:\(s\)|s|)\s?:(?:[^'"\r\n]+)(?:"\.?([0-9a-z_-]+)"|'\.?([0-9a-z_-]+)')/im
var intendedUsageRegExp = /^\s*(?:(?:\d{1,2}\.|o)\s+)?Intended\s+Usage\s*:\s*(\S+)/im
var leadingSpacesRegExp = /^\s+/
var listColonRegExp = /:(?:\s|$)/m
var nameWithNotesRegExp = /^(\S+)(?: - (.*)$| \((.*)\)$|)/
var mimeTypeLineRegExp = /^(?:\s*|[^:\s-]*\s+)(?:MIME type(?: name)?|MIME media type(?: name)?|Media type(?: name)?|Type name)\s?:\s+(.*)$/im
var mimeSubtypeLineRegExp = /^[^:\s-]*\s*(?:MIME |Media )?subtype(?: name)?\s?:\s+(?:[a-z]+ Tree\s+(?:- ?)?|(?:[a-z]+ )+- )?([^([\r\n]*).*$/im
var mimeSubtypesLineRegExp = /^[^:\s-]*\s*(?:MIME |Media )?subtype(?: names)?\s?:\s+(?:[a-z]+ Tree\s+(?:- ?)?)?(.*)$/im
var rfcReferenceRegExp = /\[(RFC[0-9]{4})]/gi
var slurpModeRegExp = /^\s{0,3}(?:[1-4]\. )?[a-z]{4,}(?: [a-z]{4,})+(?:s|\(s\))?\s*:\s*/i
var symbolRegExp = /[._-]/g
var trimQuotesRegExp = /^"|"$/gm
var urlReferenceRegExp = /\[(https?:\/\/[^\]]+)]/gi

co(function * () {
  var gens = yield [
    get('application'),
    get('audio'),
    get('font', { extensions: true }),
    get('image'),
    get('message', { extensions: true }),
    get('model'),
    get('multipart'),
    get('text'),
    get('video')
  ]

  // flatten generators
  gens = gens.reduce(concat, [])

  // get results in groups
  var results = []
  while (gens.length !== 0) {
    results.push(yield gens.splice(0, 10))
  }

  // flatten results
  results = results.reduce(concat, [])

  var json = Object.create(null)
  results.forEach(function (result) {
    var mime = result.mime

    if (mime in json) {
      throw new Error('duplicate entry for ' + mime)
    }

    json[mime] = {
      extensions: result.extensions,
      notes: result.notes,
      sources: result.sources
    }
  })

  writedb('src/iana-types.json', json)
}).then()

function addTemplateData (data, options) {
  var opts = options || {}

  if (!data.template) {
    return data
  }

  return function * get () {
    var res = yield * cogent('http://www.iana.org/assignments/media-types/' + data.template, { retries: 3 })
    var ref = data.type + '/' + data.name
    var rfc = getRfcReferences(data.reference)[0]

    if (res.statusCode === 404 && data.template !== ref) {
      console.log('template ' + data.template + ' not found, retry as ' + ref)
      data.template = ref
      res = yield * cogent('http://www.iana.org/assignments/media-types/' + ref, { retries: 3 })

      // replace the guessed mime
      if (res.statusCode === 200) {
        data.mime = data.template
      }
    }

    if (res.statusCode === 404 && rfc !== undefined) {
      console.log('template ' + data.template + ' not found, fetch ' + rfc)
      res = yield * cogent('http://tools.ietf.org/rfc/' + rfc.toLowerCase() + '.txt')
    }

    if (res.statusCode === 404) {
      console.log('template ' + data.template + ' not found')
      return data
    }

    if (res.statusCode !== 200) {
      throw new Error('got status code ' + res.statusCode + ' from template ' + data.template)
    }

    var body = yield getTemplateBody(res)
    var href = res.urls[0].href
    var mime = extractTemplateMime(body)

    // add the template as a source
    addSource(data, href)

    if (mimeEql(mime, data.mime)) {
      // use extracted mime
      data.mime = mime

      // use extracted extensions
      if (opts.extensions && extractIntendedUsage(body) === 'common') {
        data.extensions = extractTemplateExtensions(body)
      }
    }

    return data
  }
}

function extractIntendedUsage (body) {
  var match = intendedUsageRegExp.exec(body)

  return match
    ? match[1].toLocaleLowerCase()
    : undefined
}

function extractTemplateMime (body) {
  var type = mimeTypeLineRegExp.exec(body)
  var subtype = mimeSubtypeLineRegExp.exec(body)

  if (!subtype && (subtype = mimeSubtypesLineRegExp.exec(body)) && !/^[A-Za-z0-9]+$/.test(subtype[1])) {
    return
  }

  if (!type || !subtype) {
    return
  }

  type = type[1].trim().replace(trimQuotesRegExp, '')
  subtype = subtype[1].trim().replace(trimQuotesRegExp, '')

  if (!subtype) {
    return
  }

  if (subtype.substr(0, type.length + 1) === type + '/') {
    // strip type from subtype
    subtype = subtype.substr(type.length + 1)
  }

  return (type + '/' + subtype).toLowerCase()
}

function extractTemplateExtensions (body) {
  var match = extensionsRegExp.exec(body) || extensionsQuotedRegExp.exec(body)

  if (!match) {
    return
  }

  var ext = (match[1] || match[2]).toLowerCase()

  if (ext !== 'none' && ext !== 'undefined') {
    return [ext]
  }
}

function * get (type, options) {
  var res = yield * cogent('http://www.iana.org/assignments/media-types/' + encodeURIComponent(type) + '.csv', { retries: 3 })

  if (res.statusCode !== 200) {
    throw new Error('got status code ' + res.statusCode + ' from ' + type)
  }

  var mimes = yield toArray(res.pipe(parser()))
  var headers = mimes.shift().map(normalizeHeader)
  var reduceRows = generateRowMapper(headers)
  var templates = Object.create(null)

  return mimes.map(function (row) {
    var data = row.reduce(reduceRows, {type: type})

    if (data.template) {
      if (data.template === type + '/example') {
        return
      }

      if (templates[data.template]) {
        // duplicate entry
        return
      }

      templates[data.template] = true
    }

    // guess mime type
    data.mime = (data.template || (type + '/' + data.name)).toLowerCase()

    // extract notes from name
    var nameMatch = nameWithNotesRegExp.exec(data.name)
    data.name = nameMatch[1]
    data.notes = nameMatch[2] || nameMatch[3]

    // add reference sources
    parseReferences(data.reference).forEach(function (url) {
      addSource(data, url)
    })

    return addTemplateData(data, options)
  })
}

function * getTemplateBody (res) {
  var body = yield getRawBody(res, {encoding: 'ascii'})
  var lines = body.split(/\r?\n/)
  var slurp = false

  return lines.reduce(function (lines, line) {
    line = line.replace(/=20$/, ' ')

    var prev = (lines[lines.length - 1] || '')
    var match = leadingSpacesRegExp.exec(line)

    if (slurp && line.trim().length === 0 && !/:\s*$/.test(prev)) {
      slurp = false
      return lines
    }

    if (slurpModeRegExp.test(line)) {
      slurp = false
      lines.push(line)
    } else if (slurp) {
      lines[lines.length - 1] = appendToLine(prev, line)
    } else if (match && match[0].length >= 3 && match[0].trim() !== 0 && prev.trim().length !== 0 && !listColonRegExp.test(line)) {
      lines[lines.length - 1] = appendToLine(prev, line)
    } else {
      lines.push(line)
    }

    // turn on slurp mode
    slurp = slurp || slurpModeRegExp.test(line)

    return lines
  }, []).join('\n')
}

function addSource (data, url) {
  var sources = data.sources || (data.sources = [])

  if (sources.indexOf(url) === -1) {
    sources.push(url)
  }
}

function appendToLine (line, str) {
  var trimmed = line.trimRight()
  var append = trimmed.substr(-1) === '-'
    ? str.trimLeft()
    : ' ' + str.trimLeft()
  return trimmed + append
}

function concat (a, b) {
  return a.concat(b.filter(Boolean))
}

function generateRowMapper (headers) {
  return function reduceRows (obj, val, index) {
    if (val !== '') {
      obj[headers[index]] = val
    }

    return obj
  }
}

function getRfcReferences (reference) {
  var match = null
  var rfcs = []

  rfcReferenceRegExp.index = 0

  while ((match = rfcReferenceRegExp.exec(reference))) {
    rfcs.push(match[1].toUpperCase())
  }

  return rfcs
}

function getUrlReferences (reference) {
  var match = null
  var urls = []

  urlReferenceRegExp.index = 0

  while ((match = urlReferenceRegExp.exec(reference))) {
    urls.push(match[1])
  }

  return urls
}

function loadBluebird () {
  var Promise = require('bluebird')

  // Silence all warnings
  Promise.config({
    warnings: false
  })

  return Promise
}

function mimeEql (mime1, mime2) {
  return mime1 && mime2 &&
    mime1.replace(symbolRegExp, '-') === mime2.replace(symbolRegExp, '-')
}

function normalizeHeader (val) {
  return val.substr(0, 1).toLowerCase() + val.substr(1).replace(/ (.)/, function (s, c) {
    return c.toUpperCase()
  })
}

function parseReferences (reference) {
  return getUrlReferences(reference).concat(getRfcReferences(reference).map(function (rfc) {
    return 'http://tools.ietf.org/rfc/' + rfc.toLowerCase() + '.txt'
  }))
}
<|endoftext|>


<commit_before>var events = require('../apis/github/users/carolinapowers/events.json');
var assert = require('assert');
console.log(Date());

    it('should tell us that events has an input', function (){
        assert(events);
    });

function answer(){
    var pushEvents = events.filter(function(item){
        return item.type == 'PushEvent';
    });

    return {
        'total': events.length,
        'PushEvent': {
            'total': pushEvents.length,
        }
    };
}

console.log(answer());
console.log(events.length);
var theAnswer = answer();

    it('should return that answer exists, and test for total events', function(){
        assert(answer);
        assert(events.length === 30);
    });

    it('should return the length when calling answer function', function(){
       assert.equal(theAnswer.total, 30);
    });

    it('should have "PushEvent", and has a total count of PushEvents', function(){
        assert(theAnswer.PushEvent);
        assert(theAnswer.PushEvent.total);
    });
<commit_msg>last commit in gymnastics before merging<commit_after>var events = require('../apis/github/users/carolinapowers/events.json');
var assert = require('assert');
console.log(Date());

    it('should tell us that events has an input', function (){
        assert(events);
    });

function answer(){
    var pushEvents = events.filter(function(item){
        return item.type == 'PushEvent';
    });

    return {
        'total': events.length,
        'PushEvent': {
            'total': pushEvents.length,
        }

    };
}

console.log(answer());
console.log(events.length);
var theAnswer = answer();

    it('should return that answer exists, and test for total events', function(){
        assert(answer);
        assert(events.length === 30);
    });

    it('should return the length when calling answer function', function(){
       assert.equal(theAnswer.total, 30);
    });

    it('should have "PushEvent", and has a total count of PushEvents', function(){
        assert(theAnswer.PushEvent);
        assert(theAnswer.PushEvent.total);
    });
<|endoftext|>