UNPKG

@sourcegraph/scip-typescript

Version:
37 lines (36 loc) 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHumanByteSizeIntoNumber = parseHumanByteSizeIntoNumber; exports.formatByteSizeAsHumanReadable = formatByteSizeAsHumanReadable; const kilo = 1000; const mega = 1000000; const giga = 1000000000; function parseHumanByteSizeIntoNumber(humanByteSize) { let value = humanByteSize.toLowerCase(); let multiplier = 1; if (value.endsWith('kb')) { multiplier = kilo; value = value.slice(0, -2); } else if (value.endsWith('mb')) { multiplier = mega; value = value.slice(0, -2); } else if (value.endsWith('gb')) { multiplier = giga; value = value.slice(0, -2); } return Number.parseFloat(value) * multiplier; } function formatByteSizeAsHumanReadable(byteSize) { if (byteSize > giga) { return `${byteSize / giga}gb`; } if (byteSize > mega) { return `${byteSize / mega}mb`; } if (byteSize > kilo) { return `${byteSize / kilo}kb`; } return byteSize.toString(); }