Skip to content

Instantly share code, notes, and snippets.

@edulan
edulan / bubble.js
Last active February 22, 2017 20:51
Functional bubble sort (with tail call optimization)
function sort(elements, compare = (a, b) => Math.sign(b - a), acum = []) {
if (elements.length === 0) return acum;
const [first, ...rest] = bubbleUp(elements, compare);
return sort(rest, compare, [first, ...acum]);
}
function bubbleUp(elements, compare) {
return elements.reduceRight((bubbledElements, element) => {
@edulan
edulan / quota_value.rb
Last active February 2, 2017 13:10
Composite Quota type
class QuotaUnlimitedValue
include Comparable
def initialize(value = 1)
@value = value <=> 0
end
def value
@value
end
@edulan
edulan / entries_tree.js
Last active January 20, 2017 19:57
Entries tree builder
// Transforms an array of entries like this:
//
// [
// 'baz.tar.gz',
// 'foo/bar/lol.txt',
// 'foo/bar/kaka/perro/siete/lol.txt'
// ]
//
// into this one:
//
@edulan
edulan / base.js
Last active December 15, 2016 15:40
Logs unexpected props in a React component
import { Component } from 'react';
export default class BaseComponent extends Component {
componentWillReceiveProps(nextProps) {
const propTypes = this.constructor.propTypes;
if (!propTypes) {
return;
}
@edulan
edulan / quiz.js
Created September 21, 2016 21:54
Promises quiz
const p = new Promise((resolve, reject) => {
setTimeout(() => reject('fuuuu'), 2000);
})
p.then(
() => console.log('[CHAINED] fullfilled#1 :)'),
() => console.log('[CHAINED] rejected#1 :(')
).then(
() => console.log('[CHAINED] fullfilled#2 :)'),
() => console.log('[CHAINED] rejected#2 :(')
@edulan
edulan / questions.md
Created September 8, 2016 08:11
React Virtualized questions
  • How RV bypass browser's maximum scroll limits?
  • How RV supports dynamic item widths/heights? (CellMeasurer shadow rendering and size caching)
  • How to properly tune sectionSize property in a Collection component? (how it works and why the need of section grouping)
  • How RV compares with native browser scrolling (show some benchmarks and graphs)?
  • How RV supports scroll-to-first (chat style) use case?

Other questions from my workmates

  • Does RV support flexbox layout?
  • What's the data model RV uses? (No data model, just a collection length and indexes)
@edulan
edulan / index.js
Last active August 26, 2016 14:58
Logs event listeners for a DOM element and its ancestors (Chrome only)
function logListenersForElement(element, eventType) {
if (!element) return;
const eventListeners = getEventListeners(element);
if (Object.keys(eventListeners).indexOf(eventType) !== -1) {
console.log({
element,
listeners: eventListeners[eventType],
});
@edulan
edulan / comparators.js
Created August 3, 2016 12:49
Combine comparators
function combineComparators(...comparators) {
return (a, b) => {
return comparators.reduce((result, comparator) => {
if (result !== 0) return result;
return comparator(a, b);
}, 0);
}
};
@edulan
edulan / replace_branch_commits_email.sh
Created June 3, 2016 08:34
Replace branch commits committer and author email
git filter-branch -f --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "my_personal_email@example.org" ];
then
GIT_AUTHOR_EMAIL="my_work_email@example.org";
GIT_COMMITTER_EMAIL="my_work_email@example.org";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD~10..HEAD
@edulan
edulan / pre-commit
Last active May 1, 2019 09:21
Git pre-commit hook for checking Mocha .only declarations
#!/usr/bin/env bash
PATTERN="describe\.only\|it\.only"
MESSAGE="You cannot commit this SHITE"
if git diff --name-only --cached | xargs grep -Hn --color=always $PATTERN; then
echo ""
echo $MESSAGE
exit 1
else