tools.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const ARR = "Array";
  2. const NUMBER = "Number";
  3. const STRING = "String";
  4. const OBJECT = "Object";
  5. const UNDEFINED = "Undefined";
  6. const NULL = "Null";
  7. const BOOLEAN = "Boolean";
  8. // 数据类型判断
  9. const Type = {
  10. typeConstent: {
  11. 'array': ARR,
  12. 'number': NUMBER,
  13. 'string': STRING,
  14. 'object': OBJECT,
  15. 'undefined': UNDEFINED,
  16. 'null': NULL,
  17. 'boolean': BOOLEAN
  18. },
  19. tolower: (str)=> {
  20. if(typeof str !== "string") {
  21. return "";
  22. }
  23. return str.toLowerCase();
  24. },
  25. checkType: (el, type)=> {
  26. type = Type.tolower(type);
  27. type = Type.typeConstent[type];
  28. return Object.prototype.toString.call(el) === "[object "+ type +"]";
  29. }
  30. }
  31. // 判断字符串长度
  32. const regexp = {
  33. ch: (str)=> {//匹配中文
  34. if(typeof str !== "string") {
  35. return "";
  36. }
  37. return str.match(/[\u4e00-\u9fa5]/g) || "";
  38. },
  39. chLen: function (str) {//中文长度
  40. return this.ch(str).length;
  41. },
  42. strLen: function (str) {//字符串长度(一个中文当作两个英文字母)
  43. if(typeof str !== "string") {
  44. return 0;
  45. }
  46. return str.length + this.chLen(str);
  47. }
  48. }
  49. //广度遍历整个数据,递归查找pid
  50. function findNode(data, pid, parentData) {
  51. const travelWidely = (roots) => {
  52. const queue = [...roots];
  53. while (queue.length) {
  54. const node = queue.shift();
  55. //打印被遍历的一个节点
  56. if (node.id === pid) {
  57. //记录到
  58. parentData.push(node)
  59. return node;
  60. }
  61. if (node === undefined) {
  62. return;
  63. };
  64. if (node.children && node.children.length) {
  65. queue.push(...node.children)
  66. }
  67. }
  68. }
  69. return travelWidely(data)
  70. }
  71. function findAllParentByPid(data, pid, parentData) {
  72. let thisParent = findNode(data, pid, parentData)
  73. if (thisParent.hasOwnProperty("pid")) {
  74. findAllParentByPid(data, thisParent.pid, parentData)
  75. }
  76. }
  77. const clone = {
  78. getType: function(obj){
  79. //tostring会返回对应不同的标签的构造函数
  80. var toString = Object.prototype.toString;
  81. var map = {
  82. '[object Boolean]' : 'boolean',
  83. '[object Number]' : 'number',
  84. '[object String]' : 'string',
  85. '[object Function]' : 'function',
  86. '[object Array]' : 'array',
  87. '[object Date]' : 'date',
  88. '[object RegExp]' : 'regExp',
  89. '[object Undefined]': 'undefined',
  90. '[object Null]' : 'null',
  91. '[object Object]' : 'object'
  92. };
  93. if(obj instanceof Element) {
  94. return 'element';
  95. }
  96. return map[toString.call(obj)];
  97. },
  98. deep: function(data){
  99. var type = clone.getType(data);
  100. var obj = null;
  101. if(type === 'array'){
  102. obj = [];
  103. } else if(type === 'object'){
  104. obj = {};
  105. } else {
  106. //不再具有下一层次
  107. return data;
  108. }
  109. if(type === 'array'){
  110. for(var i = 0, len = data.length; i < len; i++){
  111. obj.push(clone.deep(data[i]));
  112. }
  113. } else if(type === 'object'){
  114. for(var key in data){
  115. obj[key] = clone.deep(data[key]);
  116. }
  117. }
  118. return obj;
  119. }
  120. }
  121. const isUndefined = function (str) {
  122. if(str === 0 || str === "") {
  123. return false;
  124. }
  125. return (Type.checkType(str, 'null') || Type.checkType(str, 'undefined'));
  126. }
  127. const Storage = {
  128. session: {
  129. get: function (name) {
  130. return JSON.parse(sessionStorage.getItem(name));
  131. },
  132. set: function (name, data) {
  133. sessionStorage.setItem(name, JSON.stringify(data));
  134. },
  135. remove: function (name) {
  136. sessionStorage.removeItem(name);
  137. },
  138. clear: function () {
  139. sessionStorage.clear();
  140. }
  141. }
  142. }
  143. module.exports = {
  144. checkType: Type.checkType,
  145. regexp,
  146. findAllParentByPid,
  147. clone,
  148. isUndefined,
  149. Storage
  150. }