Javaacript Utility functions


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Number.max = function (a,b) {
  2. return a<b?b:a;
  3. }
  4.  
  5. Number.min = function (a,b) {
  6. return a>b?b:a;
  7. }
  8.  
  9. Math.mod = function(val,mod) {
  10. if (val < 0) {
  11. while(val<0) val += mod;
  12. return val;
  13. } else {
  14. return val%mod;
  15. }
  16. }
  17.  
  18. window.getInnerWidth = function() {
  19. if (window.innerWidth) {
  20. return window.innerWidth;
  21. } else if (document.body.clientWidth) {
  22. return document.body.clientWidth;
  23. } else if (document.documentElement.clientWidth) {
  24. return document.documentElement.clientWidth;
  25. }
  26. }
  27.  
  28. window.getInnerHeight = function() {
  29. if (window.innerHeight) {
  30. return window.innerHeight;
  31. } else if (document.body.clientHeight) {
  32. return document.body.clientHeight;
  33. } else if (document.documentElement.clientHeight) {
  34. return document.documentElement.clientHeight;
  35. }
  36. }
  37.  
  38. String.prototype.chop = function() {
  39. if (this.length == 0) return "";
  40. return this.substring(0,this.length-1);
  41. }
  42.  
  43. String.prototype.endsWith = function(str) {
  44. return (this.length-str.length)==this.lastIndexOf(str);
  45. }
  46.  
  47. String.prototype.reverse = function() {
  48. var s = "";
  49. var i = this.length;
  50. while (i>0) {
  51. s += this.substring(i-1,i);
  52. i--;
  53. }
  54. return s;
  55. }
  56.  
  57. // this trim was suggested by Tobias Hinnerup
  58. String.prototype.trim = function() {
  59. return(this.replace(/^\s+/,'').replace(/\s+$/,''));
  60. }
  61.  
  62. String.prototype.toInt = function() {
  63. var a = new Array();
  64. for (var i = 0; i < this.length; i++) {
  65. a[i] = this.charCodeAt(i);
  66. }
  67. return a;
  68. }
  69.  
  70. Array.prototype.intArrayToString = function() {
  71. var a = new String();
  72. for (var i = 0; i < this.length; i++) {
  73. if(typeof this[i] != "number") {
  74. throw new Error("Array must be all numbers");
  75. } else if (this[i] < 0) {
  76. throw new Error("Numbers must be 0 and up");
  77. }
  78. a += String.fromCharCode(this[i]);
  79. }
  80. return a;
  81. }
  82.  
  83. Array.prototype.compareArrays = function(arr) {
  84. if (this.length != arr.length) return false;
  85. for (var i = 0; i < arr.length; i++) {
  86. if (this[i].compareArrays) { //likely nested array
  87. if (!this[i].compareArrays(arr[i])) return false;
  88. else continue;
  89. }
  90. if (this[i] != arr[i]) return false;
  91. }
  92. return true;
  93. }
  94.  
  95. Array.prototype.map = function(fnc) {
  96. var a = new Array(this.length);
  97. for (var i = 0; i < this.length; i++) {
  98. a[i] = fnc(this[i]);
  99. }
  100. return a;
  101. }
  102.  
  103. Array.prototype.foldr = function(fnc,start) {
  104. var a = start;
  105. for (var i = this.length-1; i > -1; i--) {
  106. a = fnc(this[i],a);
  107. }
  108. return a;
  109. }
  110.  
  111. Array.prototype.foldl = function(fnc,start) {
  112. var a = start;
  113. for (var i = 0; i < this.length; i++) {
  114. a = fnc(this[i],a);
  115. }
  116. return a;
  117. }
  118.  
  119. Array.prototype.exists = function (x) {
  120. for (var i = 0; i < this.length; i++) {
  121. if (this[i] == x) return true;
  122. }
  123. return false;
  124. }
  125.  
  126. Array.prototype.filter = function(fnc) {
  127. var a = new Array();
  128. for (var i = 0; i < this.length; i++) {
  129. if (fnc(this[i])) {
  130. a.push(this[i]);
  131. }
  132. }
  133. return a;
  134. }
  135.  
  136. Array.prototype.random = function() {
  137. return this[Math.floor((Math.random()*this.length))];
  138. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.