Skip to content

Instantly share code, notes, and snippets.

View aGuyNamedJonas's full-sized avatar

Jonas Peeck aGuyNamedJonas

View GitHub Profile
@aGuyNamedJonas
aGuyNamedJonas / README.md
Last active December 4, 2021 16:43
"Giant Squid" - Day 4 AdventOfCode 2021
@aGuyNamedJonas
aGuyNamedJonas / PrettyPrintJson.md
Last active January 29, 2018 10:22
Pretty print piped JSON

Install

  • Download jsonPrint.js
  • Make it executable (chmod +x ~/Download/jsonPrint.js)
  • Move it into your bin folder (mv jsonPrint.js /usr/local/bin/)

Use

curl http://some-random-json-api.com/json/data | jsonPrint

@aGuyNamedJonas
aGuyNamedJonas / computations.js
Created May 16, 2017 13:28
Calculations Example slim-redux-react v0.2
// computations/todo.js
import { compute } from 'slim-redux-react';
export const visibleTodos = compute({
todos: 'state.todos',
filter: 'state.filter',
}, () => {
// Notice how this function has no parameters, but returns a state computation
return todos.filter(todo => (filter === 'ALL' || ('filter' === 'COMPLETED' && todo.completed) || (filter === 'OPEN' && !todo.completed)))
});
@aGuyNamedJonas
aGuyNamedJonas / asyncChangeTrigger.js
Created May 16, 2017 12:40
Async Change Trigger Example slim-redux v0.2
// async/todo.js
import { asyncChangeTrigger } from 'slim-redux-react';
import { addTodoPending, addTodoSuccess, addTodoFailed } from '../changes/todo';
// First argument is a change trigger mapping
export const addTodo = asyncChangeTrigger({ addTodoPending, addTodoSuccess, addTodoFailed }, (label, state) => {
// Create todo in state pending confirmation from server
const newTodoAction = addTodoPending(label);
const newTodoId = newTodoAction.payload.id;
@aGuyNamedJonas
aGuyNamedJonas / changeTrigger.js
Last active May 16, 2017 12:26
Change Trigger Example slim-redux v0.2
// changes/todo.js
import { changeTrigger } from 'slim-redux';
export const addTodo = changeTrigger('ADD_TODO', (label, state) => {
const newId = state.todos.map((max, todo) => Math.max(max, todo,id), 0) + 1;
return {
...state,
todos: [
...state.todos,
{id: newId, label: label, checked: false},
@aGuyNamedJonas
aGuyNamedJonas / Counter.js
Last active June 13, 2017 19:01
Simple slim-redux-react counter example
import React from 'react';
import ReactDOM from 'react-dom';
import { createSlimReduxStore } from 'slim-redux';
import { slimReduxReact, Provider } from 'slim-redux-react';
// STEP #1: Create the slim redux store
// This creates a redux store with the slim-redux functionality injected and the internal reducer initialized
// Parameters: initialState (0), existingRootReducer(null), middleware(redux-devtools browser extension)
const store = createSlimReduxStore(0, null, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
var decrement = store.createChangeTrigger({
actionType: 'DECREMENT',
reducer: (state, payload, action) => {
const value = payload.value || 1;
return state - value;
},
// Payload validation - notice how it returns either reject() with a message or
// accept() to let slim-redux know whether the validation passed or not.
payloadValidation: (payload, accept, reject) => {
// Calling increment() will dispatch the following action:
// {type: 'INCREMENT', payload: {value: 10}}
increment({value: 10});
/*
This will be picked up by your reducer:
reducer: (state, payload, action) => {
return state + payload.value;
}
// Register a change with the actionType 'INCREMENT' and the appropriate reducer.
// This returns a change-trigger function (see below)
const increment = store.createChangeTrigger({
actionType: 'INCREMENT',
reducer: (state, payload, action) => {
return state + payload.value;
}
});
// Calling increment() will dispatch the following action:
// Create a store with initial state = 0
var store = createSlimReduxStore(0 /*, yourExistingRootReducer, yourExistingMiddleware*/ );