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.

DC.Util.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * @Author: Caven
  3. * @Date: 2019-12-31 17:58:01
  4. * @Last Modified by: Caven
  5. * @Last Modified time: 2020-01-21 15:54:00
  6. */
  7. const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  8. /**
  9. * 工具类
  10. * 部分代码借鉴leaflet
  11. * https://github.com/Leaflet/Leaflet/tree/master/src/core
  12. */
  13. DC.Util = class {
  14. /**
  15. *
  16. * @param {*} prefix
  17. * generate uuid
  18. */
  19. static uuid(prefix = 'D') {
  20. let uuid = []
  21. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  22. uuid[14] = '4'
  23. let r
  24. for (let i = 0; i < 36; i++) {
  25. if (!uuid[i]) {
  26. r = 0 | (Math.random() * 16)
  27. uuid[i] = CHARS[i == 19 ? (r & 0x3) | 0x8 : r]
  28. }
  29. }
  30. return prefix + '-' + uuid.join('')
  31. }
  32. /**
  33. *
  34. * @param {*} dest
  35. * @param {*} sources
  36. * Merges the properties of the `src` object (or multiple objects) into `dest` object and returns the latter.
  37. */
  38. static merge(dest, ...sources) {
  39. let i, j, len, src
  40. for (j = 0, len = sources.length; j < len; j++) {
  41. src = sources[j]
  42. for (i in src) {
  43. dest[i] = src[i]
  44. }
  45. }
  46. return dest
  47. }
  48. /**
  49. *
  50. * @param {*} str
  51. * Compatibility polyfill for [String.prototype.trim](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
  52. */
  53. static trim(str) {
  54. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '')
  55. }
  56. /**
  57. *
  58. * @param {*} str
  59. * Trims and splits the string on whitespace and returns the array of parts.
  60. */
  61. static splitWords(str) {
  62. return this.trim(str).split(/\s+/)
  63. }
  64. }