Skip to content

Instantly share code, notes, and snippets.

View karol-majewski's full-sized avatar

Karol Majewski karol-majewski

View GitHub Profile
type NonEmptyArray<T> = [T, ...T[]];
const names: NonEmptyArray<string> = ['Sheldon', 'Leonard', 'Penny', 'Rajesh', 'Howard'];
function main(names: NonEmptyArray<string>, n: number){
let index = n - 1;
while (index >= names.length) {
index = Math.floor((index - names.length) / 2);
}
import React from 'react';
import createPersistedState from 'use-persisted-state';
import { storage } from '../utilities/storage';
import { hasElements, head, isDefined } from '@rhim/utils';
const usePersistedEntityId = createPersistedState('some-entity-key', storage);
interface Response<T> {
status: 'success' | 'pending' | 'error';
data: T | null;
}
/**
* A promise tracker that will be updated
* when promise resolves or rejects
*/
const response: Response<unknown> = {
@karol-majewski
karol-majewski / getStyles.ts
Created September 28, 2023 09:52
Function composition
import { FlattenSimpleInterpolation } from 'styled-components';
import { pick } from './objects';
type Stylesheet<K extends string> = Record<K, FlattenSimpleInterpolation>;
/**
* CSS-in-JS equivalent for `classNames`.
*
* @example
@karol-majewski
karol-majewski / withDefaults.tsx
Last active April 30, 2024 16:01
Functional composition for React props
import * as React from 'react';
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
/**
* Supplies default props.
*/
export function withDefaults<T extends React.ComponentType<any>, U extends Partial<React.ComponentProps<T>>>(
Component: T,
defaults: U,
@karol-majewski
karol-majewski / gist:db48d658cbda8cf6613a0525af57d682
Created November 11, 2018 03:22
Chrome DevTools blackbox patterns
/node_modules/
webpack:///webpack
^webpack://.*/process\-update\.js$
^webpack://.*/client\.js$
^debugger://
\(webpack\)-hot-middleware
webpack/bootstrap
/jquery\.min\.js$
/jquery\-migrate\-1\.4\.1\.min\.js$
@karol-majewski
karol-majewski / tuple-operations.ts
Created February 29, 2024 16:13
Preserving tuple length
// #region Necessary types
type NonEmptyArray<T> = [head: T, ...T[]];
type Tuple<T> = [T, T];
declare function head<T>(collection: [T, ...T[]]): T;
// #endregion
const cloudflareDNS: NonEmptyArray<string> = ["1.1.1.1"];
const googleDNS: Tuple<string> = ["8.8.8.8", "8.8.4.4"]
// ✅ These Just Work™️
@karol-majewski
karol-majewski / homogeneous-array.ts
Created September 22, 2019 22:13
Homogeneous arrays in TypeScript (+ check if a type is a union)
type Singleton = string;
type Union = string | number;
type DistributedKeyOf<T> =
T extends any
? keyof T
: never;
type DistributedValueOf<T> = T[DistributedKeyOf<T>];
@karol-majewski
karol-majewski / promisify.ts
Created December 29, 2023 13:36
Convert synchronous methods to async methods
type PromisifyMethods<T> = {
[K in keyof T]: T[K] extends AnyFunction ? (...parameters: Parameters<T[K]>) => Promise<ReturnType<T[K]>> : T[K];
};
type AsynchronousStorage = PromisifyMethods<Storage>;
export function promisify(storage: Storage): AsynchronousStorage {
return new Proxy(storage, {
get(target, name: string) {
if (typeof target[name] === 'function') {
@karol-majewski
karol-majewski / is-palindrome.ts
Created July 3, 2020 00:21
The easiest way to check if a string is a palindrome in TypeScript
const isPalindrome = (phrase: string): boolean => {
const original = phrase.toLowerCase().replace(/[^A-Za-z0-9]/g, '');
const reversed = original.split('').reverse().join('');
return original === reversed
}
console.log(
isPalindrome("A man, a plan, a canal – Panama!")
)