Browse Source

refactor: make importRef to interface mapper separate fn

pull/5137/head
Arman Ozak 6 years ago
parent
commit
90ad89c6d6
  1. 42
      npm/ng-packs/packages/schematics/src/utils/model.ts

42
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -4,7 +4,7 @@ import { parseNamespace } from './namespace';
import { createTypeSimplifier } from './type'; import { createTypeSimplifier } from './type';
export function createImportRefsToModelMapper(solution: string, types: Record<string, Type>) { export function createImportRefsToModelMapper(solution: string, types: Record<string, Type>) {
const simplifyType = createTypeSimplifier(solution); const mapImportRefToInterface = createImportRefToInterfaceMapper(solution, types);
return (importRefs: string[]) => { return (importRefs: string[]) => {
const model = new Model({ const model = new Model({
@ -12,28 +12,36 @@ export function createImportRefsToModelMapper(solution: string, types: Record<st
}); });
importRefs.forEach(ref => { importRefs.forEach(ref => {
const typeDef = types[ref]; model.interfaces.push(mapImportRefToInterface(ref));
let identifier = simplifyType(ref); });
(typeDef.genericArguments ?? []).forEach((t, i) => {
identifier = identifier.replace(`T${i}`, t); model.interfaces.sort((a, b) => (a.identifier > b.identifier ? 1 : -1));
});
return model;
};
}
const base = typeDef.baseType ? simplifyType(typeDef.baseType) : null; export function createImportRefToInterfaceMapper(solution: string, types: Record<string, Type>) {
const _interface = new Interface({ identifier, base }); const simplifyType = createTypeSimplifier(solution);
typeDef.properties?.forEach(({ name, typeSimple }) => { return (ref: string) => {
name = strings.camelize(name); const typeDef = types[ref];
const type = simplifyType(typeSimple); let identifier = simplifyType(ref);
const optional = typeSimple.endsWith('?') ? '?' : ''; (typeDef.genericArguments ?? []).forEach((t, i) => {
identifier = identifier.replace(`T${i}`, t);
});
_interface.properties.push(new Property({ name, type, optional })); const base = typeDef.baseType ? simplifyType(typeDef.baseType) : null;
}); const _interface = new Interface({ identifier, base });
console.log(_interface); typeDef.properties?.forEach(({ name, typeSimple }) => {
name = strings.camelize(name);
const optional = typeSimple.endsWith('?') ? '?' : '';
const type = simplifyType(typeSimple);
model.interfaces.push(_interface); _interface.properties.push(new Property({ name, optional, type }));
}); });
return model; return _interface;
}; };
} }

Loading…
Cancel
Save