Skip to content

Instantly share code, notes, and snippets.

@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 / index.html
Last active February 12, 2024 00:06
ArcGIS JSAPI setup build with Webpack for production (start reading from webpack.config.js)
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<!-- Load Esri lib -->
<link rel="stylesheet" href="//js.arcgis.com/4.0/esri/css/main.css">
<script src="//js.arcgis.com/4.0/"></script>
<!-- Load our AMD app -->
<script type="text/javascript">
// Wrap our app startup in AMD module
@gund
gund / http-interceptor-concept.md
Last active September 19, 2022 01:08
Concept of Http Interceptor for Angular 2

First, we need to define interfaces with which we will work:

export interface Interceptable<T extends Interceptor<any, any>> {
  addInterceptor(interceptor: T): Interceptable<T>;
  removeInterceptor(interceptor: T): Interceptable<T>;
  clearInterceptors(interceptors?: T[]): Interceptable<T>;
}

export interface Interceptor<T, D> {
@gund
gund / iterate-and-drain.ts
Created November 23, 2021 23:38
Iterate and drain arrays in a nice and concise way using generators
function* drain<T>(arr: T[]): Generator<T> {
let item: T | undefined;
while ((item = arr.shift())) {
yield item;
}
}
const myArray = [1, 2, 3, 4, 5];
@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 / 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 / treeshake-class-methods.ts
Last active May 4, 2021 10:20
Tree shakable class methods implemented via visitor pattern with native Typescript support
// First define standard JS constructor container
type Constructable<T> = new (...args: any[]) => T;
type ConstructableAbstract<T> = abstract new (...args: any[]) => T;
type InferConstructable<T> =
T extends Constructable<infer C> ? C :
T extends ConstructableAbstract<infer C> ? C : never;
// Now let's define visitor interfaces
@gund
gund / example.ts
Last active March 10, 2021 00:13
New abstract constructor type in Typescript 4.2 with support of the constructor arguments
type AsFunction<T> = T extends (...args: any) => any ? T : never;
type InferConstructor<T> = T extends { constructor: infer C }
? AsFunction<C>
: () => any;
type Constructor<T> = new (...args: Parameters<InferConstructor<T>>) => T;
type AbstractConstructor<T> = abstract new (
...args: Parameters<InferConstructor<T>>
) => T;
@gund
gund / index.ts
Last active January 26, 2021 23:15
Extendable actions service library
// Type Utils
interface DefaultChecker {
__defaultChecker: true;
}
type Default<T, D> = T | DefaultChecker extends DefaultChecker ? D : T;
type MapTo<T, E, M> = T extends E ? M : T;
type AsKeyOf<K, T> = K extends keyof T ? K : never;
type Observable<T> = {};
@gund
gund / 01-merge-types.ts
Last active November 25, 2019 16:51
Helper type to merge conflicting interfaces
/**
* Simple merge of two types where first `T1` will override second `T2`
*/
export type MergeSimple<T1, T2> = T1 & Omit<T2, keyof T1>;
/**
* Recursive merge of array of types where first types take over the last ones
*/
export type Merge<T extends any[]> = MergeRecursive<Head<T>, Tail<T>>;