Using JavaScript to truncate text to a certain size (8 KB)

后端 未结 4 1032
南笙
南笙 2021-01-12 05:57

I\'m using the Zemanta API, which accepts up to 8 KB of text per call. I\'m extracting the text to send to Zemanta from Web pages using JavaScript, so I\'m looking for a fun

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 06:28

    You can do something like this since unescape is partially deprecated

    function byteCount( string ) {
        // UTF8
        return encodeURI(string).split(/%..|./).length - 1;
    }
    
    function truncateByBytes(string, byteSize) {
        // UTF8
        if (byteCount(string) > byteSize) {
            const charsArray = string.split('');
            let truncatedStringArray = [];
            let bytesCounter = 0;
            for (let i = 0; i < charsArray.length; i++) {
                bytesCounter += byteCount(charsArray[i]);
                if (bytesCounter <= byteSize) {
                    truncatedStringArray.push(charsArray[i]);
                } else {
                    break;
                }
            }
            return truncatedStringArray.join('');
        }
        return string;
    }
    

提交回复
热议问题