@avandar/models 0.1.1 → 0.2.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.
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Model/matchModel/matchModel.ts","../src/Model/ModelModule/ModelModule.ts","../src/Model/Model.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"sources":["../src/Model/matchModel/matchModel.ts","../src/Model/ModelModule/ModelModule.ts","../src/Model/Model.ts"],"names":[],"mappings":";AA2CO,SAAS,UAAA,CAGd,OAAU,GAAA,EAAqD;AAC/D,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA;AACpB,EAAA,IAAI,KAAA,IAAS,GAAA,IAAO,GAAA,CAAI,KAAK,MAAM,MAAA,EAAW;AAC5C,IAAA,IAAI,OAAO,GAAA,CAAI,KAAK,CAAA,KAAM,UAAA,EAAY;AACpC,MAAA,OAAO,GAAA,CAAI,KAAK,CAAA,CAAE,KAAK,CAAA;AAAA,IACzB;AACA,IAAA,OAAO,IAAI,KAAK,CAAA;AAAA,EAClB;AACA,EAAA,MAAM,IAAI,KAAA,CAAM,CAAA,+BAAA,EAAkC,KAAK,CAAA,CAAE,CAAA;AAC3D;;;AC2BO,IAAM,WAAA,GAA4B;AAAA,EACvC,IAAA,EAAM,CAIJ,SAAA,EACA,UAAA,KAC6B;AAC7B,IAAA,OAAO;AAAA,MACL,MAAA,EAAQ,SAAA;AAAA,MACR,GAAG;AAAA,KACL;AAAA,EACF,CAAA;AAAA,EAEA,KAAA,EAAO,UAAA;AAAA,EAEP,UAAA,EAAY,CACV,KAAA,KACoB;AACpB,IAAA,OAAO;AAAA,MACL,QAAQ,KAAA,CAAM,MAAA;AAAA,MACd,IAAI,KAAA,CAAM;AAAA,KACZ;AAAA,EACF,CAAA;AAAA,EAEA,OAAA,EAAS,CAAC,GAAA,KAAmC;AAC3C,IAAA,OACE,OAAO,QAAQ,QAAA,IACf,GAAA,KAAQ,QACR,QAAA,IAAY,GAAA,IACZ,OAAO,GAAA,CAAI,MAAA,KAAW,QAAA;AAAA,EAE1B,CAAA;AAAA,EAEA,aAAA,EAAe,CACb,GAAA,EACA,SAAA,KACyC;AACzC,IAAA,OACE,OAAO,GAAA,KAAQ,QAAA,IACf,GAAA,KAAQ,QACR,QAAA,IAAY,GAAA,IACZ,OAAO,GAAA,CAAI,MAAA,KAAW,QAAA,KACrB,SAAA,KAAc,MAAA,IAAa,IAAI,MAAA,KAAW,SAAA,CAAA;AAAA,EAE/C,CAAA;AAAA,EAEA,gBAAA,EAAkB,CAChB,SAAA,KAG0C;AAC1C,IAAA,OAAO,CAAC,CAAA,KAAM;AACZ,MAAA,OAAO,WAAA,CAAY,aAAA,CAAc,CAAA,EAAG,SAAS,CAAA;AAAA,IAC/C,CAAA;AAAA,EACF;AACF,CAAA;;;AChIO,IAAM,KAAA,GAAQ","file":"index.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { ModelBase, ModelType } from \"@models/Model/Model.types.ts\";\nimport type { UnionToIntersection } from \"type-fest\";\n\ntype ValidMatchedValues<M extends ModelBase> =\n | ((model: M) => unknown)\n | string\n | number\n | boolean\n // Spelled out rather than imported as `UnknownObject` from `@avandar/utils`.\n // Pulling that one alias in from the utils barrel degraded inference here:\n // the handler parameters in `matchModel(x, { User: (model) => ... })` fell\n // back to `any`. This is the exact definition of `UnknownObject`, and the\n // type is used once, so the import bought nothing.\n | Record<PropertyKey, unknown>;\n\ntype ValidValueRecord<M extends ModelBase> = UnionToIntersection<\n M extends any\n ? {\n [Mod in M as ModelType<Mod>]: ValidMatchedValues<Mod>;\n }\n : never\n>;\n\ntype MappedValue<\n M extends ModelBase,\n FunctionRecord extends ValidValueRecord<M>,\n> = M extends any\n ? ModelType<M> extends keyof FunctionRecord\n ? FunctionRecord[ModelType<M>] extends (model: M) => infer R\n ? R\n : FunctionRecord[ModelType<M>]\n : never\n : never;\n\nexport type MatchModelFn = <\n M extends ModelBase,\n FunctionRecord extends ValidValueRecord<M>,\n>(\n model: M,\n fns: FunctionRecord,\n) => MappedValue<M, FunctionRecord>;\n\nexport function matchModel<\n M extends ModelBase,\n FunctionRecord extends ValidValueRecord<M>,\n>(model: M, fns: FunctionRecord): MappedValue<M, FunctionRecord> {\n const mType = model.__type;\n if (mType in fns && fns[mType] !== undefined) {\n if (typeof fns[mType] === \"function\") {\n return fns[mType](model) as MappedValue<M, FunctionRecord>;\n }\n return fns[mType] as MappedValue<M, FunctionRecord>;\n }\n throw new Error(`No match found for model type: ${mType}`);\n}\n","import { matchModel } from \"@models/Model/matchModel/matchModel.ts\";\nimport type { MatchModelFn } from \"@models/Model/matchModel/matchModel.ts\";\nimport type { ModelBase, ModelTypedId } from \"@models/Model/Model.types.ts\";\nimport type { EmptyObject, Simplify } from \"type-fest\";\n\nexport type IModelModule = {\n /**\n * Make a new model instance.\n * @param modelType - The type of the model to make.\n * @param modelProps - The properties of the model to make.\n * @returns The model.\n */\n make: <\n MType extends string,\n const MProps extends Record<string, unknown> = EmptyObject,\n >(\n modelType: MType,\n modelProps: MProps,\n ) => ModelBase<MType, MProps>;\n\n /**\n * Match a model instance to a function based on its type.\n * This is useful for when you have a union of model types and want to do\n * something different depending on the type of the model.\n *\n * Example:\n * ```ts\n * // assume we have received a model which could be either User or Admin\n * // which are each of type Model<\"User\"> and Model<\"Admin\"> respectively\n * const model: User | Admin;\n *\n * const result = Models.match(model, {\n * User: (model) => doSomething();\n * Admin: (model) => doSomethingElse();\n * });\n * ```\n * @param model - The model instance to pattern match.\n * @param fns - A record of functions, mapping a model type to a function.\n * @returns The result of the function that matched the model instance.\n */\n match: MatchModelFn;\n\n /**\n * Get the typed id of a model (i.e. an object that couples the model id\n * with its model type).\n *\n * @param model - The model to get the typed id of.\n * @returns The typed id of the model.\n */\n getTypedId: <M extends ModelBase<string> & { id: unknown }>(\n model: M,\n ) => Simplify<ModelTypedId<M>>;\n\n /**\n * Checks if a value is a model object of any type.\n * @param val - The value to check.\n * @returns `true` if the value is a model, `false` otherwise.\n */\n isModel: (val: unknown) => val is ModelBase;\n\n /**\n * Checks if a value is a model of the given type.\n * @param val - The value to check.\n * @param modelType - The model type to check.\n */\n isOfModelType: <T extends string>(\n val: unknown,\n modelType: T,\n ) => val is ModelBase<T>;\n\n /**\n * Returns a function that checks if a value is a model of the given type.\n * @param modelType - The model type to check.\n * @returns A function that checks if a value is a model of the given type.\n */\n valIsOfModelType: <MType extends string>(\n modelType: MType,\n ) => <MaybeModel extends ModelBase<string>>(\n v: MaybeModel | null | undefined,\n ) => v is MaybeModel & ModelBase<MType>;\n};\n\nexport const ModelModule: IModelModule = {\n make: <\n MType extends string,\n MProps extends Record<string, unknown> = EmptyObject,\n >(\n modelType: MType,\n modelProps: MProps,\n ): ModelBase<MType, MProps> => {\n return {\n __type: modelType,\n ...modelProps,\n } as ModelBase<MType, MProps>;\n },\n\n match: matchModel,\n\n getTypedId: <M extends ModelBase<string> & { id: unknown }>(\n model: M,\n ): ModelTypedId<M> => {\n return {\n __type: model.__type,\n id: model.id,\n } as ModelTypedId<M>;\n },\n\n isModel: (val: unknown): val is ModelBase => {\n return (\n typeof val === \"object\" &&\n val !== null &&\n \"__type\" in val &&\n typeof val.__type === \"string\"\n );\n },\n\n isOfModelType: <MType extends string, MaybeModel>(\n val: MaybeModel,\n modelType: MType,\n ): val is MaybeModel & ModelBase<MType> => {\n return (\n typeof val === \"object\" &&\n val !== null &&\n \"__type\" in val &&\n typeof val.__type === \"string\" &&\n (modelType === undefined || val.__type === modelType)\n );\n },\n\n valIsOfModelType: <MType extends string>(\n modelType: MType,\n ): (<MaybeModel extends ModelBase<string>>(\n v: MaybeModel | null | undefined,\n ) => v is MaybeModel & ModelBase<MType>) => {\n return (v) => {\n return ModelModule.isOfModelType(v, modelType);\n };\n },\n};\n","/* eslint-disable @typescript-eslint/no-namespace,import-x/export */\nimport { ModelModule } from \"@models/Model/ModelModule/ModelModule.ts\";\nimport type {\n ModelBase,\n ModelType,\n ModelTypedId,\n ModelVersioned,\n} from \"@models/Model/Model.types.ts\";\nimport type { EmptyObject } from \"type-fest\";\n\nexport const Model = ModelModule;\n\nexport namespace Model {\n export type Base<\n MType extends string = string,\n MProps extends Record<string, unknown> = EmptyObject,\n > = ModelBase<MType, MProps>;\n\n /**\n * A model that has a version field.\n */\n export type Versioned<\n MType extends string = string,\n Version extends number = number,\n MProps extends Record<string, unknown> = EmptyObject,\n > = ModelVersioned<MType, Version, MProps>;\n\n /** Utility type: gets the string type of a model. */\n export type Type<M extends Base<string>> = ModelType<M>;\n\n /**\n * Get the `id` type of a model coupled with the model type key.\n *\n * Example:\n * ```ts\n * type MyType = TypedId<Model<\"User\">>;\n * // TypedId = { __type: \"User\", id: UserId }\n * ```\n */\n export type TypedId<M extends Base<string> & { id: unknown }> =\n ModelTypedId<M>;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avandar/models",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Core model primitives for typed, discriminated data objects",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"type-fest": "^5.4.4",
|
|
32
|
-
"@avandar/utils": "0.
|
|
32
|
+
"@avandar/utils": "0.2.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"zod": "^4.1.13"
|