Skip to content

Instantly share code, notes, and snippets.

@gund
gund / actions-helpers.md
Created January 16, 2018 11:21
Action creator factories made easy with decorators using Typescript

Actions Helpers

This is a draft idea to reduce boilerplate code when writing action creators for redux.

It simplifies the proces to bare minimum and provides shourtcuts for comminly used patterns like async action (where for one action you need to have 2 more that represent success or failure).

Draft

Interface Definitions

@gund
gund / bd3d4befe38185704bf0fc875e9deed6\configuration.json
Last active August 25, 2021 10:25
Visual Studio Code Settings Sync Gist
{"contents":{"launch":{"version":"0.2.0","configurations":[{"type":"chrome","request":"attach","name":"Attach: ng serve","port":9222,"webRoot":"${workspaceFolder}"},{"type":"chrome","request":"launch","name":"ng serve","url":"http://localhost:4200","webRoot":"${workspaceFolder}"},{"type":"node","request":"attach","name":"Attach by Process ID","processId":"${command:PickProcess}"},{"type":"node","request":"launch","name":"Launch Check Localizations","program":"${workspaceFolder}/scripts/check-localizations.js","args":["--tsconfig","src/tsconfig.app.json","--intl-files","../src/assets/locales/en-gb.json","--intl-files","../src/assets/locales/de-de.json","--check-intl"]}]}},"overrides":[],"keys":["launch.version","launch.configurations"]}
@gund
gund / readme.md
Created October 5, 2018 16:58
TS spread bug
// EXPECTED
new Set(...document.body.classList)
> {"l", "o", "g", "e", "d", …}

// ACTUAL
new (Set.bind.apply(Set, [void 0].concat(document.body.classList)))();
> {"logged-in", "env-production", "emoji-size-boost"}

// FIX
@gund
gund / setup-for-jest.ts
Last active January 7, 2019 22:29
Angular Testing Helpers for auto-generating host component
import { setOutputsMock } from './testing-helpers';
declare module './testing-helpers' {
interface MockedOutput extends jest.Mock {}
}
setOutputsMock(() => jest.fn());
@gund
gund / faker.ts
Last active November 7, 2021 00:34
Real randomly generated mocks for GraphQL (for unit tests and mocked GraphQL server)
// Module for loading Faket.js
let _faker: Faker.FakerStatic;
/**
* Load and cache Faker.js with just english locale
*/
export function faker(): Faker.FakerStatic {
if (!_faker) {
_faker = require('faker/locale/en');
@gund
gund / no-branching.ts
Created January 21, 2019 09:47
Example how you can convert data without branching
// function with branching
function giveAppleOrOrange(condition) {
if (condition) {
return 'apple';
} else {
return 'orange';
}
}
// function without branching
@gund
gund / extend-decorator.ts
Last active January 24, 2019 17:30
Easily extend decorator configuration by producing new decorators
type DecoratorFactory<T, D extends Function> = (config: T) => D;
type PropertyDecoratorFactory<T> = DecoratorFactory<T, PropertyDecorator>;
function extendDecoratorConfig<C>(decoratorFactory: PropertyDecoratorFactory<C>) {
return <C_CUSTOM = {}, C_NEW extends C = C>(
configFactory: (config: C & C_CUSTOM) => C_NEW,
) => (userConfig?: C & C_CUSTOM) => decoratorFactory(configFactory(userConfig));
}
function Property(config?: { required?: boolean }): PropertyDecorator {
@gund
gund / indexable-tracked.ts
Last active February 28, 2019 19:18
Helper to produce indexed objects from arrays by key
type GetValTypeIfNot<T, D> = T extends { [k: string]: infer V } ? (V extends D ? never : T) : never;
interface IndexableByDeletedProp {
__IndexableByDeletedProp: true;
}
type IndexedBy<TA extends Array<any>, K extends keyof T, T = TA[number]> = {
[P in T[K]]: GetValTypeIfNot<
{ [PP in keyof T]: T[K] extends P ? T[PP] : IndexableByDeletedProp },
IndexableByDeletedProp
@gund
gund / event-open.ts
Last active August 14, 2019 16:34
Example of extensible type definitions in TypeScript leveraging interface and enum merging feature. REPL: https://repl.it/@gund/Extensible-type-definitions-in-TypeScript
import { registerMeta } from './event';
enum EventKind {
Open = 'open',
}
interface EventMetaRegistry {
[EventKind.Open]: EventOpenMeta;
}
@gund
gund / what-is-two-way-binding.md
Last active September 14, 2019 03:04
Two way binding in a nutshell...

In Angular

Two-way binding is actually a one way binding with changes synchronized back to the same state

@Component({
  template: `<input [(ngModel)]="value">`
  // This is syntactic sugar of the following
 template: ``