You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @Author: Caven
  3. * @Date: 2019-12-31 17:58:01
  4. */
  5. const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(
  6. ''
  7. )
  8. /**
  9. * Some of the code borrows from leaflet
  10. * https://github.com/Leaflet/Leaflet/tree/master/src/core
  11. */
  12. class Util {
  13. /**
  14. * Generates uuid
  15. * @param prefix
  16. * @returns {string}
  17. */
  18. static uuid(prefix = 'D') {
  19. let uuid = []
  20. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  21. uuid[14] = '4'
  22. let r
  23. for (let i = 0; i < 36; i++) {
  24. if (!uuid[i]) {
  25. r = 0 | (Math.random() * 16)
  26. uuid[i] = CHARS[i === 19 ? (r & 0x3) | 0x8 : r]
  27. }
  28. }
  29. return prefix + '-' + uuid.join('')
  30. }
  31. /**
  32. * Merges the properties of the `src` object (or multiple objects) into `dest` object and returns the latter.
  33. * @param dest
  34. * @param sources
  35. * @returns {*}
  36. */
  37. static merge(dest, ...sources) {
  38. let i, j, len, src
  39. for (j = 0, len = sources.length; j < len; j++) {
  40. src = sources[j]
  41. for (i in src) {
  42. dest[i] = src[i]
  43. }
  44. }
  45. return dest
  46. }
  47. /**
  48. * @function splitWords(str: String): String[]
  49. * Trims and splits the string on whitespace and returns the array of parts.
  50. * @param {*} str
  51. */
  52. static splitWords(str) {
  53. return this.trim(str).split(/\s+/)
  54. }
  55. /**
  56. * @function setOptions(obj: Object, options: Object): Object
  57. * Merges the given properties to the `options` of the `obj` object, returning the resulting options. See `Class options`.
  58. * @param {*} obj
  59. * @param {*} options
  60. */
  61. static setOptions(obj, options) {
  62. if (!obj.hasOwnProperty('options')) {
  63. obj.options = obj.options ? Object.create(obj.options) : {}
  64. }
  65. for (let i in options) {
  66. obj.options[i] = options[i]
  67. }
  68. return obj.options
  69. }
  70. /**
  71. * @function formatNum(num: Number, digits?: Number): Number
  72. * Returns the number `num` rounded to `digits` decimals, or to 6 decimals by default.
  73. * @param num
  74. * @param digits
  75. * @returns {number}
  76. */
  77. static formatNum(num, digits) {
  78. let pow = Math.pow(10, digits === undefined ? 6 : digits)
  79. return Math.round(num * pow) / pow
  80. }
  81. /**
  82. * @function trim(str: String): String
  83. * Compatibility polyfill for [String.prototype.trim](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
  84. * @param {*} str
  85. */
  86. static trim(str) {
  87. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '')
  88. }
  89. /**
  90. * Data URI string containing a base64-encoded empty GIF image.
  91. * Used as a hack to free memory from unused images on WebKit-powered
  92. * mobile devices (by setting image `src` to this string).
  93. * @returns {string}
  94. */
  95. static emptyImageUrl() {
  96. return (function() {
  97. return 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
  98. })()
  99. }
  100. /**
  101. * @function checkPosition(position: Object): Boolean
  102. * Check position for validity
  103. * @param {*} position
  104. */
  105. static checkPosition(position) {
  106. return (
  107. position &&
  108. position.hasOwnProperty('_lng') &&
  109. position.hasOwnProperty('_lat') &&
  110. position.hasOwnProperty('_alt')
  111. )
  112. }
  113. /**
  114. * Creates a debounced function that delays invoking `fn` until after `delay`
  115. * @param fn
  116. * @param delay
  117. * @returns {function(): void}
  118. */
  119. static debounce(fn, delay) {
  120. let timer = null
  121. return function() {
  122. timer && clearTimeout(timer)
  123. timer = setTimeout(fn, delay)
  124. }
  125. }
  126. /**
  127. * Creates a throttled function that only invokes `fn` at most once per
  128. * @param fn
  129. * @param delay
  130. * @returns {function(): void}
  131. */
  132. static throttle(fn, delay) {
  133. let valid = true
  134. return function() {
  135. if (!valid) {
  136. return false
  137. }
  138. valid = false
  139. setTimeout(() => {
  140. fn()
  141. valid = true
  142. }, delay)
  143. }
  144. }
  145. /**
  146. *
  147. * @param dataUrl
  148. * @returns {Blob}
  149. */
  150. static dataURLtoBlob(dataUrl) {
  151. let arr = dataUrl.split(',')
  152. let mime = arr[0].match(/:(.*?);/)[1]
  153. let bStr = atob(arr[1])
  154. let len = bStr.length
  155. let u8Arr = new Uint8Array(len)
  156. while (len--) {
  157. u8Arr[len] = bStr.charCodeAt(len)
  158. }
  159. return new Blob([u8Arr], { type: mime })
  160. }
  161. }
  162. export default Util