Repository URL to install this package:
|
Version:
2.0.0-beta.20 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @file @see https://github.com/parshap/truncate-utf8-bytes
*/
function isHighSurrogate(codePoint) {
return codePoint >= 0xd800 && codePoint <= 0xdbff;
}
function isLowSurrogate(codePoint) {
return codePoint >= 0xdc00 && codePoint <= 0xdfff;
}
// Truncate string by size in bytes
function truncate(getLength, string, byteLength) {
if (typeof string !== 'string') {
throw new Error('Input must be string');
}
var charLength = string.length;
var curByteLength = 0;
var codePoint;
var segment;
for (var i = 0; i < charLength; i += 1) {
codePoint = string.charCodeAt(i);
segment = string[i];
if (isHighSurrogate(codePoint) &&
isLowSurrogate(string.charCodeAt(i + 1))) {
i += 1;
segment += string[i];
}
curByteLength += getLength(segment);
if (curByteLength === byteLength) {
return string.slice(0, i + 1);
}
else if (curByteLength > byteLength) {
return string.slice(0, i - segment.length + 1);
}
}
return string;
}
var getLength = Buffer.byteLength.bind(Buffer);
var fromStringToTruncatedUtf8 = truncate.bind(undefined, getLength);
exports.fromStringToTruncatedUtf8 = fromStringToTruncatedUtf8;
//# sourceMappingURL=fromStringToTruncatedUtf8.js.map