Skip to content

Instantly share code, notes, and snippets.

@dmorosinotto
dmorosinotto / touchp.sh
Created September 1, 2017 07:04
Add it to .bash_profile - Command to create file and all intermediate directory
function touchp() {
mkdir -p "$(dirname "$1")/" && touch "$1"
}
@dmorosinotto
dmorosinotto / base64UrlDecode.ts
Last active April 6, 2018 15:44
base64UrlDecode used to decode part of a JWT
function base64UrlDecode(s: string) {
//BASED ON https://github.com/client9/stringencoders/blob/master/javascript/base64.js BUT HANDLE SPECIAL CASE: REMOVE '=' PADDING AND CHARSET REPLACE '+' -> '-' and '/' -> '_'
function getbyte64(s: string, i: number) {
// NOTE charset differ from standard Base64: '+' -> '-' and '/' -> '_'
var idx = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'.indexOf(s.charAt(i));
if (idx === -1) {
throw "INVALID CHARACTER";
}
return idx;
}
@dmorosinotto
dmorosinotto / ISOFormatExtensions.cs
Last active March 7, 2024 09:42
C# Extension Methods to handle ISO-8601 with the same exact ISOFormat used in Javascript
public static class ISOFormatExtensions
{
const string ISOFORMAT = "yyyy-MM-dd\\THH:mm:ss.fffK"; //ISO-8601 used by Javascript (ALWAYS UTC)
public static string toISOString(this DateTime d, bool useLocal = false) {
if (!useLocal && d.Kind == DateTimeKind.Local) {
//If d is LT or you don't want LocalTime -> convert to UTC and always add K format always add 'Z' postfix
return d.ToUniversalTime().ToString(ISOFORMAT);
} else { //If d is already UTC K format add 'Z' postfix, if d is LT K format add +/-TIMEOFFSET
return d.ToString(ISOFORMAT);
}
@dmorosinotto
dmorosinotto / angular-mobx-helpers.ts
Created May 2, 2018 21:49
Some helper functions to use mobx in Angular
//ALL CREDITS TO @vivainio SEE REPO: https://github.com/vivainio/angularpack/blob/master/src/mobtool.ts
//READ MORE ON HOWTO USE IT HERE: https://medium.com/@vivainio/mobx-with-angular-part-2-patterns-perks-and-gotchas-37e2a393e0eb
//+INTRO IDEA ABOUT MOBX+ANGULAR: https://medium.com/@vivainio/mobx-with-angular-the-prelude-1c0dcfb43fe6
import { Observable } from "rxjs/Observable";
import {
reaction,
computed,
autorun,
IReactionPublic,
IReactionOptions,
@dmorosinotto
dmorosinotto / retryBackoff.ts
Created June 5, 2018 13:07
Custom RxJS operator (v6+) to handle Backoff strategy (=wait exponential increasing time before retry when errors occurs)
//ORIGINAL CODE BY https://github.com/alex-okrushko/backoff-rxjs - MORE INFO: https://blog.angularindepth.com/power-of-rxjs-when-using-exponential-backoff-a4b8bde276b0
import {iif, Observable, throwError, timer} from 'rxjs';
import {concatMap, retryWhen} from 'rxjs/operators';
export interface RetryBackoffConfig {
initialInterval: number;
maxAttempts?: number;
maxInterval?: number;
shouldRetry?: (error: any) => boolean;
@dmorosinotto
dmorosinotto / Reflect.ts
Created July 24, 2018 13:52
TS minimal reflect-metadata implementation
//Minimal (3Kb) implmentation of reflect-metadata, to make TS @decorators works with ES Reflect specification
//CODE CREDITS to https://github.com/abraham/reflection
//COMPILE FOR ES6 WITH tsconfig.json { "target": "esnext", "module": "esnext" }
export const Reflection = Object.assign({}, Reflect, {
decorate,
defineMetadata,
getMetadata,
hasOwnMetadata,
metadata,
@dmorosinotto
dmorosinotto / PushPipe.ts
Created August 8, 2018 13:49
obs$ | push as val <-- Custom Pipe to extract value from Observable handling OnPush
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, WrappedValue, ɵisObservable, ɵisPromise} from '@angular/core';
import {Observable, SubscriptionLike} from 'rxjs';
@dmorosinotto
dmorosinotto / TestTypedForms.ts
Last active September 24, 2025 20:35
Typed @angular/forms FIXED set/patchValue - strict all the way down ^_^
import { FormGroup, FormControl, FormArray, Validators } from "@angular/forms";
function testFormGroupTyped() {
var frm = new FormGroup({
a: new FormArray([new FormControl(0)]),
b: new FormControl(true),
c: new FormGroup({
s: new FormControl("abc"),
n: new FormControl(123)
})
const log = (tag: string) => tap(
next => console.log(`%c[${tag}: Next]`, 'color: #4CAF50;', next),
error => console.log(`%c${tag}: Error]`, 'color: #F44336;', error),
() => console.log(`%c[${tag}: Complete]`, 'color: #2196F3;')
);