UNPKG

kontainer-js

Version:

A media file format generator/parser that exposes a React-like API.

44 lines (40 loc) 1.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNegative = isNegative; exports.convertToNegative = convertToNegative; exports.throwException = throwException; exports.getUTF8Length = getUTF8Length; function isNegative(value, bitLength) { return !!(value & 1 << bitLength - 1); } function convertToNegative(value, bitLength) { var mask = (1 << bitLength) - 1; return -((~value & mask) + 1); } function throwException(message) { throw new Error(message); } function getUTF8Length(string) { return string.split('').map(function (ch) { var charCode = ch.charCodeAt(0); if (charCode < 0x80) { return 1; } else if (charCode >= 0x80 && charCode < 0x800) { return 2; } else if (charCode >= 0x800 && charCode < 0x10000) { return 3; } else if (charCode >= 0x10000 && charCode < 0x200000) { return 4; } else if (charCode >= 0x200000 && charCode < 0x4000000) { return 5; } else if (charCode >= 0x4000000 && charCode < 0x80000000) { return 6; } else { return 0; } }).reduce(function (a, b) { return a + b; }); }