Skip to content

Instantly share code, notes, and snippets.

View kneczaj's full-sized avatar

Kamil Neczaj kneczaj

View GitHub Profile
@kneczaj
kneczaj / index.ts
Last active September 10, 2020 03:34
A class-alike way to use Redux-saga
// 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/"
// please read it in the following order: saga-base.ts, sample-saga.ts, root-saga.ts, index.ts
// then to add sagas to a redux store in index.ts:
const sagaMiddleware = createSagaMiddleware();
const store = createStore(history, [sagaMiddleware, routerMiddleware(history)]);
const sagas = new RootSaga(axiosInstance, store);
@kneczaj
kneczaj / util.ts
Last active October 7, 2020 22:35
Null/Undefined checkers for typescript
// 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/"
// although these functions are deprecated in node they are much more readable for me than an ordinary `===` comparison
export function isNull(val: any): val is null {
return val === null;
}
export function isUndefined(val: any): val is undefined {
@kneczaj
kneczaj / store.ts
Created March 11, 2019 23:39
A redux store boilerplate used by me
// 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/"
// undefined when not fetched yet
export interface State {
a: A[] | undefined;
b: B | undefined;
}
export const initialState: State = {
@kneczaj
kneczaj / request.ts
Last active April 26, 2019 01:20
Redux with saga REST classes
// 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/"
// A proof of concept class to generate response and request action and handle reducing them
import { RequestSaga } from "../request-saga";
import { put } from "redux-saga/effects";
import { Map } from "immutable";
export type RequestType = 'GET' | 'PUT' | 'POST' | 'DELETE';
@kneczaj
kneczaj / simplified-reducer.ts
Created April 14, 2019 16:15
Simplified reducer which do not need to handle 'undefined' state and conversion functions
// 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 { Action, AnyAction, combineReducers, Reducer, ReducersMapObject } from "redux";
import { isUndefined } from "../util";
export type SimplifiedReducer<State, Action> = (state: State, action: Action) => State;
export type SimplifiedReducersMapObject<S> = {[key in keyof S]: SimplifiedReducer<S[key], any>};
/**
@kneczaj
kneczaj / AddValidator.tsx
Last active April 24, 2019 07:28
react validation
// 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 { composeValidators, ValidatorFn } from "./index";
import { WithNamespaces } from "react-i18next";
import React from "react";
import { Props } from "../components/Range";
/**
* Always adds a validator to input props, requires translation withNamespaces decorator
@kneczaj
kneczaj / gulpfile.js
Created April 24, 2019 07:30
Gulpfile to build a npm library
// 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/"
const gulp = require('gulp');
const del = require('del');
const ngc = require('@angular/compiler-cli/src/main').main;
const fs = require('fs');
var paths = {
dist: __dirname + '/dist',
@kneczaj
kneczaj / i18n.ts
Created April 24, 2019 07:33
i18next setup
// 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 i18n from "i18next";
import { reactI18nextModule } from "react-i18next";
export const translationResources = {
pl: {
translation: {
// translations here
// 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/"
/**
* A nice idea to separate REST ID from data content, so it is easier later to make e.g. a map out of such items
*/
export interface WithId<T, Id = number> {
id: Id;
data: T;
}
@kneczaj
kneczaj / by.ts
Last active July 29, 2020 15:00
Selenium test base
// 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/
import { By as ByBase } from 'selenium-webdriver';
export class By extends ByBase {
static content(content: string): By {
return ByBase.xpath(`//*[contains(text(), '${content}')]`);
}