Skip to content

Instantly share code, notes, and snippets.

Avatar

Iddan Aaronsohn iddan

View GitHub Profile
View app.jsx
import React from 'react';
import connect from './connect';
// model
class User {
name = null;
}
// view
const _App = ({ user }) => user.name === null
View with-path.js
const WithPath = (target, path = []) => new Proxy(target, {
get(target, property, receiver) {
const value = target[property];
const thisPath = [...path, property];
return {
...(typeof value !== 'object' ? value : WithPath(value, thisPath)),
[WithPath.Path]: thisPath
};
}
});
@iddan
iddan / app.jsx
Last active May 18, 2017
New Redux
View app.jsx
import Form from './form.jsx';
const App = (setState) => ({ entities }) => (
<div>
<h1>Entities Editor</h1>
<h2>Select an entity</h2>
<div>{
entities.map(entity => (
<div>
<h3>{ entity.id }</h3>
@iddan
iddan / instructions.bash
Last active Aug 20, 2018
Connect Electron to Create React App
View instructions.bash
create-react-app $NAME
npm install --save electron
npm run eject
replace scripts/start.js with this start.js
replace config/webpack.config.dev.js with this webpack.config.dev.js
@iddan
iddan / component.js
Created Apr 13, 2017
react functional
View component.js
function component(...args) {
const [func, opts = {}] = args.reverse();
if (opts.constructor !== Object) {
throw new Error('Options must be a plain object');
}
class FunctionalComponent extends (opts.impure ? React.Component : React.PureComponent) {
constructor() {
super(...arguments);
this.setState = this.setState.bind(this);
}
@iddan
iddan / form.jsx
Created Mar 14, 2017
css-modules-styled-components
View form.jsx
import { Label } from './label.scss';
export default function Form({ fields }) {
return fields.map(field => <div>
<Label color="red" highlighted>{field}</Label>
<input type="text" />
</div>)
}
@iddan
iddan / convert-json5.js
Created Feb 26, 2017
Convert JSON5 to JSON
View convert-json5.js
const fs = require('fs');
const JSON5 = require('json5');
const [file] = process.argv.slice(2);
fs.writeFileSync(
file.replace(/\.json5$/, '.json'),
JSON.stringify(
JSON5.parse( fs.readFileSync(file) ),
null,
@iddan
iddan / load-image.js
Created Feb 7, 2017
One liner to load images with a promise
View load-image.js
const loadImage = src => new Promise((resolve, reject) => Object.assign(new Image(), {
src,
onload() {
resolve(this);
},
onerror(err) {
reject(err);
},
}));
@iddan
iddan / index.js
Last active Feb 7, 2017
Promisify
View index.js
const promisify = func =>
(...args) =>
new Promise((resolve, reject) =>
func(...args, (err, result) =>
err && reject(err) || resolve(result)
)
);
@iddan
iddan / splice.js
Created Jan 22, 2017
Immutable Splice
View splice.js
function splice(array, ...args) {
let newArray = [...array];
newArray.splice(...args);
return newArray;
}