Skip to content

Instantly share code, notes, and snippets.

View koyanloshe's full-sized avatar
👁️
Focusing

Alok Shenoy koyanloshe

👁️
Focusing
View GitHub Profile
const CHUNK_SIZE = 10000000; // 10MB
async function start() {
const stream = fs.createReadStream('./file', { highWaterMark: CHUNK_SIZE });
for await(const data of stream) {
// do something with data
}
}
start();
const CHUNK_SIZE = 10000000; // 10MB
const data = fs.readFileSync('./file');
for(let bytesRead = 0; bytesRead < data.length; bytesRead = bytesRead + CHUNK_SIZE) {
// do something with data
}
@koyanloshe
koyanloshe / byteSize.js
Last active June 6, 2020 16:44
Utility functions
function bytesToSize (bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
var i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
}
@koyanloshe
koyanloshe / postgres-brew.md
Created May 5, 2020 18:08 — forked from ibraheem4/postgres-brew.md
Installing Postgres via Brew (OSX)

Installing Postgres via Brew

Pre-Reqs

Brew Package Manager

In your command-line run the following commands:

  1. brew doctor
  2. brew update
@koyanloshe
koyanloshe / trace.js
Last active April 17, 2020 08:34
Software composition by Eric Eliott #Javascript #fp
const trace = label => value => {
console.log(`{$label} : {$value}`);
return value;
}
@koyanloshe
koyanloshe / env.js
Last active April 17, 2020 08:28
process.env setter #Javascript
const fs = require('fs')
const rootDir = require('../utils/rootDir')
const env = {
// accepts a filename and if no filename, it defaults to .env
config: filename => {
filename === undefined ? (filename = '.env') : filename
fs.readFileSync(`${rootDir}/${filename}`, 'utf-8')
@koyanloshe
koyanloshe / Promisified compose in Javascript
Last active August 10, 2022 19:19
Pipe & Compose in Javascript #Javascript #fp
// Async compose
const compose = (…functions) => input => functions.reduceRight((chain, func) => chain.then(func), Promise.resolve(input));
// Functions fn1, fn2, fn3 can be standard synchronous functions or return a Promise
compose(fn3, fn2, fn1)(input).then(result => console.log(`Do with the ${result} as you please`))
@koyanloshe
koyanloshe / cloneObj
Last active April 17, 2020 08:27
Cloning objects without effects #Javascript
// Object.assign has an issue with nested Objects. This prevents that leakage
const cloneObj = (existingObj) => {
return JSON.parse(JSON.stringify(existingObj))
}
@koyanloshe
koyanloshe / basic.vim
Last active April 17, 2020 08:26 — forked from gaveen/basic.vim
Gists for the blog post about vimfiles at: https://gaveen.me/2020/02/my-vim-story/ #vim
" Basic settings
set nocompatible " behave like vim, not old vi
set modelines=0 " don't use modelines
set viminfo='20,\"50 " use a viminfo file,...
set history=50 " and limit history
set ruler " show the cursor position
set title " show title on the window
set autoread " reload file if changed outside vim
set autowrite " save file on some commands
set scrolloff=1 " minimal no. of lines around cursor
@koyanloshe
koyanloshe / server.js
Last active April 17, 2020 08:28
Basic server for most filetypes #Javascript
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request ', request.url);
var filePath = '.' + request.url;
if (filePath == './') {
filePath = './index.html';