Skip to content

Instantly share code, notes, and snippets.

@hamannjames
hamannjames / ApiLeadsController.php
Created July 19, 2017 20:44
The start of a laravel api that tracks leads. This is the controller for leads showing get and post requests (functions index and store)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Lead;
use App\Models\LeadMeta;
use App\Transformers\LeadTransformer;
class ApiLeadsController extends Controller
{
// The transformer reformats the data to uncouple it from the shcema structure
@hamannjames
hamannjames / hashTableLinkedList.js
Last active August 1, 2022 13:34
Javascript hash table with linked lists in buckets
// code pen url: https://codepen.io/jaspercreel/pen/RgXjEp
// Declaring a node and list class outside of the hash class avoids binding issues with this
// The node class should only be created with a key value pair
class Node {
constructor(key, value) {
this[key] = value;
this.next = null;
}
@hamannjames
hamannjames / mapLocator.js
Created July 26, 2017 06:44
A simple jQuery map locator. It was tailored for a specific company so some parts of it are very imperative.
// This store locator makes use the moment.js plugin to help deal with timezones.
/*
Location objects are tailored to a specific model and expected to have a title, address, open and close time, as well as
an array of specific features used for filtering
*/
/*
The initial locator object. Consolidating all the markers, stores, infowindows,
@hamannjames
hamannjames / possibilities.js
Last active July 26, 2017 22:16
Given a set of possibilities, determine the maximum reward in the possibilities given a certain constraint.
/*
For this particular example the constraint is you cannot take two possibilities
that are next to each other. A real world example might be that you cannot invest multiple months
in a row and want to have the best chance at the best ROI. Kind of dumb but the point is
if there are constraints you want to track the best possible values for every sequence of moves,
only keeping track of the best possible outcomes
*/
const bestOutcome = (arr) => {
@hamannjames
hamannjames / _mixins.scss
Created July 26, 2017 18:45
My favorite mixins
$breakpoints: (
"phone": 400px,
"phone-wide": 480px,
"phablet": 560px,
"tablet-small": 640px,
"tablet": 768px,
"tablet-wide": 991px,
"desktop": 1200px,
"desktop-wide": 1440px
);
@hamannjames
hamannjames / binSearch.js
Created July 27, 2017 18:31
Binary search of sorted array values as numbers
// It is expected that the array will be sorted, otherwise you will not get a proper search
// code pen url: https://codepen.io/jaspercreel/pen/yoYYVN
const binSearch = (arr, key) => {
if (!Array.isArray(arr)) {
console.log('This binary search requires an array');
return;
}
if (!key) {
@hamannjames
hamannjames / SorterSearcher.js
Created July 31, 2017 06:52
This is a helper object I am creating that will allow for alternative sorting and searching solutions to the native JS implementation. It sets a standard, and optimizes certain actions, as well as addressing some strange native JS behavior (like treating integers as strings)
const Sorter = function (arr = [], key = null, def = null) {
state = {
arr,
key,
def
}
const proto = {
type: 'Sorter',
function kthLargestInBST(t, k) {
var check = function (value) {
k--;
if (k === 0) {return value;}
else {return false;}
}
var loop = function (node) {
if (!node) {return false}
return loop(node.left) || check(node.value) || loop(node.right)
@hamannjames
hamannjames / largestOfDepth.js
Created August 4, 2017 03:50
Get largest value in tree depth using depth first search
function largestValuesInTreeRows(t) {
function depthSearch(current) {
let values = [];
let depth = 0;
function traverse(current, depth) {
if (!current) {
return;
}
@hamannjames
hamannjames / trueClone.js
Last active August 9, 2017 00:53
This little function deep clones an array, instead of slice which preserves references to objects and nested arrays.
// Parts of this function were borrowed from http://blog.andrewray.me/how-to-clone-a-nested-array-in-javascript/
// Link to code pen: https://codepen.io/jaspercreel/pen/jLmEWL
function trueClone(item) {
let copy, i;
if (Array.isArray(item)) {
copy = item.slice(0);
for (i = 0; i < copy.length; i++) {
copy[i] = trueClone(copy[i]);