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 / 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 / subscribe.js
Created September 8, 2020 07:23
Make subscriber
export const makeSubscriber = (subscribers) => ({
subscribe(callback) {
subscribers.push(callback);
},
});
@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 / 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 / todo.js
Created September 8, 2020 13:45
Create Todo Item
import { generateId } from "../id/index.js";
export const createTodoBase = (text) => ({ text, done: false });
export const createTodoItem = (todoObj, idObj) => ({
...todoObj,
...idObj,
});
export const getTodoItem = (text) => {
@perjo927
perjo927 / todos.test.js
Created September 8, 2020 13:47
Test Todo Generation
import {
createTodoBase,
createTodoItem,
getTodoItem,
} from "../src/factories/todo/index.js";
import assert from "assert";
describe("factories", () => {
describe("todo", () => {
@perjo927
perjo927 / actions.js
Last active September 9, 2020 05:37
Todo App Actions
export const CONSTS = {
actions: {
ADD: "ADD",
DELETE: "DELETE",
UNDO: "UNDO",
REDO: "REDO",
TOGGLE: "TOGGLE",
SET_VISIBILITY: "SET_VISIBILITY",
},
visibilityFilters: {
@perjo927
perjo927 / visibility.js
Created September 8, 2020 14:29
Visibility reducer
import { CONSTS } from "../actions";
const {
actions: { SET_VISIBILITY },
visibilityFilters: { ALL },
} = CONSTS;
export const setVisibility = (state, filter) => filter;
export const visibilityReducer = (state = ALL, action) =>
@perjo927
perjo927 / todos.js
Created September 8, 2020 14:43
Todo Reducers
import { CONSTS } from "../actions/index.js";
const {
actions: { ADD, DELETE, TOGGLE },
} = CONSTS;
export const addTodo = (state, newTodo) => [...state, newTodo];
export const deleteTodo = (state, id) => state.filter((todo) => todo.id !== id);
@perjo927
perjo927 / undoable.js
Created September 8, 2020 14:50
Undoable reducer
import { CONSTS } from "../actions/index.js";
const {
actions: { UNDO, REDO },
} = CONSTS;
/*
Read more at:
https://redux.js.org/recipes/implementing-undo-history