Giter Site home page Giter Site logo

ghosind / node-dynamic-buffer Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 774 KB

An automatically resizing storage size buffer type for Node.js that is based on Node.js builtin Buffer.

Home Page: https://www.npmjs.com/package/dynamic-buffer

License: MIT License

TypeScript 100.00%
buffer node-package nodejs npm-package typescript dynamic-buffer

node-dynamic-buffer's Introduction

Dynamic Buffer for Node.js

Latest version NPM Package Github Actions build codecov Codacy Badge License

An automatically resizing storage size buffer type for Node.js that is based on Node.js builtin Buffer.

Installation

You can install this library by npm with the following command:

npm install dynamic-buffer

You can also install by yarn or other npm alternative package managers:

yarn add dynamic-buffer

Getting Started

  1. Imports DynamicBuffer from dynamic-buffer package:
import { DynamicBuffer } from 'dynamic-buffer';
  1. Creates DynamicBuffer instance with default initial size or the specific size:
const buffer = new DynamicBuffer();
  1. Appends data into buffer:
buffer.append('Hello ');
buffer.append('world!');
  1. Exports data to string or builtin buffer object without unused bytes:
console.log(buffer.length);
// 12
const buf = buffer.toBuffer();
console.log(buf.toString());
// Hello world!
const str = buffer.toString();
console.log(str);
// Hello world!

Table of Contents

Usages

Write Data

You can write a string into a buffer by append method, it'll add the string to the end of the buffer:

buf.append('Hello');
buf.append(' ');
buf.append('world');
console.log(buf.toString());
// Hello world

And you can also set the string length to write. Like the second line of the following example, it'll write 'Script!' into buffer and without the last two '!' symbols:

buf.append('Java');
buf.append('Script!!!', 7);
console.log(buf.toString());
// JavaScript!

You can also use write method to write data to the specified position in the buffer:

buf.append('Hello world!');
buf.write('Node.js', 6);
console.log(buf.toString());
// Hello Node.js

Read Data

You can access the byte at the specified position in the buffer by read or at methods:

const buf = new DynamicBuffer('Hello world');

buf.at(0); // 72
buf.read(1); // 101

Iteration

DynamicBuffer provides three ways to iterate data from the specified buffer, you can use them with for...of statement.

  • entries() returns an iterator of key-value pair (index and byte) from the buffer.

    buf.append('Hello');
    for (const pair of buf.entries()) {
      console.log(pair);
    }
    // [ 0, 72 ]
    // [ 1, 101 ]
    // [ 2, 108 ]
    // [ 3, 108 ]
    // [ 4, 111 ]
  • values() returns an iterator of data(byte) from the buffer.

    buf.append('Hello');
    for (const value of buf.values()) {
      console.log(value);
    }
    // 72
    // 101
    // 108
    // 108
    // 111
  • keys() returns an iterator of buffer keys (indices).

Search

You can search a value in the buffer by indexOf or lastIndexOf, and get the position of the first/last occurrence in the buffer. The searching value can be a string, a number, a Buffer, an Uint8Array, or another DynamicBuffer.

buf.append('ABCABCABC');
buf.indexOf('ABC'); // 0
buf.lastIndexOf('ABC'); // 6
buf.indexOf('abc'); // -1

Comparison

You can compare DynamicBuffer object with another DynamicBuffer object, Node.js builtin Buffer object, or an Uint8Array by compare or equals methods.

For compare method, it returns a number to indicate whether the buffer comes before, after, or is the same as another buffer in sort order.

buf.append('ABC');
console.log(buf.compare(Buffer.from('ABC')));
// 0
console.log(buf.compare(Buffer.from('BCD')));
// -1

For equals method, it returns a boolean value to indicate whether the buffer is the same as the target buffer.

buf.append('ABC');
console.log(buf.equals(Buffer.from('ABC')));
// true
console.log(buf.equals(Buffer.from('BCD')));
// false

Export Data

You can export buffer content (without unused parts) to string, Buffer object, or JSON representation object.

buf.append('Hello');
console.log(buf.toString());
// Hello
const dataBuffer = buf.toBuffer();
console.log(buf.length, dataBuffer.toString());
// 5 Hello
console.log(JSON.stringify(buf)); // JSON.stringify implicitly calls toJSON method.
// {"type":"Buffer","data":[72,101,108,108,111]}

For toString and toBuffer methods, you can also set the start and end offsets to export the subset of written data.

buf.append('Hello world!!!');
console.log(buf.toString('utf8', 6, 11));
// world

Utils

We provided isDynamicBuffer function to indicating an object is a DynamicBuffer object or not.

import { isDynamicBuffer } from 'dynamic-buffer';

const buf1 = Buffer.alloc(8);
const buf2 = new DynamicBuffer();

isDynamicBuffer(buf1); // false
isDynamicBuffer(buf2); // true

Run Tests

All of test cases are written with mocha, assert, and nyc. They can be run with the following commands:

npm test
# or
yarn test

License

This project was published under MIT license, you can see more detail in LICENSE file.

node-dynamic-buffer's People

Contributors

dependabot[bot] avatar ghosind avatar mend-bolt-for-github[bot] avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

node-dynamic-buffer's Issues

NPM package version confusion

It seems some versions belong to a previously deleted NPM package with the same name. There are the versions shown in npm show command:

npm show dynamic-buffer time
{
  created: '2022-11-06T17:13:32.860Z',
  '1.0.0': '2022-08-02T15:25:38.333Z',
  modified: '2023-02-11T11:33:23.807Z',
  '1.0.1': '2022-08-02T17:44:51.127Z',
  '1.1.0': '2022-08-03T17:52:07.666Z',
  '1.1.1': '2022-08-08T14:51:08.119Z',
  '2.0.0': '2022-08-21T10:05:59.776Z',
  '2.0.1': '2022-08-21T10:08:53.197Z',
  '0.1.0': '2022-11-05T14:53:24.950Z',
  '0.1.1': '2022-11-06T17:13:33.037Z',
  '0.1.2': '2022-11-09T13:41:45.955Z',
  '0.2.0': '2022-11-15T14:17:28.093Z',
  '0.2.1': '2022-11-25T15:18:29.804Z',
  '0.3.0': '2022-12-15T15:52:50.663Z',
  '0.3.1': '2022-12-15T16:44:44.978Z',
  '0.3.2': '2023-01-30T14:25:37.840Z',
  '0.3.3': '2023-02-11T11:33:23.807Z'
}

So, the following versions must be skipped:

  • 1.0.0
  • 1.0.1
  • 1.1.1
  • 2.0.0
  • 2.0.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.