Skip to content

Commit

Permalink
doc: update bufferFactory.create()
Browse files Browse the repository at this point in the history
Signed-off-by: imnemo <291400733@qq.com>
  • Loading branch information
imnemo committed Sep 3, 2020
1 parent e7f083d commit f37ce74
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
30 changes: 29 additions & 1 deletion README-zh.md
Expand Up @@ -13,4 +13,32 @@
# Buffer Factory - ([README in English](./README.md))
`node-factory`封装了`new Buffer(args...)`和Buffer.from(args...),使得创建一个Buffer对象时,不必考虑Node版本问题,避免出错,也可使代码更好的向下兼容。

本模块有较充分的单元测试和完善的使用文档。
本模块有较充分的单元测试和完善的使用文档。

# 使用

## 安装

`npm i buffer-factory`

## 调用

```javascript
const bufferFactory = require('buffer-factory')
bufferFactory.create('2b', 'hex');

// 已废弃
bufferFactory('2b', 'hex');
```

## 实现原理

`Buffer.from` is preffered to be used when it's accessible.

`Buffer.from` is added from v5.10.0, as the api document shows. But some node version, v4.2.6 for example, `Buffer.from` is a function, however, there is an error when you call `Buffer.from(string, encoding)`(error like `hex is not function`). So I also take the node version into account.

For low versions of Node.js, this module use `new Buffer(...)`.

You're free to pass in the same params as `Buffer.from(...)` and `new Buffer(...)`, I will handle the rest for you.

You're also free to check `index.js` for the implementation code :).
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -25,7 +25,10 @@ This module has been well unit tested and documented.

```javascript
const bufferFactory = require('buffer-factory')
bufferFactory('2b', 'hex')
bufferFactory.create('2b', 'hex');

// deprecated
bufferFactory('2b', 'hex');
```

## Implementation
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "buffer-factory",
"version": "1.0.8",
"version": "1.0.9",
"description": "Make it no pain to create a Buffer object by encapsulate Buffer.from an new Buffer().",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f37ce74

Please sign in to comment.