Skip to content

Instantly share code, notes, and snippets.

@bernardo-cs
bernardo-cs / paginated-search-input.ts
Last active July 9, 2019 09:14
Angular component that fully consumes a paginated API recursivly with RxJS expand operator. Example live on: https://stackblitz.com/edit/angular-yja4k7
import { Component } from '@angular/core';
import { combineLatest, Subject, of, empty, Observable } from 'rxjs';
import { map, tap, expand } from 'rxjs/operators';
@Component({
  selector: 'my-app',
  template: `
    <input type="text" (input)="term$.next($event.target.value)" />
    <div *ngIf="users$ | async as users">
/*
* QUESTION 1:
* Write a code snippet that would transform "animalCollective" into "expected"
*/
const animalCollective = [
{type: 'dog', name: 'bobi'},
{type: 'fish', name: 'glup'},
{type: 'fish', name: 'glup the second'},
{type: 'cat', name: 'Aaron'}
@bernardo-cs
bernardo-cs / facebook_login_rails_system_test_capybara_koala_minitest.rb
Created September 11, 2018 08:48
Creates new facebook test user with koala gem. Signs in user on facebook.com. Accepts facebook "continue as #{username}" iframe.
# frozen_string_literal: true
require 'application_system_test_case'
class FacebookLoginTest < ApplicationSystemTestCase
# create new test user with Koala Gem
def setup
@test_users = Koala::Facebook::TestUsers.new
@user = @test_users.create(false)
@bernardo-cs
bernardo-cs / facebook_login_capybara_koala_minitest.rb
Created September 11, 2018 08:47
Rails system test that:Creates new facebook test user with koala gem. Signs in user on facebook.com. Accepts facebook "continue as #{username}" iframe.
# frozen_string_literal: true
require 'application_system_test_case'
class FacebookLoginTest < ApplicationSystemTestCase
# create new test user with Koala Gem
def setup
@test_users = Koala::Facebook::TestUsers.new
@user = @test_users.create(false)
@bernardo-cs
bernardo-cs / remove_ifs.js
Created October 9, 2017 10:04
how to remove ifs from js (angular.js) and use filters/reduce
var ids = [];
if ($scope.attendeesToggle) { ids = ids.concat($scope.eventAttendees); }
if ($scope.waitingListToggle) { ids = ids.concat($scope.eventWaitingList); }
//--------
[
{ condition: () => $scope.attendeesToggle, array: $scope.eventAttendees },
{ condition: () => $scope.waitingListToggle, array: $scope.eventWaitingList }
@bernardo-cs
bernardo-cs / dom_check.js
Last active September 21, 2017 09:15
Verify dom changes every second, reload after 1 minute, send notification if change happened with Tampermonkey and RxJS
// ==UserScript==
// @name Verify fitness hut class availability
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.myhut.pt/myhut/aulas/*
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.3/Rx.js
// ==/UserScript==
@bernardo-cs
bernardo-cs / editable.directive.ts
Last active September 11, 2017 13:18
Angular 2 direct, make content editable only for admins
import { AfterViewInit, Directive, ElementRef, HostListener, Input } from '@angular/core';
import { UserService } from '../shared/services/user.service';
@Directive({
selector: '[flEditable]'
})
export class EditableDirective implements AfterViewInit {
@Input('endpoint') endpoint;
@bernardo-cs
bernardo-cs / scroll_bulletin_int_view.component.ts
Created September 11, 2017 13:10
Angular 2 directive scrolls into view
import { AfterViewInit, Directive, ElementRef, Input, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
@Directive({
selector: '[flScrollBulletinIntoView]'
})
export class ScrollBulletinIntoViewDirective implements AfterViewInit, OnDestroy {
@Input() bulletinId: number;
private scrollIntoView$: Subscription;
// ==UserScript==
// @name Verify fitness hut class availability
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.myhut.pt/myhut/aulas/*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
var xhr_request = function (post_url, xsrf_token, ids, start_interval, end_interval, callback){
var request = { page_ids: ids, start: start_interval, end: end_interval};
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", callback);
oReq.open("POST", post_url);
oReq.setRequestHeader("Content-Type", "application/json");
oReq.setRequestHeader("X-CSRF-Token", xsrf_token);
oReq.send(JSON.stringify(request));
};