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.6.1 to 2.0.0

every.d.ts

7

chain.d.ts

@@ -1,3 +0,6 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
import type {IntoInterator} from './types';
export default function chain<T>(...iterators: Iterator<T>[]): ObliteratorIterator<T>;
export default function chain<T>(
...iterables: IntoInterator<T>[]
): ObliteratorIterator<T>;

@@ -5,5 +5,6 @@ /**

*
* Variadic function combining the given iterators.
* Variadic function combining the given iterables.
*/
var Iterator = require('./iterator.js');
var Iterator = require('./iterator.js'),
iter = require('./iter.js');

@@ -13,9 +14,9 @@ /**

*
* @param {...Iterator} iterators - Target iterators.
* @param {...Iterator} iterables - Target iterables.
* @return {Iterator}
*/
module.exports = function chain() {
var iterators = arguments,
current,
i = -1;
var iterables = arguments,
current,
i = -1;

@@ -26,6 +27,5 @@ return new Iterator(function iterate() {

if (i >= iterators.length)
return {done: true};
if (i >= iterables.length) return {done: true};
current = iterators[i];
current = iter(iterables[i]);
}

@@ -32,0 +32,0 @@

@@ -1,3 +0,6 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
export default function combinations<T>(array: Array<T>, r: number): ObliteratorIterator<Array<T>>;
export default function combinations<T>(
array: Array<T>,
r: number
): ObliteratorIterator<Array<T>>;

@@ -13,4 +13,3 @@ /**

function indicesToItems(target, items, indices, r) {
for (var i = 0; i < r; i++)
target[i] = items[indices[i]];
for (var i = 0; i < r; i++) target[i] = items[indices[i]];
}

@@ -27,3 +26,5 @@

if (!Array.isArray(array))
throw new Error('obliterator/combinations: first argument should be an array.');
throw new Error(
'obliterator/combinations: first argument should be an array.'
);

@@ -33,17 +34,19 @@ var n = array.length;

if (typeof r !== 'number')
throw new Error('obliterator/combinations: second argument should be omitted or a number.');
throw new Error(
'obliterator/combinations: second argument should be omitted or a number.'
);
if (r > n)
throw new Error('obliterator/combinations: the size of the subsequences should not exceed the length of the array.');
throw new Error(
'obliterator/combinations: the size of the subsequences should not exceed the length of the array.'
);
if (r === n)
return Iterator.of(array.slice());
if (r === n) return Iterator.of(array.slice());
var indices = new Array(r),
subsequence = new Array(r),
first = true,
i;
subsequence = new Array(r),
first = true,
i;
for (i = 0; i < r; i++)
indices[i] = i;
for (i = 0; i < r; i++) indices[i] = i;

@@ -55,3 +58,3 @@ return new Iterator(function next() {

indicesToItems(subsequence, array, indices, r);
return {value: subsequence};
return {value: subsequence, done: false};
}

@@ -61,3 +64,3 @@

indicesToItems(subsequence, array, indices, r);
return {value: subsequence};
return {value: subsequence, done: false};
}

@@ -67,16 +70,13 @@

while (i >= 0 && indices[i] >= (n - (r - i)))
--i;
while (i >= 0 && indices[i] >= n - (r - i)) --i;
if (i < 0)
return {done: true};
if (i < 0) return {done: true};
indices[i]++;
while (++i < r)
indices[i] = indices[i - 1] + 1;
while (++i < r) indices[i] = indices[i - 1] + 1;
indicesToItems(subsequence, array, indices, r);
return {value: subsequence};
return {value: subsequence, done: false};
});
};

@@ -17,14 +17,11 @@ /* eslint no-constant-condition: 0 */

var step,
l = arguments.length > 1 ? steps : Infinity,
i = 0;
l = arguments.length > 1 ? steps : Infinity,
i = 0;
while (true) {
if (i === l) return;
if (i === l)
return;
step = iterator.next();
if (step.done)
return;
if (step.done) return;

@@ -31,0 +28,0 @@ i++;

@@ -1,5 +0,9 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
import type {IntoInterator} from './types';
type PredicateFunction<T> = (item: T) => boolean;
export default function filter<T>(predicate: PredicateFunction<T>, iterator: Iterator<T>): ObliteratorIterator<T>;
export default function filter<T>(
target: IntoInterator<T>,
predicate: PredicateFunction<T>
): ObliteratorIterator<T>;

@@ -8,2 +8,3 @@ /**

var Iterator = require('./iterator.js');
var iter = require('./iter.js');

@@ -13,15 +14,15 @@ /**

*
* @param {Iterable} target - Target iterable.
* @param {function} predicate - Predicate function.
* @param {Iterator} target - Target iterator.
* @return {Iterator}
*/
module.exports = function filter(predicate, target) {
module.exports = function filter(target, predicate) {
var iterator = iter(target);
return new Iterator(function next() {
var step = target.next();
var step = iterator.next();
if (step.done)
return step;
if (step.done) return step;
if (!predicate(step.value))
return next();
if (!predicate(step.value)) return next();

@@ -28,0 +29,0 @@ return step;

@@ -1,1 +0,24 @@

export default function forEach(iterable: any, callback: (item: any, key: any) => void): void;
import type {Sequence} from './types';
interface ForEachTrait<K, V> {
forEach(callback: (value: V, key: K, self: this) => void): void;
}
interface PlainObject<T> {
[key: string]: T;
}
export default function forEach<K, V>(
iterable: ForEachTrait<K, V>,
callback: (value: V, key: K) => void
): void;
export default function forEach<T>(
iterable: Iterator<T> | Iterable<T> | Sequence<T>,
callback: (item: T, index: number) => void
): void;
export default function forEach<T>(
object: PlainObject<T>,
callback: (value: T, key: string) => void
): void;

@@ -7,8 +7,6 @@ /**

*/
var support = require('./support.js');
/**
* Constants.
*/
var ARRAY_BUFFER_SUPPORT = typeof ArrayBuffer !== 'undefined',
SYMBOL_SUPPORT = typeof Symbol !== 'undefined';
var ARRAY_BUFFER_SUPPORT = support.ARRAY_BUFFER_SUPPORT;
var SYMBOL_SUPPORT = support.SYMBOL_SUPPORT;

@@ -21,7 +19,6 @@ /**

*/
function forEach(iterable, callback) {
module.exports = function forEach(iterable, callback) {
var iterator, k, i, l, s;
if (!iterable)
throw new Error('obliterator/forEach: invalid iterable.');
if (!iterable) throw new Error('obliterator/forEach: invalid iterable.');

@@ -38,4 +35,3 @@ if (typeof callback !== 'function')

) {
for (i = 0, l = iterable.length; i < l; i++)
callback(iterable[i], i);
for (i = 0, l = iterable.length; i < l; i++) callback(iterable[i], i);
return;

@@ -64,3 +60,3 @@ }

while ((s = iterator.next(), s.done !== true)) {
while (((s = iterator.next()), s.done !== true)) {
callback(s.value, i);

@@ -81,81 +77,2 @@ i++;

return;
}
/**
* Same function as the above `forEach` but will yield `null` when the target
* does not have keys.
*
* @param {any} iterable - Iterable value.
* @param {function} callback - Callback function.
*/
forEach.forEachWithNullKeys = function(iterable, callback) {
var iterator, k, i, l, s;
if (!iterable)
throw new Error('obliterator/forEachWithNullKeys: invalid iterable.');
if (typeof callback !== 'function')
throw new Error('obliterator/forEachWithNullKeys: expecting a callback.');
// The target is an array or a string or function arguments
if (
Array.isArray(iterable) ||
(ARRAY_BUFFER_SUPPORT && ArrayBuffer.isView(iterable)) ||
typeof iterable === 'string' ||
iterable.toString() === '[object Arguments]'
) {
for (i = 0, l = iterable.length; i < l; i++)
callback(iterable[i], null);
return;
}
// The target is a Set
if (iterable instanceof Set) {
iterable.forEach(function(value) {
callback(value, null);
});
return;
}
// The target has a #.forEach method
if (typeof iterable.forEach === 'function') {
iterable.forEach(callback);
return;
}
// The target is iterable
if (
SYMBOL_SUPPORT &&
Symbol.iterator in iterable &&
typeof iterable.next !== 'function'
) {
iterable = iterable[Symbol.iterator]();
}
// The target is an iterator
if (typeof iterable.next === 'function') {
iterator = iterable;
i = 0;
while ((s = iterator.next(), s.done !== true)) {
callback(s.value, null);
i++;
}
return;
}
// The target is a plain object
for (k in iterable) {
if (iterable.hasOwnProperty(k)) {
callback(iterable[k], k);
}
}
return;
};
/**
* Exporting.
*/
module.exports = forEach;
export {default as Iterator} from './iterator';
export {default as iter} from './iter';
export {default as chain} from './chain';
export {default as combinations} from './combinations';
export {default as consume} from './consume';
export {default as every} from './every';
export {default as filter} from './filter';
export {default as find} from './find';
export {default as forEach} from './foreach';
export {default as forEachWithNullKeys} from './foreach-with-null-keys';
export {default as includes} from './includes';
export {default as map} from './map';

@@ -12,4 +17,5 @@ export {default as match} from './match';

export {default as range} from './range';
export {default as some} from './some';
export {default as split} from './split';
export {default as take} from './take';
export {default as takeInto} from './take-into';

@@ -7,17 +7,21 @@ /**

*/
module.exports = {
Iterator: require('./iterator.js'),
chain: require('./chain.js'),
combinations: require('./combinations.js'),
consume: require('./consume.js'),
filter: require('./filter.js'),
forEach: require('./foreach.js'),
map: require('./map.js'),
match: require('./match.js'),
permutations: require('./permutations.js'),
powerSet: require('./power-set.js'),
range: require('./range.js'),
split: require('./split.js'),
take: require('./take.js'),
takeInto: require('./take-into.js')
};
exports.Iterator = require('./iterator.js');
exports.iter = require('./iter.js');
exports.chain = require('./chain.js');
exports.combinations = require('./combinations.js');
exports.consume = require('./consume.js');
exports.every = require('./every.js');
exports.filter = require('./filter.js');
exports.find = require('./find.js');
exports.forEach = require('./foreach.js');
exports.forEachWithNullKeys = require('./foreach-with-null-keys.js');
exports.includes = require('./includes.js');
exports.map = require('./map.js');
exports.match = require('./match.js');
exports.permutations = require('./permutations.js');
exports.powerSet = require('./power-set.js');
exports.range = require('./range.js');
exports.some = require('./some.js');
exports.split = require('./split.js');
exports.take = require('./take.js');
exports.takeInto = require('./take-into.js');

@@ -0,11 +1,9 @@

import type {Sequence} from './types';
type NextFunction<V> = () => IteratorResult<V>;
export default class Iterator<V> implements IterableIterator<V> {
export default class ObliteratorIterator<V> implements IterableIterator<V> {
// Constructor
constructor(next: NextFunction<V>);
// Members
done: boolean;
// Well-known methods

@@ -16,5 +14,6 @@ next(): IteratorResult<V>;

// Static methods
static of<T>(...args: T[]): Iterator<T>;
static empty<T>(): Iterator<T>;
static of<T>(...args: T[]): ObliteratorIterator<T>;
static empty<T>(): ObliteratorIterator<T>;
static is(value: any): boolean;
static fromSequence<T>(sequence: Sequence<T>): ObliteratorIterator<T>;
}

@@ -15,37 +15,13 @@ /**

function Iterator(next) {
if (typeof next !== 'function')
throw new Error('obliterator/iterator: expecting a function!');
// Hiding the given function
Object.defineProperty(this, '_next', {
writable: false,
enumerable: false,
value: next
});
// Is the iterator complete?
this.done = false;
this.next = next;
}
/**
* Next function.
*
* @return {object}
*/
// NOTE: maybe this should dropped for performance?
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() {
Iterator.prototype[Symbol.iterator] = function () {
return this;

@@ -60,10 +36,9 @@ };

*/
Iterator.of = function() {
Iterator.of = function () {
var args = arguments,
l = args.length,
i = 0;
l = args.length,
i = 0;
return new Iterator(function() {
if (i >= l)
return {done: true};
return new Iterator(function () {
if (i >= l) return {done: true};

@@ -79,5 +54,6 @@ return {done: false, value: args[i++]};

*/
Iterator.empty = function() {
var iterator = new Iterator(null);
iterator.done = true;
Iterator.empty = function () {
var iterator = new Iterator(function () {
return {done: true};
});

@@ -88,2 +64,19 @@ return iterator;

/**
* Returning an iterator over the given indexed sequence.
*
* @param {string|Array} sequence - Target sequence.
* @return {Iterator}
*/
Iterator.fromSequence = function (sequence) {
var i = 0,
l = sequence.length;
return new Iterator(function () {
if (i >= l) return {done: true};
return {done: false, value: sequence[i++]};
});
};
/**
* Returning whether the given value is an iterator.

@@ -94,5 +87,4 @@ *

*/
Iterator.is = function(value) {
if (value instanceof Iterator)
return true;
Iterator.is = function (value) {
if (value instanceof Iterator) return true;

@@ -99,0 +91,0 @@ return (

The MIT License (MIT)
Copyright (c) 2017 Guillaume Plique (Yomguithereal)
Copyright (c) 2017-2021 Guillaume Plique (Yomguithereal)

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

@@ -1,5 +0,9 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
import type {IntoInterator} from './types';
type MapFunction<S, T> = (item: S) => T;
export default function map<S, T>(predicate: MapFunction<S, T>, iterator: Iterator<S>): ObliteratorIterator<T>;
export default function map<S, T>(
target: IntoInterator<S>,
predicate: MapFunction<S, T>
): ObliteratorIterator<T>;

@@ -8,2 +8,3 @@ /**

var Iterator = require('./iterator.js');
var iter = require('./iter.js');

@@ -13,12 +14,13 @@ /**

*
* @param {Iterator} target - Target iterable.
* @param {function} mapper - Map function.
* @param {Iterator} target - Target iterator.
* @return {Iterator}
*/
module.exports = function map(mapper, target) {
module.exports = function map(target, mapper) {
var iterator = iter(target);
return new Iterator(function next() {
var step = target.next();
var step = iterator.next();
if (step.done)
return step;
if (step.done) return step;

@@ -25,0 +27,0 @@ return {

@@ -1,3 +0,6 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
export default function match(pattern: RegExp, string: string): ObliteratorIterator<string>;
export default function match(
pattern: RegExp,
string: string
): ObliteratorIterator<string>;

@@ -21,3 +21,5 @@ /**

if (!(pattern instanceof RegExp))
throw new Error('obliterator/match: invalid pattern. Expecting a regular expression.');
throw new Error(
'obliterator/match: invalid pattern. Expecting a regular expression.'
);

@@ -27,3 +29,3 @@ if (typeof string !== 'string')

return new Iterator(function() {
return new Iterator(function () {
if (executed && !pattern.global) {

@@ -38,4 +40,3 @@ pattern.lastIndex = 0;

if (m)
return {value: m};
if (m) return {value: m};

@@ -42,0 +43,0 @@ pattern.lastIndex = 0;

{
"name": "obliterator",
"version": "1.6.1",
"description": "Higher order iterator library for JavaScript.",
"version": "2.0.0",
"description": "Higher order iterator library for JavaScript/TypeScript.",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"lint": "eslint *.js",
"prepublish": "npm run lint && npm test",
"prepublishOnly": "npm run lint && npm test",
"prettier": "prettier --write '*.js' '*.ts'",
"test": "mocha test.js && npm run test:types",

@@ -29,18 +31,17 @@ "test:types": "tsc --lib es2015,dom --noEmit --noImplicitAny --noImplicitReturns ./test-types.ts"

"devDependencies": {
"@yomguithereal/eslint-config": "^4.0.0",
"eslint": "^6.8.0",
"mocha": "^7.0.0",
"typescript": "^3.7.5"
"@yomguithereal/eslint-config": "^4.4.0",
"@yomguithereal/prettier-config": "^1.2.0",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"mocha": "^9.1.3",
"prettier": "^2.4.1",
"typescript": "^4.4.4"
},
"eslintConfig": {
"extends": "@yomguithereal/eslint-config",
"globals": {
"ArrayBuffer": true,
"Map": true,
"Set": true,
"Symbol": true,
"Uint8Array": true,
"Uint32Array": true
}
}
"extends": [
"@yomguithereal/eslint-config",
"eslint-config-prettier"
]
},
"prettier": "@yomguithereal/prettier-config"
}

@@ -1,3 +0,6 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
export default function permutations<T>(array: Array<T>, r: number): ObliteratorIterator<Array<T>>;
export default function permutations<T>(
array: Array<T>,
r: number
): ObliteratorIterator<Array<T>>;

@@ -13,4 +13,3 @@ /**

function indicesToItems(target, items, indices, r) {
for (var i = 0; i < r; i++)
target[i] = items[indices[i]];
for (var i = 0; i < r; i++) target[i] = items[indices[i]];
}

@@ -27,20 +26,25 @@

if (!Array.isArray(array))
throw new Error('obliterator/permutations: first argument should be an array.');
throw new Error(
'obliterator/permutations: first argument should be an array.'
);
var n = array.length;
if (arguments.length < 2)
r = n;
if (arguments.length < 2) r = n;
if (typeof r !== 'number')
throw new Error('obliterator/permutations: second argument should be omitted or a number.');
throw new Error(
'obliterator/permutations: second argument should be omitted or a number.'
);
if (r > n)
throw new Error('obliterator/permutations: the size of the subsequences should not exceed the length of the array.');
throw new Error(
'obliterator/permutations: the size of the subsequences should not exceed the length of the array.'
);
var indices = new Uint32Array(n),
subsequence = new Array(r),
cycles = new Uint32Array(r),
first = true,
i;
subsequence = new Array(r),
cycles = new Uint32Array(r),
first = true,
i;

@@ -50,4 +54,3 @@ for (i = 0; i < n; i++) {

if (i < r)
cycles[i] = n - i;
if (i < r) cycles[i] = n - i;
}

@@ -61,12 +64,10 @@

indicesToItems(subsequence, array, indices, r);
return {value: subsequence};
return {value: subsequence, done: false};
}
var tmp,
j;
var tmp, j;
i--;
if (i < 0)
return {done: true};
if (i < 0) return {done: true};

@@ -76,7 +77,5 @@ cycles[i]--;

if (cycles[i] === 0) {
tmp = indices[i];
for (j = i; j < n - 1; j++)
indices[j] = indices[j + 1];
for (j = i; j < n - 1; j++) indices[j] = indices[j + 1];

@@ -87,4 +86,3 @@ indices[n - 1] = tmp;

return next();
}
else {
} else {
j = cycles[i];

@@ -99,5 +97,5 @@ tmp = indices[i];

indicesToItems(subsequence, array, indices, r);
return {value: subsequence};
return {value: subsequence, done: false};
}
});
};

@@ -1,3 +0,5 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
export default function powerSet<T>(array: Array<T>): ObliteratorIterator<Array<T>>;
export default function powerSet<T>(
array: Array<T>
): ObliteratorIterator<Array<T>>;

@@ -8,4 +8,4 @@ /**

var Iterator = require('./iterator.js'),
combinations = require('./combinations.js'),
chain = require('./chain.js');
combinations = require('./combinations.js'),
chain = require('./chain.js');

@@ -25,6 +25,5 @@ /**

for (var i = 1; i < n + 1; i++)
iterators[i] = combinations(array, i);
for (var i = 1; i < n + 1; i++) iterators[i] = combinations(array, i);
return chain.apply(null, iterators);
};

@@ -1,5 +0,12 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
export default function range(end: number): ObliteratorIterator<number>;
export default function range(start: number, end: number): ObliteratorIterator<number>;
export default function range(start: number, end: number, step: number): ObliteratorIterator<number>;
export default function range(
start: number,
end: number
): ObliteratorIterator<number>;
export default function range(
start: number,
end: number,
step: number
): ObliteratorIterator<number>;

@@ -23,8 +23,7 @@ /**

if (arguments.length < 3)
step = 1;
if (arguments.length < 3) step = 1;
var i = start;
var iterator = new Iterator(function() {
var iterator = new Iterator(function () {
if (i < end) {

@@ -35,3 +34,3 @@ var value = i;

return {value: value};
return {value: value, done: false};
}

@@ -38,0 +37,0 @@

@@ -1,7 +0,9 @@

[![Build Status](https://travis-ci.org/Yomguithereal/obliterator.svg)](https://travis-ci.org/Yomguithereal/obliterator)
[![Build Status](https://github.com/Yomguithereal/obliterator/workflows/Tests/badge.svg)](https://github.com/Yomguithereal/obliterator/actions)
# Obliterator
Obliterator is a dead simple JavaScript/TypeScript library providing miscellaneous higher-order iterator functions such as combining two or more iterators into a single one.
Obliterator is a dead simple JavaScript/TypeScript library providing miscellaneous higher-order iterator/iterable functions such as combining two or more iterators into a single one.
Note that when possible, `obliterator` also consider sequences such as arrays, strings etc. as valid iterables (although they are not proper ES6 iterables values), for convenience.
# Installation

@@ -13,3 +15,3 @@

Note `obliterator` comes along with its TypeScript declarations.
Note that `obliterator` comes along with its TypeScript declarations.

@@ -20,23 +22,29 @@ # Usage

*Classes*
_Classes_
* [Iterator](#iterator)
- [Iterator](#iterator)
*Functions*
_Functions_
* [chain](#chain)
* [combinations](#combinations)
* [consume](#consume)
* [filter](#filter)
* [forEach](#foreach)
* [map](#map)
* [match](#match)
* [permutations](#permutations)
* [powerSet](#powerSet)
* [split](#split)
* [take](#take)
- [chain](#chain)
- [combinations](#combinations)
- [consume](#consume)
- [every](#every)
- [filter](#filter)
- [find](#find)
- [forEach](#foreach)
- [forEachWithNullKeys](#foreachwithnullkeys)
- [includes](#includes)
- [iter](#iter)
- [map](#map)
- [match](#match)
- [permutations](#permutations)
- [powerSet](#powerSet)
- [some](#some)
- [split](#split)
- [take](#take)
## Iterator
A handy Iterator class with safeguards and usable with ES2015's `for ... of` loop constructs & spread operator.
A handy Iterator class easily usable with ES2015's `for ... of` loop constructs & spread operator.

@@ -48,4 +56,5 @@ ```js

const iterator = new Iterator(function() {
const iterator = new Iterator(function () {
// Define what the `next` function does
return {done: false, value: 34};
});

@@ -68,3 +77,3 @@

Variadic function chaining all the given iterators.
Variadic function chaining all the given iterable-like values.

@@ -79,3 +88,3 @@ ```js

const chained = chain(set1.values(), set2.values());
const chained = chain(set1.values(), set2);

@@ -121,4 +130,3 @@ chained.next();

consume(iterator);
iterator.next().done
>>> true
iterator.next().done >>> true;

@@ -128,9 +136,24 @@ // Consuming n steps

consume(iterator, 2);
iterator.next().value
>>> 3
iterator.next().value >>> 3;
```
## every
Function returning whether all items of an iterable-like match the given predicate function.
```js
import every from 'obliterator/every';
// Or
import {every} from 'obliterator';
every([2, 4, 6], n => n % 2 === 0);
>>> true
every([1, 2, 3], n => n % 2 === 0);
>>> false
```
## filter
Function returning an iterator filtering another one's values using the given predicate.
Function returning an iterator filtering another one's values using the given predicate function.

@@ -146,8 +169,31 @@ ```js

const iterator = filter(even, set.values());
const iterator = filter(set.values(), even);
iterator.next().value
iterator.next().value >>> 2;
iterator.next().value >>> 4;
```
## find
Function returning the next item matching given predicate function in an iterable-like.
```js
import find from 'obliterator/find';
// Or
import {find} from 'obliterator';
const set = new Set([1, 2, 3, 4, 5]);
const even = x => x % 2 === 0;
const values = set.values();
find(values, even);
>>> 2
iterator.next().value
find(values, even);
>>> 4
find(values, even);
>>> undefined
```

@@ -179,4 +225,8 @@

Optionally, one can use the `forEachWithNullKeys` function to iterate over mixed values but with the twist that iterables without proper keys (lists, sets etc.), will yield `null` instead of an index key.
## forEachWithNullKeys
Variant of [forEach](#foreach) one can use to iterate over mixed values but with the twist that iterables without proper keys (lists, sets etc.), will yield `null` instead of an index key.
Supported values range from arrays, typed arrays, sets, maps, objects, strings, arguments, iterators, arbitrary iterables etc.
```js

@@ -194,2 +244,34 @@ import {forEachWithNullKeys} from 'obliterator/foreach';

## includes
Function returning whether the given value can be found in given iterable-like.
```js
import {includes} from 'obliterator';
// Or
import includes from 'obliterator/includes';
includes([1, 2, 3], 3);
>>> true;
includes('test', 'a');
>>> false;
```
## iter
Function casting any iterable-like value to a proper iterator. Will throw an error if the given value cannot be cast as an iterator.
```js
import {iter} from 'obliterator';
// Or
import iter from 'obliterator/iter';
iter('test');
iter(new Set([1, 2, 3]));
// This will throw:
iter(null);
```
## map

@@ -208,8 +290,6 @@

const iterator = map(triple, set.values());
const iterator = map(set.values(), triple);
iterator.next().value
>>> 3
iterator.next().value
>>> 6
iterator.next().value >>> 3;
iterator.next().value >>> 6;
```

@@ -228,6 +308,4 @@

iterator.next().value.index
>>> 0
iterator.next().value.index
>>> 3
iterator.next().value.index >>> 0;
iterator.next().value.index >>> 3;
```

@@ -278,2 +356,18 @@

## some
Returns whether the given iterable-like has some item matching the given predicate function.
```js
import some from 'obliterator/some';
// Or
import {some} from 'obliterator';
some(new Set([1, 2, 3]), n => n % 2 === 0);
>>> true
some('test', c => c === 'a');
>>> false
```
## split

@@ -280,0 +374,0 @@

@@ -1,3 +0,6 @@

import {default as ObliteratorIterator} from './iterator.js';
import ObliteratorIterator from './iterator';
export default function split(pattern: RegExp, string: string): ObliteratorIterator<string>;
export default function split(
pattern: RegExp,
string: string
): ObliteratorIterator<string>;

@@ -35,3 +35,5 @@ /**

if (!(pattern instanceof RegExp))
throw new Error('obliterator/split: invalid pattern. Expecting a regular expression.');
throw new Error(
'obliterator/split: invalid pattern. Expecting a regular expression.'
);

@@ -46,11 +48,10 @@ if (typeof string !== 'string')

var consumed = false,
current = 0;
current = 0;
return new Iterator(function() {
if (consumed)
return {done: true};
return new Iterator(function () {
if (consumed) return {done: true};
var match = pattern.exec(string),
value,
length;
value,
length;

@@ -62,4 +63,3 @@ if (match) {

current = length;
}
else {
} else {
consumed = true;

@@ -69,4 +69,4 @@ value = string.slice(current);

return {value: value};
return {value: value, done: false};
});
};

@@ -1,5 +0,9 @@

import {default as ObliteratorIterator} from './iterator.js';
import type {IntoInterator} from './types';
// Requires a resolution of https://github.com/microsoft/TypeScript/issues/1213
// export default function takeInto<C<~>, T>(ArrayClass: new <T>(n: number) => C<T>, iterator: Iterator<T>, n: number): C<T>;
export default function takeInto<T>(ArrayClass: new <T>(arrayLength: number) => T[], iterator: Iterator<T>, n: number): T[];
export default function takeInto<T>(
ArrayClass: new <T>(arrayLength: number) => T[],
iterator: IntoInterator<T>,
n: number
): T[];

@@ -9,2 +9,3 @@ /* eslint no-constant-condition: 0 */

*/
var iter = require('./iter.js');

@@ -15,23 +16,21 @@ /**

* @param {function} ArrayClass - Array class to use.
* @param {Iterator} iterator - Target iterator.
* @param {Iterable} iterable - Target iterable.
* @param {number} n - Number of items to take.
* @return {array}
*/
module.exports = function takeInto(ArrayClass, iterator, n) {
module.exports = function takeInto(ArrayClass, iterable, n) {
var array = new ArrayClass(n),
step,
i = 0;
step,
i = 0;
var iterator = iter(iterable);
while (true) {
if (i === n) return array;
if (i === n)
return array;
step = iterator.next();
if (step.done) {
if (i !== n) return array.slice(0, i);
if (i !== n)
return array.slice(0, i);
return array;

@@ -38,0 +37,0 @@ }

@@ -1,3 +0,6 @@

import {default as ObliteratorIterator} from './iterator.js';
import type {IntoInterator} from './types';
export default function take<T>(iterator: Iterator<T>, n: number): Array<T>;
export default function take<T>(
iterator: IntoInterator<T>,
n: number
): Array<T>;

@@ -9,2 +9,3 @@ /* eslint no-constant-condition: 0 */

*/
var iter = require('./iter.js');

@@ -14,24 +15,22 @@ /**

*
* @param {Iterator} iterator - Target iterator.
* @param {Iterable} iterable - Target iterable.
* @param {number} [n] - Optional number of items to take.
* @return {array}
*/
module.exports = function take(iterator, n) {
module.exports = function take(iterable, n) {
var l = arguments.length > 1 ? n : Infinity,
array = l !== Infinity ? new Array(l) : [],
step,
i = 0;
array = l !== Infinity ? new Array(l) : [],
step,
i = 0;
var iterator = iter(iterable);
while (true) {
if (i === l) return array;
if (i === l)
return array;
step = iterator.next();
if (step.done) {
if (i !== n) array.length = i;
if (i !== n)
return array.slice(0, i);
return array;

@@ -38,0 +37,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