@yorozu/utils 0.1.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/arrays/index.d.ts +4 -0
  3. package/arrays/typed/compare.d.ts +3 -0
  4. package/arrays/typed/find.d.ts +10 -0
  5. package/arrays/typed/index.d.ts +4 -0
  6. package/arrays/typed/misc.d.ts +10 -0
  7. package/arrays/typed/types.d.ts +2 -0
  8. package/arrays/u8/concat.d.ts +3 -0
  9. package/arrays/u8/index.d.ts +6 -0
  10. package/arrays/u8/misc.d.ts +3 -0
  11. package/arrays/u8/pool.d.ts +11 -0
  12. package/arrays/u8/reverse.d.ts +2 -0
  13. package/arrays/u8/swap.d.ts +4 -0
  14. package/arrays/u8/xor.d.ts +2 -0
  15. package/async/async-interval.d.ts +8 -0
  16. package/async/async-lock.d.ts +6 -0
  17. package/async/async-queue.d.ts +18 -0
  18. package/async/async-resource.d.ts +33 -0
  19. package/async/condition-variable.d.ts +5 -0
  20. package/async/deferred.d.ts +24 -0
  21. package/async/emitter.d.ts +11 -0
  22. package/async/index.d.ts +11 -0
  23. package/async/pool.d.ts +19 -0
  24. package/async/sleep.d.ts +1 -0
  25. package/async/timers.d.ts +8 -0
  26. package/bigint/bytes.d.ts +2 -0
  27. package/bigint/index.d.ts +2 -0
  28. package/bigint/math.d.ts +10 -0
  29. package/encoding/base64.d.ts +6 -0
  30. package/encoding/hex.d.ts +4 -0
  31. package/encoding/index.d.ts +4 -0
  32. package/encoding/utf8.d.ts +3 -0
  33. package/index.d.ts +8 -0
  34. package/index.js +1819 -0
  35. package/iterate/enumerate.d.ts +1 -0
  36. package/iterate/index.d.ts +1 -0
  37. package/misc/assert.d.ts +7 -0
  38. package/misc/composer.d.ts +4 -0
  39. package/misc/guards.d.ts +12 -0
  40. package/misc/index.d.ts +6 -0
  41. package/misc/noop.d.ts +1 -0
  42. package/misc/objects.d.ts +18 -0
  43. package/misc/string.d.ts +4 -0
  44. package/package.json +15 -17
  45. package/structures/_iterator.d.ts +2 -0
  46. package/structures/custom-map.d.ts +19 -0
  47. package/structures/custom-set.d.ts +23 -0
  48. package/structures/deque.d.ts +31 -0
  49. package/structures/index.d.ts +5 -0
  50. package/structures/lru-map.d.ts +14 -0
  51. package/structures/lru-set.d.ts +11 -0
  52. package/types/brand.d.ts +5 -0
  53. package/types/equal.d.ts +1 -0
  54. package/types/error.d.ts +6 -0
  55. package/types/index.d.ts +5 -0
  56. package/types/misc.d.ts +11 -0
  57. package/types/unions.d.ts +3 -0
  58. package/lib/index.cjs +0 -2044
  59. package/lib/index.d.cts +0 -468
  60. package/lib/index.d.ts +0 -468
  61. package/lib/index.js +0 -1958
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ivan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ import * as typed from "./typed";
2
+ import * as u8 from "./u8";
3
+ export * from './typed/types';
4
+ export { typed, u8 };
@@ -0,0 +1,3 @@
1
+ import { TypedArray } from './types';
2
+ export declare function compare<T extends TypedArray>(a: T, b: T): -1 | 0 | 1;
3
+ export declare function equal<T extends TypedArray>(a: T, b: T): boolean;
@@ -0,0 +1,10 @@
1
+ import { TypedArray } from './types';
2
+ export declare function indexOf(haystack: Exclude<TypedArray, BigInt64Array | BigUint64Array>, needle: number, start?: number): number;
3
+ export declare function indexOf(haystack: BigInt64Array | BigUint64Array, needle: bigint, start?: number): number;
4
+ export declare function lastIndexOf(haystack: Exclude<TypedArray, BigInt64Array | BigUint64Array>, needle: number, start?: number): number;
5
+ export declare function lastIndexOf(haystack: BigInt64Array | BigUint64Array, needle: bigint, start?: number): number;
6
+ export declare function indexOfArray<T extends TypedArray>(haystack: T, needle: T, start?: number): number;
7
+ export declare function lastIndexOfArray<T extends TypedArray>(haystack: T, needle: T, start?: number): number;
8
+ export declare function includes(haystack: Exclude<TypedArray, BigInt64Array | BigUint64Array>, needle: number): boolean;
9
+ export declare function includes(haystack: BigInt64Array | BigUint64Array, needle: bigint): boolean;
10
+ export declare function includesArray<T extends TypedArray>(haystack: T, needle: T): boolean;
@@ -0,0 +1,4 @@
1
+ export * from './compare';
2
+ export * from './find';
3
+ export * from './misc';
4
+ export * from './types';
@@ -0,0 +1,10 @@
1
+ import { TypedArray } from './types';
2
+ export declare function toDataView(buf: TypedArray): DataView;
3
+ export declare function view<R extends TypedArray>(ctor: {
4
+ new (buffer: ArrayBufferLike, byteOffset: number, byteLength: number): R;
5
+ BYTES_PER_ELEMENT: number;
6
+ }, buf: ArrayBufferView): R;
7
+ export declare function getPlatformByteOrder(): "little" | "big";
8
+ export declare namespace getPlatformByteOrder {
9
+ var _cachedByteOrder: boolean | null;
10
+ }
@@ -0,0 +1,2 @@
1
+ import { AnyToNever } from '../../types';
2
+ export type TypedArray = Uint8Array | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | AnyToNever<BigInt64Array> | AnyToNever<BigUint64Array>;
@@ -0,0 +1,3 @@
1
+ export declare function concat(bufs: Array<Uint8Array>): Uint8Array;
2
+ export declare function concat2(a: ArrayLike<number>, b: ArrayLike<number>): Uint8Array;
3
+ export declare function concat3(a: ArrayLike<number>, b: ArrayLike<number>, c: ArrayLike<number>): Uint8Array;
@@ -0,0 +1,6 @@
1
+ export * from './concat';
2
+ export * from './misc';
3
+ export * from './pool';
4
+ export * from './reverse';
5
+ export * from './swap';
6
+ export * from './xor';
@@ -0,0 +1,3 @@
1
+ export declare const empty: Uint8Array;
2
+ export declare function clone(buf: Uint8Array): Uint8Array;
3
+ export declare function readNthBit(byte: number, bit: number): number;
@@ -0,0 +1,11 @@
1
+ export declare class BufferPool {
2
+ #private;
3
+ readonly size: number;
4
+ readonly maxAllocSize: number;
5
+ constructor(size?: number);
6
+ allocate(size: number): Uint8Array;
7
+ reset(): void;
8
+ }
9
+ export declare function setDefaultPool(size: number): void;
10
+ export declare function allocate(size: number): Uint8Array;
11
+ export declare function allocateWith(init: ArrayLike<number>): Uint8Array;
@@ -0,0 +1,2 @@
1
+ export declare function reverse(buffer: Uint8Array): void;
2
+ export declare function toReversed(buffer: ArrayLike<number>): Uint8Array;
@@ -0,0 +1,4 @@
1
+ export declare function swap16(buf: Uint8Array): void;
2
+ export declare function swap32(buf: Uint8Array): void;
3
+ export declare function swap64(buf: Uint8Array): void;
4
+ export declare function swapNibbles(buf: Uint8Array): void;
@@ -0,0 +1,2 @@
1
+ export declare function xor(data: Uint8Array, key: Uint8Array): Uint8Array;
2
+ export declare function xorInPlace(data: Uint8Array, key: Uint8Array): void;
@@ -0,0 +1,8 @@
1
+ export declare class AsyncInterval {
2
+ #private;
3
+ constructor(handler: (abortSignal: AbortSignal) => Promise<void>, interval: number);
4
+ start(after?: number): void;
5
+ startNow(): void;
6
+ stop(): void;
7
+ onError(handler: (err: unknown) => void): void;
8
+ }
@@ -0,0 +1,6 @@
1
+ export declare class AsyncLock {
2
+ private _queue;
3
+ acquire(): Promise<void>;
4
+ release(): void;
5
+ with<T>(func: () => Promise<T>): Promise<T>;
6
+ }
@@ -0,0 +1,18 @@
1
+ import { Deque } from '../structures';
2
+ export declare class AsyncQueue<T> {
3
+ #private;
4
+ readonly queue: Deque<T>;
5
+ readonly maxSize: number | undefined;
6
+ constructor(from?: ArrayLike<T> | Deque<T>, maxSize?: number);
7
+ get length(): number;
8
+ get isFull(): boolean;
9
+ get remainingCapacity(): number;
10
+ get ended(): boolean;
11
+ enqueue(item: T): Promise<void>;
12
+ tryEnqueue(item: T): boolean;
13
+ end(): void;
14
+ peek(): T | undefined;
15
+ next(): T | undefined;
16
+ nextOrWait(): Promise<T | undefined>;
17
+ [Symbol.asyncIterator](): AsyncIterableIterator<T>;
18
+ }
@@ -0,0 +1,33 @@
1
+ import { Emitter } from './emitter';
2
+ export interface AsyncResourceContext<T> {
3
+ readonly current: T | null;
4
+ readonly currentFetchedAt: number;
5
+ readonly currentExpiresAt: number;
6
+ readonly isBackground: boolean;
7
+ readonly abort: AbortSignal | null;
8
+ }
9
+ export interface AsyncResourceOptions<T> {
10
+ autoReload?: boolean;
11
+ autoReloadAfter?: number;
12
+ /** @deprecated Use `autoReloadAfter`. */
13
+ authReloadAfter?: number;
14
+ swr?: boolean;
15
+ swrValidator?: (ctx: AsyncResourceContext<T>) => boolean;
16
+ fetcher: (ctx: AsyncResourceContext<T>) => Promise<{
17
+ data: T;
18
+ expiresIn: number;
19
+ }>;
20
+ onError?: (err: unknown, ctx: AsyncResourceContext<T>) => void;
21
+ }
22
+ export declare class AsyncResource<T> {
23
+ #private;
24
+ readonly options: AsyncResourceOptions<T>;
25
+ readonly onUpdated: Emitter<AsyncResourceContext<T>>;
26
+ constructor(options: AsyncResourceOptions<T>);
27
+ get isStale(): boolean;
28
+ setData(data: T, expiresIn: number): void;
29
+ update(force?: boolean): Promise<void>;
30
+ get(): Promise<T | null>;
31
+ getCached(): T | null;
32
+ destroy(): void;
33
+ }
@@ -0,0 +1,5 @@
1
+ export declare class ConditionVariable {
2
+ #private;
3
+ wait(): Promise<void>;
4
+ notify(): void;
5
+ }
@@ -0,0 +1,24 @@
1
+ export declare class Deferred<T = void> {
2
+ readonly resolve: (value: T) => void;
3
+ readonly reject: (reason: unknown) => void;
4
+ readonly promise: Promise<T>;
5
+ constructor();
6
+ }
7
+ export declare class DeferredTracked<T = void> {
8
+ #private;
9
+ readonly promise: Promise<T>;
10
+ readonly status: {
11
+ type: "pending";
12
+ } | {
13
+ type: "fulfilled";
14
+ value: T;
15
+ } | {
16
+ type: "rejected";
17
+ reason: unknown;
18
+ };
19
+ constructor();
20
+ get result(): T | undefined;
21
+ get error(): unknown | undefined;
22
+ resolve(value: T): void;
23
+ reject(reason: unknown): void;
24
+ }
@@ -0,0 +1,11 @@
1
+ export declare class Emitter<T> {
2
+ #private;
3
+ get length(): number;
4
+ add(listener: (value: T) => void): void;
5
+ forwardTo(emitter: Emitter<T>): void;
6
+ remove(listener: (value: T) => void): void;
7
+ emit(value: T): void;
8
+ once(listener: (value: T) => void): void;
9
+ listeners(): readonly ((value: T) => void)[];
10
+ clear(): void;
11
+ }
@@ -0,0 +1,11 @@
1
+ import * as timers from "./timers";
2
+ export * from './async-interval';
3
+ export * from './async-lock';
4
+ export * from './async-queue';
5
+ export * from './async-resource';
6
+ export * from './condition-variable';
7
+ export * from './deferred';
8
+ export * from './emitter';
9
+ export * from './pool';
10
+ export * from './sleep';
11
+ export { timers };
@@ -0,0 +1,19 @@
1
+ import { MaybePromise } from '../types';
2
+ export interface AsyncPoolOptions<T> {
3
+ limit?: number;
4
+ signal?: AbortSignal;
5
+ onErrorStrategy?: (item: T, index: number, error: unknown) => MaybePromise<"ignore" | "collect" | "throw">;
6
+ }
7
+ declare class ErrorInfo<T> {
8
+ readonly item: T;
9
+ readonly index: number;
10
+ readonly error: unknown;
11
+ constructor(item: T, index: number, error: unknown);
12
+ }
13
+ export declare class AggregateError<T> extends Error {
14
+ readonly errors: Array<ErrorInfo<T>>;
15
+ constructor(errors: Array<ErrorInfo<T>>);
16
+ }
17
+ export declare function asyncPool<T>(iterable: Iterable<T> | AsyncIterable<T>, executor: (item: T, index: number) => MaybePromise<T | void>, options?: AsyncPoolOptions<T>): Promise<void>;
18
+ export declare function parallelMap<T, R>(iterable: Iterable<T> | AsyncIterable<T>, executor: (item: T, index: number) => MaybePromise<R>, options?: AsyncPoolOptions<T>): Promise<Array<R>>;
19
+ export {};
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
@@ -0,0 +1,8 @@
1
+ import { Brand } from '../types/brand';
2
+ export type Timer = Brand<object, "Timer">;
3
+ export type Interval = Brand<object, "Interval">;
4
+ declare const setTimeoutWrap: <T extends (...args: any[]) => any>(fn: T, ms: number, ...args: Parameters<T>) => Timer;
5
+ declare const setIntervalWrap: <T extends (...args: any[]) => any>(fn: T, ms: number, ...args: Parameters<T>) => Interval;
6
+ declare const clearTimeoutWrap: (timer?: Timer) => void;
7
+ declare const clearIntervalWrap: (timer?: Interval) => void;
8
+ export { clearIntervalWrap as clearInterval, clearTimeoutWrap as clearTimeout, setIntervalWrap as setInterval, setTimeoutWrap as setTimeout, };
@@ -0,0 +1,2 @@
1
+ export declare function toBytes(value: bigint, length?: number, le?: boolean): Uint8Array;
2
+ export declare function fromBytes(buffer: Uint8Array, le?: boolean): bigint;
@@ -0,0 +1,2 @@
1
+ export * from './bytes';
2
+ export * from './math';
@@ -0,0 +1,10 @@
1
+ export declare function bitLength(n: bigint): number;
2
+ export declare function twoMultiplicity(n: bigint): bigint;
3
+ export declare function min2(a: bigint, b: bigint): bigint;
4
+ export declare function min(...args: Array<bigint>): bigint;
5
+ export declare function max2(a: bigint, b: bigint): bigint;
6
+ export declare function max(...args: Array<bigint>): bigint;
7
+ export declare function abs(a: bigint): bigint;
8
+ export declare function euclideanGcd(a: bigint, b: bigint): bigint;
9
+ export declare function modPowBinary(base: bigint, exp: bigint, mod: bigint): bigint;
10
+ export declare function modInv(a: bigint, n: bigint): bigint;
@@ -0,0 +1,6 @@
1
+ export declare const lookup: Map<number, number>;
2
+ export declare const encodeLookup: Map<number, number>;
3
+ export declare function decode(data: string, url?: boolean): Uint8Array;
4
+ export declare function encode(bytes: Uint8Array, url?: boolean): string;
5
+ export declare function encodedLength(n: number): number;
6
+ export declare function decodedLength(n: number): number;
@@ -0,0 +1,4 @@
1
+ export declare function encode(buf: Uint8Array): string;
2
+ export declare function decode(data: string): Uint8Array;
3
+ export declare function encodedLength(n: number): number;
4
+ export declare function decodedLength(n: number): number;
@@ -0,0 +1,4 @@
1
+ import * as base64 from "./base64";
2
+ import * as hex from "./hex";
3
+ import * as utf8 from "./utf8";
4
+ export { base64, hex, utf8 };
@@ -0,0 +1,3 @@
1
+ export declare const encoder: TextEncoder;
2
+ export declare const decoder: TextDecoder;
3
+ export declare function encodedLength(data: string): number;
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './arrays';
2
+ export * from './bigint';
3
+ export * from './encoding';
4
+ export * from './iterate';
5
+ export * from './misc';
6
+ export * from './async';
7
+ export * from './structures';
8
+ export * from './types';