utility.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Common functions
  2. function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
  3. function minValue(val, min) { return val >= min ? val : min; }
  4. function maxValue(val, max) { return val <= max ? val : max; }
  5. function getRandomElem(array) { return array[Math.floor(Math.random()*array.length)]; }
  6. function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); }
  7. function differenceBetween(x, y) { return x > y ? x - y : y - x; }
  8. function toCommaSeperatedList(array){
  9. if (array.length >= 3) return array.slice(0, array.length - 1).join(', ') + ", and " + array.slice(-1);
  10. else if (array.length === 2) return array[0] + " and " + array[1];
  11. else return array[0];
  12. }
  13. //accepts any value like '#ffffff', 'rgba(255,255,255,1)', 'hsl(0,100%,100%)', or 'white'
  14. function toRGBA(c) {
  15. var can = document.createElement('canvasdummy'),
  16. ctx = can.getContext('2d');
  17. can.width = can.height = 1;
  18. ctx.fillStyle = c;
  19. ctx.fillRect(0, 0, 1, 1); //paint the canvas
  20. var img = ctx.getImageData(0, 0, 1, 1),
  21. data = img.data,
  22. rgba = {
  23. r: data[0], //0-255 red
  24. g: data[1], //0-255 green
  25. b: data[2], //0-255 blue
  26. a: data[3] //0-255 opacity (0 being transparent, 255 being opaque)
  27. };
  28. return rgba;
  29. }
  30. function isCyclic(obj) {
  31. var keys = [];
  32. var stack = [];
  33. var stackSet = new Set();
  34. var detected = false;
  35. function detect(obj, key) {
  36. if (typeof obj != 'object') { return; }
  37. if (stackSet.has(obj)) { // it's cyclic! Print the object and its locations.
  38. var oldindex = stack.indexOf(obj);
  39. var l1 = keys.join('.') + '.' + key;
  40. var l2 = keys.slice(0, oldindex + 1).join('.');
  41. console.log('CIRCULAR: ' + l1 + ' = ' + l2 + ' = ' + obj);
  42. console.log(obj);
  43. detected = true;
  44. return;
  45. }
  46. keys.push(key);
  47. stack.push(obj);
  48. stackSet.add(obj);
  49. for (var k in obj) { //dive on the object's children
  50. if (obj.hasOwnProperty(k)) { detect(obj[k], k); }
  51. }
  52. keys.pop();
  53. stack.pop();
  54. stackSet.delete(obj);
  55. return;
  56. }
  57. detect(obj, 'obj');
  58. return detected;
  59. }
  60. function Enum(members){
  61. if(members.length>32){
  62. throw(new RangeError("Enum must have no more than 32 members"));
  63. }
  64. var definitions = {};
  65. var lookup={};
  66. for(var i in members){
  67. definitions[members[i]]=2**i;
  68. lookup[2**i]=members[i];
  69. }
  70. Object.freeze(definitions);
  71. Object.freeze(lookup);
  72. var values = Object.values(definitions);
  73. var keys = Object.keys(definitions);
  74. Object.freeze(values);
  75. Object.freeze(keys);
  76. var ret={};
  77. for(let d in definitions){
  78. Object.defineProperty(ret,d,{get:function(){return definitions[d];}});
  79. }
  80. ret.lookup = function(val){
  81. let ret = [];
  82. for(let l in lookup){
  83. if(val&l){
  84. ret.push(lookup[l]);
  85. }
  86. }
  87. return ret.join("|");
  88. }
  89. ret.definition = definitions;
  90. ret.keys=keys;
  91. ret.values=values;
  92. ret=Object.freeze(ret);
  93. return ret;
  94. }