Skip to content

Instantly share code, notes, and snippets.

View Restuta's full-sized avatar
🦄
Hacking fast and slow.

Anton Vynogradenko Restuta

🦄
Hacking fast and slow.
View GitHub Profile
@Restuta
Restuta / logger.js
Created November 5, 2019 22:57
Logger
const util = require('util');
const R = require('ramda');
const chalk = require('chalk');
const jsondiffpatch = require('jsondiffpatch');
const toJson = require('./to-json');
let prefix;
let coloredOutput = false;
@Restuta
Restuta / ora-progress-bar.js
Last active November 8, 2019 17:08
Ora based progress bar
/* eslint-disable no-return-assign */
const chalk = require('chalk');
const ora = require('ora');
const prettyMs = require('pretty-ms');
const throttle = require('lodash/throttle');
const getHeapUsed = throttle(
() => {
const heapUsed = process.memoryUsage().heapUsed / 1024 / 1024;
return Math.round(heapUsed, 2);
@Restuta
Restuta / audit-log-design-problem.js
Last active January 25, 2019 22:37
Audit Log Collection Design Problem
/*
Problem
We have a function that updates objects and generates audit records of the changes.
Think about financial systems or just any app where it's useful to know who made changes to what.
Audit records needs to be saved to the DB as well as updates to objects, but ideally
we would want to execute business logic, accumulate all updates and audit records in memory
and then save it to DB all at once. Let's look at the specifics.
@Restuta
Restuta / with-audit-log.js
Created January 15, 2019 07:53
With Audit Log (proto)
/* tl;dr of the problem
- We have a funtion that updates objects and produces two values [updates, auditLogRecord].
Let's call it "applyChangesWithAuditLog"
- We need to use this function in the context of another function (let's call it "update") and that another function should not be
aware of the fact that that auditLogRecord's are being produced and collected.
- When function "update" is executed, we want to collect all auditLogRecords that originated from calls
to "applyChangesWithAuditLog" inside "update" function.
- Then we want to save all collected auditLog items to DB.
Code below achieves this by building a functional wrapper.
@Restuta
Restuta / app-flow-proto-2.js
Created January 15, 2019 06:47
App Flow Proto 2
// step module
const applyUpdates = () => {};
const updateStepBody = useDb => (stepBody, updates) => {
const updatedBody = applyUpdates(stepBody, updates);
useDb(transaction => {
createHistoryRecord(updatedBody, updates);
});
@Restuta
Restuta / with-audit-log.js
Last active January 15, 2019 03:25
With Audit Log prototype
// original version with binding to module instance
const { createAuditLogRecords } = require('../commands');
const getStepFactory = require('./get-factory-by-type');
const { createStep, updateStep } = require('./commands');
const applyStepUpdates = require('./apply-step-updates');
const createContainer = (items = []) => ({
getAll: () => items,
@Restuta
Restuta / audit-log-updates.js
Last active January 12, 2019 01:21
Collect Audit Log Updates
// prototype of a pattern that allows to collect certain objects/operations
// during function exectution
const createContainer = (items = []) => ({
getAll: () => items,
add: x => items.push(x),
});
const applyUpdates = ({ onNewAuditLogRecord }) => x => {
onNewAuditLogRecord(x);
@Restuta
Restuta / effect-functor.js
Last active January 9, 2019 23:08
Effect Functor / Reader Monad
// effect functor
const Effect = f => ({
map: g => Effect(x => g(f(x))),
run: x => f(x),
join: x => f(x),
chain: f =>
Effect(f)
.map(g)
.join(),
ap: eff => {
@Restuta
Restuta / base64-encode-n-times.js
Created February 8, 2018 01:01
Base64 encode N times
// https://runkit.com/embed/ogydgfyotoqc
var base64 = require("base-64")
const R = require('ramda')
var utf8 = require('utf8')
var text = 'Yeah, right?!'
const encode = R.pipe(utf8.encode, base64.encode)
const encodeN = R.pipe(...R.times(() => encode, 16))
@Restuta
Restuta / rx-js-buffer-function-calls.js
Last active February 6, 2018 21:00
RxJS buffer function calls
console.clear()
async function main() {
const delay = time=>new Promise(resolve => setTimeout(()=>resolve(), time))
const functionInvocationsObservable = new Rx.Subject()
.bufferTime(/* buffer time*/ 500, /* restart buffer, -1 means never */ -1, /* maxBufferSize*/ 3)
.filter(x => x.length > 0)
// .subscribe(console.log)
const batchify = func => {