Skip to content

Instantly share code, notes, and snippets.

View rphansen91's full-sized avatar

Ryan Hansen rphansen91

  • Antengo
  • San Diego
View GitHub Profile
@rphansen91
rphansen91 / index.js
Last active December 14, 2016 18:25
React From Scratch in ~100 lines
import Component from './toy-react'
import { div, img } from './toy-jsx'
class Nested extends Component {
render () {
const { src, style } = this.props;
return img({ src, style })
}
}
@rphansen91
rphansen91 / App.js
Last active November 4, 2018 01:48
React redux connect implementation using react hooks.
import React from 'react';
import useStore from './useStore';
import Todo from './Todo'
import { todoActions } from './actions';
export default () => {
const [
todos,
{ addTodo, rmTodo }
] = useStore(store => store.todos, todoActions)

Make Consecutive Array

Given the following array, [6, 2, 3, 8] find the minimum number of entries needed to make the array consecutive

[2, 3, *4*, *5*, 6, *7*, 8] = 3 entries needed. 

Test cases

makeConsecutive([0, 2]) === 2
@rphansen91
rphansen91 / ContextCounter.tsx
Last active June 14, 2020 19:42
react-hawk vs context vs recoil examples
import React, {
createContext,
useContext,
useState,
FC,
Dispatch,
SetStateAction,
} from "react";
import Counter from "../Components/Counter";
@rphansen91
rphansen91 / RecoilCounter.tsx
Created June 14, 2020 19:42
RecoilCounter.tsx
import React from 'react';
import { atom, selector, useRecoilValue, useSetRecoilState } from 'recoil';
import Counter from '../Components/Counter'
const counterState = atom({
key: 'counter',
default: 0
});
@rphansen91
rphansen91 / HawkCounter.tsx
Created June 14, 2020 19:43
HawkCounter.tsx
import React from 'react';
import { hawk, hawkeye, useHawkState, useHawkSetState } from 'react-hawk';
import Counter from '../Components/Counter'
const counterState = hawk({
key: 'counter',
default: 0
});
@rphansen91
rphansen91 / MountainSchema.ts
Created June 30, 2020 06:08
MountainSchema.ts
import gql from 'graphql-tag'
export const mountainSchema = gql`
type Mountain @collection(name: "mountains", crud: true) {
id: ObjectId
name: String @insert @set @filter
meters: Float @insert @set @filter
feet: Float @insert @set @filter
location: String @insert @set @filter
}
{
"schema": ["./node_modules/ts-mongo-codegen/dist/types/mongo-types.ts", "./src/gql/*.ts"],
"generates": {
"./src/types.generated.ts": {
"plugins": [
"typescript",
"typescript-operations",
"typescript-resolvers",
"ts-mongo-codegen"
]
import { makeExecutableSchema } from '@graphql-tools/schema'
import { addResolveFunctionsToSchema } from 'apollo-server'
import { graphqlTypeDate, graphqlTypeObjectId, makeAugmentedSchema, mongoTypeDefs } from 'ts-mongo-codegen'
// The following import is the file generated in previous step
import { mountainMutationResolvers, mountainQueryResolvers, mountainResolvers } from './types.generated'
import { composeResolvers } from '@graphql-tools/resolvers-composition'
import { mountainSchema } from './gql/mountains'
import { isAuthenticated } from './auth'
// Make an executable schema with the mongo types and our custom mountain schema type
@rphansen91
rphansen91 / estimate.js
Last active August 5, 2020 20:33
Calculate estimated time until next NRG Masternode reward
const nodeApi = "https://nodeapi.energi.network/";
const owner = "YOUR_MASTERNODE_OWNER_ADDRESS"
estimateBlocksTilNextMasternodeReward(owner)
.then(({ estimatedTimestamp }) => console.log(`Next reward at ${new Date(estimatedTimestamp).toLocaleString()}`))
function listMasternodes () {
return rpc(nodeApi, "masternode_listMasternodes");
}