@ayka/domistrukt
Version:
A TypeScript library for creating structured data objects with ease.
817 lines (782 loc) • 36.4 kB
TypeScript
import * as Im from 'immutable';
/**
* Represents a class constructor type.
* @template args - An array of types representing the constructor arguments. Defaults to any[].
* @template instance - The type of the instance created by the constructor. Defaults to any.
*/
type klass$1<args extends Array<any> = Array<any>, instance = any> = {
new (...args: args): instance;
};
type anyClass = klass$1<any[], any>;
/**
* Represents a class constructor type with a single input parameter.
* @template input - The type of the single input parameter for the constructor. Defaults to any.
* @template result - The type of the instance created by the constructor. Defaults to any.
*/
type klass1<input = any, result = any> = klass$1<[input], result>;
/**
* Extracts the constructor parameters of a class type.
* @template t - The class constructor type.
*/
type classArgs<t extends klass$1> = ConstructorParameters<t>;
/**
* Extracts the nth constructor parameter of a class type.
* @template t - The class constructor type.
* @template n - The index of the constructor parameter to extract.
*/
type classArgsN<
t extends klass$1,
n extends number,
> = ConstructorParameters<t>[n];
/**
* Extracts the first constructor parameter of a class type.
* @template t - The class constructor type.
*/
type classArgs1<t extends klass$1> = classArgsN<t, 0>;
/**
* Represents a generic constructor function type.
* @type {(...args: any[]) => any}
*/
type anyObject = Record<any, any>;
type anyArgs = any[];
type anyConstructor = (...args: any[]) => anyObject;
type anyConstructor1 = (arg: any) => anyObject;
type fnParam1<fn extends anyConstructor> = Parameters<fn>[0];
type isNever<t> = [t] extends [never] ? true : false;
type Types_d_anyArgs = anyArgs;
type Types_d_anyClass = anyClass;
type Types_d_anyConstructor = anyConstructor;
type Types_d_anyConstructor1 = anyConstructor1;
type Types_d_anyObject = anyObject;
type Types_d_classArgs<t extends klass$1> = classArgs<t>;
type Types_d_classArgs1<t extends klass$1> = classArgs1<t>;
type Types_d_classArgsN<t extends klass$1, n extends number> = classArgsN<t, n>;
type Types_d_fnParam1<fn extends anyConstructor> = fnParam1<fn>;
type Types_d_isNever<t> = isNever<t>;
type Types_d_klass1<input = any, result = any> = klass1<input, result>;
declare namespace Types_d {
export type { Types_d_anyArgs as anyArgs, Types_d_anyClass as anyClass, Types_d_anyConstructor as anyConstructor, Types_d_anyConstructor1 as anyConstructor1, Types_d_anyObject as anyObject, Types_d_classArgs as classArgs, Types_d_classArgs1 as classArgs1, Types_d_classArgsN as classArgsN, Types_d_fnParam1 as fnParam1, Types_d_isNever as isNever, klass$1 as klass, Types_d_klass1 as klass1 };
}
/**
* Creates a class constructor type with no arguments.
* @template t - The type of the instance created by the constructor.
* @returns {T.klass<[], t>} A class constructor type.
*/
declare const klass: <t>() => klass$1<[], t>;
/**
* Selects specified keys from a target object in a type-safe manner.
*
* @template t - The type of the target object.
* @template key - The type of the keys to select, extending keyof t.
* @param {t} target - The target object to select keys from.
* @param {Iterable<key>} keys - An iterable of keys to select.
* @returns {{ [key in key]: t[key] }} An object with the selected keys and their values.
*
* @example
* const obj = { a: 1, b: 2, c: 3 };
* const result = selectKeys(obj, ['a', 'c']);
* console.log(result); // Output: { a: 1, c: 3 }
*
* @example
* class Person {
* constructor(public name: string, public age: number, private ssn: string) {}
* toObject() {
* return selectKeys(this, ['name', 'age']);
* }
* }
* const person = new Person('John', 30, '123-45-6789');
* const result = person.toObject();
* console.log(result); // Output: { name: 'John', age: 30 }
*/
declare const selectKeys: <t, key extends keyof t = keyof t>(target: t, keys: Exclude<Iterable<key>, string>) => { -readonly [k in key]: t[k]; };
/**
* Creates a function that initializes an instance of a given class.
*
* @template k - The type of the class constructor.
* @param {k} klass - The class constructor to create an initializer for.
* @returns {(...params: ConstructorParameters<k>) => InstanceType<k>} A function that takes constructor parameters and returns a new instance of the class.
*
* @example
* class Person {
* constructor(public name: string, public age: number) {}
* }
*
* const initPerson = makeConstructor(Person);
* const john = initPerson('John', 30);
* console.log(john); // Person { name: 'John', age: 30 }
*/
declare const makeConstructor: <k extends klass$1>(klass: k) => (...params: ConstructorParameters<k>) => InstanceType<k>;
/**
* Redefines specified properties of an object as accessors (getters and setters).
* This function mutates the original object and returns it.
* The redefined properties will behave like normal properties when accessed or modified,
* but they will be implemented as getters and setters internally.
* This is useful for hiding properties from console.log, as they will be printed as [Getter/Setter].
*
* @template t - The type of the target object.
* @template key - The type of the keys to be redefined, must be a subset of keyof t.
* @param {t} target - The object whose properties are to be redefined.
* @param {Iterable<key>} props - An iterable of property names to be redefined as accessors.
* @returns {t} The modified target object.
*
* @example
* const obj = { a: 1, b: 2, c: 3 };
* redefineAsAccessors(obj, ['a', 'b']);
* console.log(obj); // Outputs: { a: [Getter/Setter], b: [Getter/Setter], c: 3 }
* obj.a = 4;
* console.log(obj.a); // Outputs: 4
* console.log(Object.getOwnPropertyDescriptor(obj, 'a')); // Shows getter and setter
*/
declare const redefineAsAccessors: <t, key extends keyof t>(target: t, props: Iterable<key>) => t;
/**
* Options for the lazy decorator.
* @typedef {Object} lazyOpts
* @property {boolean} - Whether to use a value property instead of a getter/setter.
* @property {boolean} - Whether the property is configurable.
* @property {boolean} - Whether the property is enumerable.
* @property {boolean} - Whether the property is writable.
*/
type lazyOpts = {
useValue?: boolean;
configurable?: boolean;
enumerable?: boolean;
writable?: boolean;
};
/**
* A decorator that lazily initializes a property.
* @param {lazyOpts} [opts] - Options for the lazy initialization.
* @returns {PropertyDecorator} A property decorator function.
*
* @example
* class Example {
* @lazy()
* get expensiveValue() {
* console.log('Calculating...');
* return 42;
* }
* }
*
* const instance = new Example();
* console.log(instance.expensiveValue); // Logs "Calculating..." then "42"
* console.log(instance.expensiveValue); // Logs "42" without recalculating
*/
declare const lazy: (opts?: lazyOpts) => (_target: any, propertyKey: PropertyKey, descriptor: PropertyDescriptor) => void;
declare const promiseObject: <shape extends {
[key: string]: Promise<any>;
}>(promiseShape: shape) => Promise<{ [k in keyof shape]: Awaited<shape[k]>; }>;
declare const Lib_klass: typeof klass;
declare const Lib_lazy: typeof lazy;
type Lib_lazyOpts = lazyOpts;
declare const Lib_makeConstructor: typeof makeConstructor;
declare const Lib_promiseObject: typeof promiseObject;
declare const Lib_redefineAsAccessors: typeof redefineAsAccessors;
declare const Lib_selectKeys: typeof selectKeys;
declare namespace Lib {
export { Lib_klass as klass, Lib_lazy as lazy, type Lib_lazyOpts as lazyOpts, Lib_makeConstructor as makeConstructor, Lib_promiseObject as promiseObject, Lib_redefineAsAccessors as redefineAsAccessors, Lib_selectKeys as selectKeys };
}
type params$3<data extends anyObject> = {
data: data;
hidden: readonly PropertyKey[];
};
type constructParams = {
target: anyObject;
data: anyObject;
hidden: readonly PropertyKey[];
};
/**
* Constructs an object by copying data and redefining hidden properties as accessors.
* @param params - The parameters for construction.
* @returns The constructed target object.
*/
declare const construct: (params: constructParams) => anyObject;
declare class BasicStrukt {
constructor(params: params$3<any>);
}
/**
* Base class for Strukt.
* @template args - The type of arguments.
* @template data - The type of data.
*/
declare class StruktBase<args extends anyArgs, data extends anyObject> extends BasicStrukt {
#private;
constructor(params: params$3<data>);
/**
* Gets the type of arguments.
* @returns The type of arguments.
*/
get $$argsT(): args;
/**
* Gets the first argument type.
* @returns The first argument type.
*/
get $$args1T(): args[0];
/**
* Gets the type of data returned by the constructor.
* @returns The type of data.
*/
get $$dataT(): data;
/**
* Retrieves the keys of the data returned by the constructor.
* @returns An array of data keys.
*/
$dataKeys(): (keyof data)[];
/**
* Creates a data object from the instance.
* @returns The data object.
*/
$data(): data;
/**
* Selects specific keys from the object.
* @param keys - The keys to select.
* @returns An object with the selected keys.
*/
$selectKeys<keys extends keyof this>(keys: keys[]): { -readonly [k in keys]: this[k]; };
/**
* Creates a clone of the current object.
* @returns A cloned instance of the object.
*/
$clone(): this;
/**
* Updates the object with a patch.
* @param patch - The patch to apply.
* @returns A new instance with the applied patch.
*/
$update(patch: Partial<typeof this.$$dataT>): this;
/**
* Applies a patch function to the object.
* @param fn - The function that returns a patch.
* @returns A new instance with the applied patch.
*/
$patch(fn: (data: this) => Partial<typeof this.$$dataT>): this;
/**
* Creates a new instance of the object.
* @param args - The arguments to pass to the constructor.
* @returns A new instance of the object.
*/
$create(...args: args): this;
}
type StruktBase$1_BasicStrukt = BasicStrukt;
declare const StruktBase$1_BasicStrukt: typeof BasicStrukt;
type StruktBase$1_StruktBase<args extends anyArgs, data extends anyObject> = StruktBase<args, data>;
declare const StruktBase$1_StruktBase: typeof StruktBase;
declare const StruktBase$1_construct: typeof construct;
type StruktBase$1_constructParams = constructParams;
declare namespace StruktBase$1 {
export { StruktBase$1_BasicStrukt as BasicStrukt, StruktBase$1_StruktBase as StruktBase, StruktBase$1_construct as construct, type StruktBase$1_constructParams as constructParams, type params$3 as params, StruktBase as t };
}
/**
* Parameters for initializing a Strukt.
* @template constructor - The constructor type.
* @property {constructor} constructor - The constructor function.
* @property {(keyof ReturnType<constructor>)[]} [hidden] - Optional keys to be hidden.
*/
type params$2<constructor extends anyConstructor> = {
constructor: constructor;
hidden?: (keyof ReturnType<constructor>)[];
};
type basicStruktClass<constructor extends anyConstructor> = {
new (...params: Parameters<constructor>): BasicStrukt & ReturnType<constructor>;
};
type struktClass<constructor extends anyConstructor> = {
new (...params: Parameters<constructor>): StruktBase<Parameters<constructor>, ReturnType<constructor>> & ReturnType<constructor>;
};
/**
* Initializes a Strukt class.
* @template constructor - The constructor type.
* @param {params<constructor>} params - The parameters for initialization.
* @returns {struktClass<constructor>} The initialized Strukt class.
* @example
* class MyClass extends init({
* constructor (args: { x: number, y: number }) {
* return {
* x: args.x,
* y: args.y,
* sum: args.x + args.y
* };
* },
* hidden: ['sum']
* }) {}
* const instance = new MyClass({ x: 1, y: 2 });
* console.log(instance); // Output: MyClass { x: 1, y: 2, sum: 3 }
*/
declare const init$3: <constructor extends anyConstructor>(params: params$2<constructor>) => struktClass<constructor>;
declare const initBasic: <constructor extends anyConstructor>(params: params$2<constructor>) => basicStruktClass<constructor>;
declare const isStrukt: (value: unknown) => value is StruktBase<any, any>;
declare const isBasicStrukt: (value: unknown) => value is BasicStrukt;
type Strukt_basicStruktClass<constructor extends anyConstructor> = basicStruktClass<constructor>;
declare const Strukt_initBasic: typeof initBasic;
declare const Strukt_isBasicStrukt: typeof isBasicStrukt;
declare const Strukt_isStrukt: typeof isStrukt;
type Strukt_struktClass<constructor extends anyConstructor> = struktClass<constructor>;
declare namespace Strukt {
export { type Strukt_basicStruktClass as basicStruktClass, init$3 as init, Strukt_initBasic as initBasic, Strukt_isBasicStrukt as isBasicStrukt, Strukt_isStrukt as isStrukt, type params$2 as params, type Strukt_struktClass as struktClass };
}
/**
* Represents metadata for an error, including optional message and cause.
*/
type errorMeta = Record<string, any> & {
message?: string;
cause?: any;
};
/**
* Base class for structured errors with metadata.
*/
declare class ErrorStruktBase extends Error {
message: string;
readonly meta: errorMeta;
constructor(msg: string, metaInput?: errorMeta);
}
type staticErrorInstance = ErrorStruktBase;
type staticErrorClass = {
new (meta?: errorMeta): staticErrorInstance;
new (message: string, meta?: errorMeta): staticErrorInstance;
};
/**
* Parameters for creating a static error.
*/
type staticParams = {
message?: string;
};
/**
* Creates a static error class with optional parameters.
* @param params - Optional parameters for the static error.
* @returns A class for creating static error instances.
* @example
* // Create a static error class with a default message
* class MyError extends staticError({ message: 'Default error message' }) {}
*
* // Create an instance of the error with a specific message
* const errorInstance = new MyError('Specific error message');
* console.log(errorInstance.message); // Output: 'Specific error message'
*
* // Create an instance with metadata
* const meta = { annotation: 'test' };
* const errorWithMeta = new MyError(meta);
* console.log(errorWithMeta.meta); // Output: { annotation: 'test' }
*/
declare const staticError: (params?: staticParams) => staticErrorClass;
/**
* Represents an instance of an error with data.
*/
type errorInstance<t> = staticErrorInstance & {
data: t;
};
type errorClass<constructor extends anyErrConstructor> = fnParam1<constructor> extends Exclude<fnParam1<constructor>, undefined> ? {
new (args: fnParam1<constructor>, meta?: errorMeta): errorInstance<constructorData<constructor>>;
} : {
new (args?: fnParam1<constructor>, meta?: errorMeta): errorInstance<constructorData<constructor>>;
};
/**
* Extracts the data type from a constructor function.
*/
type constructorData<fn extends anyErrConstructor> = ReturnType<fn>['data'];
/**
* Represents a constructor function for errors.
*/
type errConstructor<input, data> = (input: input) => {
data?: data;
message?: string;
};
type anyErrConstructor = errConstructor<any, any>;
/**
* Parameters for initializing an error class.
*/
type params$1<constructor extends anyErrConstructor> = {
constructor: constructor;
};
/**
* Initializes an error class with a given constructor.
* @param params - Parameters including the constructor function.
* @returns A class for creating error instances.
* @example
* // Define a constructor function for the error
* class MyError extends init({
* constructor(input: { value: number }) {
* return {
* data: { value: input.value, isEven: input.value % 2 === 0 },
* message: `Error with value ${input.value}`,
* };
* }
* }) {}
*
* // Create an instance of the error
* const errorInstance = new MyError({ value: 42 });
* console.log(errorInstance.message); // Output: 'Error with value 42'
* console.log(errorInstance.data); // Output: { value: 42, isEven: true }
*/
declare const init$2: <constructor extends anyErrConstructor>(params: params$1<constructor>) => errorClass<constructor>;
declare const isErrorStrukt: (value: unknown) => value is ErrorStruktBase;
type Error$1_ErrorStruktBase = ErrorStruktBase;
declare const Error$1_ErrorStruktBase: typeof ErrorStruktBase;
type Error$1_anyErrConstructor = anyErrConstructor;
type Error$1_constructorData<fn extends anyErrConstructor> = constructorData<fn>;
type Error$1_errConstructor<input, data> = errConstructor<input, data>;
type Error$1_errorClass<constructor extends anyErrConstructor> = errorClass<constructor>;
type Error$1_errorInstance<t> = errorInstance<t>;
type Error$1_errorMeta = errorMeta;
declare const Error$1_isErrorStrukt: typeof isErrorStrukt;
declare const Error$1_staticError: typeof staticError;
type Error$1_staticErrorClass = staticErrorClass;
type Error$1_staticErrorInstance = staticErrorInstance;
type Error$1_staticParams = staticParams;
declare namespace Error$1 {
export { Error$1_ErrorStruktBase as ErrorStruktBase, type Error$1_anyErrConstructor as anyErrConstructor, type Error$1_constructorData as constructorData, type Error$1_errConstructor as errConstructor, type Error$1_errorClass as errorClass, type Error$1_errorInstance as errorInstance, type Error$1_errorMeta as errorMeta, init$2 as init, Error$1_isErrorStrukt as isErrorStrukt, type params$1 as params, Error$1_staticError as staticError, type Error$1_staticErrorClass as staticErrorClass, type Error$1_staticErrorInstance as staticErrorInstance, type Error$1_staticParams as staticParams };
}
type callbackFn<data, result> = (value: data, ctx: SwitchContext) => result;
type predicateFn<data> = (value: data, ctx: SwitchContext) => boolean;
type errorCallback<t> = (params: {
target: t;
ctx: SwitchContext;
cause: Error;
}) => Error;
type typeofName = 'string' | 'number' | 'boolean' | 'symbol' | 'bigint';
type dispatcher = {
test: predicateFn<any>;
callback: callbackFn<any, any>;
};
type params<target, result> = {
target?: target;
ctxData?: any;
dispatchers?: Im.List<dispatcher>;
mapper?: callbackFn<result, any>;
};
declare const continueSymbol: unique symbol;
declare class SwitchContext {
data: any;
target: any;
constructor(params: {
data: any;
target: any;
});
continue<t>(): t;
}
/**
* A class that allows for conditional execution of callbacks based on various criteria.
* @template data - The type of data being tested.
* @template result - The type of result returned by the callbacks.
* @template notChecked - The type of data that has not been checked.
*/
declare class Switch<target, result = never, notChecked = target> {
#private;
target?: target;
constructor(params?: params<target, result>);
clone(): Switch<target, result, target>;
save(): (data: target, ctxData?: any) => result;
run(target?: target, ctxData?: any): result;
verify(..._notChecked: isNever<notChecked> extends true ? [] : [never, 'Not all cases are checked:', notChecked]): this;
saveStrict(..._notChecked: isNever<notChecked> extends true ? [] : [never, 'Not all cases are checked:', notChecked]): (data: target, ctxData?: any) => result;
runStrict(..._notChecked: isNever<notChecked> extends true ? [] : [never, 'Not all cases are checked:', notChecked]): result;
when<checked = never, res = result>(test: boolean | predicateFn<target>, callback: callbackFn<isNever<checked> extends true ? target : checked, res>): Switch<target, result | res, Exclude<notChecked, checked>>;
otherwise<res>(callback: callbackFn<notChecked, res>): Switch<target, result | res, never>;
otherwiseThrow(error?: Error | errorCallback<notChecked>): Switch<target, result, never>;
whenValue<val extends notChecked[], res>(value: val, callback: callbackFn<val[number], res>): Switch<target, result | res, Exclude<notChecked, val[number]>>;
whenValue<val extends notChecked, res>(value: val, callback: callbackFn<val, res>): Switch<target, result | res, Exclude<notChecked, val>>;
whenInstance<k extends anyClass, res>(klass: k, callback: callbackFn<InstanceType<k>, res>): Switch<target, result | res, Exclude<notChecked, InstanceType<k>>>;
whenInstance<k extends anyClass[], res>(klasses: k, callback: callbackFn<InstanceType<k[number]>, res>): Switch<target, result | res, Exclude<notChecked, InstanceType<k[number]>>>;
whenString<res>(callback: callbackFn<string, res>): Switch<target, result | res, Exclude<notChecked, string>>;
whenNumber<res>(callback: callbackFn<number, res>): Switch<target, result | res, Exclude<notChecked, number>>;
whenBoolean<res>(callback: callbackFn<boolean, res>): Switch<target, result | res, Exclude<notChecked, boolean>>;
whenSymbol<res>(callback: callbackFn<symbol, res>): Switch<target, result | res, Exclude<notChecked, symbol>>;
whenBigint<res>(callback: callbackFn<bigint, res>): Switch<target, result | res, Exclude<notChecked, bigint>>;
whenTypeOf<res>(type: 'string', callback: callbackFn<string, res>): Switch<target, result | res, Exclude<notChecked, string>>;
whenTypeOf<res>(type: 'number', callback: callbackFn<number, res>): Switch<target, result | res, Exclude<notChecked, number>>;
whenTypeOf<res>(type: 'boolean', callback: callbackFn<boolean, res>): Switch<target, result | res, Exclude<notChecked, boolean>>;
whenTypeOf<res>(type: 'symbol', callback: callbackFn<symbol, res>): Switch<target, result | res, Exclude<notChecked, symbol>>;
whenTypeOf<res>(type: 'bigint', callback: callbackFn<bigint, res>): Switch<target, result | res, Exclude<notChecked, bigint>>;
whenUndefined<res>(callback: callbackFn<undefined, res>): Switch<target, result | res, Exclude<notChecked, undefined>>;
whenNull<res>(callback: callbackFn<null, res>): Switch<target, result | res, Exclude<notChecked, null>>;
whenOptional<res>(callback: callbackFn<undefined | null, res>): Switch<any, any, any>;
whenTrue<res>(callback: callbackFn<true, res>): Switch<target, result | res, Exclude<notChecked, true>>;
whenFalse<res>(callback: callbackFn<false, res>): Switch<target, result | res, Exclude<notChecked, false>>;
map<res>(fn: callbackFn<result, res>): Switch<target, res, notChecked>;
}
declare const switchCase: <target>(target: target) => Switch<target, never, target>;
declare const compileSwitch: <target>() => Switch<target, never, target>;
declare const SwitchNoMatch_base: new (args: any, meta?: errorMeta) => errorInstance<{
value: any;
}>;
declare class SwitchNoMatch extends SwitchNoMatch_base {
}
type Switch$1_Switch<target, result = never, notChecked = target> = Switch<target, result, notChecked>;
declare const Switch$1_Switch: typeof Switch;
type Switch$1_SwitchContext = SwitchContext;
declare const Switch$1_SwitchContext: typeof SwitchContext;
type Switch$1_SwitchNoMatch = SwitchNoMatch;
declare const Switch$1_SwitchNoMatch: typeof SwitchNoMatch;
type Switch$1_callbackFn<data, result> = callbackFn<data, result>;
declare const Switch$1_compileSwitch: typeof compileSwitch;
declare const Switch$1_continueSymbol: typeof continueSymbol;
type Switch$1_dispatcher = dispatcher;
type Switch$1_errorCallback<t> = errorCallback<t>;
type Switch$1_params<target, result> = params<target, result>;
type Switch$1_predicateFn<data> = predicateFn<data>;
declare const Switch$1_switchCase: typeof switchCase;
type Switch$1_typeofName = typeofName;
declare namespace Switch$1 {
export { Switch$1_Switch as Switch, Switch$1_SwitchContext as SwitchContext, Switch$1_SwitchNoMatch as SwitchNoMatch, type Switch$1_callbackFn as callbackFn, Switch$1_compileSwitch as compileSwitch, Switch$1_continueSymbol as continueSymbol, type Switch$1_dispatcher as dispatcher, type Switch$1_errorCallback as errorCallback, type Switch$1_params as params, type Switch$1_predicateFn as predicateFn, Switch$1_switchCase as switchCase, Switch as t, type Switch$1_typeofName as typeofName };
}
type mapShape = {
[key: PropertyKey]: any;
};
type findFn<t> = (value: t) => boolean;
declare class DispatchMap<shape extends mapShape> {
#private;
constructor(shape: shape);
get shape(): shape;
get $$key(): keyof shape;
get $$value(): shape[keyof shape];
get $$entry(): {
key: keyof shape;
value: shape[keyof shape];
};
has(key: unknown): key is keyof shape;
keys(): (keyof shape)[];
values(): shape[keyof shape][];
entries(): {
key: keyof shape;
value: shape[keyof shape];
}[];
get<key extends keyof shape>(key: key): shape[key];
getSafe<key extends keyof shape, notSetValue = undefined>(key: key, notSetValue?: notSetValue): shape[key] | notSetValue;
index(key: PropertyKey): shape[keyof shape];
indexSafe<notSetValue = undefined>(key: PropertyKey, notSetValue?: notSetValue): shape[keyof shape] | notSetValue;
get reverse(): DispatchMap<{
[key in typeof this.$$value]: keyof shape;
}>;
reverseFind(fn: findFn<typeof this.$$entry>): keyof shape | undefined;
reverseFindOne(fn: findFn<typeof this.$$entry>): keyof shape;
reverseFindMany(fn: findFn<typeof this.$$entry>): (keyof shape)[];
}
declare const init$1: <shape extends mapShape>(shape: shape) => DispatchMap<shape>;
declare const DispatchMapKeyNotFound_base: new (args: {
key: unknown;
map: DispatchMap<any>;
}, meta?: errorMeta) => errorInstance<{
key: unknown;
map: DispatchMap<any>;
keys: (string | number | symbol)[];
}>;
declare class DispatchMapKeyNotFound extends DispatchMapKeyNotFound_base {
}
declare const DispatchMapSearchFailed_base: new (args: {
map: DispatchMap<any>;
}, meta?: errorMeta) => errorInstance<{
map: DispatchMap<any>;
}>;
declare class DispatchMapSearchFailed extends DispatchMapSearchFailed_base {
}
type DispatchMap$1_DispatchMap<shape extends mapShape> = DispatchMap<shape>;
declare const DispatchMap$1_DispatchMap: typeof DispatchMap;
type DispatchMap$1_DispatchMapKeyNotFound = DispatchMapKeyNotFound;
declare const DispatchMap$1_DispatchMapKeyNotFound: typeof DispatchMapKeyNotFound;
type DispatchMap$1_DispatchMapSearchFailed = DispatchMapSearchFailed;
declare const DispatchMap$1_DispatchMapSearchFailed: typeof DispatchMapSearchFailed;
type DispatchMap$1_findFn<t> = findFn<t>;
type DispatchMap$1_mapShape = mapShape;
declare namespace DispatchMap$1 {
export { DispatchMap$1_DispatchMap as DispatchMap, DispatchMap$1_DispatchMapKeyNotFound as DispatchMapKeyNotFound, DispatchMap$1_DispatchMapSearchFailed as DispatchMapSearchFailed, type DispatchMap$1_findFn as findFn, init$1 as init, type DispatchMap$1_mapShape as mapShape, DispatchMap as t };
}
type enumValue = string | number;
type enumValues = Iterable<enumValue>;
type enumStrukt<values extends enumValue> = Enum<values> & {
[key in values]: key;
};
declare class Enum<values extends enumValue> {
#private;
constructor(values: Iterable<values>);
get $$t(): values;
get $values(): Im.Set<values>;
[Symbol.iterator](): IterableIterator<values>;
$has(value: unknown): value is values;
$is<target extends values>(target: target, value: unknown): value is target;
$assert<subValues extends values>(value: unknown, subValues?: Iterable<subValues>): values;
$toArray(): values[];
$subEnum<subValues extends values>(subValues: Iterable<subValues>): enumStrukt<subValues>;
$add<newValues extends enumValue>(...values: newValues[]): enumStrukt<values | newValues>;
$remove<removedValues extends values>(...values: removedValues[]): enumStrukt<Exclude<values, removedValues>>;
$switchCase(value: values): Switch<values, never, values>;
$assertSwitchCase(value: unknown): Switch<values, never, values>;
$compileSwitch(): Switch<values, never, values>;
}
declare const init: <values extends enumValue>(values: Iterable<values>) => enumStrukt<values>;
declare const EnumAssertionError_base: new (args: {
target: unknown;
expected: enumValue[];
}, meta?: errorMeta) => errorInstance<{
target: unknown;
expected: enumValue[];
}>;
declare class EnumAssertionError extends EnumAssertionError_base {
}
type Enum$1_Enum<values extends enumValue> = Enum<values>;
declare const Enum$1_Enum: typeof Enum;
type Enum$1_EnumAssertionError = EnumAssertionError;
declare const Enum$1_EnumAssertionError: typeof EnumAssertionError;
type Enum$1_enumStrukt<values extends enumValue> = enumStrukt<values>;
type Enum$1_enumValue = enumValue;
type Enum$1_enumValues = enumValues;
declare const Enum$1_init: typeof init;
declare namespace Enum$1 {
export { Enum$1_Enum as Enum, Enum$1_EnumAssertionError as EnumAssertionError, type Enum$1_enumStrukt as enumStrukt, type Enum$1_enumValue as enumValue, type Enum$1_enumValues as enumValues, Enum$1_init as init, Enum as t };
}
type Errors_DispatchMapKeyNotFound = DispatchMapKeyNotFound;
declare const Errors_DispatchMapKeyNotFound: typeof DispatchMapKeyNotFound;
type Errors_EnumAssertionError = EnumAssertionError;
declare const Errors_EnumAssertionError: typeof EnumAssertionError;
type Errors_SwitchNoMatch = SwitchNoMatch;
declare const Errors_SwitchNoMatch: typeof SwitchNoMatch;
declare namespace Errors {
export { Errors_DispatchMapKeyNotFound as DispatchMapKeyNotFound, Errors_EnumAssertionError as EnumAssertionError, Errors_SwitchNoMatch as SwitchNoMatch };
}
/**
* Represents a list of keys used to access values in a FlatObject.
*/
type keys = Im.List<string>;
/**
* Represents an iterable of strings, excluding a single string.
* Used for specifying keys in a flexible manner.
*/
type keysLike = Exclude<Iterable<string>, string>;
/**
* Represents the index map of a FlatObject, mapping keys to values.
* @template t - The type of values stored in the index.
*/
type index<t> = Im.Map<keys, t>;
/**
* Converts a nested object into a FlatObject.
* @example
* const flat = fromObject({ a: { b: 1 } });
* console.log(flat.get(['a', 'b'])); // Outputs: 1
* @param obj - The object to convert.
* @returns A FlatObject representation of the input object.
*/
declare const fromObject: <t>(obj: Record<string, any>) => FlatObject<t>;
/**
* Creates a deep copy of an object using FlatObject.
* @example
* const original = { a: { b: 1 } };
* const copyObj = copy(original);
* console.log(copyObj); // Outputs: { a: { b: 1 } }
* @param obj - The object to copy.
* @returns A deep copy of the input object.
*/
declare const copy: <t extends Record<string, any>>(obj: t) => t;
/**
* Creates a new FlatObject instance from an index map.
* @param index - The index map for the FlatObject.
* @returns A new FlatObject instance.
*/
declare const flatObject: <t>(index?: index<t>) => FlatObject<t>;
declare const makeKeys: (keys: keysLike) => keys;
declare const isTrueObject: (x: unknown) => x is object;
/**
* Represents a flat structure of a nested object.
*/
declare class FlatObject<t = any> {
#private;
constructor(index: index<t>);
get index(): index<t>;
/**
* Retrieves a value from the FlatObject.
* @example
* const value = flatObject.get<number>(['a', 'b']);
* console.log(value); // Outputs: 1
* @param keys - The keys to retrieve the value for.
* @returns The value associated with the keys.
*/
get<k>(keys: keysLike): k;
/**
* Sets a value in the FlatObject and returns a new instance.
* @example
* const newFlat = flatObject.set(['a', 'b'], 2);
* console.log(newFlat.get(['a', 'b'])); // Outputs: 2
* @param keys - The keys to set the value for.
* @param value - The value to set.
* @returns A new FlatObject with the updated value.
*/
set(keys: keysLike, value: t): FlatObject<t>;
/**
* Sets a value in the FlatObject in place.
* @example
* flatObject.setMut(['a', 'b'], 2);
* console.log(flatObject.get(['a', 'b'])); // Outputs: 2
* @param keys - The keys to set the value for.
* @param value - The value to set.
* @returns The current FlatObject instance.
*/
setMut(keys: keysLike, value: t): this;
/**
* Checks if the FlatObject contains the specified keys.
* @example
* const exists = flatObject.has(['a', 'b']);
* console.log(exists); // Outputs: true or false
* @param keys - The keys to check for.
* @returns `true` if the keys exist, `false` otherwise.
*/
has(keys: keysLike): boolean;
/**
* Converts the FlatObject back to a regular JavaScript object.
* @example
* const obj = flatObject.toJS();
* console.log(obj); // Outputs: { a: { b: 1 } }
* @returns A JavaScript object representation of the FlatObject.
* @template r - The type of the resulting JavaScript object.
*/
toJS<r>(): r;
/**
* Transforms the FlatObject using a provided function.
* @example
* const transformed = flatObject.transform(index => index.map(value => value * 2));
* @param fn - The function to transform the index.
* @returns A new FlatObject with the transformed index.
*/
transform<r>(fn: (index: index<t>) => index<r>): FlatObject<r>;
/**
* Maps each value in the FlatObject to a new value.
* @example
* const mapped = flatObject.map<number>((value, keys) => value * 2);
* @param fn - The function to map each value.
* @returns A new FlatObject with mapped values.
*/
map<r>(fn: (value: t, keys: keys) => r): FlatObject<r>;
/**
* Filters the FlatObject based on a predicate function.
* @example
* const filtered = flatObject.filter((value, keys) => value > 1);
* console.log(filtered.toJS()); // Outputs only properties with values greater than 1
* @param fn - The predicate function to filter values. It receives the value and keys as arguments.
* @returns A new FlatObject with filtered values.
*/
filter(fn: (value: t, keys: keys) => boolean): FlatObject<t>;
/**
* Merges another FlatObject into this one.
* @example
* const merged = flatObject.merge(otherFlatObject);
* @param other - The other FlatObject to merge.
* @returns A new FlatObject with merged values.
*/
merge<r>(other: FlatObject<r>): FlatObject<t | r>;
keys(): Im.List<keys>;
values(): Im.List<t>;
entries(): Im.List<{
keys: keys;
value: t;
}>;
get size(): number;
isEmpty(): boolean;
isNotEmpty(): boolean;
}
type FlatObject$1_FlatObject<t = any> = FlatObject<t>;
declare const FlatObject$1_FlatObject: typeof FlatObject;
declare const FlatObject$1_copy: typeof copy;
declare const FlatObject$1_flatObject: typeof flatObject;
declare const FlatObject$1_fromObject: typeof fromObject;
type FlatObject$1_index<t> = index<t>;
declare const FlatObject$1_isTrueObject: typeof isTrueObject;
type FlatObject$1_keys = keys;
type FlatObject$1_keysLike = keysLike;
declare const FlatObject$1_makeKeys: typeof makeKeys;
declare namespace FlatObject$1 {
export { FlatObject$1_FlatObject as FlatObject, FlatObject$1_copy as copy, FlatObject$1_flatObject as flatObject, FlatObject$1_fromObject as fromObject, type FlatObject$1_index as index, FlatObject$1_isTrueObject as isTrueObject, type FlatObject$1_keys as keys, type FlatObject$1_keysLike as keysLike, FlatObject$1_makeKeys as makeKeys, FlatObject as t, fromObject as toFlatObject };
}
export { StruktBase as Base, BasicStrukt as Basic, DispatchMap$1 as DispatchMap, Enum$1 as Enum, Error$1 as Error, Errors, FlatObject$1 as FlatObject, Lib, Strukt, StruktBase$1 as StruktBase, Switch$1 as Switch, Types_d as Types, type classArgs, type classArgs1, type classArgsN, compileSwitch, copy, init$1 as dispatchMap, init$2 as error, type params$1 as errorParams, FlatObject as flatObject, init$3 as init, initBasic, init as initEnum, flatObject as initFlatObject, isBasicStrukt, isErrorStrukt, isStrukt, isTrueObject, klass, lazy, type lazyOpts, makeConstructor, type params$2 as params, promiseObject, redefineAsAccessors, selectKeys, staticError, switchCase, fromObject as toFlatObject };