Util.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  2. /**
  3. * 工具类
  4. * 代码使用的leaflet开源工具
  5. * https://github.com/Leaflet/Leaflet/tree/master/src/core
  6. * @ignore
  7. */
  8. class Util {
  9. /**
  10. * generate uuid
  11. * @ignore
  12. * @param {*} prefix
  13. *
  14. */
  15. static uuid(prefix = 'D') {
  16. let uuid = []
  17. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  18. uuid[14] = '4'
  19. let r
  20. for (let i = 0; i < 36; i++) {
  21. if (!uuid[i]) {
  22. r = 0 | (Math.random() * 16)
  23. uuid[i] = CHARS[i == 19 ? (r & 0x3) | 0x8 : r]
  24. }
  25. }
  26. return prefix + '-' + uuid.join('')
  27. }
  28. /**
  29. *
  30. * Merges the properties of the `src` object (or multiple objects) into `dest` object and returns the latter.
  31. * @ignore
  32. * @param {*} dest
  33. * @param {*} sources
  34. *
  35. */
  36. static merge(dest, ...sources) {
  37. let i, j, len, src
  38. for (j = 0, len = sources.length; j < len; j++) {
  39. src = sources[j]
  40. for (i in src) {
  41. dest[i] = src[i]
  42. }
  43. }
  44. return dest
  45. }
  46. /**
  47. *
  48. * Compatibility polyfill for [String.prototype.trim](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
  49. * @ignore
  50. * @param {*} str
  51. *
  52. */
  53. static trim(str) {
  54. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '')
  55. }
  56. /**
  57. * Trims and splits the string on whitespace and returns the array of parts.
  58. * @ignore
  59. * @param {*} str
  60. *
  61. */
  62. static splitWords(str) {
  63. return this.trim(str).split(/\s+/)
  64. }
  65. /**
  66. *
  67. * Merges the given properties to the `options` of the `obj` object, returning the resulting options. See `Class options`.
  68. * @ignore
  69. * @param {*} obj
  70. * @param {*} options
  71. *
  72. */
  73. static setOptions(obj, options) {
  74. if (!obj.hasOwnProperty('options')) {
  75. obj.options = obj.options ? create(obj.options) : {}
  76. }
  77. for (var i in options) {
  78. obj.options[i] = options[i]
  79. }
  80. return obj.options
  81. }
  82. /**
  83. * Check position for validity
  84. * @ignore
  85. * @param {*} position
  86. */
  87. static checkPosition(position) {
  88. return (
  89. position &&
  90. position.hasOwnProperty('_lng') &&
  91. position.hasOwnProperty('_lat') &&
  92. position.hasOwnProperty('_alt')
  93. )
  94. }
  95. /**
  96. * Check positions for validity
  97. * @ignore
  98. * @param {*} position
  99. */
  100. static checkPositions(positions) {
  101. return (
  102. positions && (typeof positions === 'string' || Array.isArray(positions))
  103. )
  104. }
  105. /**
  106. * Check viewer for validity
  107. * @ignore
  108. * @param {*} position
  109. */
  110. static checkViewer(viewer) {
  111. return viewer && viewer.delegate && viewer.canvas
  112. }
  113. }
  114. export default Util