code.js 4.6 KB

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