Ahto Library

My library used my other scripts.

当前为 2015-07-12 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/10922/61510/Ahto%20Library.js

  1. // Generated by CoffeeScript 1.9.3
  2. var findMatches, randInt, safeInterval, safeParseInt, stringHashCode;
  3.  
  4. findMatches = function(selector, min, max) {
  5. var matches, ref;
  6. if (min == null) {
  7. min = 1;
  8. }
  9. if (max == null) {
  10. max = Infinity;
  11. }
  12. matches = $(selector);
  13. if ((min <= (ref = matches.length) && ref <= max)) {
  14. return matches;
  15. } else {
  16. throw Error(matches.length + " matches (expected " + min + "-" + max + ") found for selector: " + selector);
  17. }
  18. };
  19.  
  20. safeParseInt = function(s) {
  21. var n;
  22. n = parseInt(s);
  23. if (isNaN(s)) {
  24. throw new Error("Unable to parse int from \"" + s + "\"");
  25. } else {
  26. return n;
  27. }
  28. };
  29.  
  30. safeInterval = function(func, wait, times) {
  31. var interv;
  32. interv = (function(w, t) {
  33. return (function() {
  34. var e;
  35. if ((t == null) || t-- > 0) {
  36. setTimeout(interv, w);
  37. try {
  38. return func.call(null);
  39. } catch (_error) {
  40. e = _error;
  41. t = 0;
  42. throw e.toString();
  43. }
  44. }
  45. });
  46. })(wait, times);
  47. return setTimeout(interv, wait);
  48. };
  49.  
  50. stringHashCode = function(s) {
  51. var chr, hash, i, j, len;
  52. hash = 0;
  53. for (j = 0, len = s.length; j < len; j++) {
  54. i = s[j];
  55. chr = i.charCodeAt(0);
  56. hash = ((hash << 5) - hash) + chr;
  57. hash |= 0;
  58. }
  59. return hash;
  60. };
  61.  
  62. randInt = function(min, max) {
  63. return min + Math.floor(Math.random() * (max + 1 - min));
  64. };