You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.2 KiB

  1. Array.prototype.clean = function(deleteValue) {
  2. for (var i = 0; i < this.length; i++) {
  3. if (this[i] == deleteValue) {
  4. this.splice(i, 1);
  5. i--;
  6. }
  7. }
  8. return this;
  9. };
  10. /*
  11. Array.findRanges
  12. Turns this:
  13. ["a","a","a","b","b","c","c","c","c","c","a","a","c"]
  14. into this:
  15. {
  16. "a":[
  17. {
  18. "from":0,
  19. "to":2
  20. },
  21. {
  22. "from":10,
  23. "to":11
  24. }
  25. ],
  26. "b":[
  27. {
  28. "from":3,
  29. "to":4
  30. }
  31. ],
  32. "c":[
  33. {
  34. "from":5,
  35. "to":9
  36. },
  37. {
  38. "from":12,
  39. "to":12
  40. }
  41. ]
  42. }
  43. */
  44. Array.prototype.findRanges = function() {
  45. var buckets = {};
  46. for(var i = 0; i < this.length; i++) {
  47. if(!(this[i] in buckets)) {
  48. buckets[this[i]] = [{
  49. from: i,
  50. to: i
  51. }]
  52. } else {
  53. var last = buckets[this[i]][ buckets[this[i]].length-1 ];
  54. if(i == last.to + 1) {
  55. last.to = i;
  56. } else {
  57. buckets[this[i]].push({
  58. from: i,
  59. to: i
  60. })
  61. }
  62. }
  63. }
  64. return buckets;
  65. };
  66. Object.values = function(obj){ return Object.keys(obj).map(function(key){return obj[key]}) };