Skip to content

Instantly share code, notes, and snippets.

View kirpalmakanga's full-sized avatar

Kirpal Makanga kirpalmakanga

View GitHub Profile
@kirpalmakanga
kirpalmakanga / filterMap.ts
Last active June 28, 2025 10:17
filter and map array in a single loop
function filterMap<T extends unknown, R extends unknown>(
arr: T[],
filterFn: (item: T, index: number) => boolean,
mapFn: (item: T, index: number) => R
) {
return arr.reduce((acc, item, i) => {
if (filterFn(item, i)) acc.push(mapFn(item, i));
return acc;
}, [] as R[]);
@kirpalmakanga
kirpalmakanga / update.ts
Last active June 28, 2025 09:41
update object(s) in array
export function update<T extends Record<K, unknown>, K extends keyof T>(
arr: T[],
predicate: (item: T, index: number, array: T[]) => boolean,
payload: Partial<T>
) {
const targetIndex = arr.findIndex(predicate);
if (targetIndex > -1) {
return arr.with(targetIndex, { ...arr[targetIndex], ...payload });
}
export function isEqual(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (a == null || b == null) return a === b;
if (typeof a !== typeof b) return false;
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
return a.every((item, index) => isEqual(item, b[index]));
@kirpalmakanga
kirpalmakanga / pick.ts
Created February 6, 2024 17:53
Type-safe pick function
export function pick<T extends object, K extends keyof T>(
base: T,
...keys: K[]
): Pick<T, K> {
if (!keys.length) return base;
const entries = keys.map((key) => [key, base[key]]);
return Object.fromEntries(entries);
}
@kirpalmakanga
kirpalmakanga / omit.ts
Last active May 28, 2025 14:54
Type-safe omit function
export function omit<T extends object, K extends keyof T>(
base: T,
...keys: K[]
): Omit<T, K> {
if (keys.length) {
const result = { ...base };
for (const key of keys) delete result[key];
return result;
@kirpalmakanga
kirpalmakanga / onClickOutside.tsx
Created December 22, 2021 16:07
Helper to detect click outside of element
export const onClickOutside = (element: HTMLElement, callback: Function) => {
const handler = ({ target }: Event) => {
let currentElement = target;
do {
if (currentElement === element) {
return;
}
@kirpalmakanga
kirpalmakanga / ScrollContainer.tsx
Created October 19, 2021 17:55
Flex scroll container for Solid JS
import { Component } from 'solid-js';
import { HTMLElementEvent } from '../../@types/alltypes';
import { throttle } from '../lib/helpers';
interface Props {
className?: string;
children: any;
ref: (el: HTMLDivElement) => HTMLDivElement;
onScroll?: (e: HTMLElementEvent<HTMLDivElement>) => void;
onScrollEnd?: () => void;
@kirpalmakanga
kirpalmakanga / getStartOfQuarter.js
Last active August 10, 2021 11:30
Get date of intervals starts
const getStartOfQuarter = date => {
const
quarters = {
1: 1,
2: 4,
3: 7,
4: 10
},
d = date ? new Date(date) : new Date(),
q = Math.ceil((d.getMonth() + 1) / 3),
@kirpalmakanga
kirpalmakanga / mousePosition.js
Created May 11, 2020 20:39
Get mouse position
const out = document.querySelector('output');
const plaindiv = document.querySelector('div');
const absdiv = document.querySelector('div.absolute');
const floatdiv = document.querySelector('div.float');
const getposition = ev => {
let x = ev.clientX;
let y = ev.clientY;
let pos = ev.target.getBoundingClientRect();
return {
@kirpalmakanga
kirpalmakanga / stripScripts.js
Created March 3, 2020 09:58
Remove all script tags from html string
const stripScripts = (s) => {
const div = document.createElement('div');
div.innerHTML = s;
const scripts = div.querySelectorAll('script');
for (var script of scripts) {
script.parentNode.removeChild(script);
}