Skip to content

Commit

Permalink
fix: Minor fixes. + New minor version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byloth committed Jan 4, 2024
1 parent 0999dc1 commit 660df90
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 307 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@byloth/core",
"version": "1.2.0-rc.3",
"version": "1.2.0",
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
"keywords": [
"Core",
Expand Down Expand Up @@ -57,8 +57,8 @@
},
"devDependencies": {
"@byloth/eslint-config-typescript": "^2.6.8",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"typescript": "^5.3.3",
"vite": "^5.0.10"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VERSION = "1.2.0-rc.3";
export const VERSION = "1.2.0";

export { DeferredPromise, Exception, JsonStorage, SmartIterator, Subscribers } from "./models/index.js";
export {
Expand Down
54 changes: 25 additions & 29 deletions src/models/smart-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
public constructor(iterable: Iterable<T>);
public constructor(iterator: Iterator<T, R, N>);
public constructor(generatorFn: GeneratorFunction<T, R, N>);
public constructor(iterable: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>)
public constructor(argument: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>)
{
if (iterable instanceof Function)
if (argument instanceof Function)
{
this._iterator = iterable();
this._iterator = argument();
}
else if (Symbol.iterator in iterable)
else if (Symbol.iterator in argument)
{
this._iterator = iterable[Symbol.iterator]() as Iterator<T, R, N>;
this._iterator = argument[Symbol.iterator]() as Iterator<T, R, N>;
}
else
{
this._iterator = iterable;
this._iterator = argument;
}

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

public every(iteratee: Iteratee<T, boolean>): boolean
public every(predicate: Iteratee<T, boolean>): boolean
{
let index = 0;

Expand All @@ -39,12 +39,12 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
const result = this._iterator.next();

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

index += 1;
}
}
public some(iteratee: Iteratee<T, boolean>): boolean
public some(predicate: Iteratee<T, boolean>): boolean
{
let index = 0;

Expand All @@ -54,29 +54,27 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
const result = this._iterator.next();

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

index += 1;
}
}

public filter(iteratee: Iteratee<T, boolean>): SmartIterator<T, R, N>
public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R, N>
{
let index = 0;
const iterator = this._iterator;

return new SmartIterator<T, R, N>({
*[Symbol.iterator]()
return new SmartIterator<T, R, N>(function* ()
{
while (true)
{
while (true)
{
const result = iterator.next();
const result = iterator.next();

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

index += 1;
}
index += 1;
}
});
}
Expand All @@ -85,18 +83,16 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
let index = 0;
const iterator = this._iterator;

return new SmartIterator<V, R>({
*[Symbol.iterator]()
return new SmartIterator<V, R>(function* ()
{
while (true)
{
while (true)
{
const result = iterator.next();
if (result.done) { return result.value; }
const result = iterator.next();
if (result.done) { return result.value; }

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

index += 1;
}
index += 1;
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Constructor<T extends object, P extends unknown[] = []> = new (...args: P) => T;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Constructor<T extends object, P extends unknown[] = any[]> = new (...args: P) => T;

export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R, N>;
export type Iteratee<T, R = void> = (value: T, index: number) => R;
Expand Down
Loading

0 comments on commit 660df90

Please sign in to comment.