Skip to content

Instantly share code, notes, and snippets.

View jeffscottward's full-sized avatar
👷‍♂️
BUIDLing

Jeff Scott Ward jeffscottward

👷‍♂️
BUIDLing
View GitHub Profile
@jeffscottward
jeffscottward / app.js
Last active May 2, 2017 06:11
Express 4 version of app.js of "How I Setup Angular + Node Projects" by J Cole Morrison
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
@jeffscottward
jeffscottward / StateProvider.js
Created July 30, 2019 02:58
State Management in 10 lines w/ React Context & Hooks
// From https://bit.ly/2XH31rw
import React, { createContext, useContext, useReducer } from "react";
export const StateContext = createContext();
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
@jeffscottward
jeffscottward / Async-Await-getInitialProps-nextjs.js
Last active August 5, 2019 15:50
Next.js Data querying example
import React, { Component } from 'react'
const Index = () => (
<div className="IndexPage">
<label>{this.props.APIdata.key}</label>
<span>{this.props.APIdata.value}</span>
<style jsx>{`
.IndexPage { color: blue; }
`}</style>
</div>
@jeffscottward
jeffscottward / Sample-React-LoadingSpinner.js
Last active August 5, 2019 15:53
Just a fast prototype of CSS animated loading spinner
const LoadingSpinner = () => (
<div className="loadingSpinner">
<style jsx>{`
.loadingSpinner {
display: inline-block;
position: relative;
width: 300px;
height: 300px;
transform: translateZ(0);
}
@jeffscottward
jeffscottward / Sample-Reducer.js
Created August 5, 2019 15:55
Reducer to use with useStateValue
// Inside component use
// const [{data}, dispatch] = useStateValue();
// REDUCER
export default function (state, action) {
switch (action.type) {
case "SET_DATA":
return {
...state,
@jeffscottward
jeffscottward / async-use-effect.js
Created August 5, 2019 15:56
Async function w/ useEffect
useEffect(() => {
// React.js recommends embedding self-executing async function w/ useEffect
(async function getit() {
// Fuzzy filter is also ready for filtering by 'name' field from payload of /comments
const result = await get("https://jsonplaceholder.typicode.com/todos");
const jsonData = result.data;
defineInitialColumnSortStates(jsonData);
addButtonColumn(jsonData);
dispatch({
type: "SET_DATA",
@jeffscottward
jeffscottward / DOMScrape.js
Created August 5, 2019 15:58
Get Clean DOM for WebScrapping
const cheerio = require('cheerio')
const cleaner = require('clean-html')
const axios = require('axios')
module.exports = async function getCleanDOM (url) {
// Wrap in a recusive retry func
async function retry() {
try {
// AJAX to get HTML
this.siteRequest = await axios.get(url)
@jeffscottward
jeffscottward / traversal.js
Created August 5, 2019 16:01
Recursive Depth Traversal of JSON to Build DOM
let data = [
{
title: "menu 1",
children :[
{ title: "menu 1.1"},
{
title: "menu 1.2",
children: [
{title: "menu 1.2.1"},
{title: "menu 1.2.2"},
@jeffscottward
jeffscottward / BinarySearch.js
Created August 5, 2019 16:02
Binary Search Example
const doSearch = function (array, targetValue) {
let minIndex = 0;
let maxIndex = array.length - 1;
let guessIndex;
console.log(minIndex,maxIndex,guessIndex)
while(maxIndex >= minIndex){
guessIndex = Math.floor((maxIndex+minIndex)/2)
console.log({minIndex, max, guessIndex});
@jeffscottward
jeffscottward / JS-Practice.js
Created August 5, 2019 16:07
Javascript method practice
import axios from "axios";
// ====================================================
// https://codeburst.io/all-about-javascript-arrays-in-1-article-39da12170b1c
// DONT USE DELETE ON ARRAYS - use proper methods
export const myAsyncFunc = async url => {
// const payload = await window.fetch(url);
// const payloadJSON = await payload.json();
const payload = await axios(url);
const data = payload.data