Skip to content

Instantly share code, notes, and snippets.

View erdesigns-eu's full-sized avatar
$ root@erdesigns

ERDesigns - Ernst Reidinga erdesigns-eu

$ root@erdesigns
View GitHub Profile
@erdesigns-eu
erdesigns-eu / limitConcurrentPromises.ts
Created July 25, 2024 07:08
Limit the number of concurrent promises
/**
* Limit the number of concurrent promises
* @param promises The array of promise functions to execute
* @param maxConcurrent The maximum number of promises to execute concurrently
* @returns A promise that resolves with an array of results
*/
export const limitConcurrentPromises = async <T>(promises: (() => Promise<T>)[], maxConcurrent: number): Promise<T[]> => {
// Array to store the results of each promise
const results: T[] = [];
// Array to keep track of the currently executing promises
@erdesigns-eu
erdesigns-eu / translate.ts
Last active January 3, 2025 09:07
Node.js script to translate i18n JSON files using the OpenAI completions API
import fetch from 'node-fetch';
import fs from 'fs';
/**
* OpenAI API key
* @see https://platform.openai.com/docs/quickstart
* @note Do not commit the API key to version control.
*/
const apiKey = ''; // Add your OpenAI API key here
@erdesigns-eu
erdesigns-eu / ClientStorage.ts
Created July 1, 2024 07:46
TypeScript ClientStorage Class with LocalStorage and React Integration
/**
* A callback function that is called when a value changes.
* @callback SubscriptionCallback
* @param {any} value The new value.
* @returns {void}
*/
type SubscriptionCallback<T = any> = (value: T) => void;
/**
* A simple client-side storage class that uses localStorage to store values.
@erdesigns-eu
erdesigns-eu / vue.ts
Last active May 13, 2024 15:04
Find child components in a Vue3 component. (Alternative for Vue2 $children)
/**
* Find all Vue components that match the given matcher and return them in an array.
* @param parent The parent component to search.
* @param matcher The matcher to use.
* @returns An array of components that match the given matcher.
*/
export const findVueChildComponents = (parent: any, matcher: RegExp | String | undefined) => {
const found: any[] = [];
const root = parent.$.subTree;
@erdesigns-eu
erdesigns-eu / jsx.ts
Last active August 29, 2024 10:54
JSX DOM
type Attrs = {
[key: string]: any;
};
const h = (tag: string, attrs: Attrs, ...children: (Node | string)[]): HTMLElement => {
const elm = document.createElement(tag);
for (let key in attrs) {
if (key.startsWith('on')) {
const evtName = key.slice(2);
@erdesigns-eu
erdesigns-eu / lexer.ts
Last active November 10, 2023 13:49
Pascal Lexer implemented in Typescript
type TokenType = 'KEYWORD' | 'IDENTIFIER' | 'NUMBER' | 'STRING' | 'OPERATOR' | 'DELIMITER' | 'EOF' | 'UNKNOWN' | 'PROCEDURE' | 'FUNCTION' | 'CONSTANT' | 'RECORD';
class Token {
constructor(public type: TokenType, public value: string, public position: number, public line: number, public column: number) {}
}
class Lexer {
private pos: number = 0;
private line: number = 1;
private column: number = 0;
@erdesigns-eu
erdesigns-eu / fluvius.txt
Created October 25, 2023 15:27
Fluvius prepaid electricity meter api endpoint
In Belgium there is a possibility to have a prepaid smart meter for electricity. Fluvius has an app and webpage where you can see the details.
I wanted to incorporate this api into a dashboard for a client, so for this purpose i tried to fetch the data myself. Use this on you own risk!
I contacted fluvius to ask if i could access their api for this purpose, but i didnt get an answer. So im sharing it here for anyone else who likes to do the same.
GET /prepaid/api/prepaid-contracts HTTP/1.1
Host: mijn.fluvius.be
X-Fluv-Prepaid-Identification-Code: <UNIQUE-PREPAID-CODE>
X-Fluv-Prepaid-Postal-Code: <ZIPCODE>
@erdesigns-eu
erdesigns-eu / Avatar-Colors.js
Created October 23, 2023 13:12
Generate colors based on initials (for use in avatar)
// Generate a color based on letters.
export const generateColor = (letters) => {
// Generate a simple hash from a string
const getHashOfString = (str) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
hash = Math.abs(hash);
@erdesigns-eu
erdesigns-eu / parseDFM.js
Last active October 4, 2023 14:30
Parse Delphi DFM in JavaScript
const parseDFM = (text) => {
let result = null;
// Replace all returns with newlines
text = text.replace(/\r\n/g, '\n');
text = text.replace(/\r/g, '\n');
// Split into lines
const lines = text.split('\n');
let currentComponent = null;
@erdesigns-eu
erdesigns-eu / untHTTPManager.pas
Created July 18, 2023 11:27
HTTP Manager (Threaded HTTP requests)
unit untHTTPManager;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.Threading, IdHTTP, IdSSLOpenSSL,
Winapi.Windows, Winapi.Messages;
type
THTTPThreadManager = class;