truncate-utf8-bytes

Truncate string to given length in bytes

WTFPL 3 个版本
安装
npm install truncate-utf8-bytes
yarn add truncate-utf8-bytes
pnpm add truncate-utf8-bytes
bun add truncate-utf8-bytes
README

truncate-utf8-bytes build status

Truncate a string to the given length in bytes. Correctly handles multi-byte characters and surrogate pairs.

A browser implementation that doesn't use Buffer.byteLength is provided to minimize build size.

Example

var truncate = require("truncate-utf8-bytes")
var str = "a☃" // a = 1 byte, ☃ = 3 bytes
console.log(truncate(str, 2))
// -> "a"

API

var truncate = require("truncate-utf8-bytes")

When using browserify or webpack, this automatically resolves to an implementation that does not use Buffer.byteLength.

truncate(string, length)

Returns string truncated to at most length bytes in length.

版本列表
1.0.2 2016-09-29
1.0.1 2015-11-06
1.0.0 2015-11-06