Skip to content

Instantly share code, notes, and snippets.

View sunpietro's full-sized avatar

sunpietro sunpietro

View GitHub Profile
@sunpietro
sunpietro / delay.js
Created December 14, 2023 15:52
Simple function to simulate long requests
const delay = async () => {
const delayPromise = new Promise((resolve) => {
setTimeout(resolve, 20000);
});
return delayPromise;
};
// await delay();
@sunpietro
sunpietro / fixedForwardRef.ts
Created July 25, 2023 12:17
Fixed ForwardRef typing
import React, { forwardRef } from "react";
// Declare a type that works with generic components
type FixedForwardRef = <T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactElement
) => (props: P & React.RefAttributes<T>) => React.ReactElement;
// Cast the old `forwardRef` to the new one
export const fixedForwardRef =
forwardRef as FixedForwardRef;
@sunpietro
sunpietro / countries-list.json
Created January 20, 2023 11:27
Countries list with ISO codes and names in English
[
{
"name": "Afghanistan",
"code": "AF / AFG"
},
{
"name": "Albania",
"code": "AL / ALB"
},
{
@sunpietro
sunpietro / getEmojiFlag.js
Created March 29, 2022 09:37
Get country flag as emoji
const getEmojiFlag = (code) => {
const codePoints = code
.toUpperCase()
.split('')
.map(char => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
getEmojiFlag('pl'); // 🇵🇱
@sunpietro
sunpietro / detectDSTTimeZoneOffset.js
Created March 14, 2022 08:33
Detecting time zone offset change in DST
const calculateDSTDiff = (timezone) => {
const januaryOffset = moment.tz({ month: 0, day: 1 }, timezone).utcOffset();
const juneOffset = moment.tz({ month: 5, day: 1 }, timezone).utcOffset();
return Math.abs(juneOffset - januaryOffset);
};
@sunpietro
sunpietro / random-date.js
Created April 7, 2019 12:09 — forked from miguelmota/randomDate.js
Random date in JavaScript
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
console.log(randomDate(new Date(2012, 0, 1), new Date()));
@sunpietro
sunpietro / sortable-paginated-table.jsx
Created April 24, 2018 09:00
An example of ReactJS table that has sorting, pagination implemented using getDerivedStateFromProps
import React from "react";
import { render } from "react-dom";
import { MemoizedFlow } from "react-memoize";
const StatelessTable = ({ list, page, onPageChange, onSortChange }) => (
<table border={1} cellPadding={5}>
<thead>
<tr>
<th># counter</th>
<th>data from "list"</th>
@sunpietro
sunpietro / remove_git_tag
Created November 6, 2017 14:05 — forked from dearaujoj/remove_git_tag
git remove tag locally and remote
git tag -d TagName && git push origin :refs/tags/TagName
@sunpietro
sunpietro / countries.json
Created April 2, 2017 19:01
List of countries with 2-letter and 3-letter country codes
[
{"name":"Afghanistan","let2":" AF","let3":"AFG"},
{"name":"Albania","let2":"AL","let3":"ALB"},
{"name":"Algeria","let2":"DZ","let3":"DZA"},
{"name":"American Samoa","let2":"AS","let3":"ASM"},
{"name":"Andorra","let2":"AD","let3":"AND"},
{"name":"Angola","let2":"AO","let3":"AGO"},
{"name":"Anguilla","let2":"AI","let3":"AIA"},
{"name":"Antigua and Barbuda","let2":"AG","let3":"ATG"},
{"name":"Argentina","let2":"AR","let3":"ARG"},
@sunpietro
sunpietro / services.yml
Created July 20, 2016 11:52
services.yml
services:
my.block.custom:
class: My\BlockBundle\Block\CustomBlock
tags:
- { name: landing_page_field_type.block_type, alias: custom }