Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
utf8-binary-cutter
Advanced tools
A small node.js lib to truncate UTF-8 strings to a given binary size. Useful when dealing with old systems handling UTF-8 as ascii/latin-1, for ex. MySQL or Oracle database.
Interesting reads :
var Cutter = require('utf8-binary-cutter');
getBinarySize()
: returns the binary size of the given stringvar utf8String = 'abc☃☃☃'; // abc then 3 times the UTF-8 « snowman » char which takes 3 bytes
console.log( Cutter.getBinarySize( utf8String ) ); // 12 = 1 + 1 + 1 + 3 + 3 + 3
truncateToBinarySize()
truncate so that final binary size is lower or equal than the given limit :var utf8String = 'abc☃☃☃'; // abc then 3 times the UTF-8 « snowman » char which takes 3 bytes
console.log( Cutter.truncateToBinarySize( utf8String, 20 ) ); // 'abc☃☃☃' -> no change
console.log( Cutter.truncateToBinarySize( utf8String, 12 ) ); // 'abc☃☃☃' -> no change
console.log( Cutter.truncateToBinarySize( utf8String, 11 ) ); // 'abc☃...' -> to avoid cutting utf8 chars,
// the two last snowmen had to be removed. Final size = 9 bytes
console.log( Cutter.truncateToBinarySize( utf8String, 10 ) ); // 'abc☃...' -> idem
console.log( Cutter.truncateToBinarySize( utf8String, 9 ) ); // 'abc☃...' -> idem
console.log( Cutter.truncateToBinarySize( utf8String, 8 ) ); // 'abc...'
truncateFieldsToBinarySize()
multiple truncations at the same time :
var maxBinarySizes = {
title: 40,
content: 200
};
console.log( Cutter.truncateFieldsToBinarySize({
title: '☃☃☃ A véry véry long title with UTF-8 ☃☃☃',
content: 'I ❤ utf8-binary-cutter !',
foo: 42
},
maxBinarySizes
));
--> {
title: '☃☃☃ A véry véry long title wi...',
content: 'I ❤ utf8-binary-cutter !',
foo: 42
}
truncateToCharLength()
normal truncate is also provided for convenience : truncate so that final char length is lower or equal than the given limit :var utf8String = 'abc☃☃☃'; // 6 chars
console.log( Cutter.truncateToCharLength( utf8String, 10 ) ); // 'abc☃☃☃' -> no change
console.log( Cutter.truncateToBinarySize( utf8String, 6 ) ); // 'abc☃☃☃' -> no change
console.log( Cutter.truncateToBinarySize( utf8String, 5 ) ); // 'ab...' -> 5 chars, ok
truncateToBinarySize(foo, 42, function(maxBinarySize, originalString, truncatedString) {
logger.warn(...
});
truncateToCharLength(foo, 42, function(maxCharLength, originalString, truncatedString) {
logger.warn(...
});
Cutter.truncateFieldsToBinarySize({
title: '☃☃☃ A véry véry long title with UTF-8 ☃☃☃',
content: 'I ❤ utf8-binary-cutter !',
foo: 42
},
// maxBinarySizes
{
title: 40,
content: 200
},
// callback
// will be called for each member truncated.
// 4th param : the key of the member being truncated.
function(maxCharLength, originalString, truncatedString, key) {
logger.warn(...
}
);
.editorconfig
and .jshintrc
filesnpm install
npm test
Thanks !
FAQs
truncate UTF-8 strings to a given binary size
We found that utf8-binary-cutter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.