utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import _ from 'lodash';
  2. import baseX from 'base-x';
  3. import PAKO from 'pako';
  4. import {Buffer} from 'safe-buffer';
  5. export const pako = PAKO;
  6. const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  7. const BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  8. const bs58 = baseX(BASE58);
  9. const bs64 = baseX(BASE64);
  10. export function sleep(ms) {
  11. return new Promise(resolve => setTimeout(resolve, ms));
  12. }
  13. export function stringToHex(str) {
  14. return Buffer.from(str).toString('hex');
  15. }
  16. export function hexToString(str) {
  17. return Buffer.from(str, 'hex').toString();
  18. }
  19. export function randomArray(len) {
  20. const a = new Uint8Array(len);
  21. window.crypto.getRandomValues(a);
  22. return a;
  23. }
  24. export function randomHexString(len) {
  25. return Buffer.from(randomArray(len)).toString('hex');
  26. }
  27. export function formatDate(d, format) {
  28. if (!format)
  29. format = 'normal';
  30. switch (format) {
  31. case 'normal':
  32. return `${d.getDate().toString().padStart(2, '0')}.${(d.getMonth() + 1).toString().padStart(2, '0')}.${d.getFullYear()} ` +
  33. `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`;
  34. }
  35. }
  36. export function fallbackCopyTextToClipboard(text) {
  37. let textArea = document.createElement('textarea');
  38. textArea.value = text;
  39. document.body.appendChild(textArea);
  40. textArea.focus();
  41. textArea.select();
  42. let result = false;
  43. try {
  44. result = document.execCommand('copy');
  45. } catch (e) {
  46. //
  47. }
  48. document.body.removeChild(textArea);
  49. return result;
  50. }
  51. export async function copyTextToClipboard(text) {
  52. if (!navigator.clipboard) {
  53. return fallbackCopyTextToClipboard(text);
  54. }
  55. let result = false;
  56. try {
  57. await navigator.clipboard.writeText(text);
  58. result = true;
  59. } catch (e) {
  60. //
  61. }
  62. return result;
  63. }
  64. export function toBase58(data) {
  65. return bs58.encode(Buffer.from(data));
  66. }
  67. export function fromBase58(data) {
  68. return bs58.decode(data);
  69. }
  70. export function toBase64(data) {
  71. return bs64.encode(Buffer.from(data));
  72. }
  73. export function fromBase64(data) {
  74. return bs64.decode(data);
  75. }
  76. export function getObjDiff(oldObj, newObj) {
  77. const result = {__isDiff: true, change: {}, add: {}, del: []};
  78. for (const key of Object.keys(oldObj)) {
  79. if (newObj.hasOwnProperty(key)) {
  80. if (!_.isEqual(oldObj[key], newObj[key])) {
  81. if (_.isObject(oldObj[key]) && _.isObject(newObj[key])) {
  82. result.change[key] = getObjDiff(oldObj[key], newObj[key]);
  83. } else {
  84. result.change[key] = _.cloneDeep(newObj[key]);
  85. }
  86. }
  87. } else {
  88. result.del.push(key);
  89. }
  90. }
  91. for (const key of Object.keys(newObj)) {
  92. if (!oldObj.hasOwnProperty(key)) {
  93. result.add[key] = _.cloneDeep(newObj[key]);
  94. }
  95. }
  96. return result;
  97. }
  98. export function isEmptyObjDiff(diff) {
  99. return (!_.isObject(diff) || !diff.__isDiff ||
  100. (!Object.keys(diff.change).length &&
  101. !Object.keys(diff.add).length &&
  102. !diff.del.length
  103. )
  104. );
  105. }
  106. export function applyObjDiff(obj, diff, isAddChanged) {
  107. const result = _.cloneDeep(obj);
  108. if (!diff.__isDiff)
  109. return result;
  110. const change = diff.change;
  111. for (const key of Object.keys(change)) {
  112. if (result.hasOwnProperty(key)) {
  113. if (_.isObject(change[key])) {
  114. result[key] = applyObjDiff(result[key], change[key], isAddChanged);
  115. } else {
  116. result[key] = _.cloneDeep(change[key]);
  117. }
  118. } else if (isAddChanged) {
  119. result[key] = _.cloneDeep(change[key]);
  120. }
  121. }
  122. for (const key of Object.keys(diff.add)) {
  123. result[key] = _.cloneDeep(diff.add[key]);
  124. }
  125. for (const key of diff.del) {
  126. delete result[key];
  127. }
  128. return result;
  129. }