Skip to content

Instantly share code, notes, and snippets.

View a1xon's full-sized avatar
🐧
cgn.dev

Piet Althoff a1xon

🐧
cgn.dev
View GitHub Profile
@a1xon
a1xon / cache.ts
Created July 10, 2022 20:51
Lightweight Cache
export class LighweightCache {
entries: CacheEntry[];
size: number;
ttl: number;
constructor(size: number, ttl: number) {
this.entries = [];
this.size = size;
this.ttl = ttl;
}
@a1xon
a1xon / pauseableInterval.js
Created March 17, 2022 15:47
Pausable Interval in ES6
class pausableInterval {
constructor(callback, intervalMS, instantStart = true) {
this.callback = callback;
this.intervalMS = intervalMS;
this.lastInterval = -Infinity;
this.remainingMS = 0;
this.paused = false;
this.intervalId = null;
if (instantStart) {
@a1xon
a1xon / AsyncQueueManager.js
Created December 18, 2021 07:03
Queue Manager to handle n async requests
const EventEmitter = require('events');
const { randomUUID } = require('crypto');
class AsyncQueueManager {
constructor(maxSimultaneousCalls = 1) {
this.maxSimultaneousCalls = maxSimultaneousCalls;
this.currentSimultaneousCalls = 0;
this.queue = [];
this.eventEmitter = new EventEmitter();
}
@a1xon
a1xon / hexToHashtagNotation.js
Created October 21, 2021 23:28
js hex-value to hashtag notation string
const hexToHashtagNotation = (hexColor) => {
let splittedColor = {
red : (hexColor >> 16) & 0xFF,
green : (hexColor >> 8) & 0xFF,
blue : hexColor & 0xFF,
}
return Object.values(splittedColor).reduce((hexString, channelValue) => {
const colorValueString = channelValue.toString(16).toUpperCase();
hexString += colorValueString.length === 1 ? `0${colorValueString}` : `${colorValueString}`;