Skip to content

Instantly share code, notes, and snippets.

View hex13's full-sized avatar
💭
slavic code master

Łukasz Lityński hex13

💭
slavic code master
  • JavaScript developer
  • Warsaw/Poland
View GitHub Profile
@hex13
hex13 / serializeSymbols.js
Created August 28, 2017 20:27
how to serialize symbols in JavaScript
const reserializer = (mapping) => {
const map = new Map();
Object.keys(mapping).forEach(k => map.set(mapping[k], k));
return {
serialize: value => map.get(value),
deserialize: value => mapping[value],
}
}
// application
// emulation of Go's defer in JS
// helper function
const makeDefer = () => {
const deferred = [];
return {
defer(f) {
deferred.unshift(f);
},
done(result) {
macro_rules! foo {
($a: literal ) => {
$a
};
($op:tt $a:literal $b:tt) => {
foo!($op ($a) $b)
};
($op:tt $a:tt $b:literal) => {
foo!($op $a ($b))
};
@hex13
hex13 / mini-redux.js
Last active April 15, 2021 09:07
Redux-like library in 13 lines
const createStore = (reducer, state) => {
let subscribers = [];
return {
dispatch: (action) => {
state = reducer(state, action);
subscribers.forEach(f => f(state));
},
getState: () => state,
subscribe: (f) => {
subscribers.push(f);
@hex13
hex13 / render-promise-in-react.js
Created November 3, 2016 12:33
how to render promises in React
//License CC0 1.0: https://creativecommons.org/publicdomain/zero/1.0/
class Deferred extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ''
};
}
componentDidMount() {
@hex13
hex13 / action-creator.js
Last active July 23, 2018 00:53
simple way for making universal action creator for Redux
'use strict';
const AC = type => {
const ac = payload => ({type, payload});
ac.type = ac().type;
return { [type]: ac, type }
};
const { addTodo, type: ADD_TODO } = AC('addTodo');
const { removeTodo, type: REMOVE_TODO } = AC('removeTodo');
@hex13
hex13 / createStore.js
Last active December 27, 2017 17:11
Redux-like store implemented in observables
import xs from 'xstream';
// If I intended to make a serious state-management system based on observables, I would probably not try to imitate Redux
// but this was made only for self-learning and for fun.
// Redux-like store factory:
const createStore = (reducer, initial) => {
const action$ = xs.create({start() {},stop() {}});
@hex13
hex13 / gist:00ce6de4cf2d6cf48f7aefd5242735d9
Last active November 30, 2017 00:00
FunQuery - prototype of language (this is proposal - syntax and semantic can change)
// someScript.funq
def hello { | @name |
if (2 < 3) {
alert "two is less than three"
} else {
alert "two is not less than three"
};
alert ("Hello, " + @name)
};
// hoc
compose(
withState({a: 'setA', b: 'setB'}),
withProps(({ a, b }) => ({ c: a + b }))
);
// render function
return (
<div>
@hex13
hex13 / redux-oop.js
Last active September 27, 2017 04:29
redux OOP way
const reducers = {
add(state, {payload: [number]}) {
return state + number;
},
};
function reducer(state, action) {
if (reducers.hasOwnProperty(action.type))
return reducers[action.type](state, action);
return state;