Skip to content

Instantly share code, notes, and snippets.

View psenger's full-sized avatar
:octocat:
Makin Bacon

Philip A Senger psenger

:octocat:
Makin Bacon
View GitHub Profile
@psenger
psenger / greed-spy.js
Created June 9, 2023 07:46
[Greedy Spy on next value] #Array #JavaScript
const pattern = 'abcdefgh';
for (const [currentChar, nextChar] of pattern.split('').reduce((acc,value,idx,arr)=>{const entry = [value]; if ( arr.length > idx + 1 ) entry.push(arr[idx+1]); acc.push(entry); return acc},[])) {
console.log(currentChar,nextChar)
}
/**
a b
b c
c d
d e
@psenger
psenger / loadCSV.js
Last active June 6, 2023 08:33
[Best way to load CSV's without csv-parser] #JavaScript #CSV
const {readFile} = require("fs").promises;
const run = async () => {
const loadCSV = async (fileName ) => {
const contents = await readFile(fileName, 'utf8');
const lines = contents
.split('\n')
.filter(Boolean)
.map((t)=>t.trim()) // remove leading and trailing \r for non-utf8 files
.map((line)=>{
@psenger
psenger / README.md
Created June 2, 2023 02:59
[How to split a file based on line numbers] #Unix #MacOS

How to split a file on lines

Use case, you have a csv file, you want it to be split by 100 line numbers into files.. The onlys ide effect is you get sample-aa, sample-ab.... no extension

split -l 100 sample.csv sample-
@psenger
psenger / s3-aws.js
Created May 11, 2023 22:41
[S3 ES6 AWS] #AWS #S3
const AWS = require('aws-sdk')
const fs = require("fs").promises;
const {join} = require('path')
const path = require("path");
const { createReadStream, createHash } = require('crypto')
const credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'test',
secretAccessKey: process.env.AWS_SECRET_KEY || 'test' ,
}
@psenger
psenger / readme.md
Created April 28, 2023 04:19
[How to determine the files that have changed in a git commit to a specific hashcode] #git

git diff --name-only HEAD

@psenger
psenger / readme.md
Created April 25, 2023 23:36
[process.exit] #NodeJS

In Node.js, the process.exit() method is used to exit the current Node.js process with a given exit code. The number argument passed to process.exit() represents the exit code that the Node.js process will exit with.

Here are the commonly used exit codes and their meanings:

  • 0: The process exits successfully without any errors.
  • 1: The process exits due to an uncaught exception.
  • 2: The process exits due to a misconfiguration or missing file.
  • 3: The process exits due to a fatal error.
@psenger
psenger / readme.md
Created April 25, 2023 09:19
[process.nextTick setImmediate] #NodeJS

Process NextTick and SetImmediate

In Node.js, process.nextTick() is a method that allows you to schedule a function to be executed on the next iteration of the event loop. It is often used to defer the execution of a function until after the current function has completed.

The process.nextTick() method is similar to setImmediate(), but with a few key differences. While setImmediate() schedules a callback to run in the next iteration of the event loop, process.nextTick() schedules a callback to run at the end of the current iteration of the event loop, before any I/O operations.

Here is an example of how process.nextTick() can be used:

console.log('start');
@psenger
psenger / stream.md
Created April 25, 2023 09:13
[NodeJS Stream] #NodeJS #JavaScript #Stream
@psenger
psenger / 1. buffer_stream.js
Last active April 25, 2023 09:14 — forked from abdulloooh/1. buffer_stream.js
[Advanced Node.js Streams] #Stream #NodeJS
// BUFFER
// Load the whole content into a buffer/ into memory once before wrting it out to user
const http = require("http");
const media = "./testvid.mp4";
const fs = require("fs");
http
.createServer((req, res) => {
@psenger
psenger / readme.md
Created April 4, 2023 03:44
[How to find all the files that changed in a given branch since it was cut] #git

How to find all the files that changed in a given branch since it was cut

  1. you will need the HashCode of when the branch was cut.
  2. then check out the branch..
  3. from the command line execute the following
git diff --name-only <hashcode>..<branch>