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

@ts-common/iterator

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ts-common/iterator - npm Package Compare versions

Comparing version 0.0.21 to 0.0.24

18

index.d.ts

@@ -0,12 +1,20 @@

import { Tuple2 } from "@ts-common/tuple";
export declare function iterable<T>(createIterator: () => Iterator<T>): Iterable<T>;
export declare type Entry<T> = Tuple2<number, T>;
export declare const entry: <T>(key: number, value: T) => Entry<T>;
export declare function entries<T>(input: Iterable<T>): Iterable<Entry<T>>;
export declare function map<T, I>(input: Iterable<I>, func: (v: I, i: number) => T): Iterable<T>;
export declare function forEach<T>(input: Iterable<T>, func: (v: T, i: number) => void): void;
export declare function filterMap<T, I>(input: Iterable<I>, func: (v: I, i: number) => T | undefined): Iterable<T>;
export declare function filter<T>(input: Iterable<T>, func: (v: T) => boolean): Iterable<T>;
export declare function flatten<T>(input: Iterable<Iterable<T>>): Iterable<T>;
export declare function takeWhile<T>(input: Iterable<T>, func: (v: T, i: number) => boolean): Iterable<T>;
export declare function flatMap<T, I>(input: Iterable<I>, func: (v: I, i: number) => Iterable<T>): Iterable<T>;
export declare function optionalToArray<T>(v: T | undefined): ReadonlyArray<T>;
export declare function filterMap<T, I>(input: Iterable<I>, func: (v: I, i: number) => T | undefined): Iterable<T>;
export declare function filter<T>(input: Iterable<T>, func: (v: T, i: number) => boolean): Iterable<T>;
export declare function generate<T>(func: (i: number) => T, count?: number): Iterable<T>;
export declare function repeat<T>(v: T, count?: number): Iterable<T>;
export declare function reduce<T>(input: Iterable<T>, func: (a: T, b: T) => T, init: T): T;
export declare function reduce<T>(input: Iterable<T>, func: (a: T, b: T) => T): T | undefined;
export declare function lazyFold<T, A>(input: Iterable<T>, func: (a: A, b: T, i: number) => A, init: A): Iterable<A>;
export declare function last<T>(input: Iterable<T>): T | undefined;
export declare function fold<T, A>(input: Iterable<T>, func: (a: A, b: T, i: number) => A, init: A): A;
export declare function reduce<T>(input: Iterable<T>, func: (a: T, b: T, i: number) => T): T | undefined;
export declare function forEach<T>(input: Iterable<T>, func: (v: T, i: number) => void): void;
export declare function sum(input: Iterable<number>): number;

