Skip to content

Commit

Permalink
feat(serializer): supports serialization for class with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Oct 2, 2024
1 parent 087351f commit 11cab93
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/serializer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,32 @@ export function isRootNode<T>(data: unknown): data is RootNode<T> {
);
}

export function extractClassName(value: unknown): string {
if (R.isObjectType(value) && "constructor" in value) {
return value.constructor.name;
}
if (R.isFunction(value)) {
return value.name || value.constructor?.name || Function.name;
}
return extractClassName(primitiveToSerializableClass(value));
}
export const extractClassName = (() => {
const registry = new Map<string, unknown[]>();
const register = (name: string, factory: any) => {
if (!registry.has(name)) {
registry.set(name, []);
}
const target = registry.get(name)!;
let index = target.indexOf(factory);
if (index === -1) {
index = target.push(factory) - 1;
}
return [name, index].filter(Boolean).join("");
};

return (value: unknown): string => {
if (R.isObjectType(value) && "constructor" in value) {
const name = value.constructor.name;
return register(name, value.constructor);
}
if (R.isFunction(value)) {
const name = value.name || value.constructor?.name || Function.name;
return register(name, value);
}
return extractClassName(primitiveToSerializableClass(value));
};
})();

const ClassByValueType = {
string: String,
Expand Down

0 comments on commit 11cab93

Please sign in to comment.