util.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function zero2(word) {
  4. if (word.length === 1) {
  5. return '0' + word;
  6. }
  7. else {
  8. return word;
  9. }
  10. }
  11. function toArray(msg, enc) {
  12. if (Array.isArray(msg)) {
  13. return msg.slice();
  14. }
  15. if (!msg) {
  16. return [];
  17. }
  18. const res = [];
  19. if (typeof msg !== 'string') {
  20. for (let i = 0; i < msg.length; i++) {
  21. res[i] = msg[i] | 0;
  22. }
  23. return res;
  24. }
  25. if (enc === 'hex') {
  26. msg = msg.replace(/[^a-z0-9]+/ig, '');
  27. if (msg.length % 2 !== 0) {
  28. msg = '0' + msg;
  29. }
  30. for (let i = 0; i < msg.length; i += 2) {
  31. res.push(parseInt(msg[i] + msg[i + 1], 16));
  32. }
  33. }
  34. else {
  35. for (let i = 0; i < msg.length; i++) {
  36. const c = msg.charCodeAt(i);
  37. const hi = c >> 8;
  38. const lo = c & 0xff;
  39. if (hi) {
  40. res.push(hi, lo);
  41. }
  42. else {
  43. res.push(lo);
  44. }
  45. }
  46. }
  47. return res;
  48. }
  49. exports.toArray = toArray;
  50. function encodeHex(msg) {
  51. let res = '';
  52. for (let i = 0; i < msg.length; i++) {
  53. res += zero2(msg[i].toString(16));
  54. }
  55. return res;
  56. }
  57. exports.encodeHex = encodeHex;
  58. function uuid() {
  59. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
  60. // tslint:disable-next-line: no-bitwise
  61. const random = Math.random() * 16 | 0;
  62. const value = char === "x" ? random : (random % 4 + 8);
  63. return value.toString(16);
  64. });
  65. }
  66. exports.uuid = uuid;
  67. function isObject(item) {
  68. return (item && typeof item === 'object' && !Array.isArray(item));
  69. }
  70. exports.isObject = isObject;
  71. function mergeDeep(target, source) {
  72. const output = Object.assign({}, target);
  73. if (isObject(target) && isObject(source)) {
  74. Object.keys(source).forEach(key => {
  75. if (isObject(source[key])) {
  76. if (!(key in target)) {
  77. Object.assign(output, { [key]: source[key] });
  78. }
  79. else {
  80. output[key] = mergeDeep(target[key], source[key]);
  81. }
  82. }
  83. else {
  84. Object.assign(output, { [key]: source[key] });
  85. }
  86. });
  87. }
  88. return output;
  89. }
  90. exports.mergeDeep = mergeDeep;
  91. //# sourceMappingURL=util.js.map