Skip to content

Instantly share code, notes, and snippets.

@neftaly
neftaly / i3-laptop
Last active February 22, 2021 03:36
# i3 config file (v4)
#
# Please see https://i3wm.org/docs/userguide.html for a complete reference!
set $mod Mod4
# Font for window titles. Will also be used by the bar unless a different font
# is used in the bar {} block below.
font pango:monospace 8
@neftaly
neftaly / ddb-backup-parse.js
Created January 16, 2018 01:24
DynamoDB backup (v3) raw file to/from JSON
const R = require('ramda');
const typeEncoder = (mode, type) => {
if (mode !== 'fromDynamo' && mode !== 'toDynamo') {
throw new Error('Invalid mode: ' + mode);
}
const numberEncoder = mode === 'fromDynamo' ? Number : String;
switch (R.toUpper(type)) {
case 'S': return String;
case 'N': return numberEncoder;
// Split an array every time the predicate returns true
// :: f => (array -> Boolean) -> f a -> f a
const segment = R.curry((fn, array) => {
const chunks = [];
let chunk = [];
for (let value of array) {
if (fn(value)) {
chunks.push(chunk);
chunk = [];
}
@neftaly
neftaly / dynamo.js
Last active September 12, 2016 04:28
Convert DynamoDB <-> JS
import R from 'ramda';
/*
Convert JS maps to/from DynamoDB maps.
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.ResponseFormat
*/
// Convert DynamoDB data to/from JS data
// :: => 'fromDynamo' => dynamoType => dynamoValue => jsValue
// :: => 'toDynamo' => dynamoType => jsValue => dynamoValue
const uuid = () => { // Create a secure V4 UUID
const randomHexChars = qty => crypto
.randomBytes(Math.ceil(qty / 2))
.toString('hex')
.slice(0, qty);
const reserved = ['8', '9', 'a', 'b'][crypto.randomBytes(1)[0] % 4];
return [
randomHexChars(8),
randomHexChars(4),
'4' + randomHexChars(3),
@neftaly
neftaly / mapwhile.js
Created April 10, 2016 15:00
mapWhile - cancellable map function
import R from "ramda";
const mapWhile = R.curry((
predicate, // while fn - receives current partial list & value, returns bool
transformation, // map fn
list // source list
) => R.reduce((oldAcc, oldVal) => {
const newVal = transformation(oldVal);
const newAcc = [ ...oldAcc, newVal ];
return predicate(newAcc, newVal) ? newAcc : R.reduced(oldAcc);
@neftaly
neftaly / pptToMap.js
Last active May 17, 2017 00:12
parent pointer tree to map
import R from "ramda";
const stringize = value => typeof value === "number" ? value + "" : value;
const orphanIds = R.compose(
R.reduce((partial, pair) => [
...partial,
pair[0],
...orphanIds(pair[1].children)
], []),
@neftaly
neftaly / btssearch.js
Last active November 30, 2015 14:22
Binary search on time-series database
// inst: unix epoch
const hist = {
"channel1": [
{ inst: 10000, id: "a" },
{ inst: 20000, id: "b" },
{ inst: 30000, id: "c" },
{ inst: 30000, id: "d" },
{ inst: 40000, id: "e" }
]
};
@neftaly
neftaly / curry.js
Last active April 10, 2022 18:47
ES6 Auto-curry
const curry = (fn, ...oldArgs) => (...newArgs) => {
const args = [...oldArgs, ...newArgs];
return (args.length < fn.length) ? curry(fn, ...args) : fn(...args);
};
@neftaly
neftaly / compose.js
Last active December 5, 2021 14:24
ES6 Compose
const compose = (...args) => initial => args.reduceRight(
(result, fn) => fn(result),
initial
);