obliterator
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -17,3 +17,2 @@ /** | ||
var iterators = arguments, | ||
done = false, | ||
current, | ||
@@ -23,12 +22,7 @@ i = -1; | ||
return new Iterator(function iterate() { | ||
if (done) | ||
return {done: true}; | ||
if (!current) { | ||
i++; | ||
if (i >= iterators.length) { | ||
done = true; | ||
if (i >= iterators.length) | ||
return {done: true}; | ||
} | ||
@@ -35,0 +29,0 @@ current = iterators[i]; |
@@ -8,4 +8,8 @@ /** | ||
module.exports = { | ||
Iterator: require('./iterator.js'), | ||
chain: require('./chain.js'), | ||
consume: require('./consume.js') | ||
combinations: require('./combinations.js'), | ||
consume: require('./consume.js'), | ||
permutations: require('./permutations.js'), | ||
powerSet: require('./power-set.js') | ||
}; |
@@ -15,8 +15,89 @@ /** | ||
function Iterator(next) { | ||
this.next = next; | ||
// Hiding the given function | ||
Object.defineProperty(this, '_next', { | ||
writable: false, | ||
enumerable: false, | ||
value: next | ||
}); | ||
// Is the iterator complete? | ||
this.done = false; | ||
} | ||
/** | ||
* Next function. | ||
* | ||
* @return {object} | ||
*/ | ||
Iterator.prototype.next = function() { | ||
if (this.done) | ||
return {done: true}; | ||
var step = this._next(); | ||
if (step.done) | ||
this.done = true; | ||
return step; | ||
}; | ||
/** | ||
* If symbols are supported, we add `next` to `Symbol.iterator`. | ||
*/ | ||
if (typeof Symbol !== 'undefined') | ||
Iterator.prototype[Symbol.iterator] = function() { | ||
return this; | ||
}; | ||
/** | ||
* Returning an iterator of the given value. | ||
* | ||
* @param {any} value - Value. | ||
* @return {Iterator} | ||
*/ | ||
Iterator.of = function(value) { | ||
var consumed = false; | ||
return new Iterator(function() { | ||
if (consumed) | ||
return {done: true}; | ||
consumed = true; | ||
return {value: value}; | ||
}); | ||
}; | ||
/** | ||
* Returning an empty iterator. | ||
* | ||
* @return {Iterator} | ||
*/ | ||
Iterator.empty = function() { | ||
var iterator = new Iterator(null); | ||
iterator.done = true; | ||
return iterator; | ||
}; | ||
/** | ||
* Returning whether the given value is an iterator. | ||
* | ||
* @param {any} value - Value. | ||
* @return {boolean} | ||
*/ | ||
Iterator.is = function(value) { | ||
if (value instanceof Iterator) | ||
return true; | ||
return ( | ||
typeof value === 'object' && | ||
value !== null && | ||
typeof value.next === 'function' | ||
); | ||
}; | ||
/** | ||
* Exporting. | ||
*/ | ||
module.exports = Iterator; |
{ | ||
"name": "obliterator", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Higher order iterator library for JavaScript.", | ||
@@ -35,5 +35,7 @@ "main": "index.js", | ||
"globals": { | ||
"Set": true | ||
"Set": true, | ||
"Symbol": true, | ||
"Uint32Array": true | ||
} | ||
} | ||
} |
@@ -17,5 +17,33 @@ [![Build Status](https://travis-ci.org/Yomguithereal/obliterator.svg)](https://travis-ci.org/Yomguithereal/obliterator) | ||
* [Iterator](#iterator) | ||
* [chain](#chain) | ||
* [combinations](#combinations) | ||
* [consume](#consume) | ||
* [permutations](#permutations) | ||
* [powerSet](#powerSet) | ||
## Iterator | ||
A handy Iterator class with safeguards and usable with ES2015's `for ... of` loop constructs & spread operator. | ||
```js | ||
import Iterator from 'obliterator/iterator'; | ||
// Or | ||
import {Iterator} from 'obliterator'; | ||
const iterator = new Iterator(function() { | ||
// Define what the `next` function does | ||
}); | ||
// Checking that the given value is an iterator (native or else) | ||
Iterator.is(value); | ||
// Creating an empty iterator | ||
const emptyIterator = Iterator.empty(); | ||
// Creating a simple iterator from a single value | ||
const simpleIterator = Iterator.of(34); | ||
``` | ||
## chain | ||
@@ -41,2 +69,21 @@ | ||
## combinations | ||
Returns an iterator of combinations of the given array and of the given size. | ||
Note that for performance reasons, the yielded combination is always the same object. | ||
```js | ||
import combinations from 'obliterator/combinations'; | ||
// Or | ||
import {combinations} from 'obliterator'; | ||
const iterator = combinations(['A', 'B', 'C', 'D'], 2); | ||
iterator.next().value; | ||
>>> ['A', 'B'] | ||
iterator.next().value; | ||
>>> ['A', 'C'] | ||
``` | ||
## consume | ||
@@ -57,3 +104,45 @@ | ||
## permutations | ||
Returns an iterator of permutations of the given array and of the given size. | ||
Note that for performance reasons, the yielded permutation is always the same object. | ||
```js | ||
import permutations from 'obliterator/permutations'; | ||
// Or | ||
import {permutations} from 'obliterator'; | ||
let iterator = permutations([1, 2, 3]); | ||
iterator.next().value | ||
>>> [1, 2, 3] | ||
iterator.next().value | ||
>>> [1, 3, 2] | ||
iterator = permutations(['A', 'B', 'C', 'D'], 2); | ||
iterator.next().value; | ||
>>> ['A', 'B'] | ||
iterator.next().value; | ||
>>> ['A', 'C'] | ||
``` | ||
## powerSet | ||
Returns an iterator of sets composing the power set of the given array. | ||
```js | ||
import powerSet from 'obliterator/power-set'; | ||
// Or | ||
import {powerSet} from 'obliterator'; | ||
const iterator = powerSet(['A', 'B', 'C']); | ||
iterator.next().value; | ||
>>> [] | ||
iterator.next().value; | ||
>>> ['A'] | ||
``` | ||
# Contribution | ||
@@ -60,0 +149,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
12783
10
306
164
1