Skip to content

Instantly share code, notes, and snippets.

View MKRhere's full-sized avatar
😕
In a mid-youth crisis

Muthu Kumar MKRhere

😕
In a mid-youth crisis
View GitHub Profile
@MKRhere
MKRhere / XMLBuilder.js
Created September 18, 2018 14:39
XMLBuilder
const tag = (name, attrs, close, content) => {
const end = close ? "/>" : ">";
const pairs = [];
let tag;
Object.keys(attrs).forEach(key => {
if (Object.prototype.hasOwnProperty.call(attrs, key)) {
pairs.push(key + '="' + (attrs[key]) + '"');
}
});
@MKRhere
MKRhere / chained.pipe.js
Last active January 9, 2019 19:21
Chaining pipe
const _makeProxy = f => new Proxy(f, {
get (fn, prop) {
return U[prop] && U[prop].bind({ hold: fn });
}
});
const U = {
add: function (x) {
return _makeProxy(
y => (
@MKRhere
MKRhere / reducePromise.js
Last active October 25, 2018 14:37
reducePromise.js
const reduceP = (reducer, initial) => list =>
list.reduce((acc, currentValue, index, list) =>
acc.then(accumulator => reducer(accumulator, currentValue, index, list)),
Promise.resolve(initial)
);
@MKRhere
MKRhere / minio.sh
Last active August 19, 2021 12:30
Minio installer
#!/usr/bin/env bash
# Setup user
sudo useradd -m minio
cd /home/minio
# Download minio and make it executable
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
sudo chown minio:minio ./minio
chmod +x ./minio
@MKRhere
MKRhere / html2sxml.js
Last active November 26, 2018 12:52
HTML to SXML
#!/usr/bin/env node
/* USAGE:
* html2sxml /path/to/index.html > output.sxml
* html2sxml < input.html > output.sxml
*/
'use strict';
const path = require("path");
@MKRhere
MKRhere / remap.js
Last active December 5, 2018 08:14
remap objects
const map = mapper => arr => arr.map(mapper);
const reduce = reducer = arr => arr.reduce(reducer);
const path = pathSeg => obj =>
pathSeg.reduce((acc, segment) => acc && acc[segment], obj);
/* --------------vy::remap---------------- */
const remap = paths => obj =>
paths.reduce((acc, [key, seg]) => {
acc[key] = path(seg)(obj);
@MKRhere
MKRhere / comp.js
Last active December 12, 2018 05:40
List comprehension in JavaScript
/* --------------vy::comp----------------- */
const comp = (f, iter, cond = () => true, count = Infinity) => {
const list = [];
for (const item of iter) {
if (cond(item)) list.push(f(item));
if (list.length === count) break;
}
return list;
};
@MKRhere
MKRhere / argvParser.js
Created December 11, 2018 19:09
26 line forgiving argv parser
const VALUES = Symbol('VALUES');
const Parser = (argv, { subcommands } = {}) => {
argv = argv.slice(2);
return {
get (key) {
if (key === VALUES) {
return argv.filter((x, i) =>
(!subcommands || i !== 0) && !x.startsWith('-')
&& (!argv[i - 1] || !argv[i - 1].startsWith('-')));
@MKRhere
MKRhere / object.prototype.js
Last active December 12, 2018 08:04
Object.prototype extensions for JavaScript
Object.prototype[Symbol.iterator] = function () {
let i = 0;
const keys = Object.keys(this);
return {
next: () => {
return {
value: [keys[i], this[keys[i++]]],
done: i > keys.length,
};
},
@MKRhere
MKRhere / iter.js
Last active October 31, 2019 12:52
iter-fns
function* map(f, iter) {
for (const i of iter) {
yield f(i);
}
}
function* filter(f, iter) {
for (const i of iter) {
if (f(i)) yield i;
}