Skip to content

Instantly share code, notes, and snippets.

import { TestBed, tick, fakeAsync } from '@angular/core/testing';
import { UserAsyncComponent } from './user-async.component';
import { UserAsyncService } from './user-async.service';
import { Observable, Observer } from 'rxjs';
describe('User Async Component:', () => {
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [UserAsyncComponent]
});
import { Observable, Observer } from 'rxjs';
export class UserAsyncService {
user = { name: 'Mannie' };
getUserDetails() {
// Create an observables.
const userObservables = Observable.create(
(observer: Observer<{ name: string }>) => {
setTimeout(() => {
observer.next(this.user);
import { async, TestBed } from '@angular/core/testing';
import { UserComponent } from './user.component';
import { UserService } from './user.service';
describe('UserComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [UserComponent]
export interface GoogleData {
id: number;
country: string;
zipCode: string;
}
import { Component, OnInit } from '@angular/core';
import { UserAsyncService } from './user-async.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-user-async',
templateUrl: './user-async.component.html',
styleUrls: ['./user-async.component.scss'],
providers: [UserAsyncService]
})
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'excerpt' })
export class ExcerptPipe implements PipeTransform {
transform(text: string, limit: number = 5) {
if (text.length <= limit) {
return text;
}
return text.substring(0, limit) + '...';
}
@zimejin
zimejin / factory.js
Last active May 26, 2019 15:10
Factory design pattern
// Animal factory
const Animal = function(name){
const animal = {};
animal.name = name;
animal.walk = function(){
console.log(this.name + " walks");
}
return animal;
};
@zimejin
zimejin / utils.js
Last active August 17, 2020 13:47
array utilities
// Using slice() to Remove value from beginning and end of string.
sliceString = function (value) {
let result = value.slice(1, -1);
return result;
}
// Using splice() method to changes the contents of an array by removing
// or replacing existing elements and/or adding new elements
var months = ['Jan', 'March', 'April', 'June'];
@zimejin
zimejin / rxjs.js
Created May 30, 2019 15:17
RxJS snippets
//emit value every 1s
const source = interval(1000);
const example = source.pipe(
map(val => {
if (val > 5) {
//error will be picked up by retryWhen
throw val;
}
return val;
}),
(focus)="myMethod()" // An element has received focus
(blur)="myMethod()" // An element has lost focus
(submit)="myMethod()" // A submit button has been pressed
(scroll)="myMethod()"
(cut)="myMethod()"
(copy)="myMethod()"
(paste)="myMethod()"