obliterator
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -0,1 +1,2 @@ | ||
/* eslint no-constant-condition: 0 */ | ||
/** | ||
@@ -5,3 +6,3 @@ * Obliterator Consume Function | ||
* | ||
* Function consuming the given iterator into an array. | ||
* Function consuming the given iterator for n or every steps. | ||
*/ | ||
@@ -13,14 +14,22 @@ | ||
* @param {Iterator} iterator - Target iterator. | ||
* @param {number} [size] - Optional size. | ||
* @param {number} [steps] - Optional steps. | ||
* @return {array} | ||
*/ | ||
module.exports = function consume(iterator, size) { | ||
var array = arguments.length > 1 ? new Array(size) : [], | ||
step, | ||
module.exports = function consume(iterator, steps) { | ||
var step, | ||
l = arguments.length > 1 ? steps : Infinity, | ||
i = 0; | ||
while ((step = iterator.next(), !step.done)) | ||
array[i++] = step.value; | ||
while (true) { | ||
return array; | ||
if (i === l) | ||
return; | ||
step = iterator.next(); | ||
if (step.done) | ||
return; | ||
i++; | ||
} | ||
}; |
@@ -19,3 +19,5 @@ /** | ||
range: require('./range.js'), | ||
split: require('./split.js') | ||
split: require('./split.js'), | ||
take: require('./take.js'), | ||
takeInto: require('./take-into.js') | ||
}; |
@@ -32,2 +32,3 @@ /** | ||
*/ | ||
// NOTE: maybe this should dropped for performance? | ||
Iterator.prototype.next = function() { | ||
@@ -54,16 +55,17 @@ if (this.done) | ||
/** | ||
* Returning an iterator of the given value. | ||
* Returning an iterator of the given values. | ||
* | ||
* @param {any} value - Value. | ||
* @param {any...} values - Values. | ||
* @return {Iterator} | ||
*/ | ||
Iterator.of = function(value) { | ||
var consumed = false; | ||
Iterator.of = function() { | ||
var args = arguments, | ||
l = args.length, | ||
i = 0; | ||
return new Iterator(function() { | ||
if (consumed) | ||
if (i >= l) | ||
return {done: true}; | ||
consumed = true; | ||
return {value: value}; | ||
return {done: false, value: args[i++]}; | ||
}); | ||
@@ -70,0 +72,0 @@ }; |
{ | ||
"name": "obliterator", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Higher order iterator library for JavaScript.", | ||
@@ -37,2 +37,3 @@ "main": "index.js", | ||
"Symbol": true, | ||
"Uint8Array": true, | ||
"Uint32Array": true | ||
@@ -39,0 +40,0 @@ } |
@@ -33,2 +33,3 @@ [![Build Status](https://travis-ci.org/Yomguithereal/obliterator.svg)](https://travis-ci.org/Yomguithereal/obliterator) | ||
* [split](#split) | ||
* [take](#take) | ||
@@ -56,2 +57,5 @@ ## Iterator | ||
const simpleIterator = Iterator.of(34); | ||
// Creating a simple iterator from multiple values | ||
const multipleIterator = Iterator.of(1, 2, 3); | ||
``` | ||
@@ -100,3 +104,3 @@ | ||
Function consuming the given iterator and returning its values in an array. | ||
Function consuming the given iterator fully or for n steps. | ||
@@ -110,4 +114,13 @@ ```js | ||
consume(set.values()); | ||
>>> [1, 2, 3] | ||
// Consuming the whole iterator | ||
let iterator = set.values(); | ||
consume(iterator); | ||
iterator.next().done | ||
>>> true | ||
// Consuming n steps | ||
let iterator = set.values(); | ||
consume(iterator, 2); | ||
iterator.next().value | ||
>>> 3 | ||
``` | ||
@@ -250,2 +263,22 @@ | ||
## take | ||
Function taking values from given iterator and returning them in an array. | ||
```js | ||
import take from 'obliterator/take'; | ||
// Or | ||
import {take} from 'obliterator'; | ||
const set = new Set([1, 2, 3]); | ||
// To take n values from the iterator | ||
take(set.values(), 2); | ||
>>> [1, 2] | ||
// To convert the full iterator into an array | ||
take(set.values()); | ||
>>> [1, 2, 3] | ||
``` | ||
# Contribution | ||
@@ -252,0 +285,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22381
18
585
298