Guest User

Untitled

a guest
Jan 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. if (!('filter' in Array.prototype)) {
  2. // conforms with the ECMA standard
  3. Array.prototype.filter = function filter(fn, thisArg) {
  4. var index = 0, length = this.length, value, returnValue = [], that = Object(this);
  5. for (; index < length; ++index) {
  6. if (index in that) {
  7. value = that[index];
  8. if (fn.call(thisArg, value, index, that)) {
  9. returnValue.push(value);
  10. }
  11. }
  12. }
  13. return returnValue;
  14. };
  15. }
  16.  
  17. if (!('forEach' in Array.prototype)) {
  18. Array.prototype.forEach = function forEach(fn, thisArg) {
  19. var index = 0, length = this.length, that = Object(this), value;
  20. for (; index < length; ++index) {
  21. if (index in that) {
  22. value = that[index];
  23. fn.call(thisArg, value, index, that);
  24. }
  25. }
  26. };
  27. }
  28.  
  29. if (!('map' in Array.prototype)) {
  30. Array.prototype.map = function map(fn, thisArg) {
  31. var newArray = [];
  32.  
  33. this.forEach(function() {
  34. newArray.push(fn.apply(this, arguments));
  35. }, thisArg);
  36.  
  37. return newArray;
  38. };
  39. }
  40.  
  41. if (!('indexOf' in Array.prototype)) {
  42. Array.prototype.indexOf = function indexOf(needle, index) {
  43. index = Number(index);
  44. var length = this.length, that = Object(this);
  45. for (; index < length; ++index) {
  46. if (index in that && that[index] === needle) {
  47. return index;
  48. }
  49. }
  50. return -1;
  51. };
  52. }
  53.  
  54. if (!('lastIndexOf' in Array.prototype)) {
  55. Array.prototype.lastIndexOf = function lastIndexOf(needle, index) {
  56. index = Number(index);
  57. var length = this.length, that = Object(this);
  58. for (; length-- >= index;) {
  59. if (length in that && that[length] === needle) {
  60. return index;
  61. }
  62. }
  63. return -1;
  64. };
  65. }
  66.  
  67. if (!('reduce' in Array.prototype)) {
  68. Array.prototype.reduce = function reduce(fn) {
  69. var index, value, length = this.length, that = Object(this);
  70. if (arguments.length > 1) {
  71. index = 0;
  72. value = arguments[1];
  73. } else {
  74. index = 1;
  75. value = this[0];
  76. }
  77.  
  78. for (; index < length; ++index) {
  79. if (index in that) {
  80. value = fn.call(this, value, that[index], index, this);
  81. }
  82. }
  83. return value;
  84. };
  85. }
  86.  
  87. Array.prototype.count = function count(arg, thisArg) {
  88. if (typeof arg === "undefined") {
  89. return this.length;
  90. }
  91.  
  92. if (arg.call) {
  93. return this.reduce(function(previous, current) {
  94. return previous + Number(!!arg.call(this, current));
  95. }, 0);
  96. }
  97.  
  98. return this.reduce(function(previous, current) {
  99. return previous + Number(current === arg);
  100. }, 0);
  101. };
  102.  
  103. Array.prototype.unique = function unique() {
  104. var lastValue, result;
  105. return this.sort().filter(function(value) {
  106. result = value !== lastValue;
  107. lastValue = result;
  108. return result;
  109. });
  110. };
  111.  
  112. Array.prototype.compact = function compact() {
  113. var undefined;
  114. return this.filter(function(value) {
  115. return value !== null && value !== undefined;
  116. });
  117. };
  118.  
  119. Array.prototype.reject = function reject(fn, thisArg) {
  120. return this.filter(function() {
  121. return !fn.apply(this, arguments);
  122. }, thisArg);
  123. };
  124.  
  125. Array.prototype.last = function last() {
  126. return this[this.length - 1];
  127. };
  128.  
  129. Array.prototype.select = Array.prototype.filter;
  130. Array.prototype.append = Array.prototype.push;
  131. Array.prototype.prepend = Array.prototype.unshift;
Add Comment
Please, Sign In to add comment