index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. function typeOf(obj) {
  2. const toString = Object.prototype.toString;
  3. const map = {
  4. '[object Boolean]' : 'boolean',
  5. '[object Number]' : 'number',
  6. '[object String]' : 'string',
  7. '[object Function]' : 'function',
  8. '[object Array]' : 'array',
  9. '[object Date]' : 'date',
  10. '[object RegExp]' : 'regExp',
  11. '[object Undefined]': 'undefined',
  12. '[object Null]' : 'null',
  13. '[object Object]' : 'object'
  14. };
  15. return map[toString.call(obj)];
  16. }
  17. export function deepCopy(data) {
  18. const t = typeOf(data);
  19. let o;
  20. if (t === 'array') {
  21. o = [];
  22. } else if ( t === 'object') {
  23. o = {};
  24. } else {
  25. return data;
  26. }
  27. if (t === 'array') {
  28. for (let i = 0; i < data.length; i++) {
  29. o.push(deepCopy(data[i]));
  30. }
  31. } else if ( t === 'object') {
  32. for (let i in data) {
  33. o[i] = deepCopy(data[i]);
  34. }
  35. }
  36. return o;
  37. }
  38. // 判断参数是否是其中之一
  39. export function oneOf (value, validList) {
  40. for (let i = 0; i < validList.length; i++) {
  41. if (value === validList[i]) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. export function isValueNumber (value) {
  48. return (/^[1-9][0-9]*$/).test(value + '');
  49. }
  50. // 判断是不是字符串
  51. export function isString (str) {
  52. return typeOf(str) === 'string' && str.constructor === String
  53. }
  54. // 大于max时取max,小于min时取min,否则取自身
  55. export const withinNum = (origin, min, max) => Math.min(Math.max(origin, min), max);
  56. // 数字小于10时自动前面补0
  57. export const fillZero = (num) => {
  58. if (!num && num !== 0) return '';
  59. if (num < 10) return `0${num}`;
  60. return num;
  61. }
  62. export const compareArrJoined = (arr1, arr2) => {
  63. const reg = /[^\d]+/g;
  64. const remove = (str) => str.replace(reg, () => '');
  65. const startNum = parseInt(remove(arr1.join('')), 10) || 0;
  66. const endNum = parseInt(remove(arr2.join('')), 10) || 0;
  67. return startNum > endNum;
  68. };
  69. // 格式化输出的值
  70. // (val 值为时分秒的一个数组; rule 格式化的规则,eg:'HH:mm:ss')
  71. export function formatOutputValue(val, rule) {
  72. if (!val || !val.length) return '';
  73. const houtAt12 = (v) => (v >= 12 ? v - 12 : v);
  74. const maps = {
  75. HH: fillZero(val[0]), // 24时制,2位展示
  76. H: val[0], // 24时制,1位展示
  77. hh: fillZero(houtAt12(val[0])), // 12时制,2位展示
  78. h: houtAt12(val[0]), // 12时制,1位展示
  79. mm: fillZero(val[1]), // 分钟,2位展示
  80. m: val[1], // 分钟,1位展示
  81. ss: fillZero(val[2]), // 秒,2位展示
  82. s: val[2], // 秒,2位展示
  83. };
  84. const reg = /(HH)|(H)|(hh)|(h)|(mm)|(m)|(ss)|(s)/g;
  85. return rule.replace(reg, (v) => maps[v]);
  86. }
  87. // 生成格式化后的数字
  88. export function formatOutputDate(val, rule) {
  89. if(!val || val === 'undefined' || val === 'null') return
  90. const houtAt12 = (v) => (v >= 12 ? v - 12 : v);
  91. const t = parseDate(val);
  92. const maps = {
  93. yyyy: t.year, // 年,eg:2019
  94. MM: fillZero(t.month + 1), // 月, 2位,eg:03
  95. M: t.month + 1, // 月, 1位,eg:3
  96. dd: fillZero(t.date), // 日, 2位,eg:03
  97. d: t.date, // 日, 1位,eg:03
  98. HH: fillZero(t.hour), // 24时制,2位展示
  99. H: t.hour, // 24时制,1位展示
  100. hh: fillZero(houtAt12(t.hour)), // 12时制,2位展示
  101. h: houtAt12(t.hour), // 12时制,1位展示
  102. mm: fillZero(t.minute), // 分钟,2位展示
  103. m: t.minute, // 分钟,1位展示
  104. ss: fillZero(t.second), // 秒,2位展示
  105. s: t.second, // 秒,2位展示
  106. };
  107. const regStr = Object.keys(maps).map((e) => `(${e})`).join('|');
  108. const reg = new RegExp(regStr, 'g');
  109. return rule.replace(reg, (v) => maps[v]);
  110. }
  111. // 解析日期
  112. export function parseDate(value) {
  113. const t = value ? new Date(value) : new Date(Date.now());
  114. return {
  115. year: t.getFullYear(),
  116. month: t.getMonth(),
  117. date: t.getDate(),
  118. day: t.getDay(),
  119. hour: t.getHours(),
  120. minute: t.getMinutes(),
  121. second: t.getSeconds(),
  122. timeStamp: t.getTime(),
  123. };
  124. }
  125. // 获取时间戳
  126. export function getTime(...arg) {
  127. return new Date(...arg).getTime();
  128. }
  129. export function checkToday(year, month, date) {
  130. const today = parseDate(Date.now());
  131. return today.year === year && today.month === month && today.date === date;
  132. }
  133. export function checkSelectDay(obj,item) {
  134. return obj.year === item.Y && obj.month === item.M && obj.date === item.D;
  135. }