Skip to content

Instantly share code, notes, and snippets.

View nickbalestra's full-sized avatar

Nick Balestra-Foster nickbalestra

View GitHub Profile
@nickbalestra
nickbalestra / debounce.js
Created April 7, 2018 01:25
debounce Jest Workshop
/*
* Debounce a function
*
* Given a function and a delay in milliseconds create a way to debounce the given function so that,
* as long as it continues to be invoked, will not be triggered.
* The function will be called after it stops being called the given delay.
*
*/
const debounce = (fn, delay) => {
@nickbalestra
nickbalestra / twoSum.js
Last active April 7, 2018 02:34
TwoSum Jest Workshop Example
/*
* Two Sum
*
* Given an array of integers, return indices of the two numbers
* such that they add up to a specific target.
*
* You may assume that each input would have exactly one solution,
* and you may not use the same element twice.
*
* Example:
function compat(arr) {
const compatMap = new Map();
arr.forEach(tuple => {
const tupleToCompat = compatMap.get(tuple[0]);
if (tupleToCompat) {
compatMap.set(tuple[1], tupleToCompat.concat(tuple[1]));
compatMap.delete(tuple[0]);
} else {
compatMap.set(tuple[1], tuple);
@nickbalestra
nickbalestra / shiftedArrSearch.js
Last active March 23, 2018 22:07
Shifted Array Search
// A characteristic of a shifted sorted array
// is that if you brake it into two halves,
// at LEAST ONE half is ALWAYS going to be sorted:
// 4 5 6 [7] 0 1 2 -> left side is sorted
// 6 7 0 [1] 2 4 5 -> right side is sorted
// ...
// 5 6 7 [0] 1 2 4 -> both side sorted (pivot === shift)
// This means we can use this informatin to apply a BST strategy without having to look for the pivot
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const oc = require('oc');
const client = new oc.Client({
registries: {
clientRendering: 'http://localhost:3000/',
serverRendering: 'http://localhost:3000/'
}
import React from 'react';
import styles from './map.css';
import GoogleMap from './google-map';
import mapData from '../../test/geo.json';
export function Map(props) {
let map = (
<GoogleMap
markerData={mapData.features}
centerPoint={{ lat: 0, lng: 0 }}
import React from 'react';
import styles from './map.css';
import GoogleMap from './google-map';
import mapData from '../../test/geo.json';
export function Map(props) {
let map = (
<GoogleMap
markerData={mapData.features}
centerPoint={{ lat: 51.5285, lng: -0.0847 }}
import xs from 'xstream';
const model = action$ => {
const reducer = (state = 0, action) => {
switch(action.type) {
case 'INCREASE':
return state + 1
case 'DECREASE':
return state - 1
default:
import reducer, { types, actions, cycle } from './user'
describe('actions', () => {
// snapshot tests
it('should export specific action types', () => {
expect(types).toMatchSnapshot()
})
it('should export specific action creators', () => {
expect(actions).toMatchSnapshot()
})
@nickbalestra
nickbalestra / duck-on-wheels.js
Created December 28, 2016 20:51
An example of Duck Redux Reducer Bundle with Cycle.js apps to handle async
import xs from 'xstream'
import { combineCycles } from 'redux-cycle-middleware'
import { push } from 'react-router-redux'
import { API_URL } from '../constants/api'
// ACTION TYPES (Format: app-name/reducer/ACTION_TYPE)
// =======================================================
const LOGIN = 'app-name/auth/LOGIN'
const LOGIN_SUCCESS = 'app-name/auth/LOGIN_SUCCESS'
const LOGIN_FAIL = 'app-name/auth/LOGIN_FAIL'