Skip to content

Commit

Permalink
feat(createentityfeature): make entity names optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed Mar 29, 2022
1 parent 2af3771 commit 2952bf1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
6 changes: 0 additions & 6 deletions libs/ngrx-traits/src/lib/create-entity-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ export interface ProductFilter {
search: string;
}

export interface ProductDetail extends Product {
maker: string;
releaseDate: string;
image: string;
}

const clientsFeatureFactory = createEntityFeatureFactory(
{ entityName: 'client', entitiesName: 'clients' },
addLoadEntitiesTrait<Client>(),
Expand Down
38 changes: 36 additions & 2 deletions libs/ngrx-traits/src/lib/create-entity-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ export function createTraitFactory<
}): TraitFactory<State, A, S, M, KEY, C, KC> {
return f as TraitFactory<State, A, S, M, KEY, C, KC>;
}

export function createEntityFeatureFactory<F extends readonly TraitFactory[]>(
...traits: F
): EntityFeatureFactory<
'Entity',
'Entities',
ExtractStateType<F>,
ExtractActionsType<F>,
ExtractSelectorsType<F>
>;
export function createEntityFeatureFactory<
F extends readonly TraitFactory[],
EntityName extends string,
Expand All @@ -71,14 +79,40 @@ export function createEntityFeatureFactory<
ExtractStateType<F>,
ExtractActionsType<F>,
ExtractSelectorsType<F>
>;
export function createEntityFeatureFactory<
F extends readonly TraitFactory[],
EntityName extends string,
EntitiesName extends string = `${EntityName}s`
>(
namesOrFactory:
| { entityName: EntityName; entitiesName?: EntitiesName }
| TraitFactory,
...traits: F
): EntityFeatureFactory<
EntityName,
EntitiesName,
ExtractStateType<F>,
ExtractActionsType<F>,
ExtractSelectorsType<F>
> {
return ((config: Config<any, any>) => {
const { entityName, entitiesName } =
'entityName' in namesOrFactory
? (namesOrFactory as {
entityName: EntityName;
entitiesName?: EntitiesName;
})
: { entityName: 'Entity', entitiesName: 'Entities' };

const singular = capitalize(entityName);
const plural = entitiesName
? capitalize(entitiesName)
: capitalize(entityName + 's');

const sortedTraits = sortTraits([...traits]);
const sortedTraits = sortTraits(
'entityName' in namesOrFactory ? [...traits] : [namesOrFactory, ...traits]
);

const allConfigs = buildAllConfigs(sortedTraits);

Expand Down

0 comments on commit 2952bf1

Please sign in to comment.