code.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {
  2. byte2hexStr,
  3. bytesToString,
  4. hextoString,
  5. byteArray2hexStr,
  6. base64DecodeFromString,
  7. base64EncodeToString,
  8. } from './bytes.js';
  9. import type { BytesLike } from '../types/UtilsTypes.js';
  10. export function bin2String(array: BytesLike) {
  11. return bytesToString(array);
  12. }
  13. export function arrayEquals(array1: any[], array2: any[], strict = false) {
  14. if (array1.length != array2.length) return false;
  15. for (let i = 0; i < array1.length; i++) {
  16. if (strict) {
  17. if (array1[i] != array2[i]) return false;
  18. } else if (JSON.stringify(array1[i]) != JSON.stringify(array2[i])) return false;
  19. }
  20. return true;
  21. }
  22. export function stringToBytes(str: string) {
  23. const bytes = [];
  24. const len: number = str.length;
  25. let c: number;
  26. for (let i = 0; i < len; i++) {
  27. c = str.charCodeAt(i);
  28. if (c >= 0x010000 && c <= 0x10ffff) {
  29. bytes.push(((c >> 18) & 0x07) | 0xf0);
  30. bytes.push(((c >> 12) & 0x3f) | 0x80);
  31. bytes.push(((c >> 6) & 0x3f) | 0x80);
  32. bytes.push((c & 0x3f) | 0x80);
  33. } else if (c >= 0x000800 && c <= 0x00ffff) {
  34. bytes.push(((c >> 12) & 0x0f) | 0xe0);
  35. bytes.push(((c >> 6) & 0x3f) | 0x80);
  36. bytes.push((c & 0x3f) | 0x80);
  37. } else if (c >= 0x000080 && c <= 0x0007ff) {
  38. bytes.push(((c >> 6) & 0x1f) | 0xc0);
  39. bytes.push((c & 0x3f) | 0x80);
  40. } else bytes.push(c & 0xff);
  41. }
  42. return bytes;
  43. }
  44. export { byte2hexStr, bytesToString, hextoString, byteArray2hexStr, base64DecodeFromString, base64EncodeToString };
  45. export function hexChar2byte(c: string) {
  46. let d: number | undefined;
  47. if (c >= 'A' && c <= 'F') d = c.charCodeAt(0) - 'A'.charCodeAt(0) + 10;
  48. else if (c >= 'a' && c <= 'f') d = c.charCodeAt(0) - 'a'.charCodeAt(0) + 10;
  49. else if (c >= '0' && c <= '9') d = c.charCodeAt(0) - '0'.charCodeAt(0);
  50. if (typeof d === 'number') return d;
  51. else throw new Error('The passed hex char is not a valid hex char');
  52. }
  53. export function isHexChar(c: string) {
  54. if ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9')) {
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. // set strict as true: if the length of str is odd, add 0 before the str to make its length as even
  60. export function hexStr2byteArray(str: string, strict = false) {
  61. let len = str.length;
  62. if (strict) {
  63. if (len % 2) {
  64. str = `0${str}`;
  65. len++;
  66. }
  67. }
  68. const byteArray: number[] = [];
  69. let d = 0;
  70. let j = 0;
  71. let k = 0;
  72. for (let i = 0; i < len; i++) {
  73. const c = str.charAt(i);
  74. if (isHexChar(c)) {
  75. d <<= 4;
  76. d += hexChar2byte(c);
  77. j++;
  78. if (0 === j % 2) {
  79. byteArray[k++] = d;
  80. d = 0;
  81. }
  82. } else throw new Error('The passed hex char is not a valid hex string');
  83. }
  84. return byteArray;
  85. }
  86. //yyyy-MM-DD HH-mm-ss
  87. export function strToDate(str: string) {
  88. if (!/^\d{4}-\d{2}-\d{2}( \d{2}-\d{2}-\d{2}|)/.test(str)) throw new Error('The passed date string is not valid');
  89. const tempStrs = str.split(' ');
  90. const dateStrs = tempStrs[0].split('-');
  91. const year = parseInt(dateStrs[0], 10);
  92. const month = parseInt(dateStrs[1], 10) - 1;
  93. const day = parseInt(dateStrs[2], 10);
  94. if (tempStrs.length > 1) {
  95. const timeStrs = tempStrs[1].split('-');
  96. const hour = parseInt(timeStrs[0], 10);
  97. const minute = parseInt(timeStrs[1], 10);
  98. const second = parseInt(timeStrs[2], 10);
  99. return new Date(year, month, day, hour, minute, second);
  100. }
  101. return new Date(year, month, day);
  102. }
  103. export function isNumber(c: string) {
  104. if (c >= '0' && c <= '9') return 1;
  105. return 0;
  106. }
  107. //return 1: address --- 20Bytes HexString
  108. //return 2: blockNumber ------ Decimal number
  109. //return 3: assetName ------ String
  110. //return other: error
  111. export function getStringType(str: string) {
  112. if (null == str) return -1;
  113. if (str.length == 0 || str == '') return -1;
  114. let i = 0;
  115. if (str.length == 40) {
  116. for (; i < 40; i++) {
  117. const c = str.charAt(i);
  118. if (!isHexChar(c)) break;
  119. }
  120. }
  121. if (i == 40) return 1; //40 Hex, Address
  122. for (i = 0; i < str.length; i++) {
  123. const c = str.charAt(i);
  124. if (!isNumber(c)) break;
  125. }
  126. if (i == str.length) return 2; // All Decimal number, BlockNumber
  127. for (i = 0; i < str.length; i++) {
  128. const c = str.charAt(i);
  129. if (c > ' ') return 3; // At least one visible character
  130. }
  131. return -1;
  132. }