UNPKG

@amaui/utils

Version:
32 lines (31 loc) 1.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const isValid_1 = __importDefault(require("./isValid")); const binaryStringToHexadecimal = (value_) => { if ((0, isValid_1.default)('binary-string', value_)) { let value = ''; const parts = value_.match(/.{1,4}/g) || []; for (const [index, part] of parts.entries()) { const decimal = [...part].reduce((result, item) => result = (result * 2) + parseInt(item, 10), 0); const hexadecimal = decimal.toString(16); if (index === parts.length - 1) { if (part.length < 4) { if ((part.length === 1 && ['0', '1'].indexOf(hexadecimal) > -1) || (+hexadecimal > 1 && +hexadecimal <= 7)) value = '02' + value + hexadecimal; else value = '1' + String(part.length) + value + part; } else value = '00' + value + hexadecimal; } else value += hexadecimal; } return value; } }; exports.default = binaryStringToHexadecimal;