Skip to content

Instantly share code, notes, and snippets.

View johnpolacek's full-sized avatar

John Polacek johnpolacek

View GitHub Profile
@johnpolacek
johnpolacek / stockBuySell.js
Last active August 31, 2020 16:52
Given an array of numbers that represent stock prices (where each number is the price for a certain day), find 2 days when you should buy and sell your stock for the highest profit.
const stockBuySell = (days) => {
const best = days.reduce((bestResult, price, i) => {
const buyDays = days.slice(i+1)
if (buyDays.length) {
const highest = Math.max.apply(null, days.slice(i+1))
const roi = highest - price
if (roi > bestResult.roi) {
bestResult = {buyDay: i, sellDay: days.indexOf(highest), roi}
}
}
@johnpolacek
johnpolacek / prettier.package.json
Created August 15, 2020 02:25
NPM prettier format and format-watch script
{
"scripts": {
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"format-watch": "npm run format && onchange \"**/*.{js,jsx,json,md}\" -- prettier --write {{changed}}",
},
"devDependencies": {
"onchange": "^7.0.2",
"prettier": "^2.0.5"
}
}
@johnpolacek
johnpolacek / _app.js
Created May 17, 2020 20:54
Moving your context providers into _app.js prevents re-rendering of your context consuming components when navigating between pages in Next.js.
import { ThemeProvider } from "theme-ui"
import theme from "../components/theme"
import { UserProvider } from "../components/context/UserContext"
function MyApp({ Component, pageProps }) {
return (
<ThemeProvider theme={theme}>
<UserProvider user={pageProps.user}>
<Component {...pageProps} />
</UserProvider>
import React from 'react'
export default (props) => {
const words = props.children.split(' ')
return <>{words.map((word, i) => word + (i > words.length-((props.widows || 1)+1) ? '\u00a0' : ' ')).join('')}</>
}
@johnpolacek
johnpolacek / ResponsiveWidows.js
Created February 24, 2019 21:33
React component for controlling widows for responsive line breaks in your text
import PropTypes from 'prop-types'
import React from 'react'
class ResponsiveWidows extends React.Component {
constructor() {
super()
this.state = {}
}
componentDidMount() {
@johnpolacek
johnpolacek / jsrender.loadTemplates.js
Created June 2, 2017 12:10
Load and cache multiple external jsrender templates
function loadTemplates() {
var toLoad = [],
loadAll = $.Deferred();
$.each(arguments, function(i, templateName) {
var filepath = '/path/to/templates/' + templateName + '.html',
loadTemplate = $.Deferred();
toLoad.push(loadTemplate);
@johnpolacek
johnpolacek / gist:c7b4a1807623fb84173e
Created May 30, 2015 13:38
Selenium Wait For Page Load
public static void waitForPageLoad(WebDriver drv) {
sleep(50); // Make sure new page init has started...
try {
(new WebDriverWait(drv, 5))
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return ((JavascriptExecutor)d).executeScript("return document.readyState").equals("complete");
}
});
} catch (TimeoutException ex) {
@johnpolacek
johnpolacek / .gitconfig
Last active April 30, 2024 06:00
My current .gitconfig aliases
[alias]
co = checkout
cob = checkout -b
coo = !git fetch && git checkout
br = branch
brd = branch -d
brD = branch -D
merged = branch --merged
st = status
aa = add -A .
@johnpolacek
johnpolacek / Fun Data-Binding with jQuery
Last active August 29, 2015 13:59
Simple jQuery Pattern for doing cool data-binding stuff using jQuery.on() and jQuery.trigger() - see the demo at http://cdpn.io/mhCai
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
<p>Simple jQuery Pattern for doing cool data-binding stuff using <code>jQuery.on()</code> and </code>jQuery.trigger()</code></p>
<h1>Messages</h1>
<div id="output"></div>
@johnpolacek
johnpolacek / imageFill.js
Last active February 6, 2018 16:47
The jQuery plugin for making an image fill its container (and be centered) - see http://johnpolacek.github.io/imagefill.js/
/**
* imageFill.js
* Author & copyright (c) 2013: John Polacek
* Dual MIT & GPL license
*
* This gist is out of date now. I've turned this into a proper Github project at http://johnpolacek.github.io/imagefill.js/
*
*/
;(function($) {