utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. export function isArray(value) {
  2. if (typeof Array.isArray === 'function') {
  3. return Array.isArray(value);
  4. } else {
  5. return Object.prototype.toString.call(value) === '[object Array]';
  6. }
  7. }
  8. export function isObject(value) {
  9. return Object.prototype.toString.call(value) === '[object Object]';
  10. }
  11. export function isNumber(value) {
  12. return !isNaN(Number(value));
  13. }
  14. export function isFunction(value) {
  15. return typeof value == 'function';
  16. }
  17. export function isString(value) {
  18. return typeof value == 'string';
  19. }
  20. export function isEmpty(value) {
  21. if (value === '' || value === undefined || value === null){
  22. return true;
  23. }
  24. if (isArray(value)) {
  25. return value.length === 0;
  26. }
  27. if (isObject(value)) {
  28. return Object.keys(value).length === 0;
  29. }
  30. return false
  31. }
  32. export function isBoolean(value) {
  33. return typeof value === 'boolean';
  34. }
  35. export function last(data) {
  36. if (isArray(data) || isString(data)) {
  37. return data[data.length - 1];
  38. }
  39. }
  40. export function cloneDeep(obj) {
  41. const d = isArray(obj) ? [...obj] : {};
  42. if (isObject(obj)) {
  43. for (const key in obj) {
  44. if (obj[key]) {
  45. if (obj[key] && typeof obj[key] === 'object') {
  46. d[key] = cloneDeep(obj[key]);
  47. } else {
  48. d[key] = obj[key];
  49. }
  50. }
  51. }
  52. }
  53. return d;
  54. }
  55. export function clone(obj) {
  56. return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
  57. }
  58. export function deepMerge(a, b) {
  59. let k;
  60. for (k in b) {
  61. a[k] = a[k] && a[k].toString() === '[object Object]' ? deepMerge(a[k], b[k]) : (a[k] = b[k]);
  62. }
  63. return a;
  64. }
  65. export function contains(parent, node) {
  66. while (node && (node = node.parentNode)) if (node === parent) return true;
  67. return false;
  68. }
  69. export function orderBy(list, key) {
  70. return list.sort((a, b) => a[key] - b[key]);
  71. }
  72. export function deepTree(list) {
  73. const newList = [];
  74. const map = {};
  75. list.forEach((e) => (map[e.id] = e));
  76. list.forEach((e) => {
  77. const parent = map[e.parentId];
  78. if (parent) {
  79. (parent.children || (parent.children = [])).push(e);
  80. } else {
  81. newList.push(e);
  82. }
  83. });
  84. const fn = (list) => {
  85. list.map((e) => {
  86. if (e.children instanceof Array) {
  87. e.children = orderBy(e.children, 'orderNum');
  88. fn(e.children);
  89. }
  90. });
  91. };
  92. fn(newList);
  93. return orderBy(newList, 'orderNum');
  94. }
  95. export function revDeepTree(list = []) {
  96. const d = [];
  97. let id = 0;
  98. const deep = (list, parentId) => {
  99. list.forEach((e) => {
  100. if (!e.id) {
  101. e.id = id++;
  102. }
  103. e.parentId = parentId;
  104. d.push(e);
  105. if (e.children && isArray(e.children)) {
  106. deep(e.children, e.id);
  107. }
  108. });
  109. };
  110. deep(list || [], null);
  111. return d;
  112. }
  113. export function basename(path) {
  114. let index = path.lastIndexOf('/');
  115. index = index > -1 ? index : path.lastIndexOf('\\');
  116. if (index < 0) {
  117. return path;
  118. }
  119. return path.substring(index + 1);
  120. }
  121. export function isWxBrowser() {
  122. const ua = navigator.userAgent.toLowerCase();
  123. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. /**
  130. * @description 如果value小于min,取min;如果value大于max,取max
  131. * @param {number} min
  132. * @param {number} max
  133. * @param {number} value
  134. */
  135. export function range(min = 0, max = 0, value = 0) {
  136. return Math.max(min, Math.min(max, Number(value)));
  137. }