util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * 检验数据是否为空
  3. */
  4. export const isEmpty = obj => {
  5. if (obj === '' || obj === null || obj === undefined) {
  6. return true
  7. } else if (obj.constructor === Array && obj.length === 0) {
  8. return true
  9. } else if (obj.constructor === Object && Object.keys(obj).length === 0) {
  10. return true
  11. } else {
  12. return false
  13. }
  14. }
  15. /**
  16. * 生成UUID
  17. */
  18. export const generateUUID = () => {
  19. let d = new Date().getTime()
  20. let uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  21. // let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  22. let r = (d + Math.random() * 16) % 16 | 0
  23. d = Math.floor(d / 16)
  24. return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
  25. })
  26. return uuid
  27. }
  28. /**
  29. * 移除空字符
  30. */
  31. export const removeNullCharacter = str => {
  32. return str.replace(/\\u([0-9]|[a-fA-F])([0-9]|[a-fA-F])([0-9]|[a-fA-F])([0-9]|[a-fA-F])/g, '')
  33. }
  34. /**
  35. * 格式化空字符为空字符串
  36. */
  37. export const formatNullCharacter = str => {
  38. if (!str) return ''
  39. return JSON.parse(removeNullCharacter(JSON.stringify(str)))
  40. }
  41. /**
  42. * 节流函数
  43. * fn是我们需要包装的事件回调, interval是时间间隔的阈值
  44. */
  45. export const throttle = (fn, interval) => {
  46. // last为上一次触发回调的时间
  47. let last = 0
  48. interval = interval || 1000
  49. // 将throttle处理结果当作函数返回
  50. return function() {
  51. // 保留调用时的this上下文
  52. let context = this
  53. // 保留调用时传入的参数
  54. let args = arguments
  55. // 记录本次触发回调的时间
  56. let now = +new Date()
  57. // 判断上次触发的时间和本次触发的时间差是否小于时间间隔的阈值
  58. if (now - last >= interval) {
  59. // 如果时间间隔大于我们设定的时间间隔阈值,则执行回调
  60. last = now
  61. fn.apply(context, args)
  62. }
  63. }
  64. }
  65. /**
  66. * 格式化得到aid值
  67. * @param {Object} buffer
  68. */
  69. export const ab2hex = function(buffer) {
  70. let hexArr = Array.prototype.map.call(
  71. new Uint8Array(buffer),
  72. function(bit) {
  73. return ('00' + bit.toString(16)).slice(-2);
  74. }
  75. );
  76. return hexArr.join('');
  77. };
  78. /**
  79. * 字符串转ArrayBuffer
  80. * @param {Object} str
  81. */
  82. export function stringToArrayBuffer(str) {
  83. var bytes = new Array();
  84. let len, c;
  85. len = str.length;
  86. for (let i = 0; i < len; i++) {
  87. c = str.charCodeAt(i);
  88. if (c >= 0x010000 && c <= 0x10FFFF) {
  89. bytes.push(((c >> 18) & 0x07) | 0xF0);
  90. bytes.push(((c >> 12) & 0x3F) | 0x80);
  91. bytes.push(((c >> 6) & 0x3F) | 0x80);
  92. bytes.push((c & 0x3F) | 0x80);
  93. } else if (c >= 0x000800 && c <= 0x00FFFF) {
  94. bytes.push(((c >> 12) & 0x0F) | 0xE0);
  95. bytes.push(((c >> 6) & 0x3F) | 0x80);
  96. bytes.push((c & 0x3F) | 0x80);
  97. } else if (c >= 0x000080 && c <= 0x0007FF) {
  98. bytes.push(((c >> 6) & 0x1F) | 0xC0);
  99. bytes.push((c & 0x3F) | 0x80);
  100. } else {
  101. bytes.push(c & 0xFF);
  102. }
  103. }
  104. let array = new Int8Array(bytes.length);
  105. for (let i in bytes) {
  106. array[i] = bytes[i];
  107. }
  108. return array.buffer;
  109. }
  110. /**
  111. * ArrayBuffer转字符串
  112. * @param {Object} arr
  113. */
  114. export function arrayBufferToString(arr) {
  115. if (typeof arr === 'string') {
  116. return arr;
  117. }
  118. let dataview = new DataView(arr);
  119. let ints = new Uint8Array(arr.byteLength);
  120. for (let i = 0; i < ints.length; i++) {
  121. ints[i] = dataview.getUint8(i);
  122. }
  123. arr = ints;
  124. let str = '',
  125. _arr = arr;
  126. for (let i = 0; i < _arr.length; i++) {
  127. let one = _arr[i].toString(2),
  128. v = one.match(/^1+?(?=0)/);
  129. if (v && one.length == 8) {
  130. let bytesLength = v[0].length;
  131. let store = _arr[i].toString(2).slice(7 - bytesLength);
  132. for (let st = 1; st < bytesLength; st++) {
  133. store += _arr[st + i].toString(2).slice(2);
  134. }
  135. str += String.fromCharCode(parseInt(store, 2));
  136. i += bytesLength - 1;
  137. } else {
  138. str += String.fromCharCode(_arr[i]);
  139. }
  140. }
  141. return str;
  142. }
  143. export function byteLength(str) {
  144. // 匹配所有的单字节字符和双字节字符
  145. const matches = str.match(/[^\x00-\xff]/g);
  146. // 计算字符串的字节长度
  147. return str.length + (matches ? matches.length : 0);
  148. }