Skip to content

Instantly share code, notes, and snippets.

View mohanramphp's full-sized avatar

Mohan Ram mohanramphp

View GitHub Profile
@mohanramphp
mohanramphp / accessor-decorator.js
Created December 22, 2018 03:29
Accessor decorator compiled code
function enumerable(value) {
return function (target, propertyKey, descriptor) {
console.log('decorator - sets the enumeration part of the accessor');
descriptor.enumerable = value;
};
}
var Employee = /** @class */ (function () {
function Employee() {
@mohanramphp
mohanramphp / accessor-decorator.ts
Created December 22, 2018 03:24
Accessor Decorator
function enumerable(value: boolean) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
console.log('decorator - sets the enumeration part of the accessor');
descriptor.enumerable = value;
};
}
class Employee {
private _salary: number;
@mohanramphp
mohanramphp / parameter-decorator.js
Created December 21, 2018 11:36
Parameter decorator compiled code
// return the decorator function by accepting parameter index and decorator
var __param = (this && this.__param) || function (paramIndex, decorator) {
// function that returns decorator
return function (target, key) { decorator(target, key, paramIndex); }
};
var Employee = /** @class */ (function () {
function Employee() {
}
Employee.prototype.greet = function (message) {
@mohanramphp
mohanramphp / parameter-decorator.ts
Created December 21, 2018 11:31
Parameter Decorator
function logParameter(target: Object, propertyName: string, index: number) {
// generate metadatakey for the respective method
// to hold the position of the decorated parameters
const metadataKey = `log_${propertyName}_parameters`;
if (Array.isArray(target[metadataKey])) {
target[metadataKey].push(index);
}
else {
target[metadataKey] = [index];
@mohanramphp
mohanramphp / property-decorator.js
Created December 21, 2018 08:47
Property decorator compiled code
var Employee = /** @class */ (function () {
function Employee() {
}
__decorate([
logParameter
], Employee.prototype, "name");
return Employee;
}());
var emp = new Employee();
emp.name = 'Mohan Ram'; // Set: name => Mohan Ram
@mohanramphp
mohanramphp / property-decorator.ts
Created December 21, 2018 08:44
Property Decorator
function logParameter(target: Object, propertyName: string) {
// property value
let _val = this[propertyName];
// property getter method
const getter = () => {
console.log(`Get: ${propertyName} => ${_val}`);
return _val;
};
@mohanramphp
mohanramphp / method-decorator.js
Created December 21, 2018 07:54
compiled script of method decorator
"use strict";
var __decorate = (this && this.__decorate) ||
function (decorators, target, key, desc) {
// function argument length
var c = arguments.length
/**
* manipulating the result
* if - only the array of decorators and target is passed
* then - it should be an class decorator
@mohanramphp
mohanramphp / method-decorator.ts
Last active December 9, 2019 05:15
To show the method decorator usage in TypeScript
export function logMethod(
target: Object,
propertyName: string,
propertyDesciptor: PropertyDescriptor): PropertyDescriptor {
// target === Employee.prototype
// propertyName === "greet"
// propertyDesciptor === Object.getOwnPropertyDescriptor(Employee.prototype, "greet")
const method = propertyDesciptor.value;
@mohanramphp
mohanramphp / dashboard.component.ts
Last active September 21, 2018 05:42
Dashboard component typescript file
// dashboard.component.ts
import { ID } from '@datorama/akita';
import { createStudent, StudentService, StudentQuery, Student } from './state/index';
@Component({
template: `
<div class="container-fluid">
...
<div class="row">
<app-chart [studentsGraphData$]="studentQuery.studentsGraphData$"></app-chart>
@mohanramphp
mohanramphp / types.d.ts
Last active September 16, 2018 11:01
Entity State Interface
/**
* Below codes are from the type declaration of Akita
*/
// An interface to represent objects
export interface HashMap<T> {
[id: string]: T;
}
/**