Skip to content

Instantly share code, notes, and snippets.

View tomasswood's full-sized avatar

Thomas Wood tomasswood

View GitHub Profile
@tomasswood
tomasswood / CachedImage.tsx
Created December 29, 2021 22:49
React Native / Expo Fast Cached Image
import React, { useEffect, useState } from 'react';
import { Image, ImageProps, ImageURISource } from 'react-native';
import * as FileSystem from 'expo-file-system';
import * as Crypto from 'expo-crypto';
const getImageFileSystemKey = async (remoteURI: string) => {
const fileHash = await Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, remoteURI);
return `${FileSystem.cacheDirectory}${fileHash}`;
};
let r = /(\w{2,4}-\d+)/gm;
let t = Array.from(document.getElementsByClassName('subject')).reduce((acc, node) => {
const match = node.innerText.match(r);
if (match && match.length > 0) {
return acc.add(...match.map((m) => m.toUpperCase()))
}
return acc;
}, new Set());
console.log([...t].sort().join("\n"))
@tomasswood
tomasswood / machine.js
Created March 19, 2020 04:19
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@tomasswood
tomasswood / logMapStateToPropsChanges.js
Last active May 20, 2019 06:19
Log mapStateToProps changes and see what properties are actually changing and causing re-renders
/**
* Log a table in the console of all provided props and whether they have strict equality
*
* @param oldProps
* @param newProps
* @constructor
*/
export const PropDiff = (oldProps, newProps) => {
let match = {};
Object.keys(oldProps).forEach((oldPropKey) => {
@tomasswood
tomasswood / createNamedReducer.js
Created November 13, 2018 21:36
Reuse the same redux reducer code for a different slice of state
/**
* Higher-order reducer factory for creating named reducers.
* Both params are required, and it will return your reducer or state.
*
* @param key
* @param reducer
* @returns {function}
*/
const createNamedReducer = (key, reducer) => {
return (state, action) => {
@tomasswood
tomasswood / addParamToAction.js
Created November 13, 2018 21:27
Add a param to an action, eg. Overload an action with a set parameter at the start
/**
* Higher order function that takes a param, and uses it as the first argument for another function
*
* @param key
* @param action
* @returns {function(...[*]): *}
*/
const addParamToAction = (param, action) => {
return (...rest) => action(param, ...rest);
};
@tomasswood
tomasswood / setTimeoutLoop.js
Last active November 13, 2018 21:33
Self looping polling function that waits until each request has finished before it waits before attempting another request. Can also be Cancelled.
/**
* Sleep timer that can also cancel the request
*
* @param ms
* @param isCancelled
* @returns {Promise<any>}
*/
const sleep = (ms, isCancelled) => {
let timeoutHandler;