Skip to content

Instantly share code, notes, and snippets.

View perjo927's full-sized avatar
πŸ§‘β€πŸ’»

Per Jonsson perjo927

πŸ§‘β€πŸ’»
  • DevCode
  • Stockholm, Sweden
View GitHub Profile
@perjo927
perjo927 / store.js
Created September 8, 2020 07:36
Create Redux Store
import { makeSubscriber } from "./subscribe";
import { makeStateHandlers } from "./state";
import { makeDispatcher } from "./dispatch";
export const createStore = (reducer, initialState = {}) => {
const stateContainer = [initialState];
const subscribers = [];
const stateHandlers = {
...makeStateHandlers(stateContainer),
};
@perjo927
perjo927 / subscribe.test.js
Created September 8, 2020 07:27
Test subscriber
import { makeSubscriber } from "../src/redux/store/subscribe.js";
import assert from "assert";
describe("subscribe.js", () => {
describe("makeSubscriber", () => {
it("generates a function from subscribers", () => {
const subscribers = [];
const subscriber = makeSubscriber(subscribers);
@perjo927
perjo927 / subscribe.js
Created September 8, 2020 07:23
Make subscriber
export const makeSubscriber = (subscribers) => ({
subscribe(callback) {
subscribers.push(callback);
},
});
@perjo927
perjo927 / dispatch.test.js
Last active September 8, 2020 07:27
Test dispatch
import { makeDispatcher } from "../src/redux/store/dispatch.js";
import assert from "assert";
describe("dispatch.js", () => {
describe("makeDispatcher", () => {
it("generates a function from state handlers, a reducer and a callback", () => {
const stateHandlers = {
getState() {},
setState(arg) {},
};
@perjo927
perjo927 / state.js
Last active September 8, 2020 13:19
Make state handlers
export const makeStateHandlers = (stateContainer) => ({
getState() {
const [lastState] = stateContainer.slice(-1);
return lastState;
},
setState(newState) {
return stateContainer.push(newState);
},
});
@perjo927
perjo927 / dispatch.js
Created September 8, 2020 07:06
Make dispatcher
export const makeDispatcher = (stateHandlers, reducer, onDispatch) => ({
dispatch(action) {
const { getState, setState } = stateHandlers;
const state = getState();
const newState = reducer(state, action);
setState(newState);
onDispatch();
import { makeStateHandlers } from "../src/redux/store/state.js";
import assert from "assert";
describe("state.js", () => {
describe("makeStateHandlers", () => {
it("generates two functions from a state container input", () => {
const stateContainer = [{ state: "state" }];
const stateHandlers = makeStateHandlers(stateContainer);
@perjo927
perjo927 / index.html
Created September 7, 2020 17:15
Todo App Starter
<html>
<head>
<title>Todo App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script src="./index.js"></script>
</body>
</html>
@perjo927
perjo927 / compose.test.js
Last active September 8, 2020 08:55
Proof for function composition
import assert from "assert";
const compose = (...functions) => (initialArg) =>
functions.reduceRight(
(accumulatedValue, func) => func(accumulatedValue),
initialArg
);
describe("compose", () => {
it("returns a composed function h, so that h(x) is equal to f(g(x))", () => {
@perjo927
perjo927 / gitbot.ps1
Last active July 29, 2018 09:21
A git bot for Windows.
<#
Gitbot
Makes Your Git Stats Great
#>
param([string]$path="C:\")
function GenerateName {
$filepath = Resolve-Path "words.txt"
$raw = Get-Content -Path $filepath