Skip to content

Latest commit

 

History

History
273 lines (201 loc) · 5.06 KB

EXAMPLES.md

File metadata and controls

273 lines (201 loc) · 5.06 KB

Examples

@multiInherit

class Manager{
  render(){ return 42; }
  init(){ return 43; }
  t(){ return 44; }
};
class Employee{
  render(){ return 45; }
  mount(){ return 46; }
  t(){ return 47;}
};

@multiInherit( Manager, Employee )
class Person{
  t(){ return 3; }
};

const p = new Person();
p.render(); // 45
p.t(); // 3
p.init(); // 43

@partiallyInherit

class Manager{
  render(){ return 42; }
  init(){ return 43; }
  t(){ return 44; }
};

@partiallyInherit( Manager, ['render', 't'] )
class Person{
  t(){ return 3; }
};

const p = new Person();
p.render(); // 42
p.t(); // 3
p.init(); // throws error, init undefined

@compose

const r = (t) => t * 5;
const r2 = (t) => t + 3;

class Person {
  @compose([r, r2])
  doSomething() {
    return 2;
  }

  @leftCompose([r, r2])
  doSomething2() {
    return 2;
  }
};


var p = new Person();
p.doSomething(); // 13
p.doSomething2(); // 25

@valuesEqualToNumberOfArguments

class Person {
  @valuesEqualToNumberOfArguments()
  doSomething( a ) {
    return a;
  }
};


var p = new Person();
p.doSomething( 1,2,3,4,5,5,6); // throws an exception

@validateSchema

Executes the method only if the passed values are valid according to the provided schema. Example:

class Person{
  @validateSchema( { age: 'number', childs: 'object', jobs: 'array', name: 'string' } );
  setPersonDetails( obj ) { ... }
}

@acceptsObject

Executes the method only if the passed value is an object otherwise throws an exception, accepts position (default=0) or array of positions. Example:

class Person{
@acceptsObject( [0,2], true );
doSomething( obj, str, obj ) { ... }
}

@immutable

Makes a deepcopy of the passed arguments and executes the method with the copy to ensure that the initial parameters are not mutated. Example:

class Person{
@immutable();
doSomething( arg1 ) {
   arg1.test = 10;
}
}

var obj = { test: 5 };
var p = new Person();
p.dosomething( obj );
console.log( obj.test ); // 5;

@doesNotMutate

Executes the method only if it doesnt mutate the passed arguments. Useful when the class extends another class and/or calls methods from the parent. Example:

class Person{
@doesNotMutate();
doSomething( arg1 ) {
   arg1.test = 10;
}
}

var obj = { test: 5 };
var p = new Person();
p.dosomething( obj ); // throws an exception because doSomething mutates the passed object.

@memoization

class Person{
@memoization();
doSomethingTimeConsuming( arg1 ) {
   arg1.test = 10;
}
}

var obj = { test: 5 };
var p = new Person();
p.doSomethingTimeConsuming( obj ); // calls the function
p.doSomethingTimeConsuming( obj ); // immediately returns the result without calling the function

@log

class Person{
@log();
doSomething(...args){
  return args.join('-');
}
}

var p = new Person();
p.doSomething( 2 , 3 );

Will log in the console:

Logged on console

@loglocalstorage

class Person{
@loglocalstorage()
doSomething(){
  localStorage.setItem('somethingonlocalstorage', "Lorem ipsum dol'or sit amet, consectetur adipiscing elit. Morbi congue urna id enim lobortis placerat. Integer commodo nulla in ipsum pharetra, ac malesuada tellus viverra. Integer aliquam semper nisi vehicula lobortis. Ut vel felis nec sem sollicitudin eleifend. Ut sem magna, vehicula in dui in, sodales ultrices arcu. Aliquam suscipit sagittis aliquet. Phasellus convallis faucibus massa, quis tincidunt nisl accumsan sed.")
}
}

var p = new Person();
p.doSomething( );

Will log in the console something like:

Logged on console

@timeout (alias: @debounce)

class Person{
@timeout( 2000 );
doSomething(){
  ...
}
}

var p = new Person();
p.doSomething(); // Executed after 2secs

@once()

class Person{
@once();
doSomething(a, b){
  return a + b;
}
}

var p = new Person();
p.doSomething(1,2); // returns 3
p.doSomething(21,2); // keeps returning 3
p.doSomething(14,42); // keeps returning 3

@times(n)

class Person{
@times(2);
doSomething(a, b){
  return a + b;
}
}

var p = new Person();
p.doSomething(1,2); // returns 3
p.doSomething(21,2); // returns 23
p.doSomething(14,42); // keeps returning 23
p.doSomething(14,542); // keeps returning 23

@timesCalled()

class Person{
@timesCalled();
doSomething(a, b){
  return a + b;
}
}

var p = new Person();
p.doSomething(1,2);
p.doSomething(21,2);
p.doSomething(14,42);
console.log( p.doSomething.timesCalled ); // 3