Skip to content

Instantly share code, notes, and snippets.

View jpescada's full-sized avatar

João Pescada jpescada

View GitHub Profile
@jpescada
jpescada / reset_postgresql_seq_key.sql
Created January 29, 2021 15:30
Reset PostgreSQL sequence key
SELECT setval(pg_get_serial_sequence('table_name_here', 'id'), COALESCE((SELECT MAX(id) + 1 FROM table_name_here), 1), false);
@jpescada
jpescada / nginx.conf
Created October 20, 2020 23:25
NGINX block config for static PWA routing (React, Angular, Next, etc)
server {
location / {
try_files $uri $uri.html $uri/ =404;
}
}
@jpescada
jpescada / ngrok-europe-local-domain-ssl.sh
Created October 1, 2020 23:43
Start ngrok in Europe on a custom local domain over https
ngrok http --region=eu -host-header=rewrite https://domain.test
@jpescada
jpescada / is-webkit-mobile.ts
Created August 21, 2020 11:32
Detect WebKit browser on (mobile) iOS device
// Based on https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
const isWebkit = (): boolean => {
const UA = navigator.userAgent
return /\b(iPad|iPhone|iPod)\b/.test(UA) && /WebKit/.test(UA) &&
!/Edge/.test(UA) && !window.MSStream
}
@jpescada
jpescada / prevent-widows.ts
Last active August 19, 2020 13:02
Prevent text widows
// Replaces a set number of spaces in text with non-breaking spaces to prevent widows
// Example use in React: <div dangerouslySetInnerHTML={{ __html: preventWidows(text) }} />
export function preventWidows(text: string, spaces: number = 3): string {
const regx: RegExp = /\s/g
const spacesCount: number = (text.match(regx) || []).length
const spacesTarget = spacesCount - spaces
let i: number = 0
text = text.replace(regx, match => {
i++
@jpescada
jpescada / load-3rd-party-scripts-after-scroll.ts
Last active August 19, 2020 13:03
Load third-party JS scripts only after first scroll
window.addEventListener('scroll', () =>
setTimeout(() => {
// Load Fullstory, Intercom, Segment, etc
}, 1000),
{ once: true }
);
@jpescada
jpescada / video-carousel-book.jsx
Last active April 16, 2019 16:05
Memory efficient video carousel for mobile as a React component
@jpescada
jpescada / dynamic-svg-react-component.jsx
Last active November 22, 2018 13:14
React Component to generate an SVG with dynamic content (text, colour and image)
import React from 'react';
// NB: This is demo code just to explain the logic
const DynamicSVG = (props) => {
static defaultProps = {
text: 'This is the default text',
colour: '#FF0000',
image: '/static/images/default.png',
@jpescada
jpescada / get-time-left.php
Last active November 22, 2018 12:50
Get time left between dates in days, hours and minutes with PHP
function getTimeLeft($startDate)
{
$now = new DateTime();
$then = new DateTime($startDate);
$timeLeft = 0;
// Only calculate time left if $then is still a future date
if ($now < $then) {
$difference = $now->diff($then);
@jpescada
jpescada / verify-google-recaptcha.php
Last active June 12, 2024 12:55
Verify Google ReCaptcha with PHP and cURL
/*
Author @CreativForm (on GitHub)
Source: https://gist.github.com/jonathanstark/dfb30bdfb522318fc819#gistcomment-2189252
*/
function validate_recaptcha($response){
// Verifying the user's response (https://developers.google.com/recaptcha/docs/verify)
$verifyURL = 'https://www.google.com/recaptcha/api/siteverify';
// Collect and build POST data
$post_data = http_build_query(