Skip to content

Instantly share code, notes, and snippets.

View kneczaj's full-sized avatar

Kamil Neczaj kneczaj

View GitHub Profile
@kneczaj
kneczaj / Meta.keytab
Last active January 27, 2021 13:26
Konsole key mapping with Meta as Ctrl
keyboard "Meta"
key Space+Ctrl : "\x00"
key F7+AnyModifier : "\E[18;*~"
key F7-AnyModifier : "\E[18~"
key F8+AnyModifier : "\E[19;*~"
key F8-AnyModifier : "\E[19~"
key F5+AnyModifier : "\E[15;*~"
key F5-AnyModifier : "\E[15~"
key F6+AnyModifier : "\E[17;*~"
key F6-AnyModifier : "\E[17~"
@kneczaj
kneczaj / spec.ts
Last active January 29, 2021 09:18
Mock window.location
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
(global as any).window = Object.create(window);
const url = "http://dummy.com/dummy";
Object.defineProperty(window, 'location', {
value: {
href: url
}
});
@kneczaj
kneczaj / user-provider.tsx
Created January 18, 2021 10:25
Authentication with GraphQL and React context
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
import React from "react";
import { getInitialState, User } from "../models/user";
import { isNull } from "../../util";
import { createContext, createContextHook } from "../../utils/context-hook";
import { ProviderComponentProps } from "../../components/provider-group";
import { useState } from "../../hooks/state";
import { useHistory } from "../../routing/hooks/history";
@kneczaj
kneczaj / hooks-collection.spec.ts
Last active December 1, 2020 00:20
Hooks, providers, hooks mocks
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
import React from "react";
import { render } from "@testing-library/react";
import { InnerComponent } from './hooks-collection-sandbox/innner-component';
import { useTest } from "./hooks-collection-sandbox/mocks";
import * as HooksCollection from "./hooks-collection-sandbox/mocks";
import { ContextError } from "../utils/context-hook";
import { mockHookCollection } from "./hooks-collection";
@kneczaj
kneczaj / mock-context-hook.ts
Created November 23, 2020 14:07
Mock context hook
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
export const mockContextHook = <THookFn extends (...args: any[]) => any, Method extends string, Obj extends { [key in Method]: THookFn}>(obj: Obj, method: Method, defaultValue: ReturnType<THookFn>) =>
(val?: ReturnType<THookFn>) => {
jest.spyOn(obj, method).mockImplementation((...args: Parameters<THookFn>) => val || defaultValue)
}
@kneczaj
kneczaj / context-tools.ts
Created November 20, 2020 00:44
Context tools
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
import React, { Context as ReactContext, useContext } from "react";
import { isUndefined } from "../util";
/**
* Differs from react context by mandatory displayName
*/
export interface Context<T> extends ReactContext<T> {
@kneczaj
kneczaj / go.sh
Last active January 27, 2021 13:27
Move a file across all commits in the branch
#!/bin/bash
# moves .gitignore from root directory to /frontend, . is escaped by \
git filter-branch -f --index-filter 'git ls-files -s | sed "s/\.gitignore/frontend\/\.gitignore/" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD
@kneczaj
kneczaj / field-format-on-blur.ts
Last active October 28, 2020 01:11
Format on blur & format at once with react-final-form
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
import { FieldRenderProps, useField as useFieldBase, UseFieldConfig as UseFieldConfigBase } from "react-final-form";
import { isUndefined } from "../../util";
import { useMemo } from "react";
export interface UseFieldConfig<FieldValue> extends Omit<UseFieldConfigBase<FieldValue>, 'formatOnBlur'> {
formatOnBlur?: UseFieldConfigBase<FieldValue>['format'];
}
@kneczaj
kneczaj / skip-empty.ts
Created October 12, 2020 14:10
A validator decorator to validate only if the field is not empty
// To the extent possible under law, Kamil Neczaj has waived all copyright and related or neighboring rights to this code,
// which is published here under license CC0, full text of the license here: http://creativecommons.org/publicdomain/zero/1.0/"
/**
* false if no error, otherwise true or a dict with error details
*/
export type ValidationError = [ string, { [key: string]: any }] | string;
export type ValidationResult = ValidationError | null;
/**
* A validator function which returns an error in a dict format which then can be used to generate a translated error
@kneczaj
kneczaj / example.txt
Created October 7, 2020 21:40
PyCharm find replace with capturing group
find:
validators\=\{(.*)}
in the first parentheses there is the captured text
replace
validate\=\{composeValidators\($1\)}
then the captured text lands in $1