Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

obliterator

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obliterator - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

combinations.js

8

chain.js

@@ -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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc