# buffer-iterator

> buffer itaretor

Latest version **1.0.0** (published 2018-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install buffer-iterator
pnpm add buffer-iterator
yarn add buffer-iterator
bun add buffer-iterator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-05-24 |
| First published | 2018-05-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | charles.liao |
| Maintainers | charleslxh |
| Keywords | buffer |

## Links

- npm: https://www.npmjs.com/package/buffer-iterator
- Repository: https://github.com/charleslxh/buffer-iterator
- Homepage: https://github.com/charleslxh/buffer-iterator#readme
- Issues: https://github.com/charleslxh/buffer-iterator/issues
- npm.io page: https://npm.io/package/buffer-iterator

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-05-24

## README

# Buffer Itaretor

[![Build Status](https://travis-ci.org/charleslxh/buffer-iterator.svg?branch=master)](https://travis-ci.org/charleslxh/buffer-iterator)

Let Buffer can be foreach.

## Install

```bash
$ npm install buffer-iterator
```

## Methods

1. `next(needMovePointer = true): Number|Null`: Read a byte from buffer, and move pointer to next byte when `needMovePointer` is `true`(**default**), if has not next byte, return *null*.
    
    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    assert.equal(bi.next(), 0x01);
    ```

2. `prev(needMovePointer = false): Number|Null`: Read a previous byte from buffer, and move pointer to previous byte when `needMovePointer` is `true`(**default false**), if has not previous byte, return *null*.

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    bi.next();
    bi.next();
    assert.equal(bi.prev(), 0x01);
    ```

3. `current(): Number`: Read current byte, if has not current byte, return *null*..

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    bi.next();
    assert.equal(bi.current(), 0x01);
    ```

4. `hasNext(): Boolean`: If have remain bytes to read.

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    bi.next();
    assert.equal(bi.hasNext(), true);
    ```

5. `remainLength(): Number`: Count bytes which have not been read.

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    bi.next();
    assert.equal(bi.remainLength(), 1);
    ```

6. `readBytes(number = 1): Buffer|Null`: Read multi bytes.

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    assert.equal(bi.readBytes(10).equals(Buffer.from([0x01, 0x02])), true);
    ```

7. `append(buf): Null`: Append buffer.

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    bi.append(Buffer.from([0x03]));
    assert.equal(bi.buffer.equals(Buffer.from([0x01, 0x02, 0x03])), true);
    ```

8. use `for...of`.

    ```js
    const assert = require('assert');
    const BufferIterator = require('buffer-iterator');

    const bi = new BufferIterator(Buffer.from([0x01, 0x02]));
    for (let byte of bi) {
      console.log(byte);
    }

    // 1
    // 2
    ```

---
_Source: https://npm.io/package/buffer-iterator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
