hyjs-test
Version:
first hymatrix-js sdk
87 lines (86 loc) • 2.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultBase = exports.mergeTags = exports.messageToTags = exports.baseToTags = exports.toBN = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
bignumber_js_1.default.config({
EXPONENTIAL_AT: 1000
});
const toBN = (x) => {
if (isNaN(Number(x)))
return new bignumber_js_1.default(0);
if (x instanceof bignumber_js_1.default)
return x;
if (typeof x === 'string') {
if (x.indexOf('0x') === 0 || x.indexOf('-0x') === 0) {
return new bignumber_js_1.default((x).replace('0x', ''), 16);
}
}
return new bignumber_js_1.default(x);
};
exports.toBN = toBN;
const baseToTags = (b) => {
return [
{ name: 'Data-Protocol', value: b['Data-Protocol'] },
{ name: 'Variant', value: b.Variant },
{ name: 'Type', value: b.Type }
];
};
exports.baseToTags = baseToTags;
const checkDuplicateTags = (tags) => {
const seen = new Set();
for (const tag of tags) {
if (seen.has(tag.name)) {
throw new Error(`duplicate tag: ${tag.name}`);
}
seen.add(tag.name);
}
};
const messageToTags = (msg) => {
var _a;
let tags = (0, exports.baseToTags)(msg.Base);
if (msg.Action !== '') {
tags.push({ name: 'Action', value: msg.Action });
}
if (msg['From-Process'] !== '') {
tags.push({ name: 'From-Process', value: (_a = msg['From-Process']) !== null && _a !== void 0 ? _a : '' });
}
if (msg['Pushed-For'] !== '') {
tags.push({ name: 'PushedFor', value: msg['Pushed-For'] });
}
if (msg.Sequence !== '') {
tags.push({ name: 'Sequence', value: msg.Sequence });
}
if (msg.Tags != null) {
tags = tags.concat(msg.Tags);
}
checkDuplicateTags(tags);
return tags;
};
exports.messageToTags = messageToTags;
const mergeTags = (tags1, tags2) => {
const tagMap = new Map();
// 先放入 tags1 的内容,保留它们的优先级
for (const tag of tags1) {
tagMap.set(tag.name, tag.value);
}
// 加入 tags2 的项,如果 name 不存在才加
for (const tag of tags2) {
if (!tagMap.has(tag.name)) {
tagMap.set(tag.name, tag.value);
tags1.push(tag); // 保留原始结构
}
}
return tags1;
};
exports.mergeTags = mergeTags;
const getDefaultBase = (defaultBaseType, hmInfo) => {
return {
'Data-Protocol': hmInfo.Protocol,
Variant: hmInfo.Variant,
Type: defaultBaseType
};
};
exports.getDefaultBase = getDefaultBase;