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

@byloth/core

Package Overview
Dependencies
Maintainers
0
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@byloth/core - npm Package Compare versions

Comparing version 2.0.0-rc.2 to 2.0.0-rc.3

1229

dist/core.js

@@ -1,18 +0,18 @@

var j = Object.defineProperty;
var D = (s, e, t) => e in s ? j(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var u = (s, e, t) => D(s, typeof e != "symbol" ? e + "" : e, t);
const E = typeof window < "u" && typeof window.document < "u";
var S;
const J = typeof process < "u" && ((S = process.versions) == null ? void 0 : S.node);
var P = Object.defineProperty;
var j = (i, e, t) => e in i ? P(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
var u = (i, e, t) => j(i, typeof e != "symbol" ? e + "" : e, t);
const S = typeof window < "u" && typeof window.document < "u";
var b;
const $ = typeof process < "u" && ((b = process.versions) == null ? void 0 : b.node);
var k;
const W = typeof self == "object" && ((k = self.constructor) == null ? void 0 : k.name) === "DedicatedWorkerGlobalScope";
class h extends Error {
const A = typeof self == "object" && ((k = self.constructor) == null ? void 0 : k.name) === "DedicatedWorkerGlobalScope";
class f extends Error {
static FromUnknown(e) {
if (e instanceof h)
if (e instanceof f)
return e;
if (e instanceof Error) {
const t = new h(e.message);
const t = new f(e.message);
return t.stack = e.stack, t.name = e.name, t;
}
return new h(`${e}`);
return new f(`${e}`);
}

@@ -30,3 +30,3 @@ constructor(e, t, n = "Exception") {

}
class g extends h {
class _ extends f {
constructor(e, t, n = "FatalErrorException") {

@@ -39,3 +39,3 @@ e === void 0 && (e = "The program has encountered an unrecoverable error and cannot continue as expected. Please, try again later. If the problem persists, contact the support team."), super(e, t, n);

}
class Y extends g {
class J extends _ {
constructor(e, t, n = "NotImplementedException") {

@@ -48,3 +48,3 @@ e === void 0 && (e = "This feature is not implemented yet. Please, try again later."), super(e, t, n);

}
class T extends h {
class E extends f {
constructor(e, t, n = "FileException") {

@@ -57,3 +57,3 @@ super(e, t, n);

}
class z extends T {
class V extends E {
constructor(e, t, n = "FileExistsException") {

@@ -66,3 +66,3 @@ super(e, t, n);

}
class V extends T {
class W extends E {
constructor(e, t, n = "FileNotFoundException") {

@@ -75,3 +75,3 @@ super(e, t, n);

}
class B extends h {
class Y extends f {
constructor(e, t, n = "KeyException") {

@@ -84,3 +84,3 @@ super(e, t, n);

}
class K extends h {
class z extends f {
constructor(e, t, n = "NetworkException") {

@@ -93,3 +93,3 @@ super(e, t, n);

}
class G extends h {
class B extends f {
constructor(e, t, n = "PermissionException") {

@@ -102,3 +102,3 @@ super(e, t, n);

}
class q extends h {
class D extends f {
constructor(e, t, n = "ReferenceException") {

@@ -111,3 +111,3 @@ super(e, t, n);

}
class m extends h {
class y extends f {
constructor(e, t, n = "RuntimeException") {

@@ -120,3 +120,3 @@ super(e, t, n);

}
class A extends m {
class C extends y {
constructor(e, t, n = "EnvironmentException") {

@@ -129,3 +129,3 @@ super(e, t, n);

}
class C extends h {
class O extends f {
constructor(e, t, n = "TimeoutException") {

@@ -138,3 +138,3 @@ super(e, t, n);

}
class H extends h {
class K extends f {
constructor(e, t, n = "TypeException") {

@@ -147,3 +147,3 @@ super(e, t, n);

}
class f extends h {
class m extends f {
constructor(e, t, n = "ValueException") {

@@ -156,3 +156,3 @@ super(e, t, n);

}
class M extends f {
class T extends m {
constructor(e, t, n = "RangeException") {

@@ -165,3 +165,3 @@ super(e, t, n);

}
class a {
class l {
constructor(e) {

@@ -197,3 +197,3 @@ u(this, "_iterator");

const t = this._iterator;
return new a(function* () {
return new l(function* () {
let n = 0;

@@ -210,3 +210,3 @@ for (; ; ) {

const t = this._iterator;
return new a(function* () {
return new l(function* () {
let n = 0;

@@ -224,14 +224,69 @@ for (; ; ) {

if (r === void 0) {
const i = this._iterator.next();
if (i.done)
throw new f("Cannot reduce an empty iterator without an initial value.");
r = i.value, n += 1;
const s = this._iterator.next();
if (s.done)
throw new m("Cannot reduce an empty iterator without an initial value.");
r = s.value, n += 1;
}
for (; ; ) {
const i = this._iterator.next();
if (i.done)
const s = this._iterator.next();
if (s.done)
return r;
r = e(r, i.value, n), n += 1;
r = e(r, s.value, n), n += 1;
}
}
flatMap(e) {
const t = this._iterator;
return new l(function* () {
let n = 0;
for (; ; ) {
const r = t.next();
if (r.done)
return r.value;
const s = e(r.value, n);
for (const o of s)
yield o;
n += 1;
}
});
}
drop(e) {
const t = this._iterator;
return new l(function* () {
let n = 0;
for (; n < e; ) {
if (t.next().done)
return;
n += 1;
}
for (; ; ) {
const r = t.next();
if (r.done)
return r.value;
yield r.value;
}
});
}
take(e) {
const t = this._iterator;
return new l(function* () {
let n = 0;
for (; n < e; ) {
const r = t.next();
if (r.done)
return r.value;
yield r.value, n += 1;
}
});
}
find(e) {
let t = 0;
for (; ; ) {
const n = this._iterator.next();
if (n.done)
return;
if (e(n.value, t))
return n.value;
t += 1;
}
}
enumerate() {

@@ -242,3 +297,3 @@ return this.map((e, t) => [t, e]);

const e = this._iterator;
return new a(function* () {
return new l(function* () {
const t = /* @__PURE__ */ new Set();

@@ -273,2 +328,5 @@ for (; ; ) {

}
groupBy(e) {
return new d(this.map((t, n) => [e(t, n), t]));
}
toArray() {

@@ -284,4 +342,311 @@ return Array.from(this);

}
class d {
class c {
constructor(e) {
u(this, "_elements");
this._elements = new l(e);
}
every(e) {
for (const [t, [n, r]] of this._elements.enumerate())
if (!e(n, r, t))
return !1;
return !0;
}
some(e) {
for (const [t, [n, r]] of this._elements.enumerate())
if (e(n, r, t))
return !0;
return !1;
}
filter(e) {
const t = this._elements.enumerate();
return new c(function* () {
for (const [n, [r, s]] of t)
e(r, s, n) && (yield [r, s]);
});
}
map(e) {
const t = this._elements.enumerate();
return new c(function* () {
for (const [n, [r, s]] of t)
yield [r, e(r, s, n)];
});
}
reduce(e, t) {
let n = 0, r = t;
if (r === void 0) {
const s = this._elements.next();
if (s.done)
throw new m("Cannot reduce an empty iterator without an initial value.");
r = s.value[1], n += 1;
}
for (const [s, o] of this._elements)
r = e(s, r, o, n), n += 1;
return r;
}
flatMap(e) {
const t = this._elements.enumerate();
return new d(function* () {
for (const [n, [r, s]] of t)
for (const o of e(r, s, n))
yield [r, o];
});
}
drop(e) {
const t = this._elements.enumerate();
return new c(function* () {
for (const [n, [r, s]] of t)
n >= e && (yield [r, s]);
});
}
take(e) {
const t = this._elements.enumerate();
return new c(function* () {
for (const [n, [r, s]] of t) {
if (n >= e)
break;
yield [r, s];
}
});
}
enumerate() {
return this.map((e, t, n) => [n, t]);
}
unique() {
const e = this._elements;
return new c(function* () {
const t = /* @__PURE__ */ new Set();
for (const [n, r] of e)
t.has(r) || (t.add(r), yield [n, r]);
});
}
count() {
let e = 0;
for (const t of this._elements)
e += 1;
return e;
}
forEach(e) {
for (const [t, [n, r]] of this._elements.enumerate())
e(n, r, t);
}
keys() {
const e = this._elements;
return new l(function* () {
for (const [t] of e)
yield t;
});
}
items() {
return this._elements;
}
values() {
const e = this._elements;
return new l(function* () {
for (const [t, n] of e)
yield n;
});
}
toArray() {
return Array.from(this.values());
}
toMap() {
return new Map(this.items());
}
toObject() {
return Object.fromEntries(this.items());
}
get [Symbol.toStringTag]() {
return "ReducedIterator";
}
}
class w {
constructor(e) {
u(this, "_elements");
this._elements = new h(e);
}
async every(e) {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of this._elements) {
const [s, o] = t.get(n) ?? [0, !0];
o && t.set(n, [s + 1, await e(n, r, s)]);
}
return new c(function* () {
for (const [n, [r, s]] of t)
yield [n, s];
});
}
async some(e) {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of this._elements) {
const [s, o] = t.get(n) ?? [0, !1];
o || t.set(n, [s + 1, await e(n, r, s)]);
}
return new c(function* () {
for (const [n, [r, s]] of t)
yield [n, s];
});
}
filter(e) {
const t = this._elements;
return new w(async function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, s] of t) {
const o = n.get(r) ?? 0;
await e(r, s, o) && (yield [r, s]), n.set(r, o + 1);
}
});
}
map(e) {
const t = this._elements;
return new w(async function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, s] of t) {
const o = n.get(r) ?? 0;
yield [r, await e(r, s, o)], n.set(r, o + 1);
}
});
}
async reduce(e, t) {
const n = /* @__PURE__ */ new Map();
for await (const [r, s] of this._elements) {
let o, a;
if (n.has(r))
[o, a] = n.get(r);
else if (t !== void 0)
o = 0, a = await t(r);
else {
n.set(r, [0, s]);
continue;
}
n.set(r, [o + 1, await e(r, a, s, o)]);
}
return new c(function* () {
for (const [r, [s, o]] of n)
yield [r, o];
});
}
flatMap(e) {
const t = this._elements;
return new w(async function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, s] of t) {
const o = n.get(r) ?? 0, a = await e(r, s, o);
for await (const p of a)
yield [r, p];
n.set(r, o + 1);
}
});
}
drop(e) {
const t = this._elements;
return new w(async function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, s] of t) {
const o = n.get(r) ?? 0;
if (o < e) {
n.set(r, o + 1);
continue;
}
yield [r, s];
}
});
}
take(e) {
const t = this._elements;
return new w(async function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, s] of t) {
const o = n.get(r) ?? 0;
if (o >= e) {
if (n.values().every((a) => a >= e))
break;
continue;
}
yield [r, s], n.set(r, o + 1);
}
});
}
async find(e) {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of this._elements) {
let [s, o] = t.get(n) ?? [0, void 0];
o === void 0 && (await e(n, r, s) && (o = r), t.set(n, [s + 1, o]));
}
return new c(function* () {
for (const [n, [r, s]] of t)
yield [n, s];
});
}
unique() {
const e = this._elements;
return new w(async function* () {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of e) {
const s = t.get(n) ?? /* @__PURE__ */ new Set();
s.has(r) || (s.add(r), t.set(n, s), yield [n, r]);
}
});
}
async count() {
const e = /* @__PURE__ */ new Map();
for await (const [t] of this._elements) {
const n = e.get(t) ?? 0;
e.set(t, n + 1);
}
return new c(function* () {
for (const [t, n] of e)
yield [t, n];
});
}
async forEach(e) {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of this._elements) {
const s = t.get(n) ?? 0;
e(n, r, s), t.set(n, s + 1);
}
}
keys() {
const e = this._elements;
return new h(async function* () {
const t = /* @__PURE__ */ new Set();
for await (const [n] of e)
t.has(n) || (t.add(n), yield n);
});
}
items() {
return this._elements;
}
values() {
const e = this._elements;
return new h(async function* () {
for await (const [t, n] of e)
yield n;
});
}
async toArray() {
const e = await this.toMap();
return Array.from(e.values());
}
async toMap() {
const e = /* @__PURE__ */ new Map();
for await (const [t, n] of this._elements) {
const r = e.get(t) ?? [];
r.push(n), e.set(t, r);
}
return e;
}
async toObject() {
const e = {};
for await (const [t, n] of this._elements) {
const r = e[t] ?? [];
r.push(n), e[t] = r;
}
return e;
}
get [Symbol.toStringTag]() {
return "AggregatedAsyncIterator";
}
}
class h {
constructor(e) {
u(this, "_iterator");

@@ -331,3 +696,3 @@ u(this, "return");

return !0;
if (!e(n.value, t))
if (!await e(n.value, t))
return !1;

@@ -343,3 +708,3 @@ t += 1;

return !1;
if (e(n.value, t))
if (await e(n.value, t))
return !0;

@@ -351,3 +716,3 @@ t += 1;

const t = this._iterator;
return new d(async function* () {
return new h(async function* () {
let n = 0;

@@ -358,3 +723,3 @@ for (; ; ) {

return r.value;
e(r.value, n) && (yield r.value), n += 1;
await e(r.value, n) && (yield r.value), n += 1;
}

@@ -365,3 +730,3 @@ });

const t = this._iterator;
return new d(async function* () {
return new h(async function* () {
let n = 0;

@@ -372,3 +737,3 @@ for (; ; ) {

return r.value;
yield e(r.value, n), n += 1;
yield await e(r.value, n), n += 1;
}

@@ -380,14 +745,69 @@ });

if (r === void 0) {
const i = await this._iterator.next();
if (i.done)
throw new f("Cannot reduce an empty iterator without an initial value.");
r = i.value, n += 1;
const s = await this._iterator.next();
if (s.done)
throw new m("Cannot reduce an empty iterator without an initial value.");
r = s.value, n += 1;
}
for (; ; ) {
const i = await this._iterator.next();
if (i.done)
const s = await this._iterator.next();
if (s.done)
return r;
r = await e(r, i.value, n), n += 1;
r = await e(r, s.value, n), n += 1;
}
}
flatMap(e) {
const t = this._iterator;
return new h(async function* () {
let n = 0;
for (; ; ) {
const r = await t.next();
if (r.done)
return r.value;
const s = await e(r.value, n);
for await (const o of s)
yield o;
n += 1;
}
});
}
drop(e) {
const t = this._iterator;
return new h(async function* () {
let n = 0;
for (; n < e; ) {
if ((await t.next()).done)
return;
n += 1;
}
for (; ; ) {
const r = await t.next();
if (r.done)
return r.value;
yield r.value;
}
});
}
take(e) {
const t = this._iterator;
return new h(async function* () {
let n = 0;
for (; n < e; ) {
const r = await t.next();
if (r.done)
return r.value;
yield r.value, n += 1;
}
});
}
async find(e) {
let t = 0;
for (; ; ) {
const n = await this._iterator.next();
if (n.done)
return;
if (await e(n.value, t))
return n.value;
t += 1;
}
}
enumerate() {

@@ -398,3 +818,3 @@ return this.map((e, t) => [t, e]);

const e = this._iterator;
return new d(async function* () {
return new h(async function* () {
const t = /* @__PURE__ */ new Set();

@@ -429,11 +849,8 @@ for (; ; ) {

}
async toArray() {
const e = [];
for (; ; ) {
const t = await this._iterator.next();
if (t.done)
return e;
e.push(t.value);
}
groupBy(e) {
return new w(this.map(async (t, n) => [await e(t, n), t]));
}
toArray() {
return Array.fromAsync(this);
}
get [Symbol.toStringTag]() {

@@ -446,79 +863,16 @@ return "SmartAsyncIterator";

}
class l {
class d {
constructor(e) {
u(this, "_elements");
this._elements = new a(e);
this._elements = new l(e);
}
filter(e) {
const t = this._elements;
return new l(function* () {
for (const [n, [r, i]] of t.enumerate())
e(r, i, n) && (yield [r, i]);
});
}
map(e) {
const t = this._elements;
return new l(function* () {
for (const [n, [r, i]] of t.enumerate())
yield [r, e(r, i, n)];
});
}
reduce(e, t) {
let n = 0, r;
if (t !== void 0)
r = t;
else {
const i = this._elements.next();
if (i.done)
throw new f("Cannot reduce an empty iterator without an initial value.");
n += 1, r = i.value[1];
}
for (const [i, o] of this._elements)
r = e(i, r, o, n), n += 1;
return r;
}
keys() {
const e = this._elements;
return new a(function* () {
for (const [t] of e)
yield t;
});
}
items() {
return this._elements;
}
values() {
const e = this._elements;
return new a(function* () {
for (const [t, n] of e)
yield n;
});
}
toArray() {
return Array.from(this.values());
}
toMap() {
return new Map(this.items());
}
toObject() {
return Object.fromEntries(this.items());
}
get [Symbol.toStringTag]() {
return "ReducedIterator";
}
}
class w {
constructor(e) {
u(this, "_elements");
this._elements = new a(e);
}
every(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of this._elements) {
const [i, o] = t.get(n) ?? [0, !0];
o && t.set(n, [i + 1, e(n, r, i)]);
const [s, o] = t.get(n) ?? [0, !0];
o && t.set(n, [s + 1, e(n, r, s)]);
}
return new l(function* () {
for (const [n, [r, i]] of t)
yield [n, i];
return new c(function* () {
for (const [n, [r, s]] of t)
yield [n, s];
});

@@ -529,8 +883,8 @@ }

for (const [n, r] of this._elements) {
const [i, o] = t.get(n) ?? [0, !1];
o || t.set(n, [i + 1, e(n, r, i)]);
const [s, o] = t.get(n) ?? [0, !1];
o || t.set(n, [s + 1, e(n, r, s)]);
}
return new l(function* () {
for (const [n, [r, i]] of t)
yield [n, i];
return new c(function* () {
for (const [n, [r, s]] of t)
yield [n, s];
});

@@ -540,7 +894,7 @@ }

const t = this._elements;
return new w(function* () {
return new d(function* () {
const n = /* @__PURE__ */ new Map();
for (const [r, i] of t) {
for (const [r, s] of t) {
const o = n.get(r) ?? 0;
n.set(r, o + 1), e(r, i, o) && (yield [r, i]);
e(r, s, o) && (yield [r, s]), n.set(r, o + 1);
}

@@ -551,7 +905,7 @@ });

const t = this._elements;
return new w(function* () {
return new d(function* () {
const n = /* @__PURE__ */ new Map();
for (const [r, i] of t) {
for (const [r, s] of t) {
const o = n.get(r) ?? 0;
n.set(r, o + 1), yield [r, e(r, i, o)];
yield [r, e(r, s, o)], n.set(r, o + 1);
}

@@ -562,203 +916,91 @@ });

const n = /* @__PURE__ */ new Map();
for (const [r, i] of this._elements) {
let o, c;
for (const [r, s] of this._elements) {
let o, a;
if (n.has(r))
[o, c] = n.get(r), o += 1;
[o, a] = n.get(r);
else if (t !== void 0)
o = 0, c = t(r);
o = 0, a = t(r);
else {
n.set(r, [0, i]);
n.set(r, [0, s]);
continue;
}
c = e(r, c, i, o), n.set(r, [o, c]);
n.set(r, [o + 1, e(r, a, s, o)]);
}
return new l(function* () {
for (const [r, [i, o]] of n)
return new c(function* () {
for (const [r, [s, o]] of n)
yield [r, o];
});
}
unique() {
const e = this._elements;
return new w(function* () {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of e) {
const i = t.get(n) ?? /* @__PURE__ */ new Set();
i.has(r) || (i.add(r), t.set(n, i), yield [n, r]);
flatMap(e) {
const t = this._elements;
return new d(function* () {
const n = /* @__PURE__ */ new Map();
for (const [r, s] of t) {
const o = n.get(r) ?? 0, a = e(r, s, o);
for (const p of a)
yield [r, p];
n.set(r, o + 1);
}
});
}
count() {
const e = /* @__PURE__ */ new Map();
for (const [t] of this._elements) {
const n = e.get(t) ?? 0;
e.set(t, n + 1);
}
return new l(function* () {
for (const [t, n] of e)
yield [t, n];
});
}
first() {
const e = /* @__PURE__ */ new Map();
for (const [t, n] of this._elements)
e.has(t) || e.set(t, n);
return new l(function* () {
for (const [t, n] of e)
yield [t, n];
});
}
last() {
const e = /* @__PURE__ */ new Map();
for (const [t, n] of this._elements)
e.set(t, n);
return new l(function* () {
for (const [t, n] of e)
yield [t, n];
});
}
keys() {
const e = this._elements;
return new a(function* () {
const t = /* @__PURE__ */ new Set();
for (const [n] of e)
t.has(n) || (t.add(n), yield n);
});
}
items() {
return this._elements;
}
values() {
const e = this._elements;
return new a(function* () {
for (const [t, n] of e)
yield n;
});
}
toArray() {
return Array.from(this.toMap().values());
}
toMap() {
const e = /* @__PURE__ */ new Map();
for (const [t, n] of this._elements) {
const r = e.get(t) ?? [];
r.push(n), e.set(t, r);
}
return e;
}
toObject() {
const e = {};
for (const [t, n] of this._elements) {
const r = e[t] ?? [];
r.push(n), e[t] = r;
}
return e;
}
get [Symbol.toStringTag]() {
return "AggregatedIterator";
}
}
class _ {
constructor(e) {
u(this, "_elements");
this._elements = new a(e);
}
filter(e) {
return new _(this._elements.filter(e));
}
map(e) {
return new _(this._elements.map(e));
}
unique() {
return new _(this._elements.unique());
}
groupBy(e) {
return new w(this._elements.map((t, n) => [e(t, n), t]));
}
get [Symbol.toStringTag]() {
return "Aggregator";
}
}
class y {
constructor(e) {
u(this, "_elements");
this._elements = new d(e);
}
async every(e) {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of this._elements) {
const [i, o] = t.get(n) ?? [0, !0];
o && t.set(n, [i + 1, await e(n, r, i)]);
}
return new l(function* () {
for (const [n, [r, i]] of t)
yield [n, i];
});
}
async some(e) {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of this._elements) {
const [i, o] = t.get(n) ?? [0, !1];
o || t.set(n, [i + 1, await e(n, r, i)]);
}
return new l(function* () {
for (const [n, [r, i]] of t)
yield [n, i];
});
}
filter(e) {
drop(e) {
const t = this._elements;
return new y(async function* () {
return new d(function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, i] of t) {
for (const [r, s] of t) {
const o = n.get(r) ?? 0;
n.set(r, o + 1), await e(r, i, o) && (yield [r, i]);
if (o < e) {
n.set(r, o + 1);
continue;
}
yield [r, s];
}
});
}
map(e) {
take(e) {
const t = this._elements;
return new y(async function* () {
return new d(function* () {
const n = /* @__PURE__ */ new Map();
for await (const [r, i] of t) {
for (const [r, s] of t) {
const o = n.get(r) ?? 0;
n.set(r, o + 1), yield [r, await e(r, i, o)];
if (o >= e) {
if (n.values().every((a) => a >= e))
break;
continue;
}
yield [r, s], n.set(r, o + 1);
}
});
}
async reduce(e, t) {
const n = /* @__PURE__ */ new Map();
for await (const [r, i] of this._elements) {
let o, c;
if (n.has(r))
[o, c] = n.get(r), o += 1;
else if (t !== void 0)
o = 0, c = await t(r);
else {
n.set(r, [0, i]);
continue;
}
c = await e(r, c, i, o), n.set(r, [o, c]);
find(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of this._elements) {
let [s, o] = t.get(n) ?? [0, void 0];
o === void 0 && (e(n, r, s) && (o = r), t.set(n, [s + 1, o]));
}
return new l(function* () {
for (const [r, [i, o]] of n)
yield [r, o];
return new c(function* () {
for (const [n, [r, s]] of t)
yield [n, s];
});
}
enumerate() {
return this.map((e, t, n) => [n, t]);
}
unique() {
const e = this._elements;
return new y(async function* () {
return new d(function* () {
const t = /* @__PURE__ */ new Map();
for await (const [n, r] of e) {
const i = t.get(n) ?? /* @__PURE__ */ new Set();
i.has(r) || (i.add(r), t.set(n, i), yield [n, r]);
for (const [n, r] of e) {
const s = t.get(n) ?? /* @__PURE__ */ new Set();
s.has(r) || (s.add(r), t.set(n, s), yield [n, r]);
}
});
}
async count() {
count() {
const e = /* @__PURE__ */ new Map();
for await (const [t] of this._elements) {
for (const [t] of this._elements) {
const n = e.get(t) ?? 0;
e.set(t, n + 1);
}
return new l(function* () {
return new c(function* () {
for (const [t, n] of e)

@@ -768,25 +1010,14 @@ yield [t, n];

}
async first() {
const e = /* @__PURE__ */ new Map();
for await (const [t, n] of this._elements)
e.has(t) || e.set(t, n);
return new l(function* () {
for (const [t, n] of e)
yield [t, n];
});
forEach(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of this._elements) {
const s = t.get(n) ?? 0;
e(n, r, s), t.set(n, s + 1);
}
}
async last() {
const e = /* @__PURE__ */ new Map();
for await (const [t, n] of this._elements)
e.set(t, n);
return new l(function* () {
for (const [t, n] of e)
yield [t, n];
});
}
keys() {
const e = this._elements;
return new d(async function* () {
return new l(function* () {
const t = /* @__PURE__ */ new Set();
for await (const [n] of e)
for (const [n] of e)
t.has(n) || (t.add(n), yield n);

@@ -800,14 +1031,14 @@ });

const e = this._elements;
return new d(async function* () {
for await (const [t, n] of e)
return new l(function* () {
for (const [t, n] of e)
yield n;
});
}
async toArray() {
const e = await this.toMap();
toArray() {
const e = this.toMap();
return Array.from(e.values());
}
async toMap() {
toMap() {
const e = /* @__PURE__ */ new Map();
for await (const [t, n] of this._elements) {
for (const [t, n] of this._elements) {
const r = e.get(t) ?? [];

@@ -818,5 +1049,5 @@ r.push(n), e.set(t, r);

}
async toObject() {
toObject() {
const e = {};
for await (const [t, n] of this._elements) {
for (const [t, n] of this._elements) {
const r = e[t] ?? [];

@@ -828,34 +1059,13 @@ r.push(n), e[t] = r;

get [Symbol.toStringTag]() {
return "AggregatedAsyncIterator";
return "AggregatedIterator";
}
}
class p {
constructor(e) {
u(this, "_elements");
this._elements = new d(e);
}
filter(e) {
return new p(this._elements.filter(e));
}
map(e) {
return new p(this._elements.map(e));
}
unique() {
return new p(this._elements.unique());
}
groupBy(e) {
return new y(this._elements.map(async (t, n) => [await e(t, n), t]));
}
get [Symbol.toStringTag]() {
return "AsyncAggregator";
}
var x = /* @__PURE__ */ ((i) => (i[i.Millisecond = 1] = "Millisecond", i[i.Second = 1e3] = "Second", i[i.Minute = 6e4] = "Minute", i[i.Hour = 36e5] = "Hour", i[i.Day = 864e5] = "Day", i[i.Week = 6048e5] = "Week", i[i.Month = 2592e6] = "Month", i[i.Year = 31536e6] = "Year", i))(x || {});
function G(i, e, t = 864e5) {
return i = new Date(i), e = new Date(e), Math.floor((e.getTime() - i.getTime()) / t);
}
var x = /* @__PURE__ */ ((s) => (s[s.Millisecond = 1] = "Millisecond", s[s.Second = 1e3] = "Second", s[s.Minute = 6e4] = "Minute", s[s.Hour = 36e5] = "Hour", s[s.Day = 864e5] = "Day", s[s.Week = 6048e5] = "Week", s[s.Month = 2592e6] = "Month", s[s.Year = 31536e6] = "Year", s))(x || {});
function L(s, e, t = 864e5) {
return s = new Date(s), e = new Date(e), Math.floor((e.getTime() - s.getTime()) / t);
}
function Q(s, e, t = 864e5) {
return s = new Date(s), e = new Date(e), new a(function* () {
function H(i, e, t = 864e5) {
return i = new Date(i), e = new Date(e), new l(function* () {
const n = e.getTime();
let r = s.getTime();
let r = i.getTime();
for (; r < n; )

@@ -865,6 +1075,6 @@ yield new Date(r), r += t;

}
function X(s, e = 864e5) {
return s = new Date(s), new Date(Math.floor(s.getTime() / e) * e);
function L(i, e = 864e5) {
return i = new Date(i), new Date(Math.floor(i.getTime() / e) * e);
}
class R {
class M {
constructor(e, t = 30) {

@@ -876,3 +1086,3 @@ u(this, "_handle");

u(this, "_stop");
this._startTime = 0, this._isRunning = !1, E ? (this._start = () => {
this._startTime = 0, this._isRunning = !1, S ? (this._start = () => {
e(this.elapsedTime), this._handle = window.requestAnimationFrame(this._start);

@@ -897,3 +1107,3 @@ }, this._stop = () => window.cancelAnimationFrame(this._handle)) : (console.warn(

if (this._isRunning)
throw new m("The game loop has already been started.");
throw new y("The game loop has already been started.");
this._startTime = performance.now() - e, this._start(), this._isRunning = !0;

@@ -903,9 +1113,9 @@ }

if (!this._isRunning)
throw new m("The game loop hadn't yet started.");
throw new y("The game loop hadn't yet started.");
if (!this._handle)
throw new g();
throw new _();
this._stop(), this._handle = void 0, this._isRunning = !1;
}
}
class Z {
class Q {
constructor(e = !0) {

@@ -915,4 +1125,4 @@ u(this, "_preferPersistence");

u(this, "_persistent");
if (this._preferPersistence = e, !E)
throw new A(
if (this._preferPersistence = e, !S)
throw new C(
"The `JSONStorage` class can only be instantiated within a browser environment."

@@ -1053,3 +1263,3 @@ );

}
class F {
class v {
constructor(e) {

@@ -1066,2 +1276,5 @@ u(this, "_isPending");

}
static FromPromise(e) {
return new v((t, n) => e.then(t, n));
}
get isPending() {

@@ -1089,11 +1302,11 @@ return this._isPending;

}
class O extends F {
class q extends v {
constructor(t, n) {
let r, i;
super((o, c) => {
r = o, i = c;
let r, s;
super((o, a) => {
r = o, s = a;
});
u(this, "_resolve");
u(this, "_reject");
this._promise.then(t, n), this._resolve = r, this._reject = i;
this._promise.then(t, n), this._resolve = r, this._reject = s;
}

@@ -1113,11 +1326,11 @@ get resolve() {

}
class U extends F {
class X extends v {
constructor(e, t) {
super((n, r) => {
const i = (v) => {
clearTimeout(b), n(v);
}, o = (v) => {
clearTimeout(b), r(v);
}, b = setTimeout(() => o(new C("The operation has timed out.")), t);
e(i, o);
const s = (g) => {
clearTimeout(p), n(g);
}, o = (g) => {
clearTimeout(p), r(g);
}, p = setTimeout(() => o(new O("The operation has timed out.")), t);
e(s, o);
});

@@ -1129,3 +1342,3 @@ }

}
class P {
class F {
constructor() {

@@ -1139,3 +1352,3 @@ u(this, "_subscribers");

if (t < 0)
throw new q("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed.");
throw new D("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed.");
this._subscribers.splice(t, 1);

@@ -1151,11 +1364,11 @@ };

}
class ee extends R {
class Z extends M {
constructor(t = x.Second) {
super((n) => this._publisher.publish(n), t);
u(this, "_publisher");
this._publisher = new P();
this._publisher = new F();
}
start(t = 0) {
if (this._isRunning)
throw new m("The clock has already been started.");
throw new y("The clock has already been started.");
super.start(t);

@@ -1165,3 +1378,3 @@ }

if (!this._isRunning)
throw new m("The clock hadn't yet started.");
throw new y("The clock hadn't yet started.");
super.stop();

@@ -1171,16 +1384,16 @@ }

if (n < 0)
throw new M("The tick step must be a non-negative number.");
throw new T("The tick step must be a non-negative number.");
if (n === 0)
return this._publisher.subscribe(t);
let r = 0;
return this._publisher.subscribe((i) => {
i - r < n || (t(i), r = i);
return this._publisher.subscribe((s) => {
s - r < n || (t(s), r = s);
});
}
}
class te extends R {
class U extends M {
constructor(t, n = x.Second) {
super(() => {
const i = this.remainingTime;
this._publisher.publish(i), i <= 0 && this.stop();
const s = this.remainingTime;
this._publisher.publish(s), s <= 0 && this.stop();
}, n);

@@ -1190,3 +1403,3 @@ u(this, "_deferrer");

u(this, "_duration");
this._publisher = new P(), this._duration = t;
this._publisher = new F(), this._duration = t;
}

@@ -1201,12 +1414,12 @@ get duration() {

if (this._isRunning)
throw new m("The countdown has already been started.");
throw new y("The countdown has already been started.");
if (this._deferrer)
throw new g();
return this._deferrer = new O(), super.start(this.duration - t), this._deferrer;
throw new _();
return this._deferrer = new q(), super.start(this.duration - t), this._deferrer;
}
stop(t) {
if (!this._isRunning)
throw new m("The countdown hadn't yet started.");
throw new y("The countdown hadn't yet started.");
if (!this._deferrer)
throw new g();
throw new _();
super.stop(), t !== void 0 ? this._deferrer.reject(t) : this._deferrer.resolve(), this._deferrer = void 0;

@@ -1216,12 +1429,12 @@ }

if (n < 0)
throw new M("The tick step must be a non-negative number.");
throw new T("The tick step must be a non-negative number.");
if (n === 0)
return this._publisher.subscribe(t);
let r = 0;
return this._publisher.subscribe((i) => {
r - i < n || (t(i), r = i);
return this._publisher.subscribe((s) => {
r - s < n || (t(s), r = s);
});
}
}
class I {
class R {
static Boolean(e = 0.5) {

@@ -1238,7 +1451,7 @@ return Math.random() < e;

if (e.length === 0)
throw new f("You must provide at least one element.");
throw new m("You must provide at least one element.");
return this.Integer(e.length);
}
static Choice(e) {
return e[I.Index(e)];
return e[R.Index(e)];
}

@@ -1249,17 +1462,17 @@ // eslint-disable-next-line no-useless-constructor

}
async function ne(s) {
return new Promise((e, t) => setTimeout(e, s));
function ee(i) {
return new Promise((e) => setTimeout(e, i));
}
async function re() {
return new Promise((s, e) => requestAnimationFrame(() => s()));
function te() {
return new Promise((i) => requestAnimationFrame(() => i()));
}
function se(s, e = "text/javascript") {
function ne(i, e = "text/javascript") {
return new Promise((t, n) => {
const r = document.createElement("script");
r.async = !0, r.defer = !0, r.src = s, r.type = e, r.onload = () => t(), r.onerror = () => n(), document.body.appendChild(r);
r.async = !0, r.defer = !0, r.src = i, r.type = e, r.onload = () => t(), r.onerror = () => n(), document.body.appendChild(r);
});
}
function ie(...s) {
return new a(function* () {
for (const e of s)
function re(...i) {
return new l(function* () {
for (const e of i)
for (const t of e)

@@ -1269,26 +1482,26 @@ yield t;

}
function oe(s) {
if (Array.isArray(s))
return s.length;
function se(i) {
if (Array.isArray(i))
return i.length;
let e = 0;
for (const t of s)
for (const t of i)
e += 1;
return e;
}
function ue(s) {
return new a(function* () {
function ie(i) {
return new l(function* () {
let e = 0;
for (const t of s)
for (const t of i)
yield [e, t], e += 1;
});
}
function ae(s, e, t = 1) {
return new a(function* () {
e === void 0 && (e = s, s = 0), s > e && (t = t ?? -1);
for (let n = s; n < e; n += t)
function oe(i, e, t = 1) {
return new l(function* () {
e === void 0 && (e = i, i = 0), i > e && (t = t ?? -1);
for (let n = i; n < e; n += t)
yield n;
});
}
function ce(s) {
const e = Array.from(s);
function ue(i) {
const e = Array.from(i);
for (let t = e.length - 1; t > 0; t -= 1) {

@@ -1300,45 +1513,45 @@ const n = Math.floor(Math.random() * (t + 1));

}
function le(s) {
return new a(function* () {
function ae(i) {
return new l(function* () {
const e = /* @__PURE__ */ new Set();
for (const t of s)
for (const t of i)
e.has(t) || (e.add(t), yield t);
});
}
function N(s, e) {
return new a(function* () {
const t = s[Symbol.iterator](), n = e[Symbol.iterator]();
function I(i, e) {
return new l(function* () {
const t = i[Symbol.iterator](), n = e[Symbol.iterator]();
for (; ; ) {
const r = t.next(), i = n.next();
if (r.done || i.done)
const r = t.next(), s = n.next();
if (r.done || s.done)
break;
yield [r.value, i.value];
yield [r.value, s.value];
}
});
}
function he(s, e) {
function le(i, e) {
if (e === void 0) {
let i = 0, o = 0;
for (const c of s)
i += c, o += 1;
let s = 0, o = 0;
for (const a of i)
s += a, o += 1;
if (o === 0)
throw new f("You must provide at least one value.");
return i / o;
throw new m("You must provide at least one value.");
return s / o;
}
let t = 0, n = 0, r = 0;
for (const [i, o] of N(s, e)) {
for (const [s, o] of I(i, e)) {
if (o <= 0)
throw new f(`The weight for the value #${r} must be greater than zero.`);
t += i * o, n += o, r += 1;
throw new m(`The weight for the value #${r} must be greater than zero.`);
t += s * o, n += o, r += 1;
}
if (r === 0)
throw new f("You must provide at least one value and weight.");
throw new m("You must provide at least one value and weight.");
if (n > 0)
throw new f("The sum of weights must be greater than zero.");
throw new m("The sum of weights must be greater than zero.");
return t / n;
}
function fe(s) {
function ce(i) {
let e = 0;
for (let t = 0; t < s.length; t += 1) {
const n = s.charCodeAt(t);
for (let t = 0; t < i.length; t += 1) {
const n = i.charCodeAt(t);
e = (e << 5) - e + n, e |= 0;

@@ -1348,67 +1561,65 @@ }

}
function de(s) {
function fe(i) {
let e = 0;
for (const t of s)
for (const t of i)
e += t;
return e;
}
function me(s) {
return `${s.charAt(0).toUpperCase()}${s.slice(1)}`;
function he(i) {
return `${i.charAt(0).toUpperCase()}${i.slice(1)}`;
}
const we = "2.0.0-rc.2";
const de = "2.0.0-rc.3";
export {
y as AggregatedAsyncIterator,
w as AggregatedIterator,
_ as Aggregator,
p as AsyncAggregator,
ee as Clock,
te as Countdown,
O as DeferredPromise,
h as Exception,
g as FatalErrorException,
T as FileException,
z as FileExistsException,
V as FileNotFoundException,
R as GameLoop,
Z as JSONStorage,
B as KeyException,
K as NetworkException,
Y as NotImplementedException,
G as PermissionException,
P as Publisher,
I as Random,
M as RangeException,
l as ReducedIterator,
q as ReferenceException,
m as RuntimeException,
d as SmartAsyncIterator,
a as SmartIterator,
F as SmartPromise,
w as AggregatedAsyncIterator,
d as AggregatedIterator,
Z as Clock,
U as Countdown,
q as DeferredPromise,
f as Exception,
_ as FatalErrorException,
E as FileException,
V as FileExistsException,
W as FileNotFoundException,
M as GameLoop,
Q as JSONStorage,
Y as KeyException,
z as NetworkException,
J as NotImplementedException,
B as PermissionException,
F as Publisher,
R as Random,
T as RangeException,
c as ReducedIterator,
D as ReferenceException,
y as RuntimeException,
h as SmartAsyncIterator,
l as SmartIterator,
v as SmartPromise,
x as TimeUnit,
U as TimedPromise,
C as TimeoutException,
H as TypeException,
we as VERSION,
f as ValueException,
he as average,
me as capitalize,
ie as chain,
oe as count,
L as dateDifference,
Q as dateRange,
X as dateRound,
ne as delay,
ue as enumerate,
fe as hash,
E as isBrowser,
J as isNode,
W as isWebWorker,
se as loadScript,
re as nextAnimationFrame,
ae as range,
ce as shuffle,
de as sum,
le as unique,
N as zip
X as TimedPromise,
O as TimeoutException,
K as TypeException,
de as VERSION,
m as ValueException,
le as average,
he as capitalize,
re as chain,
se as count,
G as dateDifference,
H as dateRange,
L as dateRound,
ee as delay,
ie as enumerate,
ce as hash,
S as isBrowser,
$ as isNode,
A as isWebWorker,
ne as loadScript,
te as nextAnimationFrame,
oe as range,
ue as shuffle,
fe as sum,
ae as unique,
I as zip
};
//# sourceMappingURL=core.js.map
{
"name": "@byloth/core",
"version": "2.0.0-rc.2",
"version": "2.0.0-rc.3",
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",

@@ -50,11 +50,10 @@ "keywords": [

"devDependencies": {
"@byloth/eslint-config-typescript": "^2.8.2",
"@byloth/eslint-config-typescript": "^2.8.3",
"@types/node": "^20.16.5",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"eslint": "^8.57.0",
"husky": "^9.1.5",
"eslint": "^8.57.1",
"husky": "^9.1.6",
"typescript": "^5.6.2",
"vite": "^5.4.4",
"vitest": "^2.0.5"
"vite": "^5.4.7"
},

@@ -61,0 +60,0 @@ "scripts": {

@@ -1,2 +0,2 @@

export const VERSION = "2.0.0-rc.2";
export const VERSION = "2.0.0-rc.3";

@@ -10,4 +10,2 @@ export type { Constructor, Interval, Timeout } from "./core/types.js";

AggregatedAsyncIterator,
Aggregator,
AsyncAggregator,
Clock,

@@ -44,19 +42,19 @@ Countdown,

AsyncGeneratorFunction,
AsyncIterLike,
AsyncIteratorLike,
FulfilledHandler,
GeneratorFunction,
Iteratee,
IterLike,
IteratorLike,
JSONArray,
JSONObject,
JSONValue,
KeyIteratee,
KeyReducer,
KeyTypeGuardIteratee,
MaybeAsyncKeyIteratee,
MaybeAsyncKeyReducer,
MaybeAsyncKeyTypeGuardIteratee,
KeyedIteratee,
KeyedReducer,
KeyedTypeGuardIteratee,
MaybeAsyncKeyedIteratee,
MaybeAsyncKeyedReducer,
MaybeAsyncKeyedTypeGuardIteratee,
MaybeAsyncReducer,
MaybeAsyncIteratee,
MaybeAsyncIterLike,
MaybeAsyncIteratorLike,
MaybeAsyncTypeGuardIteratee,

@@ -63,0 +61,0 @@ MaybePromise,

import { SmartAsyncIterator } from "../iterators/index.js";
import type { AsyncGeneratorFunction, GeneratorFunction, MaybeAsyncIterLike } from "../iterators/types.js";
import type {
GeneratorFunction,
AsyncGeneratorFunction,
MaybeAsyncGeneratorFunction,
MaybeAsyncIteratorLike
} from "../iterators/types.js";
import type { MaybePromise } from "../types.js";
import ReducedIterator from "./reduced-iterator.js";
import type { MaybeAsyncKeyIteratee, MaybeAsyncKeyTypeGuardIteratee, MaybeAsyncKeyReducer } from "./types.js";
import type { MaybeAsyncKeyedIteratee, MaybeAsyncKeyedTypeGuardIteratee, MaybeAsyncKeyedReducer } from "./types.js";

@@ -18,4 +24,4 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>

public constructor(generatorFn: AsyncGeneratorFunction<[K, T]>);
public constructor(argument: MaybeAsyncIterLike<[K, T]>);
public constructor(argument: MaybeAsyncIterLike<[K, T]>)
public constructor(argument: MaybeAsyncIteratorLike<[K, T]> | MaybeAsyncGeneratorFunction<[K, T]>);
public constructor(argument: MaybeAsyncIteratorLike<[K, T]> | MaybeAsyncGeneratorFunction<[K, T]>)
{

@@ -25,13 +31,13 @@ this._elements = new SmartAsyncIterator(argument);

public async every(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
public async every(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
{
const indexes = new Map<K, [number, boolean]>();
const values = new Map<K, [number, boolean]>();
for await (const [key, element] of this._elements)
{
const [index, result] = indexes.get(key) ?? [0, true];
const [index, result] = values.get(key) ?? [0, true];
if (!(result)) { continue; }
indexes.set(key, [index + 1, await predicate(key, element, index)]);
values.set(key, [index + 1, await predicate(key, element, index)]);
}

@@ -41,19 +47,16 @@

{
for (const [key, [_, result]] of indexes)
{
yield [key, result];
}
for (const [key, [_, result]] of values) { yield [key, result]; }
});
}
public async some(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
public async some(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
{
const indexes = new Map<K, [number, boolean]>();
const values = new Map<K, [number, boolean]>();
for await (const [key, element] of this._elements)
{
const [index, result] = indexes.get(key) ?? [0, false];
const [index, result] = values.get(key) ?? [0, false];
if (result) { continue; }
indexes.set(key, [index + 1, await predicate(key, element, index)]);
values.set(key, [index + 1, await predicate(key, element, index)]);
}

@@ -63,12 +66,9 @@

{
for (const [key, [_, result]] of indexes)
{
yield [key, result];
}
for (const [key, [_, result]] of values) { yield [key, result]; }
});
}
public filter(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>;
public filter<S extends T>(predicate: MaybeAsyncKeyTypeGuardIteratee<K, T, S>): AggregatedAsyncIterator<K, S>;
public filter(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>
public filter(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>;
public filter<S extends T>(predicate: MaybeAsyncKeyedTypeGuardIteratee<K, T, S>): AggregatedAsyncIterator<K, S>;
public filter(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>
{

@@ -85,9 +85,9 @@ const elements = this._elements;

if (await predicate(key, element, index)) { yield [key, element]; }
indexes.set(key, index + 1);
if (await predicate(key, element, index)) { yield [key, element]; }
}
});
}
public map<V>(iteratee: MaybeAsyncKeyIteratee<K, T, V>): AggregatedAsyncIterator<K, V>
public map<V>(iteratee: MaybeAsyncKeyedIteratee<K, T, V>): AggregatedAsyncIterator<K, V>
{

@@ -104,15 +104,15 @@ const elements = this._elements;

yield [key, await iteratee(key, element, index)];
indexes.set(key, index + 1);
yield [key, await iteratee(key, element, index)];
}
});
}
public async reduce(reducer: MaybeAsyncKeyReducer<K, T, T>): Promise<ReducedIterator<K, T>>;
public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue: (key: K) => MaybePromise<A>)
public async reduce(reducer: MaybeAsyncKeyedReducer<K, T, T>): Promise<ReducedIterator<K, T>>;
public async reduce<A>(reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue: (key: K) => MaybePromise<A>)
: Promise<ReducedIterator<K, A>>;
public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue?: (key: K) => MaybePromise<A>)
public async reduce<A>(reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue?: (key: K) => MaybePromise<A>)
: Promise<ReducedIterator<K, A>>
{
const accumulators = new Map<K, [number, A]>();
const values = new Map<K, [number, A]>();

@@ -124,8 +124,3 @@ for await (const [key, element] of this._elements)

if (accumulators.has(key))
{
[index, accumulator] = accumulators.get(key)!;
index += 1;
}
if (values.has(key)) { [index, accumulator] = values.get(key)!; }
else if (initialValue !== undefined)

@@ -138,3 +133,3 @@ {

{
accumulators.set(key, [0, (element as unknown) as A]);
values.set(key, [0, (element as unknown) as A]);

@@ -144,5 +139,3 @@ continue;

accumulator = await reducer(key, accumulator, element, index);
accumulators.set(key, [index, accumulator]);
values.set(key, [index + 1, await reducer(key, accumulator, element, index)]);
}

@@ -152,5 +145,22 @@

{
for (const [key, [_, accumulator]] of accumulators)
for (const [key, [_, accumulator]] of values) { yield [key, accumulator]; }
});
}
public flatMap<V>(iteratee: MaybeAsyncKeyedIteratee<K, T, Iterable<V>>): AggregatedAsyncIterator<K, V>
{
const elements = this._elements;
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, V]>
{
const indexes = new Map<K, number>();
for await (const [key, element] of elements)
{
yield [key, accumulator];
const index = indexes.get(key) ?? 0;
const values = await iteratee(key, element, index);
for await (const value of values) { yield [key, value]; }
indexes.set(key, index + 1);
}

@@ -160,3 +170,3 @@ });

public unique(): AggregatedAsyncIterator<K, T>
public drop(count: number): AggregatedAsyncIterator<K, T>
{

@@ -167,13 +177,14 @@ const elements = this._elements;

{
const keys = new Map<K, Set<T>>();
const indexes = new Map<K, number>();
for await (const [key, element] of elements)
{
const values = keys.get(key) ?? new Set<T>();
const index = indexes.get(key) ?? 0;
if (index < count)
{
indexes.set(key, index + 1);
if (values.has(element)) { continue; }
continue;
}
values.add(element);
keys.set(key, values);
yield [key, element];

@@ -183,31 +194,43 @@ }

}
public async count(): Promise<ReducedIterator<K, number>>
public take(limit: number): AggregatedAsyncIterator<K, T>
{
const counters = new Map<K, number>();
const elements = this._elements;
for await (const [key] of this._elements)
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
{
const count = counters.get(key) ?? 0;
const indexes = new Map<K, number>();
counters.set(key, count + 1);
}
for await (const [key, element] of elements)
{
const index = indexes.get(key) ?? 0;
if (index >= limit)
{
if (indexes.values().every((value) => value >= limit)) { break; }
return new ReducedIterator(function* ()
{
for (const [key, count] of counters)
{
yield [key, count];
continue;
}
yield [key, element];
indexes.set(key, index + 1);
}
});
}
public async first(): Promise<ReducedIterator<K, T>>
public async find(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, T | void>>;
public async find<S extends T>(predicate: MaybeAsyncKeyedTypeGuardIteratee<K, T, S>)
: Promise<ReducedIterator<K, S | void>>;
public async find(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, T | void>>
{
const firsts = new Map<K, T>();
const values = new Map<K, [number, T | undefined]>();
for await (const [key, element] of this._elements)
{
if (firsts.has(key)) { continue; }
let [index, finding] = values.get(key) ?? [0, undefined];
firsts.set(key, element);
if (finding !== undefined) { continue; }
if (await predicate(key, element, index)) { finding = element; }
values.set(key, [index + 1, finding]);
}

@@ -217,4 +240,23 @@

{
for (const [key, element] of firsts)
for (const [key, [_, finding]] of values) { yield [key, finding]; }
});
}
public unique(): AggregatedAsyncIterator<K, T>
{
const elements = this._elements;
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
{
const keys = new Map<K, Set<T>>();
for await (const [key, element] of elements)
{
const values = keys.get(key) ?? new Set<T>();
if (values.has(element)) { continue; }
values.add(element);
keys.set(key, values);
yield [key, element];

@@ -224,9 +266,12 @@ }

}
public async last(): Promise<ReducedIterator<K, T>>
public async count(): Promise<ReducedIterator<K, number>>
{
const lasts = new Map<K, T>();
const counters = new Map<K, number>();
for await (const [key, element] of this._elements)
for await (const [key] of this._elements)
{
lasts.set(key, element);
const count = counters.get(key) ?? 0;
counters.set(key, count + 1);
}

@@ -236,9 +281,20 @@

{
for (const [key, element] of lasts)
{
yield [key, element];
}
for (const [key, count] of counters) { yield [key, count]; }
});
}
public async forEach(iteratee: MaybeAsyncKeyedIteratee<K, T>): Promise<void>
{
const indexes = new Map<K, number>();
for await (const [key, element] of this._elements)
{
const index = indexes.get(key) ?? 0;
iteratee(key, element, index);
indexes.set(key, index + 1);
}
}
public keys(): SmartAsyncIterator<K>

@@ -271,6 +327,3 @@ {

{
for await (const [_, element] of elements)
{
yield element;
}
for await (const [_, element] of elements) { yield element; }
});

@@ -277,0 +330,0 @@ }

import { SmartIterator } from "../iterators/index.js";
import type { GeneratorFunction } from "../iterators/types.js";
import type { GeneratorFunction, IteratorLike } from "../iterators/types.js";
import ReducedIterator from "./reduced-iterator.js";
import type { KeyIteratee, KeyTypeGuardIteratee, KeyReducer } from "./types.js";
import type { KeyedIteratee, KeyedTypeGuardIteratee, KeyedReducer } from "./types.js";

@@ -14,4 +14,4 @@ export default class AggregatedIterator<K extends PropertyKey, T>

public constructor(generatorFn: GeneratorFunction<[K, T]>);
public constructor(argument: Iterable<[K, T]> | Iterator<[K, T]> | GeneratorFunction<[K, T]>);
public constructor(argument: Iterable<[K, T]> | Iterator<[K, T]> | GeneratorFunction<[K, T]>)
public constructor(argument: IteratorLike<[K, T]> | GeneratorFunction<[K, T]>);
public constructor(argument: IteratorLike<[K, T]> | GeneratorFunction<[K, T]>)
{

@@ -21,13 +21,13 @@ this._elements = new SmartIterator(argument);

public every(predicate: KeyIteratee<K, T, boolean>): ReducedIterator<K, boolean>
public every(predicate: KeyedIteratee<K, T, boolean>): ReducedIterator<K, boolean>
{
const indexes = new Map<K, [number, boolean]>();
const values = new Map<K, [number, boolean]>();
for (const [key, element] of this._elements)
{
const [index, result] = indexes.get(key) ?? [0, true];
const [index, result] = values.get(key) ?? [0, true];
if (!(result)) { continue; }
indexes.set(key, [index + 1, predicate(key, element, index)]);
values.set(key, [index + 1, predicate(key, element, index)]);
}

@@ -37,19 +37,16 @@

{
for (const [key, [_, result]] of indexes)
{
yield [key, result];
}
for (const [key, [_, result]] of values) { yield [key, result]; }
});
}
public some(predicate: KeyIteratee<K, T, boolean>): ReducedIterator<K, boolean>
public some(predicate: KeyedIteratee<K, T, boolean>): ReducedIterator<K, boolean>
{
const indexes = new Map<K, [number, boolean]>();
const values = new Map<K, [number, boolean]>();
for (const [key, element] of this._elements)
{
const [index, result] = indexes.get(key) ?? [0, false];
const [index, result] = values.get(key) ?? [0, false];
if (result) { continue; }
indexes.set(key, [index + 1, predicate(key, element, index)]);
values.set(key, [index + 1, predicate(key, element, index)]);
}

@@ -59,12 +56,9 @@

{
for (const [key, [_, result]] of indexes)
{
yield [key, result];
}
for (const [key, [_, result]] of values) { yield [key, result]; }
});
}
public filter(predicate: KeyIteratee<K, T, boolean>): AggregatedIterator<K, T>;
public filter<S extends T>(predicate: KeyTypeGuardIteratee<K, T, S>): AggregatedIterator<K, S>;
public filter(predicate: KeyIteratee<K, T, boolean>): AggregatedIterator<K, T>
public filter(predicate: KeyedIteratee<K, T, boolean>): AggregatedIterator<K, T>;
public filter<S extends T>(predicate: KeyedTypeGuardIteratee<K, T, S>): AggregatedIterator<K, S>;
public filter(predicate: KeyedIteratee<K, T, boolean>): AggregatedIterator<K, T>
{

@@ -81,9 +75,9 @@ const elements = this._elements;

if (predicate(key, element, index)) { yield [key, element]; }
indexes.set(key, index + 1);
if (predicate(key, element, index)) { yield [key, element]; }
}
});
}
public map<V>(iteratee: KeyIteratee<K, T, V>): AggregatedIterator<K, V>
public map<V>(iteratee: KeyedIteratee<K, T, V>): AggregatedIterator<K, V>
{

@@ -100,13 +94,13 @@ const elements = this._elements;

yield [key, iteratee(key, element, index)];
indexes.set(key, index + 1);
yield [key, iteratee(key, element, index)];
}
});
}
public reduce(reducer: KeyReducer<K, T, T>): ReducedIterator<K, T>;
public reduce<A>(reducer: KeyReducer<K, T, A>, initialValue: (key: K) => A): ReducedIterator<K, A>;
public reduce<A>(reducer: KeyReducer<K, T, A>, initialValue?: (key: K) => A): ReducedIterator<K, A>
public reduce(reducer: KeyedReducer<K, T, T>): ReducedIterator<K, T>;
public reduce<A>(reducer: KeyedReducer<K, T, A>, initialValue: (key: K) => A): ReducedIterator<K, A>;
public reduce<A>(reducer: KeyedReducer<K, T, A>, initialValue?: (key: K) => A): ReducedIterator<K, A>
{
const accumulators = new Map<K, [number, A]>();
const values = new Map<K, [number, A]>();

@@ -118,8 +112,3 @@ for (const [key, element] of this._elements)

if (accumulators.has(key))
{
[index, accumulator] = accumulators.get(key)!;
index += 1;
}
if (values.has(key)) { [index, accumulator] = values.get(key)!; }
else if (initialValue !== undefined)

@@ -132,3 +121,3 @@ {

{
accumulators.set(key, [0, (element as unknown) as A]);
values.set(key, [0, (element as unknown) as A]);

@@ -138,5 +127,3 @@ continue;

accumulator = reducer(key, accumulator, element, index);
accumulators.set(key, [index, accumulator]);
values.set(key, [index + 1, reducer(key, accumulator, element, index)]);
}

@@ -146,5 +133,22 @@

{
for (const [key, [_, accumulator]] of accumulators)
for (const [key, [_, accumulator]] of values) { yield [key, accumulator]; }
});
}
public flatMap<V>(iteratee: KeyedIteratee<K, T, Iterable<V>>): AggregatedIterator<K, V>
{
const elements = this._elements;
return new AggregatedIterator(function* ()
{
const indexes = new Map<K, number>();
for (const [key, element] of elements)
{
yield [key, accumulator];
const index = indexes.get(key) ?? 0;
const values = iteratee(key, element, index);
for (const value of values) { yield [key, value]; }
indexes.set(key, index + 1);
}

@@ -154,3 +158,3 @@ });

public unique(): AggregatedIterator<K, T>
public drop(count: number): AggregatedIterator<K, T>
{

@@ -161,13 +165,14 @@ const elements = this._elements;

{
const keys = new Map<K, Set<T>>();
const indexes = new Map<K, number>();
for (const [key, element] of elements)
{
const values = keys.get(key) ?? new Set<T>();
const index = indexes.get(key) ?? 0;
if (index < count)
{
indexes.set(key, index + 1);
if (values.has(element)) { continue; }
continue;
}
values.add(element);
keys.set(key, values);
yield [key, element];

@@ -177,31 +182,41 @@ }

}
public count(): ReducedIterator<K, number>
public take(limit: number): AggregatedIterator<K, T>
{
const counters = new Map<K, number>();
const elements = this._elements;
for (const [key] of this._elements)
return new AggregatedIterator(function* ()
{
const count = counters.get(key) ?? 0;
const indexes = new Map<K, number>();
counters.set(key, count + 1);
}
for (const [key, element] of elements)
{
const index = indexes.get(key) ?? 0;
if (index >= limit)
{
if (indexes.values().every((value) => value >= limit)) { break; }
return new ReducedIterator(function* ()
{
for (const [key, count] of counters)
{
yield [key, count];
continue;
}
yield [key, element];
indexes.set(key, index + 1);
}
});
}
public first(): ReducedIterator<K, T>
public find(predicate: KeyedIteratee<K, T, boolean>): ReducedIterator<K, T | void>;
public find<S extends T>(predicate: KeyedTypeGuardIteratee<K, T, S>): ReducedIterator<K, S | void>;
public find(predicate: KeyedIteratee<K, T, boolean>): ReducedIterator<K, T | void>
{
const firsts = new Map<K, T>();
const values = new Map<K, [number, T | undefined]>();
for (const [key, element] of this._elements)
{
if (firsts.has(key)) { continue; }
let [index, finding] = values.get(key) ?? [0, undefined];
firsts.set(key, element);
if (finding !== undefined) { continue; }
if (predicate(key, element, index)) { finding = element; }
values.set(key, [index + 1, finding]);
}

@@ -211,4 +226,27 @@

{
for (const [key, element] of firsts)
for (const [key, [_, finding]] of values) { yield [key, finding]; }
});
}
public enumerate(): AggregatedIterator<K, [number, T]>
{
return this.map((_, value, index) => [index, value]);
}
public unique(): AggregatedIterator<K, T>
{
const elements = this._elements;
return new AggregatedIterator(function* ()
{
const keys = new Map<K, Set<T>>();
for (const [key, element] of elements)
{
const values = keys.get(key) ?? new Set<T>();
if (values.has(element)) { continue; }
values.add(element);
keys.set(key, values);
yield [key, element];

@@ -218,9 +256,12 @@ }

}
public last(): ReducedIterator<K, T>
public count(): ReducedIterator<K, number>
{
const lasts = new Map<K, T>();
const counters = new Map<K, number>();
for (const [key, element] of this._elements)
for (const [key] of this._elements)
{
lasts.set(key, element);
const count = counters.get(key) ?? 0;
counters.set(key, count + 1);
}

@@ -230,9 +271,20 @@

{
for (const [key, element] of lasts)
{
yield [key, element];
}
for (const [key, count] of counters) { yield [key, count]; }
});
}
public forEach(iteratee: KeyedIteratee<K, T>): void
{
const indexes = new Map<K, number>();
for (const [key, element] of this._elements)
{
const index = indexes.get(key) ?? 0;
iteratee(key, element, index);
indexes.set(key, index + 1);
}
}
public keys(): SmartIterator<K>

@@ -265,6 +317,3 @@ {

{
for (const [_, element] of elements)
{
yield element;
}
for (const [_, element] of elements) { yield element; }
});

@@ -275,3 +324,5 @@ }

{
return Array.from(this.toMap().values());
const map = this.toMap();
return Array.from(map.values());
}

@@ -278,0 +329,0 @@ public toMap(): Map<K, T[]>

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

import Aggregator from "./aggregator.js";
import AsyncAggregator from "./async-aggregator.js";
import AggregatedIterator from "./aggregated-iterator.js";

@@ -7,2 +5,2 @@ import AggregatedAsyncIterator from "./aggregated-async-iterator.js";

export { Aggregator, AsyncAggregator, AggregatedIterator, AggregatedAsyncIterator, ReducedIterator };
export { AggregatedIterator, AggregatedAsyncIterator, ReducedIterator };

@@ -5,3 +5,4 @@ import { ValueException } from "../exceptions/index.js";

import type { KeyIteratee, KeyReducer, KeyTypeGuardIteratee } from "./types.js";
import AggregatedIterator from "./aggregated-iterator.js";
import type { KeyedIteratee, KeyedReducer, KeyedTypeGuardIteratee } from "./types.js";

@@ -21,26 +22,42 @@ export default class ReducedIterator<K extends PropertyKey, T>

public filter(predicate: KeyIteratee<K, T, boolean>): ReducedIterator<K, T>;
public filter<S extends T>(predicate: KeyTypeGuardIteratee<K, T, S>): ReducedIterator<K, S>;
public filter(predicate: KeyIteratee<K, T, boolean>): ReducedIterator<K, T>
public every(predicate: KeyedIteratee<K, T, boolean>): boolean
{
const elements = this._elements;
for (const [index, [key, element]] of this._elements.enumerate())
{
if (!(predicate(key, element, index))) { return false; }
}
return true;
}
public some(predicate: KeyedIteratee<K, T, boolean>): boolean
{
for (const [index, [key, element]] of this._elements.enumerate())
{
if (predicate(key, element, index)) { return true; }
}
return false;
}
public filter(predicate: KeyedIteratee<K, T, boolean>): ReducedIterator<K, T>;
public filter<S extends T>(predicate: KeyedTypeGuardIteratee<K, T, S>): ReducedIterator<K, S>;
public filter(predicate: KeyedIteratee<K, T, boolean>): ReducedIterator<K, T>
{
const elements = this._elements.enumerate();
return new ReducedIterator(function* ()
{
for (const [index, [key, element]] of elements.enumerate())
for (const [index, [key, element]] of elements)
{
if (predicate(key, element, index))
{
yield [key, element];
}
if (predicate(key, element, index)) { yield [key, element]; }
}
});
}
public map<V>(iteratee: KeyIteratee<K, T, V>): ReducedIterator<K, V>
public map<V>(iteratee: KeyedIteratee<K, T, V>): ReducedIterator<K, V>
{
const elements = this._elements;
const elements = this._elements.enumerate();
return new ReducedIterator(function* ()
{
for (const [index, [key, element]] of elements.enumerate())
for (const [index, [key, element]] of elements)
{

@@ -51,23 +68,15 @@ yield [key, iteratee(key, element, index)];

}
public reduce(reducer: KeyReducer<K, T, T>): T;
public reduce<A>(reducer: KeyReducer<K, T, A>, initialValue: A): A;
public reduce<A>(reducer: KeyReducer<K, T, A>, initialValue?: A): A
public reduce(reducer: KeyedReducer<K, T, T>): T;
public reduce<A>(reducer: KeyedReducer<K, T, A>, initialValue: A): A;
public reduce<A>(reducer: KeyedReducer<K, T, A>, initialValue?: A): A
{
let index = 0;
let accumulator: A;
if (initialValue !== undefined)
let accumulator = initialValue;
if (accumulator === undefined)
{
accumulator = initialValue;
}
else
{
const firstElement = this._elements.next();
if (firstElement.done)
{
throw new ValueException("Cannot reduce an empty iterator without an initial value.");
}
const result = this._elements.next();
if (result.done) { throw new ValueException("Cannot reduce an empty iterator without an initial value."); }
accumulator = (result.value[1] as unknown) as A;
index += 1;
accumulator = (firstElement.value[1] as unknown) as A;
}

@@ -85,2 +94,81 @@

public flatMap<V>(iteratee: KeyedIteratee<K, T, Iterable<V>>): AggregatedIterator<K, V>
{
const elements = this._elements.enumerate();
return new AggregatedIterator(function* ()
{
for (const [index, [key, element]] of elements)
{
for (const value of iteratee(key, element, index)) { yield [key, value]; }
}
});
}
public drop(count: number): ReducedIterator<K, T>
{
const elements = this._elements.enumerate();
return new ReducedIterator(function* ()
{
for (const [index, [key, element]] of elements)
{
if (index >= count) { yield [key, element]; }
}
});
}
public take(count: number): ReducedIterator<K, T>
{
const elements = this._elements.enumerate();
return new ReducedIterator(function* ()
{
for (const [index, [key, element]] of elements)
{
if (index >= count) { break; }
yield [key, element];
}
});
}
public enumerate(): ReducedIterator<K, [number, T]>
{
return this.map((_, element, index) => [index, element]);
}
public unique(): ReducedIterator<K, T>
{
const elements = this._elements;
return new ReducedIterator(function* ()
{
const values = new Set<T>();
for (const [key, element] of elements)
{
if (values.has(element)) { continue; }
values.add(element);
yield [key, element];
}
});
}
public count(): number
{
let index = 0;
for (const _ of this._elements) { index += 1; }
return index;
}
public forEach(iteratee: KeyedIteratee<K, T>): void
{
for (const [index, [key, element]] of this._elements.enumerate())
{
iteratee(key, element, index);
}
}
public keys(): SmartIterator<K>

@@ -87,0 +175,0 @@ {

@@ -0,14 +1,19 @@

/* eslint-disable max-len */
import type { MaybePromise } from "../promises/types.js";
export type KeyIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) => R;
export type MaybeAsyncKeyIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) =>
MaybePromise<R>;
export type KeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) => R;
export type AsyncKeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) => Promise<R>;
export type MaybeAsyncKeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) => MaybePromise<R>;
export type KeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
(key: K, value: T, index: number) => value is R;
export type MaybeAsyncKeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
(key: K, value: MaybePromise<T>, index: number) => value is Awaited<R>;
export type KeyedTypeGuardIteratee<K extends PropertyKey, T, R extends T> = (key: K, value: T, index: number) => value is R;
export type KeyReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => A;
export type MaybeAsyncKeyReducer<K extends PropertyKey, T, A> =
(key: K, accumulator: A, value: T, index: number) => MaybePromise<A>;
// @ts-expect-error - This is an asyncronous type guard keyed-iteratee that guarantees the return value is a promise.
export type AsyncKeyedTypeGuardIteratee<K extends PropertyKey, T, R extends T> = (key: K, value: T, index: number) => value is Promise<R>;
// @ts-expect-error - This may be an asyncronous type guard keyed-iteratee that guarantees the return value may be a promise.
export type MaybeAsyncKeyedTypeGuardIteratee<K extends PropertyKey, T, R extends T> = (key: K, value: T, index: number) => value is MaybePromise<R>;
export type KeyedReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => A;
export type AsyncKeyedReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => Promise<A>;
export type MaybeAsyncKeyedReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => MaybePromise<A>;
export {
Aggregator,
AsyncAggregator,
AggregatedIterator,

@@ -5,0 +3,0 @@ AggregatedAsyncIterator,

@@ -0,9 +1,12 @@

import AggregatedAsyncIterator from "../aggregators/aggregated-async-iterator.js";
import { ValueException } from "../exceptions/index.js";
import type {
GeneratorFunction,
AsyncGeneratorFunction,
GeneratorFunction,
MaybeAsyncGeneratorFunction,
MaybeAsyncIteratee,
MaybeAsyncReducer,
MaybeAsyncIterLike,
MaybeAsyncIterable,
MaybeAsyncIteratorLike,
MaybeAsyncTypeGuardIteratee

@@ -26,4 +29,4 @@

public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
public constructor(argument: MaybeAsyncIterLike<T, R, N>);
public constructor(argument: MaybeAsyncIterLike<T, R, N>)
public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>);
public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>)
{

@@ -104,3 +107,3 @@ if (argument instanceof Function)

if (result.done) { return true; }
if (!(predicate(result.value, index))) { return false; }
if (!(await predicate(result.value, index))) { return false; }

@@ -120,3 +123,3 @@ index += 1;

if (result.done) { return false; }
if (predicate(result.value, index)) { return true; }
if (await predicate(result.value, index)) { return true; }

@@ -142,3 +145,3 @@ index += 1;

if (result.done) { return result.value; }
if (predicate(result.value, index)) { yield result.value; }
if (await predicate(result.value, index)) { yield result.value; }

@@ -162,3 +165,3 @@ index += 1;

yield iteratee(result.value, index);
yield await iteratee(result.value, index);

@@ -196,2 +199,90 @@ index += 1;

public flatMap<V>(iteratee: MaybeAsyncIteratee<T, MaybeAsyncIterable<V>>): SmartAsyncIterator<V, R>
{
const iterator = this._iterator;
return new SmartAsyncIterator<V, R>(async function* ()
{
let index = 0;
while (true)
{
const result = await iterator.next();
if (result.done) { return result.value; }
const elements = await iteratee(result.value, index);
for await (const element of elements)
{
yield element;
}
index += 1;
}
});
}
public drop(count: number): SmartAsyncIterator<T, R | void>
{
const iterator = this._iterator;
return new SmartAsyncIterator<T, R | void>(async function* ()
{
let index = 0;
while (index < count)
{
const result = await iterator.next();
if (result.done) { return; }
index += 1;
}
while (true)
{
const result = await iterator.next();
if (result.done) { return result.value; }
yield result.value;
}
});
}
public take(limit: number): SmartAsyncIterator<T, R | void>
{
const iterator = this._iterator;
return new SmartAsyncIterator<T, R | void>(async function* ()
{
let index = 0;
while (index < limit)
{
const result = await iterator.next();
if (result.done) { return result.value; }
yield result.value;
index += 1;
}
return;
});
}
public async find(predicate: MaybeAsyncIteratee<T, boolean>): Promise<T | void>
{
let index = 0;
// eslint-disable-next-line no-constant-condition
while (true)
{
const result = await this._iterator.next();
if (result.done) { return; }
if (await predicate(result.value, index)) { return result.value; }
index += 1;
}
}
public enumerate(): SmartAsyncIterator<[number, T], R>

@@ -257,16 +348,17 @@ {

public async toArray(): Promise<T[]>
public groupBy<K extends PropertyKey>(iteratee: MaybeAsyncIteratee<T, K>): AggregatedAsyncIterator<K, T>
{
const elements: T[] = [];
// eslint-disable-next-line no-constant-condition
while (true)
return new AggregatedAsyncIterator(this.map(async (element, index) =>
{
const result = await this._iterator.next();
if (result.done) { return elements; }
const key = await iteratee(element, index);
elements.push(result.value);
}
return [key, element] as [K, T];
}));
}
public toArray(): Promise<T[]>
{
return Array.fromAsync(this as AsyncIterable<T>);
}
public get [Symbol.toStringTag]() { return "SmartAsyncIterator"; }

@@ -273,0 +365,0 @@

@@ -0,4 +1,5 @@

import AggregatedIterator from "../aggregators/aggregated-iterator.js";
import { ValueException } from "../exceptions/index.js";
import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer, IterLike } from "./types.js";
import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer, IteratorLike } from "./types.js";

@@ -12,7 +13,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterator<T, R, N>

public constructor(iterable: Iterable<T>);
public constructor(iterable: Iterable<T, R, N>);
public constructor(iterator: Iterator<T, R, N>);
public constructor(generatorFn: GeneratorFunction<T, R, N>);
public constructor(argument: IterLike<T, R, N>);
public constructor(argument: IterLike<T, R, N>)
public constructor(argument: IteratorLike<T, R, N> | GeneratorFunction<T, R, N>);
public constructor(argument: IteratorLike<T, R, N> | GeneratorFunction<T, R, N>)
{

@@ -32,4 +33,4 @@ if (argument instanceof Function)

if (this._iterator.return) { this.return = (value?: R) => this._iterator.return!(value); }
if (this._iterator.throw) { this.throw = (error?: unknown) => this._iterator.throw!(error); }
if (this._iterator.return) { this.return = (value) => this._iterator.return!(value); }
if (this._iterator.throw) { this.throw = (error) => this._iterator.throw!(error); }
}

@@ -135,2 +136,89 @@

public flatMap<V>(iteratee: Iteratee<T, Iterable<V>>): SmartIterator<V, R>
{
const iterator = this._iterator;
return new SmartIterator<V, R>(function* ()
{
let index = 0;
while (true)
{
const result = iterator.next();
if (result.done) { return result.value; }
const iterable = iteratee(result.value, index);
for (const value of iterable)
{
yield value;
}
index += 1;
}
});
}
public drop(count: number): SmartIterator<T, R | void>
{
const iterator = this._iterator;
return new SmartIterator<T, R | void>(function* ()
{
let index = 0;
while (index < count)
{
const result = iterator.next();
if (result.done) { return; }
index += 1;
}
while (true)
{
const result = iterator.next();
if (result.done) { return result.value; }
yield result.value;
}
});
}
public take(limit: number): SmartIterator<T, R | void>
{
const iterator = this._iterator;
return new SmartIterator<T, R | void>(function* ()
{
let index = 0;
while (index < limit)
{
const result = iterator.next();
if (result.done) { return result.value; }
yield result.value;
index += 1;
}
return;
});
}
public find(predicate: Iteratee<T, boolean>): T | void;
public find<S extends T>(predicate: TypeGuardIteratee<T, S>): S | void;
public find(predicate: Iteratee<T, boolean>): T | void
{
let index = 0;
// eslint-disable-next-line no-constant-condition
while (true)
{
const result = this._iterator.next();
if (result.done) { return; }
if (predicate(result.value, index)) { return result.value; }
index += 1;
}
}
public enumerate(): SmartIterator<[number, T], R>

@@ -175,2 +263,3 @@ {

}
public forEach(iteratee: Iteratee<T>): void

@@ -197,2 +286,12 @@ {

public groupBy<K extends PropertyKey>(iteratee: Iteratee<T, K>): AggregatedIterator<K, T>
{
return new AggregatedIterator(this.map((element, index) =>
{
const key = iteratee(element, index);
return [key, element] as [K, T];
}));
}
public toArray(): T[]

@@ -199,0 +298,0 @@ {

@@ -0,20 +1,31 @@

/* eslint-disable max-len */
import type { MaybePromise } from "../promises/types.js";
export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> | AsyncIterable<T, R, N>;
export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> | AsyncIterator<T, R, N>;
export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N> | AsyncGenerator<T, R, N>;
export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R, N>;
export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGenerator<T, R, N>;
export type MaybeAsyncGeneratorFunction<T, R = void, N = undefined> = () => MaybeAsyncGenerator<T, R, N>;
export type Iteratee<T, R = void> = (value: T, index: number) => R;
export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>;
export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => MaybePromise<R>;
export type TypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is R;
export type MaybeAsyncTypeGuardIteratee<T, R extends T> = (value: MaybePromise<T>, index: number) =>
value is MaybePromise<R>;
// @ts-expect-error - This is an asyncronous type guard iteratee that guarantees the return value is a promise.
export type AsyncTypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is Promise<R>;
// @ts-expect-error - This may be an asyncronous type guard iteratee that guarantees the return value may be a promise.
export type MaybeAsyncTypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is MaybePromise<R>;
export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Promise<A>;
export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number) => MaybePromise<A>;
export type IterLike<T, R = void, N = undefined> = Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>;
export type AsyncIterLike<T, R = void, N = undefined> =
AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>;
export type MaybeAsyncIterLike<T, R = void, N = undefined> = IterLike<T, R, N> | AsyncIterLike<T, R, N>;
export type IteratorLike<T, R = void, N = undefined> = Iterable<T> | Iterator<T, R, N>;
export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T> | AsyncIterator<T, R, N>;
export type MaybeAsyncIteratorLike<T, R = void, N = undefined> = IteratorLike<T, R, N> | AsyncIteratorLike<T, R, N>;

@@ -5,2 +5,7 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types.js";

{
public static FromPromise<T>(promise: Promise<T>): SmartPromise<T>
{
return new SmartPromise((resolve, reject) => promise.then(resolve, reject));
}
protected _isPending: boolean;

@@ -7,0 +12,0 @@ protected _isFulfilled: boolean;

@@ -9,3 +9,3 @@ import { TimeUnit } from "../../utils/date.js";

{
protected _publisher: Publisher<[number], void>;
protected _publisher: Publisher<[number]>;

@@ -12,0 +12,0 @@ public constructor(fpsIfNotBrowser: number = TimeUnit.Second)

@@ -12,3 +12,3 @@ import { TimeUnit } from "../../utils/date.js";

protected _deferrer?: DeferredPromise<void>;
protected _publisher: Publisher<[number], void>;
protected _publisher: Publisher<[number]>;

@@ -15,0 +15,0 @@ protected _duration: number;

export type {
KeyIteratee,
MaybeAsyncKeyIteratee,
KeyTypeGuardIteratee,
MaybeAsyncKeyTypeGuardIteratee,
KeyReducer,
MaybeAsyncKeyReducer
KeyedIteratee,
MaybeAsyncKeyedIteratee,
KeyedTypeGuardIteratee,
MaybeAsyncKeyedTypeGuardIteratee,
KeyedReducer,
MaybeAsyncKeyedReducer

@@ -20,5 +20,5 @@ } from "./aggregators/types.js";

MaybeAsyncReducer,
IterLike,
AsyncIterLike,
MaybeAsyncIterLike
IteratorLike,
AsyncIteratorLike,
MaybeAsyncIteratorLike

@@ -25,0 +25,0 @@ } from "./iterators/types.js";

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

export async function delay(milliseconds: number): Promise<void>
export function delay(milliseconds: number): Promise<void>
{
return new Promise<void>((resolve, reject) => setTimeout(resolve, milliseconds));
return new Promise<void>((resolve) => setTimeout(resolve, milliseconds));
}
export async function nextAnimationFrame(): Promise<void>
export function nextAnimationFrame(): Promise<void>
{
return new Promise<void>((resolve, reject) => requestAnimationFrame(() => resolve()));
return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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