Skip to content

Instantly share code, notes, and snippets.

View marcoslhc's full-sized avatar
🏳️‍🌈

Marcos Hernández marcoslhc

🏳️‍🌈
View GitHub Profile
@marcoslhc
marcoslhc / classList_v2.js
Last active March 30, 2017 19:27
ClassList 2
ClassList.prototype.reduce = function (reducer, initialValue) {
return ClassList.of(this._classes.reduce(reducer, initialValue));
}
// Notice how we can define "filter" by reducing the previous array
// into a new array with only the values that satisfies the predicate
ClassList.prototype.filter = function (predicate) {
return ClassList.of(this.getClasses()).reduce((newList, currentClass) => {
if (predicate(currentClass)) newList.push(currentClass);
const mapFn = fn => lst => lst.map(fn);
const solarize = cls => cls.concat('--solarized');
const mapSolarize = mapFn(solarize);
ClassList.of([
'btn',
'btn-primary',
'btn-primary--active'
]).map(mapSolarize).getClasses();
@marcoslhc
marcoslhc / flattenArray.js
Last active March 13, 2017 03:09
flattenArray.js
function flattenArray(array) {
let temp = [];
function flatten(arr) {
if (Array.isArray(arr)) {
const [head, tail] = [arr[0], arr.slice(1)]
flatten(head);
if (tail.length > 0) flatten(tail);
function countWatchers(scope) {
if (!scope) {
return 0;
}
if (scope.vm) {
let log = [scope.vm.__proto__.constructor.name, scope.$$watchers && scope.$$watchers.length]
console.log(log.join(': '));
}
@marcoslhc
marcoslhc / pad.js
Last active February 15, 2017 01:50
export const pad = (char) => (
(number, padLeft = false) => (
(str) => (
number
? (padLeft
? pad(char)(--number)('' + char).concat(str + '')
: pad(char)(--number)('' + str).concat(char + ''))
: str
)));
@marcoslhc
marcoslhc / slim-redux.js
Created February 7, 2017 03:06 — forked from gaearon/slim-redux.js
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
if (fn(obj[key])) {
@marcoslhc
marcoslhc / adt.js
Created January 14, 2017 02:20 — forked from yelouafi/adt.js
Algebraic Data Types in javascript (see http://tech.pro/blog/6885/javascript-and-type-thinking)
function eachKey(obj, f) {
for(var key in obj) {
if( obj.hasOwnProperty(key) )
f(key, obj[key]);
}
}
function adtcase (base, proto, key) {
return (...args) => {
var inst = new base();
@marcoslhc
marcoslhc / dabblet.css
Created January 12, 2017 16:40
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
background: linear-gradient(135deg, #0e0d26 0%, #16152c 24%, #0c1a26 47%, #05161b 73%, #020d13 93%, #020D14 100%);
min-height: 100%;
function isArray(elm) {
return ('object' === typeof elm && elm.length >= 0);
}
function cons(elm1, elm2) {
if (!isArray(elm1)) elm1 = [elm1];
return elm1.concat(elm2);
}
@marcoslhc
marcoslhc / sprintf.js
Created May 9, 2016 19:29 — forked from canon0909/sprintf.js
sprintf
var sprintf = function (str) {
var args = Array.prototype.slice.call(arguments, 1),
hash = {
'%s': String,
'%d': parseInt,
'%f': parseFloat
};
return str.replace(/%0(\d+)d/g, function (m, num) {
var r = String(args.shift()),
c = '';