npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

str2gbk

v0.0.5

Published

JS string to GBK encoding ultra-lite implementation

Downloads

97

Readme

str2gbk

JS 字符串转 GBK 编码超轻量实现。代码压缩后不到 1KB,并且性能非常高。

NPM Version NPM Install Size

Install

npm i str2gbk

该项目使用 TextDecoder API,因此 IE 不支持。查看兼容性

Usage

import str2gbk from 'str2gbk'

str2gbk('你好123')  // Uint8Array(7) [ 196, 227,   186, 195,   49, 50, 51 ]

str2gbk('€100')    // Uint8Array(4) [ 128,   49, 48, 48 ]

Demo: https://jsbin.com/vuxawul/edit?html,output

API

str2gbk(str, opt?)

str

必选。输入 JS 字符串。

opt.onError

可选。输入 JS 字符串中每当遇到 GBK 不支持的字符时,触发该回调。

如果未提供回调,默认使用 63(即 ? 字符)代替不支持的字符。例如:

str2gbk('12©34®56')
// [49, 50,   63,   51, 52,   63,   53, 54]  ("12?34?56")

如果提供回调,则使用回调的返回值代替:

str2gbk('12©34®56', {
  onError: () => 32
})
// [49, 50,   32,   51, 52,   32,   53, 54]  ("12 34 56")

如果回调返回 -1,程序则终止解析,并返回已解析的数据:

str2gbk('12©34®56', {
  onError: () => -1
})
// [49, 50]

回调类型:

(index: number, input: string) => number
  • index: 非法字符的位置

  • input: 输入 JS 字符串

使用案例:

str2gbk('123©456', {
  onError: (index, input) => {
    console.warn('error index:', index, 'char:', input[index])
    return -1
  }
})
// [WARN] error index: 3 char: ©

opt.onAlloc

可选。自定义返回结果的内存申请。

默认情况下,浏览器中使用 Uint8Array,Node.js 中使用 Buffer。通过该参数可强制类型,例如:

str2gbk('赢', {
  onAlloc: (len) => new Uint8Array(len)
})  // Uint8Array(2) [ 211, 174 ]

str2gbk('赢', {
  onAlloc: (len) => Buffer.alloc(len)
})  // <Buffer d3 ae>

此外,灵活使用该选项,有些场合下可将结果直接写入目标缓冲区,避免内存复制。例如:

const targetBuf = ....
const gbkBuf = str2gbk(str, {
  onAlloc: () => targetBuf.subarray(pos)
})
pos += gbkBuf.length

通过 subarray 引用 targetBuf 的一部分作为存储,这样 str2gbk 执行完成后,结果已在 targetBuf 上,无需额外复制。

Decode

GBK 数据转字符串 JS 原生就已支持,因此本项目不再提供。

const gbkDecoder = new TextDecoder('gbk')

function gbk2str(gbkBuf) {
  return gbkDecoder.decode(gbkBuf)
}
gbk2str(new Uint8Array([196, 227,   186, 195,   49, 50, 51]))   // "你好123"
gbk2str(new Uint8Array([128,   49, 48, 48]))                    // "€100"

License

MIT