Skip to content

Latest commit

 

History

History
44 lines (29 loc) · 2.61 KB

README-zh.md

File metadata and controls

44 lines (29 loc) · 2.61 KB

Build Status Coverage Status Try buffer-factory on RunKit DeepScan grade

Buffer Factory - (README in English)

node-factory封装了new Buffer(args...)和Buffer.from(args...),使得创建一个Buffer对象时,不必考虑Node版本问题,避免出错,也可使代码更好的向下兼容。

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

使用

安装

npm i buffer-factory

调用

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 :).