New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

truncate-utf8-bytes

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

truncate-utf8-bytes

Truncate string to given length in bytes

1.0.2
latest
Source
npm
Version published
Weekly downloads
2M
-19.61%
Maintainers
2
Weekly downloads
 
Created

What is truncate-utf8-bytes?

The 'truncate-utf8-bytes' npm package is designed to truncate a string to a specified number of bytes, ensuring that the resulting string is valid UTF-8. This is particularly useful when dealing with systems that have byte-length constraints for strings, such as databases or network protocols.

What are truncate-utf8-bytes's main functionalities?

Truncate a string to a specified number of bytes

This feature allows you to truncate a string to a specified number of bytes. In this example, the string 'Hello, world!' is truncated to 5 bytes, resulting in 'Hello'.

const truncate = require('truncate-utf8-bytes');
const str = 'Hello, world!';
const truncatedStr = truncate(str, 5);
console.log(truncatedStr); // Output: 'Hello'

Handle multi-byte characters correctly

This feature ensures that multi-byte characters are handled correctly. In this example, the Japanese string 'こんにちは' is truncated to 6 bytes, resulting in 'こん', which is a valid UTF-8 string.

const truncate = require('truncate-utf8-bytes');
const str = 'こんにちは'; // 'Hello' in Japanese
const truncatedStr = truncate(str, 6);
console.log(truncatedStr); // Output: 'こん'

Other packages similar to truncate-utf8-bytes

Keywords

truncate

FAQs

Package last updated on 29 Sep 2016

Did you know?

Socket

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.

Install

Related posts