This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type State = string; | |
type Message = string; | |
type StatesList = readonly State[]; | |
type MessagesConfig = Record<State, Record<Message, State>>; | |
type OneOf<S extends StatesList> = S[number]; | |
type Send< | |
SBConfig extends StateBuilderConfig<StatesList, State, State>, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const alphabetLength = 26; | |
const numberOfDigits = 9; | |
const toStr = (num: number): string => { | |
return Number(num + numberOfDigits).toString(alphabetLength + numberOfDigits + 1).toLocaleUpperCase(); | |
} | |
const stringify = (num: number): string | undefined => { | |
if (num <= 0) { | |
return undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useMemo } from 'react'; | |
import { useSearchParams } from 'react-router-dom'; | |
import { z } from 'zod'; | |
export const useTypedParams = <T extends z.SomeZodObject>(zodSchema: T) => { | |
type TParams = z.infer<T>; | |
const [urlSearchParams, setUrlSearchParams] = useSearchParams(); | |
const searchParams = useMemo<TParams>(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod'; | |
export const PageSchema = z | |
.string() | |
.refine((s) => { | |
const n = Number(s); | |
return Number.isFinite(n) && !Number.isNaN(n); | |
}) | |
.transform(Number); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect, useCallback, useContext } from 'react'; | |
import { useAuth } from '../AuthContext'; | |
const allPermissions = [ | |
'posts:write', | |
'posts:read', | |
'comments:write', | |
'comments:read', | |
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type InBoundsOf<L extends string, N extends number = number> = N & { __inBoundsOf: L }; | |
type OutsideOfBoundsOf<L extends string, N extends number = number> = N & { __outsideOfBounds: L }; | |
type BoundedNumberOf<L extends string, N extends number = number> = InBoundsOf<L, N> | OutsideOfBoundsOf<L, N> | |
class List<T, L extends string> { | |
constructor(public readonly arr: T[]) { } | |
at(pos: InBoundsOf<L>): T; | |
at(pos: OutsideOfBoundsOf<L>): undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isEnum = <T extends string | number, TEnumValue extends string>(enumVariable: { [key in T]: TEnumValue }) => { | |
const enumValues = Object.values(enumVariable); | |
return (value: unknown): value is TEnumValue => (typeof value === 'string' || typeof value === 'number') && enumValues.includes(value); | |
}; | |
// with number indexes not allowed | |
export const isEnum = <T extends string, TEnumValue extends string>(enumVariable: { [key in T]: TEnumValue }) => { | |
const enumValues = Object.values(enumVariable); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type EndsWithResult<S extends string, E extends string> = S extends `${string}${E}` ? S : `${S}${E}` | |
const endsWith = <E extends string>(end: E) => <S extends string>(str: S): str is EndsWithResult<S, E> => { | |
return str.slice(-end.length) === end; | |
} | |
const str = 'something' as `some${string}`; | |
const qqq = endsWith('foo')(str); | |
if (qqq) { | |
str; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const { mapValues } = require('lodash'); | |
const lock = require('./package-lock.json'); | |
const pack = require('./package.json'); | |
const newDependencies = mapValues( | |
pack.dependencies, | |
(depVersion, depName) => lock.dependencies[depName].version | |
); | |
const newDevDepencies = mapValues( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type FilterUnion<T extends string, Keys extends string> = { | |
[Key in Keys extends infer R ? (R extends `${string}${T}${string}` ? R : never) : never]: string; | |
} extends infer O | |
? keyof O | |
: never; | |
export const stringIncludes = <T extends string>(text: T) => <S extends string>( | |
str: S | |
): str is FilterUnion<T, S> => str.includes(text); |
OlderNewer