Skip to content

Instantly share code, notes, and snippets.

View sebjwallace's full-sized avatar

Sebastian Wallace sebjwallace

  • United Kingdom
View GitHub Profile
@sebjwallace
sebjwallace / reduxObjectLiteral.js
Last active May 25, 2022 06:44
Redux reducer using object literal instead of a switch statement
import {createStore} from 'redux';
const counter = (state = 0, action) => {
const index = {
'INCREMENT': () => {
return state+1;
},
'DECREMENT': () => {
return state-1;
},
@sebjwallace
sebjwallace / domTraversal.js
Last active March 16, 2016 22:33
dom traversal with callback option on each node - http://jsbin.com/giluzi/edit
// http://jsbin.com/giluzi/edit
const traverseElement = (node,callback) => {
let level = 0;
const traverse = (node) => {
if(!node.nodeName) return;
@sebjwallace
sebjwallace / pathFinder.js
Last active May 9, 2017 09:21
pathfinder - get element path in the dom, similar to xpath
// #document/HTML/BODY/BODY/DIV/DIV
var getElementPath = function(el){
var path = el.nodeName;
var parent = el.parentNode;
while(parent){
path = parent.nodeName + '/' + path;
parent = parent.parentNode;
}
return path;
@sebjwallace
sebjwallace / README.md
Last active April 8, 2016 14:58
virtual-dom todos <100 LOC

This was a small challenge to write a standard and unstyled todos app within 100 lines of code, while maintaining readability.

It can be found on JS Bin

This project was to also prove that components are nothing more than a function that returns a vTree. The only purpose of creating an instance of the component is to maintain internal state. If there is none, then a component can just be a pure function.

Although a trivial application, it might also demonstrate that such things can be built without frameworks or virtual-dom libraries - only the virtual-dom is needed.

I did use my own fork from the virtual-dom repo for this project so that I could use the vDOM object.

@sebjwallace
sebjwallace / README.md
Last active April 9, 2016 15:52
Convert an array-tree into an vTree

Writing hyperscript can get tedious when repeating this syntax for every element.

h('div', 'this is an element', [
  h('ul', [
    h('li', 'writing all these h calls')
    h('li', 'can get boring...')
  ])  
])
@sebjwallace
sebjwallace / shallowMerge.js
Created April 9, 2016 18:07
Shallow merge objects without overwriting or mutation
function mergeObjects(merger,mergee){
var mergent = {};
for(var prop in mergee){
mergent[prop] = mergee[prop]
}
for(prop in merger){
if(mergent[prop])
mergent[prop] = mergeObjects(mergent[prop],merger[prop])
else mergent[prop] = merger[prop]
@sebjwallace
sebjwallace / no_JSX.js
Last active April 22, 2016 10:25
Less React, more JS
// https://jsfiddle.net/reactjs/5vjqabv3/
var Hello = React.createClass({
displayName: 'Hello',
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});
ReactDOM.render(
function Hello(name){
return React.createElement("div", null, "Hello " + name);
}
ReactDOM.render(
Hello("World"),
document.getElementById('container')
);
function Hello(name){
return ["div", null, "Hello " + name];
}
ReactDOM.render(
React.createElement.apply(this, Hello('world')),
document.getElementById('container')
);