Skip to content

Commit

Permalink
feat(core/transform): adds asImplementation and asDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 15, 2019
1 parent e434dbb commit 62390ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/transform/as.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CollectionTree, CollectionTreeImplementation, Service } from '~/types';
import { replace } from './replace';
import { isElementService, isServiceImplementation } from '~/inspect';

export function asImplementation<T extends CollectionTree>(
collection: T,
fn: (service: Service, data: { path: string[]; route: string[] }) => Service
): T & CollectionTreeImplementation {
return replace(collection, (element, next, data) => {
element = next(element);
return isElementService(element) ? fn(element, data) : element;
}) as T & CollectionTreeImplementation;
}

export function asDeclaration(collection: CollectionTree): CollectionTree {
return replace(collection, (element, next) => {
element = next(element);

if (isElementService(element) && isServiceImplementation(element)) {
const { resolve, ...service } = element;
return service;
}

return element;
});
}
1 change: 1 addition & 0 deletions packages/core/src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './replace';
export * from './normalize';
export * from './as';
export * from './routes';

0 comments on commit 62390ad

Please sign in to comment.