prototypes.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Interates through each element of an array and calls a callback function.
  3. * The callback should accept the following arguments:
  4. * element - The current element
  5. * index - The current index
  6. *
  7. * This is different to Array.forEach in that if the callback returns false, the loop returns
  8. * immediately without processing the remaining elements.
  9. *
  10. * @param {Function} callback
  11. * @return {Bool}
  12. */
  13. if (!Array.prototype.every) {
  14. Array.prototype.every = function(callback) {
  15. var len = this.length, i;
  16. for (i = 0; i < len; i++) {
  17. if (i in this && !callback.call(null, this[i], i)) {
  18. return false;
  19. }
  20. }
  21. return true;
  22. };
  23. }
  24. /**
  25. * Interates through each element of an array and calls callback a function.
  26. * The callback should accept the following arguments:
  27. * element - The current element
  28. * index - The current index
  29. *
  30. * This is different to Array.every in that the callback's return value is ignored and every
  31. * element of the array will be parsed.
  32. *
  33. * @param {Function} callback
  34. * @return {Void}
  35. */
  36. if (!Array.prototype.forEach) {
  37. Array.prototype.forEach = function(callback) {
  38. var len = this.length, i;
  39. for (i = 0; i < len; i++) {
  40. if (i in this) {
  41. callback.call(null, this[i], i);
  42. }
  43. }
  44. };
  45. }
  46. /**
  47. * Left trim
  48. *
  49. * Removes a character from the left side of a string.
  50. * @param {String} str The character to remove
  51. * @return {String}
  52. */
  53. if (!String.prototype.ltrim) {
  54. String.prototype.ltrim = function(str) {
  55. if (this.indexOf(str) === 0) {
  56. return this.substring(str.length);
  57. } else {
  58. return this;
  59. }
  60. };
  61. }