Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / mask.md
Last active March 20, 2024 09:21
Simple Angular mask directive

Simple Angular mask directive

This directive does not create it's own value accessor - it simply reuses whatever element is using already and just hooks in.

Also it is fully abstracted off of the HTML implementation and so can be safely used in WebWorker and server side environment.

Usage

@gund
gund / advanced-renderer.md
Last active August 12, 2017 19:24
Example of abstract advanced renderer for Angular to execute arbitrary function on different safe thread (ex. webworker)

Advanced Renderer

The idea here is to have an abstraction to safely run browser dependent code in non browser-native environment (like webworker or server-side) without breaking the app and not changing any application logic code.

execute(expression: string, args?: ExpressionArguments): Promise

import { Injectable, NgModule, PLATFORM_INITIALIZER, Provider } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {
@gund
gund / readme.md
Last active July 28, 2017 12:14
Run angular application in custom zone

This shows how you can customize zone behavior in which Angular App will run.

Can be useful for example if you want to intercept all events that angular dispatches to your handlers.

First, create a custom-zone.ts file:

import 'zone.js';

interface TaskInfo {
  delegate: ZoneDelegate;