Skip to content

Commit

Permalink
feat(core): adjust typings to support more custom type converter
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Dec 20, 2021
1 parent 961160c commit 6ef280f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
*/
export function applyTypeConverters(
mapping: Mapping,
typeConverters: WeakMap<
typeConverters: Map<
PrimitiveConstructorExtended,
WeakMap<PrimitiveConstructorExtended, ValueSelector | Converter>
Map<PrimitiveConstructorExtended, ValueSelector | Converter>
>
): void {
const [, , initializedProps] = mapping;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/create-mapper/create-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export function createMapper<TKey = unknown>({
const plugin = pluginInitializer(errorHandler);

// type converters
const typeConverters = new WeakMap<
const typeConverters = new Map<
PrimitiveConstructorExtended,
WeakMap<PrimitiveConstructorExtended, ValueSelector | Converter>
Map<PrimitiveConstructorExtended, ValueSelector | Converter>
>();

return {
Expand All @@ -50,7 +50,7 @@ export function createMapper<TKey = unknown>({

typeConverters.set(
source,
new WeakMap<PrimitiveConstructorExtended, ValueSelector | Converter>([
new Map<PrimitiveConstructorExtended, ValueSelector | Converter>([
[destination, converter],
])
);
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/lib/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TransformationType } from './enums';
import type { CreateMapOptions, MapArrayOptions, MapOptions } from './options';
import type {
Dictionary,
PrimitiveConstructor,
PrimitiveConstructorExtended,
PrimitiveExtended,
Selector,
Expand Down Expand Up @@ -161,7 +162,9 @@ export type MapInitializeReturn<

export type PrimitiveConstructorReturnType<
TType extends PrimitiveConstructorExtended
> = TType extends DateConstructor ? InstanceType<TType> : ReturnType<TType>;
> = TType extends DateConstructor | Exclude<TType, PrimitiveConstructor>
? InstanceType<TType>
: ReturnType<Extract<TType, PrimitiveConstructor>>;

export interface Mapper {
name: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/lib/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export type PrimitiveConstructor =

export type PrimitiveConstructorExtended =
| PrimitiveConstructor
| DateConstructor;
| DateConstructor
| (new (...args: any[]) => any);

export interface Selector<
TObject extends Dictionary<TObject> = any,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/utils/is-date-constructor.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* @param {Function} value
*/
export function isDateConstructor(value: unknown): boolean {
return value === Date;
return Object.getPrototypeOf(value) === Date || value === Date;
}
9 changes: 8 additions & 1 deletion packages/core/src/lib/utils/is-primitive-constructor.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
* @param {Function} value
*/
export function isPrimitiveConstructor(value: unknown): boolean {
const proto = Object.getPrototypeOf(value);
return (
value === String || value === Number || value === Boolean || value === Array
proto === String ||
proto === Number ||
proto === Boolean ||
value === String ||
value === Number ||
value === Boolean ||
value === Array
);
}

0 comments on commit 6ef280f

Please sign in to comment.