ripunzel/src/utils/truncate-bytes.js

24 lines
399 B
JavaScript

'use strict';
function truncateBytes(str, limit) {
if (Buffer.from(str).length <= limit) {
return str;
}
let acc = '';
for (let i = 0; i < str.length; i++) {
const newString = acc + str.charAt(i);
if (Buffer.from(newString).length > limit) {
break;
}
acc = newString;
}
return acc;
}
module.exports = truncateBytes;