diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a934ef8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +(The MIT License) + +Copyright (C) 2014-2017 by Vitaly Puzrin and Andrei Tuputcyn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1c122f --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +## Buffer + +浏览器版的Buffer对象模拟, 提供近似Node.js的BUffer对象的API + + +[![@bytedo/buffer](https://img.shields.io/npm/v/@bytedo/buffer.svg)](https://www.npmjs.com/package/@bytedo/buffer) \ No newline at end of file diff --git a/build.js b/build.js new file mode 100644 index 0000000..88f7b05 --- /dev/null +++ b/build.js @@ -0,0 +1,16 @@ +/** + * {build} + * @author yutent + * @date 2021/08/09 11:59:41 + */ + +import Es from 'esbuild' + +Es.build({ + entryPoints: ['src/index.js'], + outdir: 'dist', + target: 'es2017', + format: 'esm', + bundle: true, + minify: true +}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..d38257b --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "@bytedo/buffer", + "version": "1.0.0", + "type": "module", + "main": "dist/index.js", + "files": [ + "dist/*" + ], + "description": "Buffer浏览器版本,提供常用的API封装", + "keywords": [ + "Buffer", + "base64" + ], + "author": "Yutent ", + "scripts": { + "start": "node ./build.js" + }, + "repository": { + "type": "git", + "url": "https://git.wkit.fun/bytedo/buffer" + }, + "license": "MIT" +} diff --git a/src/helper.js b/src/helper.js index 5c62c01..ac763ec 100644 --- a/src/helper.js +++ b/src/helper.js @@ -45,3 +45,9 @@ export function hex2u8(str) { export function sum(list) { return list.reduce((total, it) => total + it.length, 0) } + +export function compare(buf1, buf2) { + let s1 = buf1 + '' + let s2 = buf2 + '' + return s1.localeCompare(s2) +} diff --git a/src/index.js b/src/index.js index a886072..ec3665b 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,15 @@ * @date 2024/05/29 12:29:17 */ -import { encode, decode, getType, to16, hex2u8, sum } from './helper.js' +import { + encode, + decode, + getType, + to16, + hex2u8, + sum, + compare +} from './helper.js' import { base64encode, base64decode, str2bin, bin2str } from './base64.js' /** @@ -28,7 +36,7 @@ function any2u8(data = '', encoding = 'utf8') { } return new Uint8Array(data) } else { - switch (encoding) { + switch (encoding.toLowerCase()) { case 'base64': data = base64decode(data) break @@ -53,6 +61,19 @@ export default class Buffer extends Uint8Array { return 'Buffer' } + /** + * 返回指定内容的字节数 + */ + static byteLength(data, encoding = 'utf8') { + let u8 = any2u8(data, encoding) + return u8.length + } + + /** + * 从给定的内容中创建Buffer对象 + * @param data + * @return + */ static from(data, offsetOrEncoding, end) { let buf = data let start = 0 @@ -71,6 +92,12 @@ export default class Buffer extends Uint8Array { return new Buffer(buf.slice(start, end)) } + /** + * 从给定的内容中创建Buffer对象 + * @param len Buffer长度 + * @param str 创建Buffer时, 填充的内容 + * @return + */ static alloc(len, str = '', encoding) { let buf = new Buffer(len) if (str) { @@ -79,6 +106,9 @@ export default class Buffer extends Uint8Array { return buf } + /** + * 合并多个Buffer对象 + */ static concat(list, encoding) { let len = sum(list) let buf = Buffer.alloc(len) @@ -92,10 +122,21 @@ export default class Buffer extends Uint8Array { return buf } + /** + * 判断是否为Buffer对象 + */ static isBuffer(target) { return getType(target) === 'Buffer' } + /** + * 比较2个对象 + */ + static compare = compare + + /** + * 替换指定字节内容, fill, copy, write, 都会用到这个方法 + */ #replace(target, start, end) { for (let i = 0; i < end - start; i++) { this[start + i] = target[i] @@ -195,4 +236,32 @@ export default class Buffer extends Uint8Array { } return written } + + /** + * 与目标对象比较 + */ + compare(buf) { + return compare(this, buf) + } + + /** + * 判断2个Buffer对象是否相等 + */ + equals(buf) { + return this.toString() === buf.toString() + } + + /** + * 判断是否包含指定内容 + * 原生的includes只支持bytes, 这里拓展支持完整的类型 + */ + includes(data, offset = 0, encoding = 'utf8') { + if (typeof data === 'number') { + return super.includes(data, offset) + } + let u8 = any2u8(data, encoding) + let str = decode(u8) + + return this.toString().indexOf(str, offset) > -1 + } }