Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
mgtitimoli / Test.tsx
Created Jan 21, 2021
StateAccessor (getter, setter) Hook => useStateAccessor
View Test.tsx
import React from "react";
import type {ReactStateAccessor} from "./useStateAccessor";
type State = {
count: number;
};
type StateAccessor = ReactStateAccessor<State>
View useNonCachedQuery.ts
import {useEffect} from "react";
import {useQuery, useQueryClient} from "react-query";
import type {
QueryKey,
QueryFunction,
UseQueryOptions,
UseQueryResult
} from "react-query";
@mgtitimoli
mgtitimoli / httpStatusCode.ts
Created Jul 4, 2020
TypeScript HTTP Status Codes
View httpStatusCode.ts
/**
* Hypertext Transfer Protocol (HTTP) response status codes.
* @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes}
*/
const httpStatusCodes = {
/**
* The server has received the request headers and the client should proceed to send the request body
* (in the case of a request for which a body needs to be sent; for example, a POST request).
* Sending a large request body to a server after a request has been rejected for inappropriate headers would be inefficient.
* To have a server check the request's headers, a client must send Expect: 100-continue as a header in its initial request
@mgtitimoli
mgtitimoli / README.md
Last active May 31, 2020
Find the minimum number of flips to get an alternating array of binary numbers
View README.md

Exercise

Given an array of binary numbers, find the minimum number of flips (inversions) required to get an alternating version of it.

Solution

Given that we are dealing with binary numbers, there are 2 possible alternating versions of it, the one that starts with 0, and the one that starts with 1. This means that the input array should be compared with these 2 possibilities, get the number of required flips on each of them, and finally the result will be the minimum of these 2 counts.

@mgtitimoli
mgtitimoli / sortCsvFile.ts
Created Apr 23, 2020
Sort in Node.JS using OS commands
View sortCsvFile.ts
import * as childProcess from 'child_process';
import { promisify } from 'util';
type OptionalParams = {
fieldSeparator?: string;
hasHeader?: boolean;
};
type MandatoryParams = {
byColumn: number;
View git-diff-with-filter.sh
git diff --name-only | grep -v $EXCLUDE_PATTERN | xargs git diff $OTHER_OPTIONS --
View createStore.js
// @flow
import React, {createContext, useContext, useState} from "react";
import type {Context, Node, StatelessFunctionalComponent} from "react";
import useEffectOnUpdate from "./useEffectOnUpdate";
type UseStateStateUpdater<TState> = (state: TState) => TState;
@mgtitimoli
mgtitimoli / createCancellable.js
Last active Apr 5, 2018
Yet another cancellation promise module
View createCancellable.js
// @flow
type CancellablePromise<Result> = {
promise: Promise<Result>,
cancel: (reason?: string) => void
};
// $FlowFixMe: can not add custom properties to Error
const setCancelled = (error, cancelled) => Object.assign(error, {cancelled});
View versions.gradle
allprojects {
ext {
buildTools = "27.0.3"
compileSdk = 27
googlePlayServicesVersion = "11.8.0"
minSdk = 16
targetSdk = 23
}
configurations.all {
@mgtitimoli
mgtitimoli / set-state.js
Last active Mar 3, 2018
Promisified Conditional setState
View set-state.js
// @flow
import {curry} from "flow-static-land/lib/Fun";
import type {ComponentType, ElementRef} from "react";
type State = Object;
type Updater<Props: Object> = (prevState: State, props: Props) => State;