Skip to content

Instantly share code, notes, and snippets.

imkrish

Block or report user

Report or block imkrish

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View palindrom.js
//Make the following test cases pass
function isPalindrome(phrase) {
const SPACE = " ";
const lowerCharsInPhrase = phrase
.toLowerCase()
.split("")
.filter(char => char !== SPACE);
return lowerCharsInPhrase.join() === lowerCharsInPhrase.reverse().join();
}
View optional-operator.ts
const maybePerson = getPerson();
console.log(maybePerson?.name);
console.log(maybePerson?.name?.firstName);
console.log(maybePerson?.name?.lastName);
console.log(maybePerson?.name?.getFullName?.());
View handle-with-if.ts
const maybePerson = getPerson();
if (maybePerson) {
console.log(maybePerson.name);
}
if (maybePerson && maybePerson.name) {
console.log(maybePerson.name.firstName);
console.log(maybePerson.name.lastName);
}
View getPerson.ts
function getRandomNumber() {
return Math.floor(Math.random() * 100);
}
function isEvenNumber(number: number) {
return number % 2 == 0;
}
function getPerson() {
if (isEvenNumber(getRandomNumber())) {
View unsubscribe-02.ts
export class AppComponent implements OnInit, OnDestroy {
@Selector(CustomersState.SelectedCustomer)
private selectedCustomer$: Observable<Customer>;
@Selector(OrdersState.NewOrder)
private newOrder$: Observable<Order>;
private onComponentDestroy$: Subject<void>;
constructor() {
View unsubscribe-01.ts
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
export class RxJSUtil {
static unsubscribe = (subscriptions: Subscription[]) =>
subscriptions.forEach(subscription => subscription.unsubscribe());
}
@Component({
selector: 'nx-tutorial-root',
View deep-read-only.ts
type DeepReadonlyObject<T> = { readonly [K in keyof T]: DeepReadonly<T[K]> };
type DeepReadonly<T> = T extends (infer E)[][]
? ReadonlyArray<ReadonlyArray<DeepReadonlyObject<E>>>
: T extends (infer E)[]
? ReadonlyArray<DeepReadonlyObject<E>>
: T extends object
? DeepReadonlyObject<T>
: T;
View 06-put-it-together.ts
interface User {
readonly id: number;
readonly name: string;
readonly wifeName: string;
readonly parents: ReadonlyArray<string>;
}
const user: User = {
id: 1,
name: "John",
View 05-readonly-array.ts
const numbers: ReadonlyArray<number> = [1, 2, 3, 4, 5];
numbers.push(6); // Property 'push' does not exist on type 'ReadonlyArray<number>'
View 04-readonly-array-1.ts
const numbers: number[] = [1, 2, 3, 4, 5];
numbers.push(6); // There is nothing wrong with this
You can’t perform that action at this time.