Skip to content

Instantly share code, notes, and snippets.

@joergpatz
joergpatz / nodejs-eventloop-timers-and-nexttick.js
Last active April 16, 2021 12:19
Node.js Eventloop Timers and NextTick
/**
* nextTick vs setImmediate, visual explanation
*
* https://stackoverflow.com/questions/17502948/nexttick-vs-setimmediate-visual-explanation
* https://nodejs.dev/learn/understanding-process-nexttick
*/
// SETIMMEDIATE
setImmediate(function A() {
@joergpatz
joergpatz / tagged-templates.js
Created July 5, 2019 09:12
ES6 tagged templates examples
const math = ([x, y], operation) =>
operation(Number(x), Number(y))
const plus = (x, y) => x + y
const minus = (x, y) => x - y
const times = (x, y) => x * y
const div = (x, y) => x / y
math`3 ${plus} 4` //7
math`3 ${minus} 4` //-1
@joergpatz
joergpatz / js-snippets.js
Last active May 10, 2022 11:46
neat javascript helpers
/**
* A pipe function takes an n sequence of operations
* in which each operation takes an argument process it and gives the processed output as an input for the next operation in the sequence.
* The result of a pipe function is a function that is a bundled up version of the sequence of operations.
*
* const pipe = (op1, op2, op3, ... , opN) => {
* return (arg) => opN(op3(op2(op1(arg))));
* }
*/
const bundle = (a, b) => arg => b(a(arg));
@joergpatz
joergpatz / focus.forward.directive.ts
Last active March 14, 2017 18:01
ng2-focus-forward-directive
import { Directive, HostListener, ElementRef, Renderer } from '@angular/core';
@Directive({
selector: 'input'
})
export class InputFocusForwarderDirective {
constructor(private elRef: ElementRef, private renderer: Renderer) {
}
@joergpatz
joergpatz / nvm_update.sh
Created November 14, 2016 16:04
script for updating Node Version Manager
#!/bin/sh
set -e
cd ~/.nvm
git fetch --tags
TAG=$(git describe --tags `git rev-list --tags --max-count=1`)
echo "Checking out tag $TAG..."
git checkout "$TAG"
@joergpatz
joergpatz / one-curl-statement-for-loop-download.sh
Created June 21, 2016 22:49
How to download assets from a url with one statement
curl -u user:password -o "#1.jpg" https://example.org/\[1-10\].jpg
@joergpatz
joergpatz / save-console-object-output.txt
Created March 15, 2016 17:10
How to save the output of a console.log(object) to a file
- Right click on the object in console and click "Save as a global variable"
- the output will be something like "temp1"
- type in console copy(temp1)
- paste to your favorite text editor
@joergpatz
joergpatz / getimagesizefromstring-curl.php
Created March 25, 2015 12:24
getimagesizefromstring-curl
//because of security option: allow_url_fopen=0
$uri = 'https://example.com/example.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$bin = curl_exec($ch);
$data = getimagesizefromstring($bin);
@joergpatz
joergpatz / keybase.md
Created March 10, 2015 23:02
Keybase

Keybase proof

I hereby claim:

  • I am joergpatz on github.
  • I am joergpatz (https://keybase.io/joergpatz) on keybase.
  • I have a public key whose fingerprint is 05FE 7718 9BCC ED57 3D55 F740 32FC 2F86 6D54 97AE

To claim this, I am signing this object:

@joergpatz
joergpatz / fork-cli-php-script-from-webserver-php-script.php
Created January 9, 2015 10:13
bookmark: fork cli php script from webserver php script
//fork a CLI script from a webserver PHP script using the popen() function
//This allows to nicely transfer parameters to the new script instance like this:
$bgproc = popen('php "/my/path/my-bckgrnd-proc.php"', 'w');
if($bgproc===false){
die('Could not open bgrnd process');
}else{
// send params through stdin pipe to bgrnd process:
$p1 = serialize($param1);
$p2 = serialize($param2);
$p3 = serialize($param3);