@hyperjump/pact
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -15,2 +15,16 @@ export const map: ( | ||
export const tap: ( | ||
<A>(fn: Tapper<A>, iterator: Iterable<A>) => Generator<A> | ||
) & ( | ||
<A>(fn: Tapper<A>) => (iterator: Iterable<A>) => Generator<A> | ||
); | ||
export type Tapper<A> = (item: A) => void; | ||
export const asyncTap: ( | ||
<A>(fn: AsyncTapper<A>, iterator: AsyncIterable<A>) => AsyncGenerator<A> | ||
) & ( | ||
<A>(fn: AsyncTapper<A>) => (iterator: AsyncIterable<A>) => AsyncGenerator<A> | ||
); | ||
export type AsyncTapper<A> = (item: A) => void; | ||
export const filter: ( | ||
@@ -30,2 +44,20 @@ <A>(fn: Predicate<A>, iterator: Iterable<A>) => Generator<A> | ||
export const scan: ( | ||
<A, B>(fn: Reducer<A, B>, acc: B, iter: Iterable<A>) => Generator<B> | ||
) & ( | ||
<A, B>(fn: Reducer<A, B>, acc: B) => (iter: Iterable<A>) => Generator<B> | ||
); | ||
export const asyncScan: ( | ||
<A, B>(fn: AsyncReducer<A, B>, acc: B, iter: AsyncIterable<A>) => AsyncGenerator<B> | ||
) & ( | ||
<A, B>(fn: AsyncReducer<A, B>, acc: B) => (iter: AsyncIterable<A>) => AsyncGenerator<B> | ||
); | ||
export const flatten: <A>(iterator: NestedIterable<A>, depth?: number) => Generator<A | NestedIterable<A>>; | ||
export type NestedIterable<A> = Iterable<A | NestedIterable<A>>; | ||
export const asyncFlatten: <A>(iterator: NestedAsyncIterable<A>, depth?: number) => AsyncGenerator<A | NestedIterable<A> | NestedAsyncIterable<A>>; | ||
export type NestedAsyncIterable<A> = AsyncIterable<A | NestedAsyncIterable<A> | NestedIterable<A>>; | ||
export const drop: ( | ||
@@ -98,14 +130,15 @@ <A>(count: number, iterator: Iterable<A>) => Generator<A> | ||
export const collectArray: <A>(fn: Iterable<A>) => A[]; | ||
export const asyncCollectArray: <A>(fn: AsyncIterable<A>) => Promise<A[]>; | ||
export const collectArray: <A>(iterator: Iterable<A>) => A[]; | ||
export const asyncCollectArray: <A>(iterator: AsyncIterable<A>) => Promise<A[]>; | ||
export const collectSet: <A>(fn: Iterable<A>) => Set<A>; | ||
export const asyncCollectSet: <A>(fn: AsyncIterable<A>) => Promise<Set<A>>; | ||
export const collectSet: <A>(iterator: Iterable<A>) => Set<A>; | ||
export const asyncCollectSet: <A>(iterator: AsyncIterable<A>) => Promise<Set<A>>; | ||
export const collectMap: <A, B>(fn: Iterable<[A, B]>) => Map<A, B>; | ||
export const asyncCollectMap: <A, B>(fn: AsyncIterable<[A, B]>) => Promise<Map<A, B>>; | ||
export const collectMap: <A, B>(iterator: Iterable<[A, B]>) => Map<A, B>; | ||
export const asyncCollectMap: <A, B>(iterator: AsyncIterable<[A, B]>) => Promise<Map<A, B>>; | ||
export const collectObject: <A>(fn: Iterable<[string, A]>) => Record<string, A>; | ||
export const asyncCollectObject: <A>(fn: AsyncIterable<[string, A]>) => Promise<Record<string, A>>; | ||
export const collectObject: <A>(iterator: Iterable<[string, A]>) => Record<string, A>; | ||
export const asyncCollectObject: <A>(iterator: AsyncIterable<[string, A]>) => Promise<Record<string, A>>; | ||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any | ||
export const pipe: <A>(iterator: Iterable<any> | AsyncIterable<any>, ...fns: Function[]) => A; |
@@ -16,2 +16,16 @@ import curry from "just-curry-it"; | ||
export const tap = curry(function* (fn, iter) { | ||
for (const n of iter) { | ||
fn(n); | ||
yield n; | ||
} | ||
}); | ||
export const asyncTap = curry(async function* (fn, iter) { | ||
for await (const n of iter) { | ||
await fn(n); | ||
yield n; | ||
} | ||
}); | ||
export const filter = curry(function* (fn, iter) { | ||
@@ -33,2 +47,36 @@ for (const n of iter) { | ||
export const scan = curry(function* (fn, acc, iter) { | ||
for (const item of iter) { | ||
acc = fn(acc, item); | ||
yield acc; | ||
} | ||
}); | ||
export const asyncScan = curry(async function* (fn, acc, iter) { | ||
for await (const item of iter) { | ||
acc = await fn(acc, item); | ||
yield acc; | ||
} | ||
}); | ||
export const flatten = function* (iter, depth = 1) { | ||
for (const n of iter) { | ||
if (depth > 0 && n !== null && typeof n !== "string" && n[Symbol.iterator]) { | ||
yield* flatten(n, depth - 1); | ||
} else { | ||
yield n; | ||
} | ||
} | ||
}; | ||
export const asyncFlatten = async function* (iter, depth = 1) { | ||
for await (const n of iter) { | ||
if (depth > 0 && n !== null && typeof n !== "string" && (n[Symbol.asyncIterator] || n[Symbol.iterator])) { | ||
yield* asyncFlatten(n, depth - 1); | ||
} else { | ||
yield n; | ||
} | ||
} | ||
}; | ||
export const drop = curry(function* (count, iter) { | ||
@@ -75,2 +123,3 @@ let index = 0; | ||
export const range = function* (from, to) { | ||
// eslint-disable-next-line no-unmodified-loop-condition | ||
for (let n = from; n < to || to === undefined; n++) { | ||
@@ -77,0 +126,0 @@ yield n; |
{ | ||
"name": "@hyperjump/pact", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Higher order functions for iterators and async iterators", | ||
@@ -8,3 +8,4 @@ "type": "module", | ||
"exports": { | ||
".": "./lib/index.js" | ||
".": "./lib/index.js", | ||
"./async": "./lib/async.js" | ||
}, | ||
@@ -11,0 +12,0 @@ "scripts": { |
@@ -16,6 +16,6 @@ # Hyperjump Pact | ||
```javascript | ||
import { pipe, range, map, filter, reduce, asyncMap, asyncFilter, asyncReduce } from "./lib/index.js"; | ||
import { pipe, range, map, filter, reduce } from "@hyperjump/pact"; | ||
const foo = pipe( | ||
const result = pipe( | ||
range(1, 10), | ||
@@ -26,10 +26,17 @@ filter((n) => n % 2 === 0), | ||
); | ||
console.log(foo); | ||
console.log(result); | ||
``` | ||
```javascript | ||
import { pipe, asyncMap, asyncFilter, asyncReduce } from "@hyperjump/pact"; | ||
// You can alternatively import the async functions without the prefix | ||
// import { pipe, map, filter, reduce } from "@hyperjump/pact/async"; | ||
const asyncSequence = async function* () { | ||
yield Promise.resolve(1); | ||
yield Promise.resolve(2); | ||
yield Promise.resolve(3); | ||
yield Promise.resolve(4); | ||
yield Promise.resolve(5); | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
yield 4; | ||
yield 5; | ||
}; | ||
@@ -41,3 +48,3 @@ | ||
const asyncFoo = await pipe( | ||
const result = await pipe( | ||
asyncSequence(), | ||
@@ -48,3 +55,3 @@ asyncFilter((n) => n % 2 === 0), | ||
); | ||
console.log(asyncFoo); | ||
console.log(result); | ||
``` | ||
@@ -59,2 +66,9 @@ | ||
Same as `map`, but works with promises. | ||
* **tap**: (fn: Function, iterator: Iterable) => Generator | ||
Apply a function to every value in the iterator, but yield the original | ||
value, not the result of the function. | ||
* **asyncTap**: (fn: Function, iterator: AsyncIterable) => AsyncGenerator | ||
Same as `tap`, but works with promises. | ||
* **filter**: (fn: Function, iterator: Iterable) => Generator | ||
@@ -66,2 +80,15 @@ | ||
Same as `filter`, but works with promises. | ||
* **scan**: (fn: Function, acc: any, iter: Iterable) => any | ||
Same as `reduce` except it emits the accumulated value after each update | ||
* **asyncScan**: (fn: Function, acc: any, iter: AsyncIterable) => Promise<any> | ||
Same as `scan`, but works with promises. | ||
* **flatten**: (iterator: NestedIterable, depth: number = 1) => Generator | ||
Yields values from the iterator with all sub-iterator elements concatenated | ||
into it recursively up to the specified depth. | ||
* **asyncFlatten**: (iterator: NestedAsyncIterable, depth: number = 1) => AsyncGenerator | ||
Same as `flatten`, but works with promises. | ||
* **drop**: (n: number, iterator: Iterable) => Generator | ||
@@ -95,3 +122,3 @@ | ||
Reduce an iterator to a single value. | ||
* **asyncReduce**: (fn: AsyncReducer, acc: any, iter: AsyncIterable) => Promise<any> | ||
* **asyncReduce**: (fn: Function, acc: any, iter: AsyncIterable) => Promise<any> | ||
@@ -103,3 +130,3 @@ Same as `reduce`, but works with promises. | ||
passes the predicate function. | ||
* **asyncEvery**: (fn: AsyncPredicate, iterator: AsyncIterable) => Promise<boolean> | ||
* **asyncEvery**: (fn: Function, iterator: AsyncIterable) => Promise<boolean> | ||
@@ -111,27 +138,27 @@ Same as `every`, but works with promises. | ||
iterator that passes the predicate function. | ||
* **asyncSome**: (fn: AsyncPredicate, iterator: AsyncIterable) => Promise<boolean> | ||
* **asyncSome**: (fn: Function, iterator: AsyncIterable) => Promise<boolean> | ||
Same as `some`, but works with promises. | ||
* **collectArray**: (fn: Iterable) => Array; | ||
* **collectArray**: (iterator: Iterable) => Array; | ||
Collect all the items in the iterator into an array. | ||
* **asyncCollectArray**: (fn: AsyncIterable) => Promise<Array>; | ||
* **asyncCollectArray**: (iterator: AsyncIterable) => Promise<Array>; | ||
Same as `collectArray`, but works with promises. | ||
* **collectSet**: (fn: Iterable) => Set; | ||
* **collectSet**: (iterator: Iterable) => Set; | ||
Collect all the items in the iterator into a Set. | ||
* **asyncCollectSet**: (fn: AsyncIterable) => Promise<Set>; | ||
* **asyncCollectSet**: (iterator: AsyncIterable) => Promise<Set>; | ||
Same as `collectSet`, but works with promises. | ||
* **collectMap**: (fn: Iterable) => Map; | ||
* **collectMap**: (iterator: Iterable) => Map; | ||
Collect all the key/value tuples in the iterator into a Map. | ||
* **asyncCollectMap**: (fn: AsyncIterable) => Promise<Map>; | ||
* **asyncCollectMap**: (iterator: AsyncIterable) => Promise<Map>; | ||
Same as `collectMap`, but works with promises. | ||
* **collectObject**: (fn: Iterable) => Object; | ||
* **collectObject**: (iterator: Iterable) => Object; | ||
Collect all the key/value tuples in the iterator into an Object. | ||
* **asyncCollectObject**: (fn: AsyncIterable) => Promise<Object>; | ||
* **asyncCollectObject**: (iterator: AsyncIterable) => Promise<Object>; | ||
@@ -138,0 +165,0 @@ Same as `collectObject`, but works with promises. |
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
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
18615
346
180
0