Skip to content

Instantly share code, notes, and snippets.

View andregoncalves's full-sized avatar
🏠
Working from home

Andre Goncalves andregoncalves

🏠
Working from home
View GitHub Profile
@andregoncalves
andregoncalves / class_decorator.ts
Last active May 3, 2021 07:07 — forked from remojansen/class_decorator.ts
[TypeScript Decorators] #typescript
function logClass(target: any) {
// save a reference to the original constructor
var original = target;
// a utility function to generate instances of a class
function construct(constructor, args) {
var c : any = function () {
return constructor.apply(this, args);
}
@andregoncalves
andregoncalves / warning.css
Created May 26, 2018 11:19
[CSS rule to display warning icon with unicode] #css
.invalid-feedback:before {
content: '⚠️'
}
@andregoncalves
andregoncalves / speed.js
Created May 10, 2018 09:23
[Page load time] measure page load time in js #devtools #js
(function(){
const t = window.performance && performance.timing;
if (!t) {
return;
}
const loadTime = (t.loadEventEnd - t.navigationStart) / 1000;
alert(`This page loaded in ${loadTime} seconds`);
}())
@andregoncalves
andregoncalves / example.js
Created March 25, 2018 10:52
[Throttling and debouncing] #js
// Throttling
// ES6 code
function throttled(delay, fn) {
let lastCall = 0;
return function (...args) {
const now = (new Date).getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
@andregoncalves
andregoncalves / colors.js
Created March 6, 2018 11:28
[Devtools console colors] #devtools
console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');
@andregoncalves
andregoncalves / aspect.css
Last active November 18, 2017 11:57
CSS maintain block aspect ratio
<style>
.aspect-ratio {
padding-top: calc(1 / (16 / 9) * 100%);
position : relative;
}
.aspect-wrapper {
position: absolute;
top:0;
left:0;
@andregoncalves
andregoncalves / recaptcha.php
Last active November 18, 2017 11:59
Verify google recaptcha with PHP
# Verify captcha
$post_data = http_build_query(
array(
'secret' => CAPTCHA_SECRET,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
)
);
$opts = array('http' =>
array(
@andregoncalves
andregoncalves / request.js
Created July 10, 2017 07:09
HTTP Request Retry with exponential backoff with Async
function wait (timeout) {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, timeout)
})
}
async function requestWithRetry (url) {
const MAX_RETRIES = 10
@andregoncalves
andregoncalves / jquery-es6-compat.js
Last active November 18, 2017 12:04
jQuery like syntax with ES6 #es6
/**
* --------------------------------------------------------------------
* jQuery compatibility layer
* --------------------------------------------------------------------
*/
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
};
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
@andregoncalves
andregoncalves / slack.php
Created December 19, 2016 17:12
Send a slack webhook notification in php
<?php
// (string) $message - message to be passed to Slack
// (string) $room - room in which to write the message, too
// (string) $icon - You can set up custom emoji icons to use with each message
public static function slack($message, $room = "engineering", $icon = ":longbox:") {
$room = ($room) ? $room : "engineering";
$data = "payload=" . json_encode(array(
"channel" => "#{$room}",
"text" => $message,