utilservice.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. (function (app) {
  2. //工具类服务
  3. app.factory("util", function () {
  4. var obj = {
  5. trim: function (e) {
  6. var e = e.replace(/^\s\s*/, ""),
  7. t = /\s/,
  8. i = e.length;
  9. while (t.test(e.charAt(--i)));
  10. return e.slice(0, i + 1)
  11. },
  12. rtrim: function (e, t) {
  13. if (e[e.length - 1] === t) return e.substr(0, e.length - 1);
  14. return e
  15. },
  16. format: function () {
  17. if (arguments.length == 0) return "";
  18. if (arguments.length == 1) return arguments[0];
  19. var e = arguments[0];
  20. var t = Array.prototype.slice.call(arguments).splice(1);
  21. return e.replace(/{(\d+)}/g, function () {
  22. return t[arguments[1]]
  23. })
  24. },
  25. empty: function (t) {
  26. if (!t) return true;
  27. if (Array.isArray(t) && t.length == 0) return true;
  28. for (var i in t) if (t.hasOwnProperty(i)) return false;
  29. return false;
  30. },
  31. sub: function (e, t, i) {
  32. i = i || "";
  33. var o = /[^\x00-\xff]/g;
  34. if (e.replace(o, "mm").length <= t) return e;
  35. var n = Math.floor(t / 2);
  36. for (var a = n; a < e.length; a++) {
  37. if (e.substr(0, a).replace(o, "mm").length >= t) return e.substr(0, a) + i
  38. }
  39. return e
  40. },
  41. test: function (e, t) {
  42. return new RegExp(e, "i").test(t)
  43. },
  44. leftPad: function (e, t, i) {
  45. return Array(t - String(e).length + 1).join(i || "0") + e
  46. },
  47. unixTime: function (e) {
  48. e = e || new Date;
  49. return Math.round(+e / 1e3)
  50. },
  51. extend: function (e, t) {
  52. if (!t || typeof t !== "object") return e;
  53. var i = Object.keys(t);
  54. var o = i.length;
  55. while (o--) e[i[o]] = t[i[o]];
  56. return e
  57. },
  58. isString: function (e) {
  59. return Object.prototype.toString.call(e) === "[object String]"
  60. },
  61. isFunction: function (e) {
  62. return typeof e === "function"
  63. },
  64. isObject: function (e) {
  65. return typeof e === "object"
  66. },
  67. isArray: function (e) {
  68. return Array.isArray(e)
  69. },
  70. rand: function (e, t) {
  71. return Math.floor(Math.random() * (t - e)) + e
  72. },
  73. isMobile: function (e) {
  74. return e.match(/^(^(13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/gi)
  75. }
  76. };
  77. return obj;
  78. });
  79. })(angular.module('app.services'));