utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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().toString(32).slice(-8);
  55. stepNums.push(ranNum);
  56. }
  57. return stepNums.join("");
  58. }
  59. /**
  60. *
  61. * @param {String} format 时间格式
  62. * @param {Date} date 需要格式化的时间对象
  63. */
  64. function formatDate(format = "YYYY/MM/DD HH:mm:ss", date = new Date()) {
  65. if (objTypeOf(date) !== "date") return;
  66. const options = {
  67. "Y+": date.getFullYear(),
  68. "M+": date.getMonth() + 1,
  69. "D+": date.getDate(),
  70. "H+": date.getHours(),
  71. "m+": date.getMinutes(),
  72. "s+": date.getSeconds(),
  73. };
  74. Object.entries(options).map(([key, val]) => {
  75. if (new RegExp("(" + key + ")").test(format)) {
  76. const zeros = key === "Y+" ? "0000" : "00";
  77. const value = (zeros + val).substr(("" + val).length);
  78. format = format.replace(RegExp.$1, value);
  79. }
  80. });
  81. return format;
  82. }
  83. /**
  84. * 获取本地时间,格式:年月日时分秒
  85. */
  86. function localNowDateTime() {
  87. return formatDate("YYYY年MM月DD日HH时mm分ss秒");
  88. }
  89. /**
  90. * 获取指定元素个数的数组
  91. * @param {Number} num
  92. */
  93. function getNumList(num) {
  94. return "#".repeat(num).split("");
  95. }
  96. /**
  97. * 清除html标签
  98. * @param {String} str html字符串
  99. */
  100. function removeHtmlTag(str) {
  101. return str.replace(/<[^>]+>/g, "");
  102. }
  103. /**
  104. * 计算总数
  105. * @param {Array} dataList 需要统计的数组
  106. */
  107. function calcSum(dataList) {
  108. return dataList.reduce(function (total, item) {
  109. return total + item;
  110. }, 0);
  111. }
  112. function isEmptyObject(obj) {
  113. return !Object.keys(obj).length;
  114. }
  115. function getElementId() {
  116. return `element-${randomCode()}`;
  117. }
  118. export {
  119. objTypeOf,
  120. deepCopy,
  121. objAssign,
  122. randomCode,
  123. formatDate,
  124. localNowDateTime,
  125. getNumList,
  126. removeHtmlTag,
  127. calcSum,
  128. isEmptyObject,
  129. getElementId,
  130. };