Skip to content

Instantly share code, notes, and snippets.

View PuruVJ's full-sized avatar
🏠
I'm just a simple man trying to quit Vim

Puru Vijay PuruVJ

🏠
I'm just a simple man trying to quit Vim
View GitHub Profile
import { replaceState } from '$app/navigation';
import { page } from '$app/state';
import { tick } from 'svelte';
export function box<T>(getter: () => T, setter?: (value: T) => void) {
let derived_val = $derived(getter());
$effect(() => {
setter?.($state.snapshot(derived_val) as T);
});
export class EventEmitter {
#events: Map<string, Set<Function>> = new Map();
on(event: string, callback: Function): void {
let callbacks = this.#events.get(event);
if (!callbacks) {
callbacks = new Set();
this.#events.set(event, callbacks);
}
callbacks.add(callback);
import { parse, type Literal } from 'acorn';
import { type Node } from 'estree-walker';
import { walk } from 'zimmerframe';
const ast = parse(output, {
ecmaVersion: 2022,
sourceType: 'module',
sourceFile: full_path,
});
@PuruVJ
PuruVJ / svelte-compiler-sizes.json
Created June 8, 2024 15:07
All the svelte compiler sizes from v3 onwards, fully bundled with rollup and esbuild
{
"3.0.0": "784.72kb",
"3.0.0-alpha1": "763.83kb",
"3.0.0-alpha10": "789.48kb",
"3.0.0-alpha11": "789.48kb",
"3.0.0-alpha12": "789.48kb",
"3.0.0-alpha13": "788.25kb",
"3.0.0-alpha14": "787.04kb",
"3.0.0-alpha15": "787.04kb",
"3.0.0-alpha16": "787.03kb",
@PuruVJ
PuruVJ / simplify_debt.ts
Last active November 8, 2023 12:44
How to simplify debt among a bunch of users within a group
class Ledger {
users = new Set<string>(); // Holds unique users
balances: Record<string, number> = {}; // Balance of each user
transactions: Record<`${string}:${string}`, number> = {}; // person1:person2 -> amount
simplified = false;
#results: Record<
'simplified' | 'non_simplified',
{
payer_user_id: string;
import { FormError } from '$lib/errors.js';
import { db } from '$lib/server/db.js';
import { GROUP_QUERY } from '$lib/server/queries/group.query.js';
import {
expenses_table,
group_members_table,
ledger_table,
users_table,
} from '$lib/server/schema.js';
import { listify_names, sum_arr } from '$lib/utils.js';
@PuruVJ
PuruVJ / vite-css-inline-plugin.ts
Last active September 19, 2022 11:05
Rollup plugin implementing Vite's CSS Modules, and inline functionality. No preprocessors
// @ts-check
import { dataToEsm } from '@rollup/pluginutils';
import fs from 'fs';
import lightningcss from 'lightningcss';
import { resolve } from 'path';
/** @type {() => import('rollup').Plugin} */
export const processCSS = () => {
/** @type {Map<string, import('lightningcss').TransformResult>} */
const idMap = new Map();
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
@PuruVJ
PuruVJ / Component.svelte
Last active August 1, 2021 12:36
Interval as a Svelte Store
<script>
import { createIntervalStore } from './interval.store.ts'
// Create interval of 6 seconds
const interval = createIntervalStore(6000);
$: $interval, doSomething();
</script>
import { useAtom } from 'jotai';
import { useEffect, useLayoutEffect } from 'preact/hooks';
import { themeAtom } from '__/stores/theme.store';
type Theme = 'light' | 'dark';
// This is needed here
let isFirstUpdate = true;
const localValue = localStorage.getItem<Theme>('theme:type');