utils.js 4.3 KB

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