Skip to content

Instantly share code, notes, and snippets.

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

Аневризма aneurysmjs

💭
асинхорнный
View GitHub Profile
@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}]}
];
@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'}]},
@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 / scope.js
Last active February 10, 2023 09:42
practical use of 'var' statement
/*
* @desc closure in loops
*/
// the problem here, is we're thinking we're getting
// a new 'i' for each iteration, so at the end it only
// prints 6, because 'i' is 6 at the end of the loop and
// the timers runs after so it'll print 6.
for (var i = 0; i <= 5; i += 1) {
setTimeout(function () {
@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);
// 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);
};
}
// The type of a box is always defined by the 'display' property:
/**
* block:
* 100% of parent's width
* vertically, one after another
*/
/**
* inline-block:
@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 / 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 / reducers-performance.js
Last active October 8, 2019 12:55
benchmarking each type of reducer
const { PerformanceObserver, performance } = require('perf_hooks');
const obs = new PerformanceObserver((items) => {
console.log(items.getEntries()[0]);
performance.clearMarks();
});
obs.observe({ entryTypes: ['measure'] });
const has = Object.prototype.hasOwnProperty;