<commit_before>// shape.js
//
// much-needed abstraction for a certain shape
//
function Shape(thickness, r,g,b,a, points, id) {
  // Build a new shape
  this.t = thickness;
  this.r = r;
  this.g = g;
  this.b = b;
  this.a = a;

  this.p = points;

  this.id = id; // I REALLY don't like putting this here.
}
// we serialize our points to something similar to base64
// major changes: we use _ instead of / and - instead of + as the last
// characters (it's being sent in a query string)
var b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
function numToB64(num) {
  // return a 3-digit base64 number that represents num
  num = num + 131072; // this bias is 64**3 / 2 and allows us to represent negative numbers
  var result = "";
  for (var i = 0; i < 3; i++) {
    result = b64[ num%64 ] + result;
    num = parseInt(num / 64,10);
  }
  return result;
}
function b64ToNum(b) {
  var result = 0;
  for (var i=0,l=b.length; i<l; i++) {
    var chr = b64.indexOf(b[i]);
    if (chr == -1) {
      throw new Error("contained a char that wasn't our base64");
    }
    result *= 64;
    result += chr;
  }
  return result - 131072;
}
function b64ToPoints(b) {
  if ((b.length % 6) !== 0) {
    throw new Error("invalid point length");
  }
  var points = [];
  for (var i=0,l=b.length; i<l; i+=6) {
    var x = b64ToNum(b.substring(i,   i+3)),
        y = b64ToNum(b.substring(i+3, i+6));
      points.push( [x,y] );
  }
  return points;
}
Shape.fromParams = function(points, t, r,g,b,a){
  try {
    // Returns a new shape with given points (p), thickness t, color rgba.
    t = t? parseFloat(t):3;
    r = r? parseFloat(r):0;
    g = g? parseFloat(g):0;
    b = b? parseFloat(b):0;
    a = a? parseFloat(a):1;
    // now verify points
    points = b64ToPoints(points);
    if (points.length===0) {
      throw new Error("No points");
    } else {
      return new Shape(t, r,g,b,a, points, 0);
    }
  } catch (e) {
    console.log(e.stack); // wth?
    return false;
  }
};
Shape.fromJSON = function(data){
  // this json is assumed to contain normal points (e.g. it's saved on disk)
  return new Shape(data.t,  data.r,data.g,data.b,data.a, data.p, data.id||0);
};

Shape.prototype.serializePoints = function(){
  var result = "";
  for (var i=0,l=this.p.length; i<l; i++) {
    result += numToB64(this.p[i][0])+numToB64(this.p[i][1]);
  }
  return result;
};

Shape.prototype.toJSON = function(){
  return {
    t: this.t,
    r: this.r,
    g: this.g,
    b: this.b,
    a: this.a,
    p: this.p,
    id: this.id
  };
};
Shape.prototype.toJSONWithSerializedPoints = function(){<commit_msg>bugfix: always import shapes as integers<commit_after>// shape.js
//
// much-needed abstraction for a certain shape
//
function Shape(thickness, r,g,b,a, points, id) {
  // Build a new shape
  this.t = thickness;
  this.r = r;
  this.g = g;
  this.b = b;
  this.a = a;

  this.p = points;

  this.id = id; // I REALLY don't like putting this here.
}
// we serialize our points to something similar to base64
// major changes: we use _ instead of / and - instead of + as the last
// characters (it's being sent in a query string)
var b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
function numToB64(num) {
  // return a 3-digit base64 number that represents num
  num = num + 131072; // this bias is 64**3 / 2 and allows us to represent negative numbers
  var result = "";
  for (var i = 0; i < 3; i++) {
    result = b64[ num%64 ] + result;
    num = parseInt(num / 64,10);
  }
  return result;
}
function b64ToNum(b) {
  var result = 0;
  for (var i=0,l=b.length; i<l; i++) {
    var chr = b64.indexOf(b[i]);
    if (chr == -1) {
      throw new Error("contained a char that wasn't our base64");
    }
    result *= 64;
    result += chr;
  }
  return result - 131072;
}
function b64ToPoints(b) {
  if ((b.length % 6) !== 0) {
    throw new Error("invalid point length");
  }
  var points = [];
  for (var i=0,l=b.length; i<l; i+=6) {
    var x = b64ToNum(b.substring(i,   i+3)),
        y = b64ToNum(b.substring(i+3, i+6));
      points.push( [x,y] );
  }
  return points;
}
Shape.fromParams = function(points, t, r,g,b,a){
  try {
    // Returns a new shape with given points (p), thickness t, color rgba.
    t = t? parseFloat(t):3;
    r = r? parseFloat(r):0;
    g = g? parseFloat(g):0;
    b = b? parseFloat(b):0;
    a = a? parseFloat(a):1;
    // now verify points
    points = b64ToPoints(points);
    if (points.length===0) {
      throw new Error("No points");
    } else {
      return new Shape(t, r,g,b,a, points, 0);
    }
  } catch (e) {
    console.log(e.stack); // wth?
    return false;
  }
};
Shape.fromJSON = function(data){
  // this json is assumed to contain normal points (e.g. it's saved on disk)
  return new Shape(data.t,  data.r,data.g,data.b,data.a,
    // sometimes points are stored as floats. boooo.
    data.p.map(function(point){ return [parseInt(point[0],10), parseInt(point[1],10)]; }),
    data.id||0);
};

Shape.prototype.serializePoints = function(){
  var result = "";
  for (var i=0,l=this.p.length; i<l; i++) {
    result += numToB64(this.p[i][0])+numToB64(this.p[i][1]);
  }
  return result;
};

Shape.prototype.toJSON = function(){
  return {
    t: this.t,
    r: this.r,
    g: this.g,
    b: this.b,
    a: this.a,
    p: this.p,
    id: this.id
  };
};
Shape.prototype.toJSONWithSerializedPoints = function(){
{'fraction_non_alphanumeric': 0.16367339381596, 'fraction_numerical': 0.03295685984077023, 'mean_word_length': 2.7908771929824563, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '3255006', 'n_tokens_mistral': 2184, 'n_tokens_neox': 2048, 'n_words': 578}