Skip to content

Instantly share code, notes, and snippets.

View amatsagu's full-sized avatar

Krzysztof Rapior amatsagu

View GitHub Profile
@amatsagu
amatsagu / auto-grid.scss
Last active May 29, 2023 21:07
Simple utility classes to help with grid usage.
.grid-container {
$max-columns: 6;
& {
--gap: 0.5rem;
--columns: 6;
--row-height: 400px;
box-sizing: border-box;
padding: var(--gap);
function createLoader(text: string) {
const encoder = new TextEncoder();
const icons = ["[ ]", "[# ]", "[## ]", "[###]"];
let state = 0;
const loader = setInterval(() => {
Deno.stdout.writeSync(encoder.encode(`\r${icons[state++]} ${text}`));
state %= 4;
}, 500);
@amatsagu
amatsagu / object_watcher.ts
Created December 27, 2021 14:11
Log which field has been edited for later use.
function watch<T extends Record<string | symbol, unknown>>(obj: Record<string | symbol, unknown>): T & { $changes: string[] } {
const cache: Record<string | symbol, unknown> = Object.create(null);
Object.defineProperty(obj, "$changes", { get: () => cache });
return new Proxy(obj, {
set: function(target, key, value) {
if (key !== "$changes") {
target[key] = value;
if (value !== undefined) cache[key] = value;
}
@amatsagu
amatsagu / deno_collection.ts
Last active February 17, 2022 16:23
Utility class that can hold a bunch of something in way you can easily access. Collection is a combination of Array & Map.
/** Holds a bunch of something. */
export class Collection<K, V> extends Map<K, V> {
/**
* Returns first item that meet the condition specified in your filter function.
* Identical in behavior to [Array#find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
* */
find(fn: (value: V) => boolean) {
const iter = this.values();
for (const value of iter) {
if (fn(value)) return value;
@amatsagu
amatsagu / deno_event_emitter.ts
Last active November 24, 2021 19:52
Strictly typed, fast Event Emitter.
interface Event<T> {
once?: boolean;
fn: T[keyof T];
}
// deno-lint-ignore no-explicit-any
type Arguments<T> = [T] extends [(...args: infer U) => any] ? U : [T] extends [void] ? [] : [T];
/** Strictly typed, fast Event Emitter. */
export class Emitter<T> {