This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
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
:
We have plenty of code like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// step module | |
const applyUpdates = () => {}; | |
const updateStepBody = useDb => (stepBody, updates) => { | |
const updatedBody = applyUpdates(stepBody, updates); | |
useDb(transaction => { | |
createHistoryRecord(updatedBody, updates); | |
}); |