File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
TypeScriptBasic/0x_Decorators Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ import { StringUtility } from "./Decorators" ;
2+
3+ test ( "[TypeScriptBasic/0x_Decorators] Pengecekan Class Decorator" , ( ) => {
4+ expect ( new StringUtility ( [ "Test" ] ) . data [ 0 ] ) . toBe ( "Teks pengganti argumen pertama" ) ;
5+ } )
6+
7+ test ( "[TypeScriptBasic/0x_Decorators] Pengecekan Property Decorator" , ( ) => {
8+ // @ts -expect-error
9+ expect ( new StringUtility ( "abc" ) ) . toThrowError ( ) ;
10+ } )
11+
12+ test ( "[TypeScriptBasic/0x_Decorators] Pengecekan Method Decorator" , ( ) => {
13+ expect ( StringUtility . prototype . sambungData = ( ) => { return "" } ) . toThrowError ( ) ;
14+ } )
Original file line number Diff line number Diff line change 1+ // Penjelasan akan ditulis nanti.
2+
3+ function gantiArgumenConstructor ( constructor : typeof StringUtility ) : any {
4+ return class extends constructor {
5+ constructor ( args : string [ ] ) {
6+ super ( [ "Teks pengganti argumen pertama" , ...args ] ) ;
7+ }
8+ }
9+ }
10+
11+ function buatMethodTidakDapatDiubah ( target : any , key : string , descriptor : PropertyDescriptor ) {
12+ descriptor . writable = false ;
13+ }
14+
15+ function harusBerupaArray ( target : any , key : string ) {
16+ if ( ! Array . isArray ( target [ key ] ) ) {
17+ throw new Error ( `${ key } harus berupa array` ) ;
18+ }
19+ }
20+
21+ @gantiArgumenConstructor
22+ class StringUtility {
23+ @harusBerupaArray
24+ public data : string [ ] ;
25+
26+ constructor ( data : string [ ] ) {
27+ this . data = data ;
28+ }
29+
30+ @buatMethodTidakDapatDiubah
31+ sambungData ( separator : string ) : string {
32+ return this . data . join ( separator ) ;
33+ }
34+
35+ // Bisa digunakan juga pada accessor property
36+ @buatMethodTidakDapatDiubah
37+ get dataKapital ( ) : string [ ] {
38+ return this . data . map ( data => data . toUpperCase ( ) ) ;
39+ }
40+ }
41+
42+ export {
43+ StringUtility
44+ }
You can’t perform that action at this time.
0 commit comments