@@ -13,0 +21,0 @@ export declare function min(input: Iterable<number>): number;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tuple_1 = require("@ts-common/tuple");
function iterable(createIterator) {
return {
[Symbol.iterator]() { return createIterator(); },
};
return { [Symbol.iterator]: createIterator };
}
exports.iterable = iterable;
function map(input, func) {
exports.entry = tuple_1.tuple2;
function entries(input) {
function* iterator() {
let i = 0;
for (const v of input) {
yield func(v, i);
++i;
let index = 0;
/* tslint:disable-next-line:no-loop-statement */
for (const value of input) {
yield exports.entry(index, value);
/* tslint:disable-next-line:no-expression-statement */
++index;
}

@@ -19,20 +21,8 @@ }

}
exports.map = map;
function forEach(input, func) {
let i = 0;
for (const v of input) {
func(v, i);
++i;
}
}
exports.forEach = forEach;
function filterMap(input, func) {
exports.entries = entries;
function map(input, func) {
function* iterator() {
let i = 0;
for (const v of input) {
const result = func(v, i);
if (result !== undefined) {
yield result;
}
++i;
/* tslint:disable-next-line:no-loop-statement */
for (const [index, value] of entries(input)) {
yield func(value, index);
}

@@ -42,9 +32,8 @@ }

}
exports.filterMap = filterMap;
function filter(input, func) {
exports.map = map;
function flatten(input) {
function* iterator() {
/* tslint:disable-next-line:no-loop-statement */
for (const v of input) {
if (func(v)) {
yield v;
}
yield* v;
}

@@ -54,7 +43,12 @@ }

}
exports.filter = filter;
function flatten(input) {
exports.flatten = flatten;
function takeWhile(input, func) {
function* iterator() {
for (const v of input) {
yield* v;
/* tslint:disable-next-line:no-loop-statement */
for (const [index, value] of entries(input)) {
/* tslint:disable-next-line:no-if-statement */
if (!func(value, index)) {
return;
}
yield value;
}

@@ -64,3 +58,3 @@ }

}
exports.flatten = flatten;
exports.takeWhile = takeWhile;
function flatMap(input, func) {

@@ -70,7 +64,19 @@ return flatten(map(input, func));

exports.flatMap = flatMap;
function generate(func, count) {
const f = count === undefined ? () => true : i => i < count;
function optionalToArray(v) {
return v === undefined ? [] : [v];
}
exports.optionalToArray = optionalToArray;
function filterMap(input, func) {
return flatMap(input, (v, i) => optionalToArray(func(v, i)));
}
exports.filterMap = filterMap;
function filter(input, func) {
return flatMap(input, (v, i) => func(v, i) ? [v] : []);
}
exports.filter = filter;
function infinite() {
function* iterator() {
for (let i = 0; f(i); ++i) {
yield func(i);
/* tslint:disable-next-line:no-loop-statement */
while (true) {
yield;
}

@@ -80,24 +86,57 @@ }

}
function generate(func, count) {
return map(takeWhile(infinite(), (_, i) => i !== count), (_, i) => func(i));
}
exports.generate = generate;
function repeat(v, count) {
return generate(_ => v, count);
return generate(() => v, count);
}
exports.repeat = repeat;
function reduce(input, func, init) {
function lazyFold(input, func, init) {
function* iterator() {
let result = init;
/* tslint:disable-next-line:no-loop-statement */
for (const [index, value] of entries(input)) {
/* tslint:disable-next-line:no-expression-statement */
result = func(result, value, index);
yield result;
}
}
return iterable(iterator);
}
exports.lazyFold = lazyFold;
function last(input) {
let result;
/* tslint:disable-next-line:no-loop-statement */
for (const v of input) {
init = init === undefined ? v : func(init, v);
/* tslint:disable-next-line:no-expression-statement */
result = v;
}
return init;
return result;
}
exports.last = last;
function fold(input, func, init) {
const result = last(lazyFold(input, func, init));
return result !== undefined ? result : init;
}
exports.fold = fold;
function reduce(input, func) {
return fold(input, (a, b, i) => a !== undefined ? func(a, b, i) : b, undefined);
}
exports.reduce = reduce;
function forEach(input, func) {
/* tslint:disable-next-line:no-expression-statement */
fold(input, (_, v, i) => { func(v, i); }, undefined);
}
exports.forEach = forEach;
function sum(input) {
return reduce(input, (a, b) => a + b, 0);
return fold(input, (a, b) => a + b, 0);
}
exports.sum = sum;
function min(input) {
return reduce(input, Math.min, Infinity);
return fold(input, (a, b) => Math.min(a, b), Infinity);
}
exports.min = min;
function max(input) {
return reduce(input, Math.max, -Infinity);
return fold(input, (a, b) => Math.max(a, b), -Infinity);
}

@@ -109,12 +148,14 @@ exports.max = max;

const iterators = inputs.map(i => i[Symbol.iterator]());
/* tslint:disable-next-line:no-loop-statement */
while (true) {
const result = new Array(inputs.length);
let i = 0;
for (const it of iterators) {
/* tslint:disable-next-line:no-loop-statement */
for (const [index, it] of entries(iterators)) {
const v = it.next();
/* tslint:disable-next-line:no-if-statement */
if (v.done) {
return;
}
result[i] = v.value;
++i;
/* tslint:disable-next-line:no-object-mutation no-expression-statement */
result[index] = v.value;
}

@@ -128,5 +169,7 @@ yield result;

function arrayEqual(a, b, e) {
/* tslint:disable-next-line:no-if-statement */
if (a === b) {
return true;
}
/* tslint:disable-next-line:no-if-statement */
if (a === undefined || b === undefined) {

@@ -137,6 +180,9 @@ return false;

const bl = b.length;
/* tslint:disable-next-line:no-if-statement */
if (al !== bl) {
return false;
}
/* tslint:disable-next-line:no-loop-statement */
for (let i = 0; i < al; ++i) {
/* tslint:disable-next-line:no-if-statement */
if (!e(a[i], b[i])) {

@@ -143,0 +189,0 @@ return false;

{
"name": "@ts-common/iterator",
"version": "0.0.21",
"version": "0.0.24",
"description": "Iterator library for JavaScript and TypeScript",

@@ -16,3 +16,3 @@ "main": "index.js",

"prepack": "npm install && tsc",
"test": "tsc && nyc mocha"
"test": "tsc && npm run tslint && nyc mocha"
},

@@ -45,3 +45,5 @@ "repository": {

},
"dependencies": {}
"dependencies": {
"@ts-common/tuple": "0.0.0"
}
}

@@ -6,1 +6,5 @@ # iterator

Iterator library for TypeScript and JavaScript
## fold vs reduce
https://stackoverflow.com/questions/25158780/difference-between-reduce-and-foldleft-fold-in-functional-programming-particula
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