Skip to content

Instantly share code, notes, and snippets.

View iddan's full-sized avatar

Iddan Aaronsohn iddan

View GitHub Profile
@iddan
iddan / app.jsx
Created June 29, 2017 09:12
repx
import React from 'react';
import connect from './connect';
// model
class User {
name = null;
}
// view
const _App = ({ user }) => user.name === null
@iddan
iddan / with-path.js
Created May 17, 2017 22:26
With Path
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 10:27
New Redux
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 August 20, 2018 03:18
Connect Electron to Create React App
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 April 13, 2017 07:24
react functional
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 March 14, 2017 15:51
css-modules-styled-components
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 February 26, 2017 10:24
Convert JSON5 to JSON
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 February 7, 2017 17:36
One liner to load images with a promise
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 February 7, 2017 02:11
Promisify
const promisify = func =>
(...args) =>
new Promise((resolve, reject) =>
func(...args, (err, result) =>
err && reject(err) || resolve(result)
)
);
@iddan
iddan / splice.js
Created January 22, 2017 08:40
Immutable Splice
function splice(array, ...args) {
let newArray = [...array];
newArray.splice(...args);
return newArray;
}