UNPKG

hyjs-test

Version:
76 lines (75 loc) 2.16 kB
import BN from 'bignumber.js'; BN.config({ EXPONENTIAL_AT: 1000 }); export const toBN = (x) => { if (isNaN(Number(x))) return new BN(0); if (x instanceof BN) return x; if (typeof x === 'string') { if (x.indexOf('0x') === 0 || x.indexOf('-0x') === 0) { return new BN((x).replace('0x', ''), 16); } } return new BN(x); }; export const baseToTags = (b) => { return [ { name: 'Data-Protocol', value: b['Data-Protocol'] }, { name: 'Variant', value: b.Variant }, { name: 'Type', value: b.Type } ]; }; 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); } }; export const messageToTags = (msg) => { var _a; let tags = 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; }; export 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; }; export const getDefaultBase = (defaultBaseType, hmInfo) => { return { 'Data-Protocol': hmInfo.Protocol, Variant: hmInfo.Variant, Type: defaultBaseType }; };