Skip to content

Instantly share code, notes, and snippets.

View ehpc's full-sized avatar
💾

Eugene Maslovich ehpc

💾
View GitHub Profile
@ehpc
ehpc / ts-any-to-lines.sh
Created August 3, 2022 11:21
Get metric of how many `any`'s are there compared to line count for TypeScript codebase (in percents)
#!/bin/sh
# Calculates a metric of how many `any`'s are there compared to line count for TypeScript codebase (in percents)
# Basically we count all `any` occurences, then count all lines in the codebase, and lastly divide one by another
echo "scale = 4; $(find -name "*.ts?" -not -path "./node_modules/*" | xargs grep -o '\bany\b' | wc -l) * 100 / $(find -name "*.ts?" -not -path "./node_modules/*" | xargs wc -l | tail -1 | awk '{print $1}')" | bc
@ehpc
ehpc / move.js
Created October 12, 2021 07:45
JS animate movement with requestAnimationFrame and transition
function move(el, distance, duration) {
let startTime;
function recur() {
window.requestAnimationFrame((time) => {
if (startTime === undefined) {
startTime = time;
}
const elapsed = time - startTime;
const fraction = elapsed / duration;
const dx = Math.min(distance, distance * fraction);
@ehpc
ehpc / ramda-promises-compose.js
Last active July 17, 2023 03:29
How to compose promises with Ramda
// Custom promise-based compose
const composeWithPromise = (...args) =>
R.composeWith((f, val) => {
if (val && val.then) {
return val.then(f);
}
if (Array.isArray(val) && val.length && val[0] && val[0].then) {
return Promise.all(val).then(f);
}
return f(val);
@ehpc
ehpc / nodejs-mongo-async-await.js
Created March 31, 2020 08:45
Node.JS MongoDb Async/Await
const MongoClient = require('mongodb').MongoClient;
const uri = 'mongodb://localhost:27017/';
const mongoClient = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
(async () => {
const connection = await mongoClient.connect();
@ehpc
ehpc / async-is-not-asynchronous.js
Last active February 5, 2020 07:37
async keyword doesn't make your function asynchronous
function heavyFunc() {
const limit = Math.pow(10, 8);
let res = 0;
for (let i = 1; i < limit; i++) {
res += Math.atan2(i, i) * Math.random();
}
return res;
}
async function heavyFuncWithAsync() {
@ehpc
ehpc / async-patterns.js
Created January 27, 2020 17:04
Examples of dIfferent async patterns in JavaScript
/**
* Разные асинхронные паттерны в JS, решающие одну и ту же задачу
*/
///////////////// callbacks ///////////////////////////////////
function asyncFunction(callback) {
setTimeout(callback.bind(this, 42), 100);
}
asyncFunction(console.log.bind(this, 'asyncFunction:'));
@ehpc
ehpc / nodejs-readline-nonrecursive-example.js
Created January 25, 2020 20:43
Node.JS readline usage without recursion
const readline = require('readline');
const questions = [
['Can the fox be recursive?', 'O_o'],
['Can readline be non-recursive?', 'Yep']
];
const cmd = readline.createInterface({
input: process.stdin,
output: process.stdout
@ehpc
ehpc / nodejs-readline-example.js
Created January 24, 2020 20:18
Simple quiz using Node.JS readline module
const readline = require('readline');
const questions = [
['What does the fox say?', 'Yeeeee'],
['Will you be coding this weekend?', 'Whaaaatt?!']
];
const cmd = readline.createInterface({
input: process.stdin,
output: process.stdout
@ehpc
ehpc / .editorconfig
Created November 26, 2019 19:36
Editorconfig 2 spaces
root = true
[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
@ehpc
ehpc / binance-postman-auth.js
Created September 17, 2019 11:27
Code for managing Binance authentication with Postman
let secret = pm.variables.get('secret-key'),
timestamp = Date.now(),
query = pm.request.url.query
.filter(x => !x.disabled && x.key !== 'signature')
.map(x => {
if (x.key === 'timestamp') {
return `${x.key}=${timestamp}`;
}
else {
return `${x.key}=${x.value}`;