Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View OysteinAmundsen's full-sized avatar

Øystein Amundsen OysteinAmundsen

  • Bouvet ASA
  • Haugesund
View GitHub Profile
@OysteinAmundsen
OysteinAmundsen / dateUtil.ts
Created March 11, 2024 11:07
Find date format from date string
import { addDays, getHours, isAfter, isBefore, parse, setHours, startOfDay, subDays } from 'date-fns';
export class dateUtil {
/**
* Find the correct date format, by comparing two consecutive days.
*
* @param date The date string to find the format for
* @param refDate The date string to compare. This should be either the next or previous day.
* This is only needed to determine if the day is before or after the month in the format.
*/
@OysteinAmundsen
OysteinAmundsen / prevent-zoom.directive.ts
Last active October 16, 2023 12:30
Prevent zoom on iOS when input fields are focused
import { Directive, HostListener, signal } from '@angular/core';
@Directive({ selector: '[flxInput]', standalone: true })
export class PreventZoomDirective {
zoomDisabled = signal(false);
hasFocus = signal(false);
get meta() {
return Array.from(document.getElementsByTagName('meta') || []).find(m => m?.getAttribute('name') === 'viewport');
}
@OysteinAmundsen
OysteinAmundsen / network-queue.interceptor.ts
Last active March 3, 2023 08:59
This will pick up all client requests, and make them poor-network-connection safe.
import { Injectable, EventEmitter } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpSentEvent, HttpHeaderResponse, HttpProgressEvent, HttpResponse, HttpUserEvent, HttpClient } from '@angular/common/http';
import { Observable, Observer, Subscription, throwError, timer } from 'rxjs';
import { retryWhen, mergeMap, finalize } from 'rxjs/operators';
@Injectable()
export class NetworkQueueInterceptor implements HttpInterceptor {
onChanges = new EventEmitter<boolean>();
private lastValue = this.isOnline();
/**
* Debounce function decorator
*
* @param delay
* @param immediate
* @returns the function debounced
*/
export function Debouncer(delay = 200, immediate = false): MethodDecorator {
return function (target: Record<string, unknown>, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const map = new WeakMap();
@OysteinAmundsen
OysteinAmundsen / shared-observable.ts
Last active April 7, 2022 09:52
Decorator for sharing the result given from function that returns an observable
import { finalize, Observable, share } from 'rxjs';
/**
* If you have more than one component calling a function which returns an observable,
* this will create the observable many times. For http requests, this will result in
* many requests being made and thus redundant http calls.
*
* The SharedObservable will make sure that all calls made simultaneously will only
* perform 1 http call and all subscribers share the same response.
* This will also take into account different input parameters and create a new