Skip to content

Instantly share code, notes, and snippets.

@minhntm
Created November 27, 2020 13:29
Show Gist options
  • Save minhntm/48ff80c6c03958cedf6c7f833ca73ef7 to your computer and use it in GitHub Desktop.
Save minhntm/48ff80c6c03958cedf6c7f833ca73ef7 to your computer and use it in GitHub Desktop.
function uppercase( target: any, name: string, desc: PropertyDescriptor ) {
return {
get: function (): string {
return desc.get?.call( this ).toUpperCase();
},
set: function ( name: string ) {
desc.set?.call( this, name.split(' ') );
}
};
}
function capitalize(target: any, name: string, desc: PropertyDescriptor){
return {
value: function (): string {
const str = desc.value.call(this);
return str[0].toUpperCase() + str.slice(1);
}
}
}
function addVersion<T extends { new (...args: any[]): any }>(constructor: T): T{
return class extends constructor {
static version: string = '1.0';
}
}
@addVersion
class Person {
constructor(
public fname: string, public lname: string,
) {}
@capitalize
getFirstName(): string {
return this.fname;
}
@capitalize
getLastName(): string {
return this.lname;
}
@uppercase
get fullname(): string {
return this.fname + ' ' + this.lname;
}
set fullname( [ fname, lname ] ) {
this.fname = fname;
this.lname = lname;
}
}
var person = new Person( 'ross', 'geller' );
console.log( 'fullname ->', person.fullname );
console.log( 'first name ->', person.getFirstName());
console.log( 'last name ->', person.getLastName());
console.log( 'version ->', (Person as any).version);
// console.log( 'person ->', person );
person.fullname = 'chandler bing';
console.log( 'fullname* ->', person.fullname );
// console.log( 'person* ->', person );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment