Skip to content

Commit 9b818e6

Browse files
committed
fix: export Mapper interface allowing to export mapper functions
1 parent 0dd99ce commit 9b818e6

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/morphism.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,16 @@ interface Constructable<T> {
213213
new (...args: any[]): T;
214214
}
215215

216-
interface Mapper<Target> {
216+
export interface Mapper<Target> {
217217
<Source>(source: Source[]): Target[];
218218
<Source>(source: Source): Target;
219219
}
220220

221221
function transformItems<T, TSchema extends Schema<T>>(schema: TSchema): Mapper<{ [P in keyof TSchema]: any }>;
222-
function transformItems<T, TSchema extends Schema<T>>(schema: TSchema, type: Constructable<T>): Mapper<{ [P in keyof TSchema]: any }>;
222+
function transformItems<T, TSchema extends Schema<T>>(
223+
schema: TSchema,
224+
type: Constructable<T>
225+
): Mapper<{ [P in keyof TSchema]: any }>;
223226

224227
function transformItems<T, TSchema extends Schema<T>>(schema: TSchema, type?: Constructable<T>) {
225228
function mapper(source: any): any {
@@ -293,8 +296,16 @@ export function morphism<Target>(schema: Schema<Target>): Mapper<Target>; // mor
293296
export function morphism(schema: Schema<any>, items: null): undefined; // Reflects a specific use case where Morphism({}, null) return undefined
294297
export function morphism(schema: null, items: {}): undefined; // Reflects a specific use case where Morphism(null, {}) return undefined
295298

296-
export function morphism<Target, Source>(schema: Schema<Target>, items: null, type: Constructable<Target>): Mapper<Target>; // morphism({}, null, T) => mapper(S) => T
297-
export function morphism<Target, Source>(schema: Schema<Target>, items: Source[], type: Constructable<Target>): Target[]; // morphism({}, [], T) => T[]
299+
export function morphism<Target, Source>(
300+
schema: Schema<Target>,
301+
items: null,
302+
type: Constructable<Target>
303+
): Mapper<Target>; // morphism({}, null, T) => mapper(S) => T
304+
export function morphism<Target, Source>(
305+
schema: Schema<Target>,
306+
items: Source[],
307+
type: Constructable<Target>
308+
): Target[]; // morphism({}, [], T) => T[]
298309
export function morphism<Target, Source>(schema: Schema<Target>, items: Source, type: Constructable<Target>): Target; // morphism({}, {}, T) => T
299310

300311
export function morphism<Target, Source>(schema: Schema<Target>, items?: Source, type?: Constructable<Target>) {
@@ -439,7 +450,9 @@ export class MorphismRegistry implements IMorphismRegistry {
439450
if (!schema) {
440451
throw new Error(`The schema must be an Object. Found ${schema}`);
441452
} else if (!this.exists(type)) {
442-
throw new Error(`The type ${type.name} is not registered. Register it using \`Mophism.register(${type.name}, schema)\``);
453+
throw new Error(
454+
`The type ${type.name} is not registered. Register it using \`Mophism.register(${type.name}, schema)\``
455+
);
443456
} else {
444457
let fn = morphism(schema, null, type);
445458
this._registry.cache.set(type, fn);

0 commit comments

Comments
 (0)