-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Entity): receive Props instead of id
BREAKING CHANGE: This change require props object always on Entity #4
- Loading branch information
Showing
13 changed files
with
270 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
// MIT © 2017 azu | ||
import { Identifier } from "./Identifier"; | ||
import { EntityLike, EntityLikeProps } from "./EntityLike"; | ||
|
||
export abstract class Entity<Id extends Identifier<any>> { | ||
public readonly id: Id; | ||
export const isEntity = (v: any): v is Entity<any> => { | ||
return v instanceof Entity; | ||
}; | ||
|
||
constructor(id: Id) { | ||
this.id = id; | ||
export class Entity<Props extends EntityLikeProps<Identifier<any>>> implements EntityLike<Props> { | ||
props: Props; | ||
|
||
constructor(props: Props) { | ||
this.props = props; | ||
} | ||
|
||
/** | ||
/* | ||
* Check equality by identifier | ||
*/ | ||
equals(object?: Entity<Id>): boolean { | ||
equals(object?: Entity<Props>): boolean { | ||
if (object == null || object == undefined) { | ||
return false; | ||
} | ||
if (this === object) { | ||
return true; | ||
} | ||
return this.id.equals(object.id); | ||
if (!isEntity(object)) { | ||
return false; | ||
} | ||
return this.props.id.equals(object.props.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ImmutableEntity } from "./immutable/ImmutableEntity"; | ||
import { Identifier } from "./Identifier"; | ||
|
||
export const isEntityLike = (entity: any): entity is EntityLike<any> => { | ||
if (!entity) { | ||
return false; | ||
} | ||
return entity instanceof ImmutableEntity; | ||
}; | ||
|
||
export interface EntityLikeProps<Id extends Identifier<any>> { | ||
id: Id; | ||
} | ||
|
||
export interface EntityLike<Props extends EntityLikeProps<Identifier<any>>> { | ||
props: Props; | ||
|
||
equals(object?: EntityLike<Props>): boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// MIT © 2017 azu | ||
import { Identifier } from "../Identifier"; | ||
import { EntityLike, EntityLikeProps } from "../EntityLike"; | ||
|
||
export const isImmutableEntity = (entity: any): entity is ImmutableEntity<{ id: Identifier<any> }> => { | ||
if (!entity) { | ||
return false; | ||
} | ||
return entity instanceof ImmutableEntity; | ||
}; | ||
|
||
/** | ||
* ImmutableEntity is readonly Entity. | ||
* It is created with Props and It has `props` property. | ||
* This entity has received through constructor. | ||
* | ||
* @see https://www.quora.com/Why-did-React-use-props-as-an-abbreviation-for-property-properties | ||
*/ | ||
export class ImmutableEntity<Props extends EntityLikeProps<Identifier<any>>> implements EntityLike<Props> { | ||
props: Readonly<Props>; | ||
|
||
constructor(props: Props) { | ||
this.props = Object.freeze(props); | ||
} | ||
|
||
/* | ||
* Check equality by identifier | ||
*/ | ||
equals(object?: ImmutableEntity<Props>): boolean { | ||
if (object == null || object == undefined) { | ||
return false; | ||
} | ||
if (this === object) { | ||
return true; | ||
} | ||
if (!isImmutableEntity(object)) { | ||
return false; | ||
} | ||
return this.props.id.equals(object.props.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { shallowEqual } from "shallow-equal-object"; | ||
|
||
export class ImmutableValueObject<Props extends object> { | ||
props: Readonly<Props>; | ||
constructor(props: Props) { | ||
this.props = Object.freeze(props); | ||
} | ||
/** | ||
* Check equality by shallow equals of properties. | ||
* It can be override. | ||
*/ | ||
equals(object?: ImmutableValueObject<{}>): boolean { | ||
return shallowEqual(this, object); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.