Skip to content

Instantly share code, notes, and snippets.

View Gergling's full-sized avatar
🦇
Behind a keyboard

Greg Gergling

🦇
Behind a keyboard
View GitHub Profile
@Gergling
Gergling / config-example.ts
Created August 18, 2025 12:04
Some utility functions and types for setting up a consistent electron app IPC layer. `core.ts` is the abstracted functions and types, and the other files illustrate the implementation examples.
// This is the property indicating where the functions will be exposed. E.g. window.ipc.testIPC('test');
export const IPC_EXPOSURE_PROPERTY_NAME = 'ipc';
// Minimalist configuration type for the renderer side.
export type IpcInvocationConfig = IpcInvocationConfigBase<{
testIPC: (message: string) => string;
runQuery: (query: string) => { success: boolean; error?: string };
}>;
// The handler configuration will run functions from the backend.
import { useQueries, useQuery } from "@tanstack/react-query";
import { useEffect, useReducer, useState } from "react";
// We're going to be listing items of this type:
type Item = {
base: {
id: number;
name: string;
speed: number;
};
@Gergling
Gergling / get-aggregation.ts
Last active March 23, 2025 14:32
A simple aggregation function for handling lists of data.
type KeyGenerator<ItemProps> = (item: ItemProps) => string;
type Reducer<AggregatedProps, ItemProps> = (
aggregation: AggregatedProps,
item: ItemProps,
idx: number,
) => AggregatedProps;
type Aggregation<AggregatedProps> = {
key: string;
items: ItemProps[];
values: AggregatedProps;
@Gergling
Gergling / index.js
Created October 20, 2018 19:56
Star Trek Engineering Crisis
const getRandomItem = list => list[Math.floor(Math.random() * list.length)];
const components = [
'matter/antimatter manifold',
'main deflector dish',
'kettle',
'Captain\'s copy of TMNT Issue #1 signed by Roger Moore',
'baffle plate',
'navigational laser',
'secondary plasma relay'
@Gergling
Gergling / file-size-listing.js
Last active October 10, 2018 13:48
Script to print the sizes of files in order of largest to smallest within their parent folders. Useful for investigating large folders for things which should be deleted or archived for space.
var fs = require("fs"); //Load the filesystem module
const { join } = require('path');
class FileNode {
constructor(path) {
const isDirectory = fs.lstatSync(path).isDirectory();
this.path = path;
if (isDirectory) {
function stringifyExplicitObject(subject) {
return '{' + Object.entries(subject).map(function (entry) {
return entry[0] + ':' + stringify(entry[1]);
}).join(',') + '}';
}
function stringifyArray(subject) {
return '[' + subject.map(function (element) {
return stringify(element);
}).join(',') + ']';
}
@Gergling
Gergling / MutableObject.js
Created April 12, 2018 09:46
A mutable object for functional programming.
// Deliberately not using class because I want data to be private.
function MutableObject() {
const data = {};
const get = name => (name === undefined) ? data : data[name];
const setObject = value => Object.assign(data, value);
const setByFunction = fnc => fnc(this);
const setNameValue = (name, value) => data[name] = value;
const set = (a, b) => {
if (typeof a === 'object') {
@Gergling
Gergling / mischief.js
Created April 9, 2018 13:50
Canvas attachment for mischief
(function (document) {
var canvas = document.createElement('canvas');
canvas.style.width='100%';
canvas.style.height='100%';
canvas.style.top=0;
canvas.style.left=0;
canvas.style.position='absolute';
document.body.appendChild(canvas);
return canvas;
}(document));
@Gergling
Gergling / functionalise.js
Last active April 9, 2018 13:26
Putting the FUN in FUNctional programming.
// Takes a function with two parameters and allows them to be used in the format fnc(x)(y)().
function functionalise(fnc) {
return function functionalised(x) {
return function (y) {
if (y === undefined) {
return x;
} else {
return functionalised(fnc(x, y));
}
};
@Gergling
Gergling / pdf-fields.php
Last active April 9, 2018 13:22
Takes a `pdftk` field dump and turns it into a PHP-copyable output.
<?php
// Usage example: pdftk stuff.pdf dump_data_fields | php pdf-fields.php
$stdin = file('php://stdin');
// print_r($stdin);
$data = [];
$i = 0;
foreach($stdin as $idx => $line) {
$chunks = explode(': ', $line);