Skip to content

Instantly share code, notes, and snippets.

View sgb-io's full-sized avatar
🎯
Focusing

Sam Brown sgb-io

🎯
Focusing
View GitHub Profile
import React, { useState, useEffect } from "react";
type NativeEventInputType =
| "insertText"
| "deleteContentBackward"
| "insertFromPaste"
| "formatBold"
| "deleteByCut";
function isNativeEventTyping(inputType: NativeEventInputType) {
@sgb-io
sgb-io / interest.ts
Created April 10, 2023 14:37
Compound Interest
interface InvestmentPeriod {
periodNumber: number;
startingBalance: number;
additionalInvestment: number;
periodInterest: number;
endingBalance: number;
}
interface CompoundResult {
startingBalance: number;
@sgb-io
sgb-io / persistence.ts
Last active October 31, 2021 20:42
Recoil URL persistence example
import type { AtomEffect } from "recoil";
import qs from "query-string";
import { atomDefaults } from "./defaults"; // Contain some default values e.g. { foo: 12, bar: 'hello' }
// In this example, we opt to store some state in the URL as raw params, and the rest as a base64 encoded string in the special `state` param
// e.g. http://localhost:3000?foo=12&bar=hello&state=<base 64 string of some other values>
const IS_BROWSER = typeof window !== "undefined";
const getWindowSearchLocation = () => {
@sgb-io
sgb-io / recoil-url-persistence.ts
Created January 25, 2021 15:53
Example Recoil Atom effect that syncs Atom states to the URL (search params).
import type { AtomEffect } from 'recoil'
import qs from 'query-string'
import { atomDefaults } from './defaults'
const getStateFromUrl = () => {
return qs.parse(window.location.search, {
parseBooleans: true,
parseNumbers: true,
})
}
// String.prototype.padStart + String.prototype.padEnd (ES2017)
function stringPadding(str, width, char) {
const msgs = str.split(' ')
msgs.forEach((msg) => {
const wordLength = msg.length
let totalPadding = width - wordLength
const padding = {
start: totalPadding / 2,
@sgb-io
sgb-io / asyncFunctionsExample.js
Created October 9, 2017 15:04
Example usage of async functions (ES7)
function foo(delay) {
return new Promise(resolve => {
setTimeout(() => {
resolve(Math.random())
}, delay)
})
}
async function run(delaySeconds) {
const a = foo(delaySeconds * 1000)
// Combine these to write sentences!
(+!+[]) // 1 - use it to pick an index from any of the strings in this list
(-(+!+[])+(+!+[])) // 0
([![]]+[][[]]) // "falseundefined", you can then pick a character via index by adding 1s
(![]+[]) // "false"
(!+[]+[]) // "true"
// JavaScript return types we are exploiting:
// false, true, undefined, 0, 1
@sgb-io
sgb-io / raw.json
Created February 1, 2017 10:51
Visit Planner
{"status":200,"message":"OK","body":{"titleInternal":"Visit Planner Page","pageProfiles":{"titleInternal":"Visit Planner Page (default)","title":{"titleInternal":"Visit Planner Page: Title","text":"Plan your day at the Museum","contextProfile":null,"link":[],"contentType":"contextualShortText"},"description":{"titleInternal":"Page profile: Visit Planner Page: Description","text":"We've put together some key information to help you customise your itinerary for your visit to the Museum.","title":[],"contextProfile":null,"link":[],"contentType":"contextualLongText"},"heroWidget":[],"heroFooterBlock":[],"leadImage":{"titleInternal":"Contextual image: Page divider: Clock detail","image":{"titleInternal":"Image: Page divider: Clock detail","notesInternal":null,"caption":null,"image":{"title":"visual banner objects 4 1920x1000","file":{"fileName":"visual_banner_objects_4_1920x1000.jpg","contentType":"image/jpeg","details":{"size":1655645,"image":{"width":1920,"height":1000}},"url":"//images.contentful.com/ux95v64xm8
@sgb-io
sgb-io / .bash_profile
Created July 25, 2013 23:51
Peep Show essentials for terminal.
# Welcome message
open_green_escape="\033[36m"
open_yellow_escape="\033[31m"
close_escape="\033[0m"
# Some of my fave peep show quotes.
funny_quotes=(
'What if I lose it? Im not gonna do a poo am I, Jez?'
'For the worst thing that could possibly happen, this is actually going extremely well.'
'Is this a terrible idea? It cant be. Its in a film. They wouldnt put a terrible idea in a film, theyd get sued.'
INSERT INTO `videos` (`url`, `id`, `title`, `description`) VALUES
('7rl6gtTrj04', 171, 'Snoop Dogg X Wiz Khalifa - That Good (HD/Download)', 'http://smokethechronic.tumblr.com/\r\n\r\nSnoop Dogg X Wiz Khalifa - That Good (HD/Download)\r\nSnoop Dogg X Wiz Khalifa - That Good (HD/Download)\r\nSnoop Dogg X Wiz Khalifa - That Good (HD/Download)\r\n\r\nDownload @ http://hulkshare.com/a2mkyr07mqx7'),
('fBFECD00fGw', 107, 'Emalkay - A.G.S. (Angie Got Stoned)', 'Subscribe if you like the videos.\r\nAll Copyright©(P) Goes To The Rightfull Owners of the Tracks. \r\nPlease Check Out My Website http://www.UpBeat-Music.co.uk\r\n\r\nThanks! :)\r\n\r\nBuy this track here\r\nhttp://www.chemical-records.co.uk/sc/servlet/Info?Track=DP034'),
('ldQUDv4bxxI', 108, 'Distance - Falling (Original Radio Edit)', 'big tune from Distance.'),
('_4vKWFCWw04', 68, 'Xxplosive - Dr. Dre', 'R.I.P. Nate Dogg\r\n\r\nClassic track by Dr. Dre from his album 2001\r\n\r\nSong: Xxplosive\r\nArtist: Dr. Dre ft. Hittman, Kurupt, Nate Dogg, & Six-Two\