Util.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Created by juho on 2016-03-29.
  3. */
  4. var Util = {
  5. // 자릿수 맞춰 문자열 반환
  6. zeroStr: function (num, total) {
  7. var str = num.toString();
  8. var i = total - str.length;
  9. while (i--) {
  10. str = '0' + str;
  11. }
  12. return str;
  13. },
  14. // 랜덤 숫자 반환
  15. randomNumber: function (total, point) {
  16. var p = 10 * point || 1;
  17. return Math.floor((Math.random() * total * p) / p);
  18. },
  19. hitTest: function (bound, point) {
  20. if (point.x >= bound.x && point.x <= (bound.x + bound.width) && point.y >= bound.y && point.y <= (bound.y + bound.height)) return true;
  21. return false;
  22. },
  23. // 배열에 중복값 삭제
  24. uniqueArray: function (arr) {
  25. var a = [];
  26. for (var i = 0, l = arr.length; i < l; i++)
  27. if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
  28. a.push(arr[i]);
  29. return a;
  30. },
  31. // 문자열에 콤마 추가
  32. comma: function (str) {
  33. str = String(str);
  34. return str.replace(/(\d)(?=(?:\d{3})+(?!\d))/g, '$1,');
  35. },
  36. // 배열 섞기
  37. shuffle: function (a) {
  38. var j, x, i;
  39. for (i = a.length; i; i--) {
  40. j = Math.floor(Math.random() * i);
  41. x = a[i - 1];
  42. a[i - 1] = a[j];
  43. a[j] = x;
  44. }
  45. },
  46. // 특정 값만 배열에서 삭제
  47. grep: function (ary, removeItem) {
  48. return jQuery.grep(ary, function (value) {
  49. return value != removeItem;
  50. });
  51. },
  52. // url 파라미터 반환
  53. getParam: function (str) {
  54. var v = window.location.search.match(new RegExp('(?:[\?\&]' + str + '=)([^&]+)'));
  55. return v ? v[1] : null;
  56. },
  57. mobileCheck: function () {
  58. if (navigator.userAgent.match(/Android/i)
  59. || navigator.userAgent.match(/webOS/i)
  60. || navigator.userAgent.match(/iPhone/i)
  61. || navigator.userAgent.match(/iPad/i)
  62. || navigator.userAgent.match(/iPod/i)
  63. || navigator.userAgent.match(/BlackBerry/i)
  64. || navigator.userAgent.match(/Windows Phone/i)) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. },
  70. radiansToDegrees: function (degrees) {
  71. return degrees * Math.PI / 180;
  72. },
  73. degreesToradians: function (radians) {
  74. return radians * 180 / Math.PI;
  75. },
  76. getRandomNum: function (maxNum) {
  77. var num = Math.floor(Math.random() * 10000 % maxNum) + 1;
  78. return num;
  79. },
  80. getDecimal: function (num, total) {
  81. var str = num + "", index = str.indexOf("."), length = 0;
  82. if (index != "-1") {
  83. length = str.length - (index + 1);
  84. if (total <= length) {
  85. str = str.substr(0, index + total + 1);
  86. } else {
  87. while (length--) {
  88. str += "0";
  89. }
  90. }
  91. } else {
  92. if (total > 0) {
  93. str += ".";
  94. while (total--) {
  95. str += "0";
  96. }
  97. }
  98. }
  99. return str;
  100. }
  101. };
  102. module.exports = Util