Skip to content

Instantly share code, notes, and snippets.

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

Alvaro Nicoli mrnkr

🏠
Working from home
View GitHub Profile
@mrnkr
mrnkr / List.tsx
Last active February 10, 2019 19:18
React List component which implements infinite scroll automatically with intersection observer
import React, { Component } from 'react';
import get from 'lodash/get';
import EmptyListPlaceholder from './list/EmptyListPlaceholder';
import Loading from './list/Loading';
import './list/List.scss';
interface Props<T> {
className?: string;
grid?: boolean;
@mrnkr
mrnkr / LazyImage.tsx
Created February 3, 2019 22:34
React component which renders a blurred preview of an image until it enters the viewport (uses intersection observer)
import React, { Component, CSSProperties } from 'react';
export interface Props {
'className'?: string;
'style'?: CSSProperties;
'src': string;
'data-src': string;
[key: string]: any;
}
@mrnkr
mrnkr / Gallery.tsx
Last active February 10, 2019 19:20
React carousel implemented using css transformations and hammerjs
@mrnkr
mrnkr / Cell.scss
Created February 10, 2019 19:22
React list cell implementation with buttons hidden below the cell content when on mobile (alla Apple Mail on iOS)
.my-cell {
position: relative;
height: 64px;
width: 100%;
margin-top: 1rem;
margin-bottom: 1rem;
.my-cell-content {
position: absolute;
top: 0;
@mrnkr
mrnkr / auth.ts
Created February 10, 2019 21:20
Redux middleware to exchange a refresh token prior to any request if the access token is expired
async function refresh(dispatch: Dispatch, getState: () => MyState): Promise<void> {
const { auth: { authenticated, refresh_token: refresh } } = getState();
if (!authenticated)
return;
try {
const { access_token, refresh_token, token_type } = await (async () => {
const res = await fetch(`${baseUrl}/oauth2/token`, {
method: 'POST',
@mrnkr
mrnkr / FileUpload.tsx
Created February 13, 2019 02:33
Simple React component which handles the uploading of jpeg images to my rest api
import React, { Component } from 'react';
import { connect } from 'react-redux';
import uuid from 'uuid/v4';
import { baseUrl } from '../../environment.json';
import { MyState, MyThunkDispatch } from '../../typings/state';
import { User } from '../../typings/user';
type Type =
'avatar' |
'item' |
@mrnkr
mrnkr / phone.js
Created March 3, 2019 16:55
Uruguayan phone number validation regex for javascript
// accepts cell phone numbers like 091345876 and allows for spacing every three numbers
// accepts landline numbers like 26230945 (optional spacing 2 623 09 45)
const phone = /^((09[1-9](\s?)([0-9]{3})(\s?)([0-9]{3}))|((2|4)(\s?)([0-9]{3})(\s?)([0-9]{2})(\s?)([0-9]{2})))$/g;
@mrnkr
mrnkr / Program.cs
Created June 10, 2019 17:29
A simple example of reflection. Getting the first class that implements a given interface and calling one of its methods.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using FindMe;
namespace TestProj
{
class Program
{
@mrnkr
mrnkr / RectangleFinder.hs
Created December 16, 2019 14:16
Quick coding challenge - find all horizontal rectangles formed given a list of points
module RectangleFinder where
import Prelude hiding ((*))
import qualified Data.Map as Map
type Point = (Int, Int)
countRects :: [Point] -> Int
countRects pts = fst $ foldl processPoints (0, Map.empty) $ filter isAbove $ pts * pts
@mrnkr
mrnkr / profit.c
Last active December 16, 2019 14:20
Quick coding challenge - given an array of numbers let i the current element, calculate x as the difference between i and some element after it so as to maximize x
#include<stdio.h>
int max(int a, int b) {
return a > b ? a : b;
}
int max_profit(int* values, size_t len) {
if (len == 1) return 0;
int ret = 0;