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 / modified-z-score.js
Last active November 22, 2020 02:06
Outliers: z-score and modified z-score method in JavaScript
// z-score
const { mean, deviation } = require('d3-array')
const zscore = input => {
const arrMean = mean(input)
// here the n-1 : http://duramecho.com/Misc/WhyMinusOneInSd.html
const arrDeviation = deviation(input)
return input.map(i => ({
zscore: (i - arrMean) / arrDeviation,
item: i,
@Restuta
Restuta / readme.md
Last active September 25, 2020 19:46
GitHub Badges test via Shields

Base PRs

#20

Stacked on top of this PR

#23

#24

@Restuta
Restuta / null-or-undefined.md
Last active September 24, 2020 01:59
null or undefined

null in javascript is an object, which has few important, arguably confusing implications. Unfortunately JS has two ways to declare that something is "nothing", and the default way to do so is undefined.

Having two ways to declare that something is "nothing" might be confusing and could be unnecessary. I can't come up with a use-case where I'd need two (and Douglas C. thinks the same), therefore we should pick one or another.

Below is why I think we should pick undefined and not null:

ES5 world

We have plenty of code like this:

@Restuta
Restuta / tranducers-experiments.js
Created August 13, 2020 06:32
Tranducers and mergeWith experiments
const R = require('ramda')
const numbers = [1, 2, 8, 9]
const transducer = R.compose(
R.filter(x => {
console.log('filter', x)
return x > 3
}),
R.map(x => {
@Restuta
Restuta / neat-stack.js
Last active March 12, 2020 19:46
neat-stack.js
import cleanStack from 'clean-stack';
// dims non-useful log lines in stack traces
const neatStack = (colorize, stack) => {
// add parts of stack trace lines here that should be ignored
const regexParts = [
'(node_modules)',
'(WEBPACK_IMPORTED)',
'(next_tick.js)',
'(domain.js)',
@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 / 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 / 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);
});