util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Codes from "./enum/Codes.js"
  2. import CodeErrorMap from "./error/CodeErrorMap.js"
  3. import InternalError from "./error/InternalError.js"
  4. var util = {
  5. uuid() {
  6. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, i=>{
  7. const e = Math.random() * 16 | 0;
  8. return (i === "x" ? e : e & 3 | 8).toString(16)
  9. }
  10. )
  11. },
  12. getFormattedDate(i) {
  13. const e = i.getMonth() + 1
  14. , t = i.getDate()
  15. , r = i.getHours()
  16. , n = i.getMinutes()
  17. , o = i.getSeconds()
  18. , a = i.getMilliseconds()
  19. , s = (e < 10 ? "0" : "") + e
  20. , l = (t < 10 ? "0" : "") + t
  21. , u = (r < 10 ? "0" : "") + r
  22. , c = (n < 10 ? "0" : "") + n
  23. , h = (o < 10 ? "0" : "") + o;
  24. return i.getFullYear() + "-" + s + "-" + l + " " + u + ":" + c + ":" + h + "." + a
  25. },
  26. createInstance(i) {
  27. var e = new Axios(i)
  28. , t = bind(Axios.prototype.request, e);
  29. return utils.extend(t, Axios.prototype, e),
  30. utils.extend(t, e),
  31. t.create = function(n) {
  32. return createInstance(mergeConfig(i, n))
  33. }
  34. ,
  35. t
  36. },
  37. mapLimit(i, e, t) {
  38. return new Promise((r,n)=>{
  39. const o = i.length;
  40. let a = e - 1
  41. , s = 0;
  42. const l = u=>{
  43. u.forEach(c=>{
  44. t(c).then(()=>{
  45. if (s++,
  46. s === o) {
  47. r();
  48. return
  49. }
  50. a++;
  51. const h = i[a];
  52. h && l([h])
  53. }
  54. , h=>{
  55. n(h)
  56. }
  57. )
  58. }
  59. )
  60. }
  61. ;
  62. l(i.slice(0, e))
  63. }
  64. )
  65. },
  66. isWebAssemblySupported(){
  67. try {
  68. if (typeof WebAssembly == "object" && typeof WebAssembly.instantiate == "function") {
  69. const i = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
  70. if (i instanceof WebAssembly.Module)
  71. return new WebAssembly.Instance(i)instanceof WebAssembly.Instance
  72. }
  73. } catch {}
  74. return console.log("wasm is not supported"),
  75. !1
  76. },
  77. isSupported() {
  78. return typeof RTCPeerConnection == "function" && this.isWebAssemblySupported()
  79. },
  80. objectParseFloat(i){
  81. const e = {};
  82. return i && Object.keys(i).forEach(t=>{
  83. e[t] = parseFloat(i[t])
  84. }
  85. ),
  86. e
  87. },
  88. getRandomItem(i){
  89. if(i.length === 0){
  90. return null
  91. }
  92. else{
  93. return i[Math.floor(Math.random() * i.length)]
  94. }
  95. //i.length === 0 ? null : i[Math.floor(Math.random() * i.length)]
  96. },
  97. getErrorByCode(i) {
  98. if (i === Codes.Success)
  99. return InternalError;
  100. const e = CodeErrorMap[i];
  101. return e || console.warn("unkown code", i),
  102. e || InternalError
  103. },
  104. getDistance(i, e) {
  105. const {x: t, y: r, z: n} = i
  106. , {x: o, y: a, z: s} = e;
  107. return Math.sqrt(Math.abs(t - o) ** 2 + Math.abs(r - a) ** 2 + Math.abs(n - s) ** 2)
  108. }
  109. }
  110. export default util