utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @_ignore
  3. */
  4. import { getBytesCopy, assertArgument, toUtf8Bytes } from "../utils/index.js";
  5. export function looseArrayify(hexString) {
  6. if (typeof (hexString) === "string" && !hexString.startsWith("0x")) {
  7. hexString = "0x" + hexString;
  8. }
  9. return getBytesCopy(hexString);
  10. }
  11. export function zpad(value, length) {
  12. value = String(value);
  13. while (value.length < length) {
  14. value = '0' + value;
  15. }
  16. return value;
  17. }
  18. export function getPassword(password) {
  19. if (typeof (password) === 'string') {
  20. return toUtf8Bytes(password, "NFKC");
  21. }
  22. return getBytesCopy(password);
  23. }
  24. export function spelunk(object, _path) {
  25. const match = _path.match(/^([a-z0-9$_.-]*)(:([a-z]+))?(!)?$/i);
  26. assertArgument(match != null, "invalid path", "path", _path);
  27. const path = match[1];
  28. const type = match[3];
  29. const reqd = (match[4] === "!");
  30. let cur = object;
  31. for (const comp of path.toLowerCase().split('.')) {
  32. // Search for a child object with a case-insensitive matching key
  33. if (Array.isArray(cur)) {
  34. if (!comp.match(/^[0-9]+$/)) {
  35. break;
  36. }
  37. cur = cur[parseInt(comp)];
  38. }
  39. else if (typeof (cur) === "object") {
  40. let found = null;
  41. for (const key in cur) {
  42. if (key.toLowerCase() === comp) {
  43. found = cur[key];
  44. break;
  45. }
  46. }
  47. cur = found;
  48. }
  49. else {
  50. cur = null;
  51. }
  52. if (cur == null) {
  53. break;
  54. }
  55. }
  56. assertArgument(!reqd || cur != null, "missing required value", "path", path);
  57. if (type && cur != null) {
  58. if (type === "int") {
  59. if (typeof (cur) === "string" && cur.match(/^-?[0-9]+$/)) {
  60. return parseInt(cur);
  61. }
  62. else if (Number.isSafeInteger(cur)) {
  63. return cur;
  64. }
  65. }
  66. if (type === "number") {
  67. if (typeof (cur) === "string" && cur.match(/^-?[0-9.]*$/)) {
  68. return parseFloat(cur);
  69. }
  70. }
  71. if (type === "data") {
  72. if (typeof (cur) === "string") {
  73. return looseArrayify(cur);
  74. }
  75. }
  76. if (type === "array" && Array.isArray(cur)) {
  77. return cur;
  78. }
  79. if (type === typeof (cur)) {
  80. return cur;
  81. }
  82. assertArgument(false, `wrong type found for ${type} `, "path", path);
  83. }
  84. return cur;
  85. }
  86. /*
  87. export function follow(object: any, path: string): null | string {
  88. let currentChild = object;
  89. for (const comp of path.toLowerCase().split('/')) {
  90. // Search for a child object with a case-insensitive matching key
  91. let matchingChild = null;
  92. for (const key in currentChild) {
  93. if (key.toLowerCase() === comp) {
  94. matchingChild = currentChild[key];
  95. break;
  96. }
  97. }
  98. if (matchingChild === null) { return null; }
  99. currentChild = matchingChild;
  100. }
  101. return currentChild;
  102. }
  103. // "path/to/something:type!"
  104. export function followRequired(data: any, path: string): string {
  105. const value = follow(data, path);
  106. if (value != null) { return value; }
  107. return logger.throwArgumentError("invalid value", `data:${ path }`,
  108. JSON.stringify(data));
  109. }
  110. */
  111. // See: https://www.ietf.org/rfc/rfc4122.txt (Section 4.4)
  112. /*
  113. export function uuidV4(randomBytes: BytesLike): string {
  114. const bytes = getBytes(randomBytes, "randomBytes");
  115. // Section: 4.1.3:
  116. // - time_hi_and_version[12:16] = 0b0100
  117. bytes[6] = (bytes[6] & 0x0f) | 0x40;
  118. // Section 4.4
  119. // - clock_seq_hi_and_reserved[6] = 0b0
  120. // - clock_seq_hi_and_reserved[7] = 0b1
  121. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  122. const value = hexlify(bytes);
  123. return [
  124. value.substring(2, 10),
  125. value.substring(10, 14),
  126. value.substring(14, 18),
  127. value.substring(18, 22),
  128. value.substring(22, 34),
  129. ].join("-");
  130. }
  131. */
  132. //# sourceMappingURL=utils.js.map