0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-25 08:19:38 +01:00
nodejs/test/parallel/test-buffer-iterator.js
Vladimir Kurchatkin 45d8d9f826 buffer: implement iterable interface
This makes possible to use `for..of` loop with
buffers. Also related `keys`, `values` and `entries`
methods are added for feature parity with `Uint8Array`.

PR-URL: https://github.com/iojs/io.js/pull/525
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-28 16:40:15 +03:00

62 lines
877 B
JavaScript

var common = require('../common');
var assert = require('assert');
var buffer = new Buffer([1, 2, 3, 4, 5]);
var arr;
var b;
// buffers should be iterable
arr = [];
for (b of buffer)
arr.push(b);
assert.deepEqual(arr, [1, 2, 3, 4, 5]);
// buffer iterators should be iterable
arr = [];
for (b of buffer[Symbol.iterator]())
arr.push(b);
assert.deepEqual(arr, [1, 2, 3, 4, 5]);
// buffer#values() should return iterator for values
arr = [];
for (b of buffer.values())
arr.push(b);
assert.deepEqual(arr, [1, 2, 3, 4, 5]);
// buffer#keys() should return iterator for keys
arr = [];
for (b of buffer.keys())
arr.push(b);
assert.deepEqual(arr, [0, 1, 2, 3, 4]);
// buffer#entries() should return iterator for entries
arr = [];
for (var b of buffer.entries())
arr.push(b);
assert.deepEqual(arr, [
[0, 1],
[1, 2],
[2, 3],
[3, 4],
[4, 5]
]);