Guest User

Untitled

a guest
Jul 15th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. /**
  2. * Javascript prototypes.
  3. */
  4.  
  5.  
  6. /**
  7. * Array.remove() prototype.
  8. */
  9. if (!Array.prototype.remove)
  10. {
  11. Array.prototype.remove = function(value)
  12. {
  13. if (value == undefined) return false;
  14. var j = this.length;
  15. for (var i = 0; i < j; i++)
  16. {
  17. if (this[i] == value)
  18. {
  19. this.splice(i, 1);
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. }
  26.  
  27. /**
  28. * Array.indexOf() prototype.
  29. */
  30. if (!Array.prototype.indexOf)
  31. {
  32. Array.prototype.indexOf = function(elm, i)
  33. {
  34. var j = this.length;
  35. if (!i) i = 0;
  36. if (i >= 0)
  37. {
  38. while(i < j)
  39. {
  40. if (this[i++] === elm)
  41. {
  42. i = i - 1 + j;
  43. j = i - j;
  44. }
  45. }
  46. }
  47. else
  48. j = this.indexOf(elm, j + i);
  49. return j !== this.length ? j : -1;
  50. }
  51. }
  52.  
  53. /**
  54. * Array.lastIndexOf() prototype.
  55. */
  56. if (!Array.prototype.lastIndexOf)
  57. {
  58. Array.prototype.lastIndexOf = function(elm, i)
  59. {
  60. var j = -1;
  61. if (!i) i = this.length;
  62. if (i >= 0)
  63. {
  64. do
  65. {
  66. if (this[i--] === elm)
  67. {
  68. j = i + 1;
  69. i = 0;
  70. }
  71. }
  72. while (i > 0)
  73. }
  74. else
  75. {
  76. if (i >= this.length) j = this.lastIndexOf(elm, this.length + i);
  77. }
  78. return j;
  79. }
  80. }
  81.  
  82. /**
  83. * Array.map() prototype.
  84. */
  85. if (!Array.prototype.map)
  86. {
  87. Array.prototype.map = function(func /*, thisp*/)
  88. {
  89. if (typeof func != "function") throw new TypeError();
  90. var len = this.length;
  91. var res = new Array(len);
  92. var thisp = arguments[1];
  93. for (var i = 0; i < len; i++) if (i in this) res[i] = func.call(thisp, this[i], i, this);
  94. return res;
  95. }
  96. }
  97.  
  98. /**
  99. * Array.forEach() prototype.
  100. */
  101. if (!Array.prototype.forEach)
  102. {
  103. Array.prototype.forEach = function(func /*, thisp*/)
  104. {
  105. if (typeof func != "function") throw new TypeError();
  106. var len = this.length;
  107. var thisp = arguments[1];
  108. for (var i = 0; i < len; i++) if (i in this) func.call(thisp, this[i], i, this);
  109. return true;
  110. }
  111. }
  112.  
  113. /**
  114. * Array.every() prototype.
  115. */
  116. if (!Array.prototype.every)
  117. {
  118. Array.prototype.every = function(fun /*, thisp*/)
  119. {
  120. var len = this.length;
  121. if (typeof fun != "function") throw new TypeError();
  122. var thisp = arguments[1];
  123. for (var i = 0; i < len; i++)
  124. {
  125. if (i in this && !fun.call(thisp, this[i], i, this)) return false;
  126. }
  127. return true;
  128. }
  129. }
  130.  
  131. /**
  132. * Array.filter() prototype.
  133. */
  134. if (!Array.prototype.filter)
  135. {
  136. Array.prototype.filter = function(fun /*, thisp*/)
  137. {
  138. if (typeof fun != "function") throw new TypeError();
  139. var j = this.length;
  140. var res = new Array();
  141. var thisp = arguments[1];
  142. for (var i = 0; i < j; i++)
  143. {
  144. if (i in this)
  145. {
  146. var val = this[i];
  147. if (fun.call(thisp, val, i, this)) res.push(val);
  148. }
  149. }
  150. return res;
  151. }
  152. }
  153.  
  154. /**
  155. * Array.reduce() prototype.
  156. */
  157. if (!Array.prototype.reduce)
  158. {
  159. Array.prototype.reduce = function(fun /*, initial*/)
  160. {
  161. if (typeof fun != "function") throw new TypeError();
  162. var j = this.length;
  163. if (j == 0 && arguments.length == 1) throw new TypeError();
  164. var i = 0;
  165. if (arguments.length >= 2)
  166. {
  167. var rv = arguments[1];
  168. }
  169. else
  170. {
  171. do
  172. {
  173. if (i in this)
  174. {
  175. rv = this[i++];
  176. break;
  177. }
  178. if (++i >= j) throw new TypeError();
  179. }
  180. while (true);
  181. }
  182. for (; i < j; i++)
  183. {
  184. if (i in this) rv = fun.call(null, rv, this[i], i, this);
  185. }
  186. return rv;
  187. }
  188. }
  189.  
  190. /**
  191. * Array.reduceRight() prototype.
  192. */
  193. if (!Array.prototype.reduceRight)
  194. {
  195. Array.prototype.reduceRight = function(func /*, initial*/)
  196. {
  197. if (typeof func != "function") throw new TypeError();
  198. var j = this.length;
  199. if (j == 0 && arguments.length == 1) throw new TypeError();
  200. var i = j - 1;
  201. if (arguments.length >= 2)
  202. {
  203. var rv = arguments[1];
  204. }
  205. else
  206. {
  207. do
  208. {
  209. if (i in this)
  210. {
  211. rv = this[i--];
  212. break;
  213. }
  214. if (--i < 0) throw new TypeError();
  215. }
  216. while (true);
  217. }
  218. for (; i >= 0; i--)
  219. {
  220. if (i in this) rv = func.call(null, rv, this[i], i, this);
  221. }
  222. return rv;
  223. }
  224. }
  225.  
  226. /**
  227. * Array.some() prototype.
  228. */
  229. if (!Array.prototype.some)
  230. {
  231. Array.prototype.some = function(func /*, thisp*/)
  232. {
  233. var len = this.length;
  234. if (typeof func != "function") throw new TypeError();
  235. var thisp = arguments[1];
  236. for (var i = 0; i < len; i++)
  237. {
  238. if (i in this && func.call(thisp, this[i], i, this)) return true;
  239. }
  240. return false;
  241. }
  242. }
  243.  
  244. /**
  245. * Object.toSource() prototype.
  246. */
  247. if (!Object.prototype.toSource)
  248. {
  249. Object.prototype.toSource = function()
  250. {
  251. if (typeof this == 'string' || this instanceof String) return '"' + this + '"';
  252.  
  253. if (typeof this == 'number' || this instanceof Number) return '"' + this + '"';
  254.  
  255. if (typeof this == 'boolean' || this instanceof Boolean) return '"' + this + '"';
  256.  
  257. if (typeof this == 'function' || this instanceof Function)
  258. {
  259. var r = this.toString();
  260. r = r.replace(/function\(/, 'function \(').replace(/\)\{/, ') {');
  261. return '(' + r + ')';
  262. }
  263.  
  264. if (this instanceof Array)
  265. {
  266. var r = [];
  267. for (var i = 0, len = this.length; i < len; i++)
  268. {
  269. if (typeof this[i] == 'undefined') r.push('undefined');
  270. else if (this[i] == null) r.push('null');
  271. else r.push(this[i].toSource());
  272. }
  273. return '[' + r.join(', ') + ']';
  274. }
  275.  
  276. if (this instanceof Object)
  277. {
  278. var r = [];
  279. for (var i in this)
  280. {
  281. if (i == 'toSource') continue;
  282. if (typeof this[i] == 'undefined') r.push(i + ':undefined');
  283. else if (this[i] == null) r.push(i + ':null');
  284. else r.push(i + ':' + this[i].toSource());
  285. }
  286. return '{' + r.join(', ') + '}';
  287. }
  288.  
  289. return 'unknown';
  290. }
  291. }
  292.  
  293. /**
  294. * Date.format() prototype.
  295. */
  296. if (!Date.prototype.format)
  297. {
  298. Date.prototype.format = function(format)
  299. {
  300. var YYYY, YY, MMMM, MMM, MM, M, DDDD, DDD, DD, D, hhh, hh, h, mm, m, ss, s;
  301. YY = ((YYYY = this.getFullYear()) + '').substr(2, 2);
  302. MM = (M = this.getMonth() + 1) < 10 ? ('0' + M) : M;
  303. MMM = (MMMM =
  304. [
  305. "January", "February", "March", "April", "May", "June",
  306. "July", "August", "September", "October", "November", "December"
  307. ][M - 1]).substr(0, 3);
  308. DD = (D = this.getDate()) < 10 ? ('0' + D) : D;
  309. DDD = (DDDD =
  310. [
  311. "Sunday", "Monday", "Tuesday", "Wednesday",
  312. "Thursday", "Friday", "Saturday"
  313. ][this.getDay()]).substr(0, 3);
  314. h = (hhh = this.getHours());
  315. if (h == 0) h = 24;
  316. if (h > 12) h -= 12;
  317. hh = h < 10 ? ('0' + h) : h;
  318. mm = (m = this.getMinutes()) < 10 ? ('0' + m) : m;
  319. ss = (s = this.getSeconds()) < 10 ? ('0' + s) : s;
  320. return format.replace("#YYYY#", YYYY).replace("#YY#", YY).replace("#MMMM#", MMMM)
  321. .replace("#MMM#", MMM).replace("#MM#", MM).replace("#M#", M).replace("#DDDD#", DDDD)
  322. .replace("#DDD#", DDD).replace("#DD#", DD).replace("#D#", D).replace("#hhh#", hhh)
  323. .replace("#hh#", hh).replace("#h#", h).replace("#mm#", mm).replace("#m#", m).
  324. replace("#ss#", ss).replace("#s#", s);
  325. }
  326. }
  327.  
  328. /**
  329. * Date.timestamp() prototype.
  330. */
  331. if (!Date.prototype.timestamp)
  332. {
  333. Date.prototype.timestamp = function()
  334. {
  335. return Math.round(this.getTime() / 1000);
  336. }
  337. }
Add Comment
Please, Sign In to add comment