Skip to content

Instantly share code, notes, and snippets.

@nilsmehlhorn
nilsmehlhorn / variables.scss
Created February 20, 2020 15:00
Variables for (S)CSS MediaQueries on Material breakpoints
$mat-lt-sm: "(max-width: 599px)";
$mat-lt-md: "(max-width: 959px)";
$mat-lt-lg: "(max-width: 1279px)";
$mat-lt-xl: "(max-width: 1919px)";
$mat-gt-xs: "(min-width: 600px)";
$mat-gt-sm: "(min-width: 960px)";
$mat-gt-md: "(min-width: 1280px)";
$mat-gt-xl: "(min-width: 1920px)";
$mat-xs: $mat-lt-sm;
$mat-sm: $mat-gt-xs and $mat-lt-md;
@nilsmehlhorn
nilsmehlhorn / observe.directive.spec.ts
Last active October 11, 2022 19:41
Angular Structural Directive for Handling Observables Better than NgIf & AsyncPipe
import {Component, DebugElement} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {ObserveDirective} from './observe.directive';
import {BehaviorSubject, Observable, of, throwError} from 'rxjs';
@Component({
selector: 'test-component',
template: `
<span id="next-value" *observe="value$ as value; before loading; error error;">
@nilsmehlhorn
nilsmehlhorn / matcher.ts
Last active January 31, 2020 17:31
Small and configurable full-text search on objects in TypeScript
export type Matcher<T> = (query: string) => T[];
export type ValueProject<T> = (target: T) => Array<string | number>;
export type MatcherFactory = <T>(targets: T[], project: ValueProject<T>) => Matcher<T>;
export const defaultProject = <T>(target: T) => Object.values(target);
const isMatchable = prop => typeof prop == 'number' || typeof prop == 'string'
@nilsmehlhorn
nilsmehlhorn / index.js
Last active March 20, 2022 00:31
Firebase Cloud Function: Thumbnail Generator (Serverless)
const { join, dirname } = require('path')
const sharp = require('sharp')
const { Storage } = require('@google-cloud/storage')
const { promisify } = require('util')
const pump = promisify(require('pump'))
const gcs = new Storage()
const SIZE = 128
const PREFIX = 'thumb@'
@nilsmehlhorn
nilsmehlhorn / operators.ts
Last active February 17, 2020 02:53
Miscellaneous RxJs Operators
import {filter, map, shareReplay, catchError} from 'rxjs/operators'
import {Observable, Subject, throwError, defer} from 'rxjs'
import {Option} from 'fp-ts/lib/Option'
import {HttpErrorResponse} from '@angular/common/http'
import {HttpStatus} from 'http-status-codes'
/**
* Deserializes plain object to class instance.
*
* @param ClassType class to instantiate
@nilsmehlhorn
nilsmehlhorn / 0-throw-for-codes.ts
Last active March 17, 2020 13:51
RxJS operator for throwing semantic errors for certain HTTP status codes (now part of ngx-operators)
import { Observable, throwError } from 'rxjs'
import { catchError } from 'rxjs/operators'
import { HttpErrorResponse } from '@angular/common/http'
export const throwForCodes = (codeErrors: Array<[number, () => Error]>) => {
const mappedCodeErrors = new Map(codeErrors)
return <T>(source: Observable<T>) =>
source.pipe(catchError(error => {
if (error instanceof HttpErrorResponse) {
const mappedErrorFn = mappedCodeErrors.get(error.status)
@nilsmehlhorn
nilsmehlhorn / AndroidManifest.xml
Last active November 16, 2019 13:46
Android push notifications with EddyVerbruggen/nativescript-plugin-firebase
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"