Skip to content

Instantly share code, notes, and snippets.

View sanjeevsubedi's full-sized avatar

Sanjeev Subedi sanjeevsubedi

View GitHub Profile
@sanjeevsubedi
sanjeevsubedi / timer.ts
Created March 7, 2023 15:22
Angular method decorator to calcuate the time taken to execute a function
/**
* Angular method decorator to caculate the time taken to execute a function
*
*/
export function timer(
prototype: any,
name: string,
descriptor: PropertyDescriptor
) {
@sanjeevsubedi
sanjeevsubedi / unsubscribe.ts
Created March 7, 2023 15:17
Angular class decorator to auto unsubscribe from all subscriptions
/**
* Angular class decorator to auto unsubscribe from all subscriptions
*
*/
export function unsubscribe() {
return function (constructor: any) {
const originalDestroy = constructor.prototype.ngOnDestroy;
constructor.prototype.ngOnDestroy = function () {
@sanjeevsubedi
sanjeevsubedi / setTimeout.ts
Last active March 7, 2023 15:19
Angular method decorator to implement setTimeout
/**
* Angular method decorator to implement setTimeout
*
*/
export function timeout(milliseconds: number = 0) {
return function (
prototype: any,
name: string,
descriptor: PropertyDescriptor
/**
* Angular read only property decorator
*
*/
export function readOnly(prototype: any, name: string) {
Object.defineProperty(prototype, name, {
writable: false,
});
}
@sanjeevsubedi
sanjeevsubedi / throttle.ts
Last active March 7, 2023 15:19
Angular method decorator to implement throttle
/**
* Angular method decorator to implement throttle
*
*/
import t from 'lodash/throttle';
export function throttle(milliseconds: number = 500) {
return function (
prototype: any,
@sanjeevsubedi
sanjeevsubedi / debounce.ts
Last active March 7, 2023 15:18
Angular method decorator to implement debounce
/**
* Angular method decorator to implement debounce
*
*/
import d from 'lodash/debounce';
export function debounce(milliseconds: number = 500) {
return function (
prototype: any,
@sanjeevsubedi
sanjeevsubedi / page-analytics.ts
Created March 7, 2023 14:57
Angular class decorator to track the page visit
/**
* Angular class decorator to track the page visit
*
*/
import { Injector } from '@angular/core';
import { AnalyticService } from '../analytics.service';
export function PageAnalytics(pageName: string) {
return function (constructor: Function) {
@sanjeevsubedi
sanjeevsubedi / get-selector-by-text.
Created February 20, 2023 22:44
Find the DOM element by the matching text content recursively.
function getSelectorByText(
node: HTMLElement,
text: string
): HTMLElement | undefined {
if (node?.innerHTML?.trim() === text) {
return node;
}
for (let i = 0; i < node.children.length; i++) {
const found = getSelectorByText(node.children[i] as HTMLElement, text);
@sanjeevsubedi
sanjeevsubedi / app.js
Created December 4, 2013 07:49 — forked from clarle/app.js
// Module dependencies
var express = require('express'),
mysql = require('mysql');
// Application initialization
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',