File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , '__esModule' , { value : true } ) ;
4+
5+ // Value Object
6+
7+ class UserName {
8+ value ;
9+ constructor ( value ) {
10+ this . value = value ;
11+ if ( typeof value !== 'string' ) {
12+ throw new Error ( 'Name expected to be string' ) ;
13+ }
14+ if ( value . length < 2 ) {
15+ throw new Error ( 'Name is too short' ) ;
16+ }
17+ if ( value . indexOf ( ' ' ) === - 1 ) {
18+ throw new Error ( 'Name is too simple' ) ;
19+ }
20+ }
21+ }
22+
23+ // Usage
24+
25+ try {
26+ const name1 = new UserName ( 'Marcus Aurelius' ) ;
27+ console . log ( { name1 } ) ;
28+ } catch ( error ) {
29+ console . error ( error ) ;
30+ }
31+
32+ try {
33+ const name2 = new UserName ( 'Marcus' ) ;
34+ } catch ( error ) {
35+ console . error ( error ) ;
36+ }
37+
38+ try {
39+ const name3 = new UserName ( 'M' ) ;
40+ } catch ( error ) {
41+ console . error ( error ) ;
42+ }
Original file line number Diff line number Diff line change 1+ // Value Object
2+
3+ class UserName {
4+ constructor ( public value : string ) {
5+ if ( typeof value !== 'string' ) {
6+ throw new Error ( 'Name expected to be string' ) ;
7+ }
8+ if ( value . length < 2 ) {
9+ throw new Error ( 'Name is too short' ) ;
10+ }
11+ if ( value . indexOf ( ' ' ) === - 1 ) {
12+ throw new Error ( 'Name is too simple' ) ;
13+ }
14+ }
15+ }
16+
17+ // Usage
18+
19+ try {
20+ const name1 = new UserName ( 'Marcus Aurelius' ) ;
21+ console . log ( { name1 } ) ;
22+ } catch ( error ) {
23+ console . error ( error ) ;
24+ }
25+
26+ try {
27+ const name2 = new UserName ( 'Marcus' ) ;
28+ } catch ( error ) {
29+ console . error ( error ) ;
30+ }
31+
32+ try {
33+ const name3 = new UserName ( 'M' ) ;
34+ } catch ( error ) {
35+ console . error ( error ) ;
36+ }
You can’t perform that action at this time.
0 commit comments