Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
Pondering

Jamie Perkins inorganik

🤔
Pondering
View GitHub Profile
View angular-validators.ts
import {
AbstractControl,
AsyncValidatorFn,
ValidationErrors,
ValidatorFn,
Validators,
} from '@angular/forms';
export class CustomValidators {
@inorganik
inorganik / calculateObjectSize.js
Created Feb 26, 2020
Calculate the size of a js object in bytes
View calculateObjectSize.js
export function roughSizeOfObject(object) {
const objectList = [];
const stack = [object];
let bytes = 0;
while (stack.length) {
const value = stack.pop();
if (typeof value === 'boolean') {
bytes += 4;
} else if (typeof value === 'string') {
bytes += value.length * 2;
@inorganik
inorganik / auth-interceptor.ts
Created Dec 13, 2019
Example of an angular auth interceptor
View auth-interceptor.ts
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AuthContextManagerService } from './some/where/authContextService.ts';
@Injectable()
export class AuthorizationInterceptor implements HttpInterceptor {
constructor(private authContextService: AuthContextManagerService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
@inorganik
inorganik / scroll-spy.directive.ts
Created Nov 26, 2019
Angular scroll spy directive
View scroll-spy.directive.ts
import { Directive, Inject, Input, Output, EventEmitter, OnInit, ElementRef } from '@angular/core';
import { WINDOW } from '../../services/window.service';
import { Router, NavigationEnd } from '@angular/router';
import { fromEvent, Subject, Observable } from 'rxjs';
import { map, distinctUntilChanged, filter, takeUntil, debounceTime } from 'rxjs/operators';
/*
* Emits boolean event when element passes
* threshold of either fromBottom or fromTop,
* or emits when element is in view
@inorganik
inorganik / getUsersLocale.ts
Created Aug 26, 2019
Angular: get user's language and locale
View getUsersLocale.ts
getUsersLocale(defaultValue: string): string {
if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
return defaultValue;
}
const wn = window.navigator as any;
let lang = wn.languages ? wn.languages[0] : defaultValue;
lang = lang || wn.language || wn.browserLanguage || wn.userLanguage;
return lang;
}
@inorganik
inorganik / WebSocketMock.ts
Created Jul 12, 2019
An abstract websocket endpoint class for node express
View WebSocketMock.ts
import * as expressWs from 'express-ws';
import * as WebSocket from 'ws';
/**
* Extend this class to make a new WebSocket endpoint.
*
* Derived classes should pass an Express app object and the
* string endpoint, and must implement handleMessage()
*/
export abstract class WebSocketMock {
@inorganik
inorganik / cloudSettings
Last active Jul 17, 2020
Visual Studio Code Settings Sync Gist
View cloudSettings
{"lastUpload":"2020-07-17T20:23:56.317Z","extensionVersion":"v3.4.3"}
@inorganik
inorganik / euroCountries.js
Created Mar 5, 2019
A javascript array of country codes using the Euro €
View euroCountries.js
const euroCountries = [
'BE', // Belgique/België Belgium Belgique Belgien
'BG', // Bulgarija Bulgaria Bulgarie Bulgarien
'CZ', // Česko Czechia Tchéquie Tschechien
'DK', // Danmark Denmark Danemark Dänemark
'DE', // Deutschland Germany Allemagne Deutschland
'EE', // Eesti Estonia Estonie Estland
'IE', // Éire/Ireland Ireland Irlande Irland
'EL', // Elláda Greece Grèce Griechenland
'ES', // España Spain Espagne Spanien
@inorganik
inorganik / countUp-jquery.js
Last active Aug 27, 2021
A CountUp extension for jQuery
View countUp-jquery.js
// Dependency: CountUp.js: https://github.com/inorganik/CountUp.js
(function ($) {
$.fn.countup = function (params) {
// make sure dependency is present
if (typeof CountUp !== 'function') {
console.error('countUp.js is a required dependency of countUp-jquery.js.');
return;
}
@inorganik
inorganik / confetti.directive.ts
Created Jan 29, 2019
An Angular directive around dom-confetti
View confetti.directive.ts
import { Directive, ElementRef, OnInit, Input, HostListener } from '@angular/core';
import { confetti } from 'dom-confetti';
interface ConfettiConfig {
angle: number; // - direction of the explosion in degrees, defaults to 90.
spread: number; // - spread of the explosion in degrees, deafults to 45.
startVelocity: number; // - Initial velocity of the particles, defaults to 45.
width: number; // - width of the confetti elements
height: number; // - height of the confetti elements
elementCount: number; // - Number of particle elements, defaults to 50.