Skip to content

Instantly share code, notes, and snippets.

View RayLuxembourg's full-sized avatar

Ray Luxembourg RayLuxembourg

View GitHub Profile
const romanNumerics = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
};
@RayLuxembourg
RayLuxembourg / DragEvent.ts
Last active November 14, 2018 08:59
Rxjs Universal drag event
export const DragEvent = (selector:string):Observable<boolean> => {
//element where the drag event should be recorded (make sure element is in the dom)
const elem = document.querySelector(selector);
// touch events to handle mobile devices
const touchStart$ = fromEvent<TouchEvent>(elem, 'touchstart');
const touchEnd$ = fromEvent<TouchEvent>(elem, 'touchend');
@RayLuxembourg
RayLuxembourg / bag.spec.ts
Last active September 10, 2018 10:16
more detailed test
//testing both success and fail status
it('should handle Add to bag Message', () => {
const msg: BagResponse = {
actionType: 'add',
category: 'bag',
product: { colorId: '01', productId: '23423', sizeId: 'SM003' },
status: 'success'
};
component.chooseMessageHandler(msg);
expect(component.bag[0]).toBe(component.buildIdentifier(msg.product));
@RayLuxembourg
RayLuxembourg / counter.spec.ts
Created September 10, 2018 09:55
Simple counter test
describe('CounterComponent', () => {
let component: CounterComponent;
let fixture: ComponentFixture<CounterComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [CounterComponent]
}).compileComponents();
}));
@RayLuxembourg
RayLuxembourg / dry-example.ts
Created August 19, 2018 14:58
dry-example.ts
interface BaseProduct {
name: string;
id: number;
price: number;
}
interface ProductsList {
top: BaseProduct[];
bottom: BaseProduct[];
@RayLuxembourg
RayLuxembourg / example.repeatable-code.ts
Created August 19, 2018 14:55
example.repeatable-code.ts
interface BaseProduct {
name: string;
id: number;
price: number;
}
interface ProductsList {
top: BaseProduct[];
bottom: BaseProduct[];
@RayLuxembourg
RayLuxembourg / product.component.ts
Created August 19, 2018 14:50
Component using resolver
export class ProductComponent implements OnInit {
product: Product;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.product = this.route.snapshot.data['product'];
}
}
@RayLuxembourg
RayLuxembourg / ProductResolver.guard.ts
Last active August 19, 2018 14:49
ProductResolver
@Injectable()
export class ProductResolver implements Resolve<Product> {
constructor(private api: ApiService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Product> {
return this.api.product(route.params['id']);
}
}
@RayLuxembourg
RayLuxembourg / product.component.ts
Created August 19, 2018 14:47
product component
export class ProductComponent implements OnInit {
product$: Observable<Product>;
constructor(private api: ApiService,private route:ActivatedRoute) {}
ngOnInit() {
const id = this.route.snapshop.params['id'];
this.product$ = this.api.product(id);
// ...
}
@RayLuxembourg
RayLuxembourg / api.service.ts
Last active August 19, 2018 14:46
Api Service
//src/common/services/api.service.ts
@Injectable({ providedIn: 'root' })
export class ApiService {
monitor = "/monitor";
analytics = "/analytics";
api = "/api"
constructor(private httpClient: HttpClient) { }