Skip to content

Instantly share code, notes, and snippets.

View AhsanAyaz's full-sized avatar

Muhammad Ahsan Ayaz AhsanAyaz

View GitHub Profile
@AhsanAyaz
AhsanAyaz / du-union.ts
Created September 6, 2019 07:12
Understanding Discriminated Unions in Typescript
type Vehicle = IMotorcycle | ICar | ITruck;
@AhsanAyaz
AhsanAyaz / du-type-guards-example.ts
Last active September 6, 2019 07:15
Understanding Discriminated Unions in Typescript
const evaluationFactor = Math.PI; // some global factor
function evaluatePrice(vehicle: Vehicle) {
return vehicle.capacity * evaluationFactor;
}
const myTruck: ITruck = {vType: "truck", capacity: 9.5};
evaluatePrice(myTruck);
@AhsanAyaz
AhsanAyaz / du-type-guards.ts
Created September 6, 2019 07:15
Understanding Discriminated Unions in Typescript
function evaluatePrice(vehicle: Vehicle) {
switch(vehicle.vType) {
case "car":
return vehicle.transmission * evaluationFactor;
case "truck":
return vehicle.capacity * evaluationFactor;
case "motorcycle":
return vehicle.make * evaluationFactor;
}
}
@AhsanAyaz
AhsanAyaz / du-bicycle-interface-addition.ts
Created September 6, 2019 07:16
Understanding Discriminated Unions in Typescript
interface IBicycle {
vType: "bicycle";
make: number;
}
type Vehicle = IMotorcycle | ICar | ITruck | IBicycle;
@AhsanAyaz
AhsanAyaz / du-type-guards-update.ts
Created September 6, 2019 07:17
Understanding Discriminated Unions in Typescript
function evaluatePrice(vehicle: Vehicle) {
switch(vehicle.vType) {
case "car":
return vehicle.transmission * evaluationFactor;
case "truck":
return vehicle.capacity * evaluationFactor;
case "motorcycle":
return vehicle.make * evaluationFactor;
default:
const invalidVehicle: never = vehicle;
@AhsanAyaz
AhsanAyaz / du-type-guards-final.ts
Created September 6, 2019 07:20
Understanding Discriminated Unions in Typescript
function evaluatePrice(vehicle: Vehicle) {
switch(vehicle.vType) {
case "car":
return vehicle.transmission * evaluationFactor;
case "truck":
return vehicle.capacity * evaluationFactor;
case "motorcycle":
return vehicle.make * evaluationFactor;
case "bicycle":
return vehicle.make * evaluationFactor;
@AhsanAyaz
AhsanAyaz / angular-title-case-extended.pipe.html
Last active September 18, 2019 12:56
Custom Angular title case pipe built on top of Angular's titleCase pipe
<!-- USAGE -->
<!-- Transoforming strings -->
<div class="users">
<div class="users__user" *ngFor="let user of users">
{{user.name | appTitleCase}}
</div>
</div>
<!-- Transforming Arrays -->
@AhsanAyaz
AhsanAyaz / pointy-little-popovers__src__styles.scss
Last active February 19, 2021 21:27
Global Styles for Pointy Little Popover Recipe
/* You can add global styles to this file, and also import other style files */
.cdk-overlay-container {
display: block;
&.z-index-top {
z-index: 2050;
}
}
.duplicate-modal-overlay {
@AhsanAyaz
AhsanAyaz / pointy-little-popovers__src__app_directives_popover-positional-class.directive.ts
Last active February 19, 2021 21:38
Pointy Little Popovers PopoverPositionalClassDirective
import { AfterViewInit, ChangeDetectorRef, Directive, Input, Renderer2, SimpleChanges } from '@angular/core';
@Directive({
selector: '[appPopoverPositionalClass]'
})
export class PopoverPositionalClassDirective implements AfterViewInit {
@Input() originY: string;
@Input() targetSelector: string;
@Input() inverseClass;
@Input() initialDirection = 'bottom';
@AhsanAyaz
AhsanAyaz / Code.gs
Last active April 1, 2024 17:53
Email automation App Script
/**
Replace the "<DOCID>" with your document ID, or the entire URL per say. Should be something like:
var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/asdasdakvJZasdasd3nR8kmbiphqlykM-zxcrasdasdad/edit?usp=sharing';
*/
var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/<DOCID>/edit?usp=sharing';
var EMAIL_SUBJECT = 'This is an important email';
/**
* Sends a customized email for every response on a form.