utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const deepmerge = require("deepmerge");
  2. /**
  3. * 判断对象类型
  4. * @param {*} obj 对象
  5. */
  6. function objTypeOf(obj) {
  7. const toString = Object.prototype.toString;
  8. const map = {
  9. "[object Boolean]": "boolean",
  10. "[object Number]": "number",
  11. "[object String]": "string",
  12. "[object Function]": "function",
  13. "[object Array]": "array",
  14. "[object Date]": "date",
  15. "[object RegExp]": "regExp",
  16. "[object Undefined]": "undefined",
  17. "[object Null]": "null",
  18. "[object Object]": "object"
  19. };
  20. return map[toString.call(obj)];
  21. }
  22. /**
  23. * 深拷贝
  24. * @param {Object/Array} data 需要拷贝的数据
  25. */
  26. function deepCopy(data, options) {
  27. const defObj = objTypeOf(data) === "array" ? [] : {};
  28. return deepmerge(defObj, data, options || {});
  29. }
  30. /**
  31. * 将目标对象中有的属性值与源对象中的属性值合并
  32. * @param {Object} target 目标对象
  33. * @param {Object} sources 源对象
  34. */
  35. function objAssign(target, sources) {
  36. let targ = { ...target };
  37. for (let k in targ) {
  38. targ[k] = Object.prototype.hasOwnProperty.call(sources, k)
  39. ? sources[k]
  40. : targ[k];
  41. }
  42. return targ;
  43. }
  44. /**
  45. * 获取随机code,默认获取16位
  46. * @param {Number} len 推荐8的倍数
  47. *
  48. */
  49. function randomCode(len = 16) {
  50. if (len <= 0) return;
  51. let steps = Math.ceil(len / 8);
  52. let stepNums = [];
  53. for (let i = 0; i < steps; i++) {
  54. let ranNum = Math.random()
  55. .toString(32)
  56. .slice(-8);
  57. stepNums.push(ranNum);
  58. }
  59. return stepNums.join("");
  60. }
  61. /**
  62. *
  63. * @param {String} format 时间格式
  64. * @param {Date} date 需要格式化的时间对象
  65. */
  66. function formatDate(format = "YYYY/MM/DD HH:mm:ss", date = new Date()) {
  67. if (objTypeOf(date) !== "date") return;
  68. const options = {
  69. "Y+": date.getFullYear(),
  70. "M+": date.getMonth() + 1,
  71. "D+": date.getDate(),
  72. "H+": date.getHours(),
  73. "m+": date.getMinutes(),
  74. "s+": date.getSeconds()
  75. };
  76. Object.entries(options).map(([key, val]) => {
  77. if (new RegExp("(" + key + ")").test(format)) {
  78. const zeros = key === "Y+" ? "0000" : "00";
  79. const value = (zeros + val).substr(("" + val).length);
  80. format = format.replace(RegExp.$1, value);
  81. }
  82. });
  83. return format;
  84. }
  85. /**
  86. * 获取本地时间,格式:年月日时分秒
  87. */
  88. function localNowDateTime() {
  89. return formatDate("YYYY年MM月DD日HH时mm分ss秒");
  90. }
  91. /**
  92. * 获取指定元素个数的数组
  93. * @param {Number} num
  94. */
  95. function getNumList(num) {
  96. return "#".repeat(num).split("");
  97. }
  98. /**
  99. * 清除html标签
  100. * @param {String} str html字符串
  101. */
  102. function removeHtmlTag(str) {
  103. return str.replace(/<[^>]+>/g, "");
  104. }
  105. /**
  106. * 计算总数
  107. * @param {Array} dataList 需要统计的数组
  108. */
  109. function calcSum(dataList) {
  110. return dataList.reduce(function(total, item) {
  111. return total + item;
  112. }, 0);
  113. }
  114. function isEmptyObject(obj) {
  115. return !Object.keys(obj).length;
  116. }
  117. function getElementId() {
  118. return `element-${randomCode()}`;
  119. }
  120. export {
  121. objTypeOf,
  122. deepCopy,
  123. objAssign,
  124. randomCode,
  125. formatDate,
  126. localNowDateTime,
  127. getNumList,
  128. removeHtmlTag,
  129. calcSum,
  130. isEmptyObject,
  131. getElementId
  132. };