code.js 5.6 KB

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