Skip to content

Instantly share code, notes, and snippets.

View dinocarl's full-sized avatar

Carl Albrecht-Buehler dinocarl

View GitHub Profile
const {
compose,
concat,
curry,
flatten,
flip,
identity,
isNil,
isEmpty,
join,
@dinocarl
dinocarl / kramdownAttrsRemark.js
Last active March 16, 2020 18:34
Adding kramdown-style attributes to remark
const remark = require('remark');
const html = require('remark-html');
const visit = require('unist-util-visit');
const { log } = console;
const inputMD = `
[A Link](#){: data-track="footer-link" .xclass onclick=alert() } with text that has *some*{: .emphclass } emphasis and something **strong**{: .strngclss}
[![github logo](https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png){: height=100 width=100 }](/){: .rspnsv-img data-track="logo click" }
grep -R --exclude-dir={node_modules,build,.git} --exclude=*.{png,jpg,vim,swp,swo} 'searchString' ./
const md = require('markdown-it')();
const markdownItAttrs = require('markdown-it-attrs');
const src = `
# header {: .title-class #title-id }
some text {: with=attrs and="attrs with space" }
more body copy with some *emphasis*{: .emph-class } thrown in {: .para-class}
- item 1
- item 2 {: .item-class }
- item 3
@dinocarl
dinocarl / groupBy.js
Created March 6, 2020 18:17
Vanilla JS groupBy implementation
// utilities
const includes = (needle) => (haystack) => (haystack.indexOf(needle) > -1);
const includesAtPos = (needle) => (position) => (haystack) => (haystack.indexOf(needle) === position);
const testFor = (re) => (str) => re.test(str);
const and = (...fnList) => (data) => fnList.every((fn) => fn(data));
const groupBy = (fn) => (arr) => arr.reduce(
(out, val) => {
let by = fn(val);
(out[by] = out[by] || []).push(val);
const izzer = (str, mod) => (num) => modulo(num, mod) === 0
? str
: '';
const izzerStr = compose(
join(''),
juxt([
izzer('fizz', 3),
izzer('buzz', 5),
])
@dinocarl
dinocarl / avgToThsnds.js
Created June 5, 2020 15:43
Takes an array of numbers and returns their average, rounded to the thousands place
const toThsnds = compose(
multiply(1000),
Math.round,
multiply(1/1000)
);
const avgToThsnds = (arr) => compose(
toThsnds,
multiply(1/length(arr)),
reduce(add, 0)
@dinocarl
dinocarl / mergeWithStrategy.js
Created June 10, 2020 16:29
Apply an object of functions to 2 others of Data to arrive at a new one where each key has had its special function run against the other's values.
const selectBrevity= (l, r) => lte(length(l), length(r))
? l
: r;
const lispy = ([fn, ...args]) => fn(...args);
const makeSet = (arr) => uniq(flatten(arr));
const zipObjLisped = (arr) => ({ [head(arr)]: lispy(tail(arr)) });
@dinocarl
dinocarl / propsOr.js
Last active July 17, 2020 21:14
Uses a fallback value, then an array of strings that serve as an ordered list of preferred props from most to least to return from an object. The first valid prop gets returned.
const validVal = compose(
not,
anyPass([isEmpty, isNil])
);
const validProp = curry((propName, obj) => validVal(prop(propName, obj)));
const propsOr = curry((fallback, propsList, obj) => compose(
cond,
append([T, always(fallback)]),
const isntEmpty = complement(isEmpty);
const isntNil = complement(isNil);
const isntNaN = complement(isNaN);
const pageLens = lensProp('page');
const numberValidations = [
isntNaN,
isntNil,
isntEmpty,