Skip to content

Instantly share code, notes, and snippets.

View fabriciofmsilva's full-sized avatar

Fabrício Silva fabriciofmsilva

View GitHub Profile
@fabriciofmsilva
fabriciofmsilva / events.md
Created February 17, 2018 12:29
DOM Events

DOM Events

load

The load event is fired when a resource and its dependent resources have finished loading.

window.addEventListener("load", function(event) {
  console.log("All resources finished loading!");
});
@fabriciofmsilva
fabriciofmsilva / array-deep-copy.md
Created February 17, 2018 12:33
JavaScript Array Deep Copy

map

var arr = [1, 2, 3, 4, 5];
var arr2 = arr.map(function(item) {
  return item;
});

slice

@fabriciofmsilva
fabriciofmsilva / types.md
Last active February 17, 2018 12:52
Checking Types in Javascript

Checking Types in Javascript

typeof

typeof 3; // "number"
typeof 'abc'; // "string"
typeof {}; // "object"
typeof true; // "boolean"
typeof undefined; // "undefined"
@fabriciofmsilva
fabriciofmsilva / upload.js
Created February 26, 2018 13:23
Upload File with jQuery + FormData
var Upload = function (file) {
this.file = file;
};
Upload.prototype.getType = function() {
return this.file.type;
};
Upload.prototype.getSize = function() {
return this.file.size;
};
@fabriciofmsilva
fabriciofmsilva / 1.js
Created March 6, 2018 21:53 — forked from chrisbuttery/1.js
Fade in / Fade out
// fade out
function fade(el) {
var op = 1;
var timer = setInterval(function () {
if (op <= 0.1){
clearInterval(timer);
el.style.display = 'none';
}
el.style.opacity = op;
@fabriciofmsilva
fabriciofmsilva / index.html
Created March 13, 2018 18:36
Browser events: bubbling, capturing, and delegation
<body>
<p>
<a><span>Hello</span></a>
</p>
<script>
const spanEl = document.querySelector('span');
spanEl.addEventListener('click', handler, false);
const aEl = document.querySelector('a');
@fabriciofmsilva
fabriciofmsilva / pipeable.js
Created March 15, 2018 22:04
Pipeable operators | RxJS
import { range } from 'rxjs/observable/range';
import { map, filter, scan } from 'rxjs/operators';
const source$ = range(0, 10);
source$.pipe(
filter(x => x % 2 === 0),
map(x => x + x),
scan((acc, x) => acc + x, 0)
)
@fabriciofmsilva
fabriciofmsilva / operator.js
Created March 15, 2018 22:04
Build Your Own Operators Easily | RxJS
import { interval } from 'rxjs/observable/interval';
import { map, take, toArray } from 'rxjs/operators';
/**
* an operator that takes every Nth value
*/
const takeEveryNth = (n: number) => <T>(source: Observable<T>) =>
new Observable(observer => {
let count = 0;
return source.subscribe({
@fabriciofmsilva
fabriciofmsilva / lettable.js
Created March 15, 2018 22:23
Understanding Lettable Operators | RxJS
// Using the bundle
import * as Rx from "rxjs";
const name = Rx.Observable.ajax
.getJSON<{ name: string }>("/api/employees/alice")
.map(employee => employee.name)
.catch(error => Rx.Observable.of(null));
// Using prototype patching
import { Observable } from "rxjs/Observable";
{
  // Force to use pipeable in RxJS
  "rxjs-no-add": { "severity": "error" },
  "rxjs-no-operator": { "severity": "error" },
  "rxjs-no-patched": { "severity": "error" },
  "rxjs-no-wholesale": { "severity": "error" }
  // Force to use pipeable in RxJS
}