Skip to content

Instantly share code, notes, and snippets.

View sheniff's full-sized avatar

Sergio Almécija sheniff

  • Dropbox Inc
  • San Francisco
View GitHub Profile
**Please read:** This is a project in progress. Thus, every bug, suggestion, improvement or whatever you want to do to it will be welcome. Once the project is in a decent state, it'll be ported to an independent gem.
Description
---
AB Tester makes Front End AB Test generation simpler than ever!
It allows the developer to create a copy of the Front End of the project based on default assets or on a previously generated test.
What AB Tester does is making a copy of the layout file the application is using to load the one-page app (generally, `application.html.haml`) along with the respective css and js manifests, renaming them to the name of the test.
@sheniff
sheniff / checkBalancedParsMapReduce.js
Last active August 29, 2015 14:19
I wanted to try solving parenthesis balance checking using map-reduce approach in JS :)
function balancedPars(str) {
var result = 0;
if(typeof(str) !== 'string') return false;
if(str) {
result = str.split('')
.map(function(elem){
return elem === '(' ? 1 : (elem === ')' ? -1 : 0);
})
@sheniff
sheniff / linkedList.js
Last active August 29, 2015 14:19
Linked lists in JS... Useful but only for very specific cases (they kick array's ass in prepending items, as expected)
// LinkedListNode Class (Module pattern with cached functions)
!function(){
LinkedListNode = function LinkedListNode(data) {
this.node = [data, null];
}
LinkedListNode.prototype.getNext = getNext;
LinkedListNode.prototype.getData = getData;
LinkedListNode.prototype.setNext = setNext;
LinkedListNode.prototype.setData = setData;
@sheniff
sheniff / catalanNumber.js
Created April 19, 2015 20:22
Catalan Numbers to mathematically verify number of combinations (http://en.wikipedia.org/wiki/Catalan_number)
// Catalan Numbers to mathematically verify number of combinations
// E.g, the number of possible combinations of parentheses
// http://en.wikipedia.org/wiki/Catalan_number
function catalanNumber(numParens) {
var total = 1;
for(var i = 2; i <= numParens; i++) {
total *= (numParens + i) / i;
}
@sheniff
sheniff / findAllParensCombinations.js
Created April 19, 2015 20:29
Finding all possible combinations of parentheses and verify result's right using Catalan Number
function findAllCombinations(numParens) {
var totalCombs = 0;
if(numParens > 0) {
totalCombs = attachParen('', numParens, numParens, totalCombs);
}
return totalCombs;
}
function attachParen(str, open, close, total) {
if(open > 0) {
@sheniff
sheniff / bubbleSort.js
Created April 22, 2015 04:23
BubbleSort in JS
function bubbleSort (arr) {
var l = arr.length,
tmp = null,
j = l - 1,
i;
if(l < 2) return arr;
for(; j >= 0; j--) {
for(i = 0; i < j; i++) {
@sheniff
sheniff / mergeSort.js
Created April 22, 2015 04:24
MergeSort in JS
function mergeSort (arr, st, end) {
var res;
st = st || 0;
end = end || arr.length;
if(end - st > 1) {
var mid = st + Math.round((end - st)/2);
var a1 = mergeSort(arr, st, mid);
var a2 = mergeSort(arr, mid, end);
res = merge(a1, a2);
@sheniff
sheniff / quickSort.js
Created April 22, 2015 04:26
QuickSort in JS
function quickSort (arr) {
if(arr.length <= 1) {
return arr;
}
var pivot = choosePivot(arr);
var divider = divide(arr, pivot);
var a1 = quickSort(arr.slice(0, divider));
var a2 = quickSort(arr.slice(divider + 1, arr.length));
a1.push(arr[divider]);
@sheniff
sheniff / functionClosure.js
Created April 26, 2015 17:24
Just a silly check... making sure a function declaration as "function XXX" keeps internal to the closure
// closure
(function() {
function funcA() {}
var funcB = function() {};
funcC = function() {};
})()
console.log(typeof funcA); // undefined
console.log(typeof funcB); // undefined
console.log(typeof funcC); // function
@sheniff
sheniff / inheritance.js
Created April 27, 2015 04:39
Inheriting a parent class in JS. Instantiating parent class in prototype to make only instance methods to be available for child class.
// Class generator
var Class = function(parentCls) {
var Klass = function() {
this.init.apply(this, arguments);
};
if(parentCls){
var subClass = function(){};
subClass.prototype = parentCls.prototype;
Klass.prototype = new subClass;