Skip to content

Instantly share code, notes, and snippets.

View aneurysmjs's full-sized avatar
💭
асинхорнный

Аневризма aneurysmjs

💭
асинхорнный
View GitHub Profile
@aneurysmjs
aneurysmjs / connect.js
Created September 30, 2019 11:33
Simplified version of react-redux's connect HOC
const connect = (mapStateToProps) => (Component) => {
return class Connect extends React.Component {
state = mapStateToProps(store.getState());
componentDidMount() {
// когда store меняется, обновит компонент
this.unsubscribe = store.subscribe(this.update);
}
componentWillUnmount() {
@aneurysmjs
aneurysmjs / proto-tricks.js
Last active September 30, 2019 08:06
couple of tricks about prototypal inheritance
/**
* understanding .constructor property
*/
function User() {
}
// when functions are created, they're given a "prototype" property which is an object
console.log(User.prototype); // {}
@aneurysmjs
aneurysmjs / filtering-ramda.js
Created April 19, 2018 16:32
filtering nested array of obejct using Ramda.js
const data = [
{username: 'bob', age: 30, tags: ['work', 'boring']},
{username: 'jim', age: 25, tags: ['home', 'fun']},
{username: 'jane', age: 30, tags: ['vacation', 'fun']}
];
R.filter(R.where({tags: R.contains('fun')}))(data);
const users = [
{username: 'bob', age: 30, tags: [{label: 'work'}, {label: 'boring'}]},
// The type of a box is always defined by the 'display' property:
/**
* block:
* 100% of parent's width
* vertically, one after another
*/
/**
* inline-block:
// Partial application and currying are two different techniques for specializing a generalized function
// Partial application takes some of the arguments now and then takes all of the rest arguments later
function partial(fn, ...firstArgs) {
return function applied(...lastArgs) {
return fn(...firstArgs, ...lastArgs);
};
}
@aneurysmjs
aneurysmjs / monad.js
Created April 15, 2019 21:42
monad axioms
// Axioms
bind(init(value), f) === f(value)
bind(monad, unit) === monad
bind(bind(monad, f), g) ===
bind(monad, function (value) {
return bind(f(value), g);
@aneurysmjs
aneurysmjs / curry.js
Created August 29, 2018 12:59 — forked from amatiasq/curry.js
Simple way to recursively curry javascript functions http://jsfiddle.net/amatiasq/osrsomq0/
/**
* @param {Function} fn Function to curry.
* @param {Number} lenght The arguments required to invoke the function. Optional. By default is fn.length
* @returns {Function} The currified function.
*/
function curry(fn, length) {
length = length || fn.length;
return function currified() {
var args = [].slice.call(arguments);
@aneurysmjs
aneurysmjs / filterArraysRamda.md
Created April 19, 2018 15:39 — forked from cezarneaga/filterArraysRamda.md
Filter array of objects by nested values using ramda: Sometimes you dont have access to backend and you want to filter the response from an endpoint based on certain criteria. While trivial on flat arrays, this gets a bit tricky if the property you want to query is deeply nested. This is where Ramda shines.

Say we have a prop.users of the shape:

const users = [
    {username: 'bob', age: 30, tags: [{name: 'work', id: 1}, {name: 'boring', id: 2}]},
    {username: 'jim', age: 25, tags: [{name: 'home', id: 3}, {name: 'fun', id: 4}]},
    {username: 'jane', age: 30, tags: [{name: 'vacation', id: 5}, {name: 'fun', id: 4}]}
];