Skip to content

Instantly share code, notes, and snippets.

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

ERDesigns - Ernst Reidinga erdesigns-eu

$ root@erdesigns-eu
View GitHub Profile
@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.js
Created November 28, 2023 08:26
JSX DOM
const h = (tag, attrs, ...children) => {
const elm = document.createElement(tag)
for (let key in attrs) {
if (key.slice(0, 2) == 'on') {
const evtName = key.slice(2)
const cb = attrs[key]
if (cb == null) continue // we can use null or undefnied to suppress
elm.addEventListener(evtName, cb)
} else if (['disabled', 'autocomplete', 'selected', 'checked'].indexOf(key) > -1) {
if (attrs[key]) {
@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;
@erdesigns-eu
erdesigns-eu / license.js
Last active July 13, 2023 15:15
License key generator and validator in Javascript
/*
This generator does not generate valid keys for any software, its merely a demonstration of
how to create a simple license key registration and validation system. You could store the
generated license keys on the server, and add online-validation when the user wants to use
the entered key to register your application.
** DO NOT ** use these as is in your production code (DUHH!)
*/
// Master key is the sum of the license key characters. This sum is the same for every key.
@erdesigns-eu
erdesigns-eu / dependency.test.js
Created November 23, 2022 14:33
Test dependencies in package.json
/**
* Terminal colors and style
*/
const Color = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",