Skip to content

Instantly share code, notes, and snippets.

View materkel's full-sized avatar
🕺

Matthias Merkel materkel

🕺
View GitHub Profile
@materkel
materkel / EventEmitter.zig
Last active February 13, 2024 09:37
Basic Event Emitter in Zig 0.11
const std = @import("std");
const String = []const u8;
const ListenersList = std.ArrayList(*const fn(String) void);
pub const EventEmitter = struct {
allocator: *std.mem.Allocator,
listeners: std.StringHashMap(*ListenersList),
pub fn init(allocator: *std.mem.Allocator) EventEmitter {
return EventEmitter{
function fetchFileDimensionsAsBlob(file: File) {
const src = URL.createObjectURL(file);
const image = new Image();
image.onload = function() {
resolve({
src,
dimensions: {
width: image.width,
height: image.height,
},
@materkel
materkel / filterByConditions.ts
Created August 9, 2020 00:38
filter array with multiple conditions by making use of destructuring
function filterByConditions(arr: any[], ...filterFunctionConditions: Function[]): any[][] {
const results = [[]];
filterFunctionConditions.forEach(() => results.push([]));
for (const value of arr) {
let conditionMet = false;
for (let i = 0; i < filterFunctionConditions.length; i++) {
if (filterFunctionConditions[i](value)) {
results[i].push(value);
conditionMet = true;
break;
@materkel
materkel / divideBy.ts
Last active August 6, 2020 14:03
return both true and false values from filtering an array (with destructuring)
function divideBy(arr: any[], divideFn: Function) {
return arr.reduce((agg, curr) => divideFn(curr) ?
[[...agg[0], curr], agg[1]] :
[agg[0], [...agg[1], curr]],
[[], []]);
}
const fruits = ['apple', 'orange', 'banana'];
const divideFn = (fruit) => fruit === 'apple';

Keybase proof

I hereby claim:

  • I am mfressdorf on github.
  • I am mfressdorf (https://keybase.io/mfressdorf) on keybase.
  • I have a public key ASCS8qpFmO3_jtzKKSX2F6Z-2-ouPO49nGw0oJFvLeOe5Ao

To claim this, I am signing this object:

@materkel
materkel / amqplib-delayed-message.js
Last active August 31, 2024 02:04
Scheduling messages with RabbitMQ, using the rabbitmq_delayed_message_exchange plugin and amqplib in NodeJS
/**
* Install and enable the rabbitmq_delayed_message_exchange plugin as described by Alvaro Videla in this blogpost:
* https://www.rabbitmq.com/blog/2015/04/16/scheduling-messages-with-rabbitmq/
*/
const amqp = require('amqplib');
const exchange = 'yourExchangeName';
const queue = 'yourQueueName';
const queueBinding = 'yourQueueBindingName';
// Message consumer
@materkel
materkel / fb-app-secret-proof.js
Last active September 7, 2021 11:27
create facebook appsecret proof in NodeJS
const crypto = require('crypto');
let accessToken = 'your fb accesstoken' || 'facebookClientId' + '|' + 'facebookClientSecret'
let clientSecret = 'your fb client secret'
let appsecret_proof: crypto.createHmac('sha256', clientSecret).update(accessToken).digest('hex')
#!/bin/bash
# empty Trash
rm -rf ~/.local/share/Trash/*
# output to stdout & file
ls | tee file.txt
# output to stdout & append to file
ls | tee -a file.txt
@materkel
materkel / docker-commands
Last active May 16, 2016 19:06
collection of useful docker commands
#!/bin/bash
# Get IP Address of all containers
docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
# remove all containers
docker rm -f $(docker ps -aq)
# remove all images
docker rmi -f $(docker images -q)