Skip to content

Instantly share code, notes, and snippets.

🦄
Hacking fast and slow.

Anton Vynogradenko Restuta

🦄
Hacking fast and slow.
Block or report user

Report or block Restuta

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View logger.js
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 Nov 8, 2019
Ora based progress bar
View ora-progress-bar.js
/* 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 Jan 25, 2019
Audit Log Collection Design Problem
View audit-log-design-problem.js
/*
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 Jan 15, 2019
With Audit Log (proto)
View with-audit-log.js
/* 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.
View app-flow-proto-2.js
// 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 Jan 15, 2019
With Audit Log prototype
View with-audit-log.js
// 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 Jan 12, 2019
Collect Audit Log Updates
View audit-log-updates.js
// 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 Jan 9, 2019
Effect Functor / Reader Monad
View effect-functor.js
// 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 => {
View base64-encode-n-times.js
// 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 Feb 6, 2018
RxJS buffer function calls
View rx-js-buffer-function-calls.js
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 => {
You can’t perform that action at this time.