utils.js 3.3 KB

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