Skip to content

Instantly share code, notes, and snippets.

@merlosy
merlosy / logger.ts
Created April 4, 2017 13:50
safe logger in Typescript
import { environment } from './../../../environments/environment';
export class Logger {
public static isActive = () => !environment.production;
private static noop = function(){};
public static info = Logger.isActive()? ( console && console.info ) && console.info.bind(console) : Logger.noop;
public static log = Logger.isActive()? ( console && console.log ) && console.log.bind(console) : Logger.noop;
@merlosy
merlosy / README.md
Last active November 11, 2022 15:41
Paginated list in Typescript (from Content-Range header)

Pagination abstraction layer

  • Define Pagination class
  • Define PaginatedList<T> generic class
  • Utility functions for parsing http response
@merlosy
merlosy / README.md
Last active May 22, 2021 22:13
Angular 4+ Material input field

Angular Material - File Upload field

  • Compatible with model driven validation (Reactive forms)
  • tested on Angular Material Beta 12
  • Implemented with Angular 4.4.x

For easier maintaining, I moved all sources on Plunker: SEE LIVE DEMO ON PLUNKER

Note: Getting the model value, returns an FileInput object (type FileInput)

@merlosy
merlosy / common-error.model.ts
Last active July 15, 2019 22:52
Blob to Json
blobToJson(blob: Blob): Observable<any> {
let loadend: Observable<any>;
if (blob.type === 'application/json') {
const reader = new FileReader();
loadend = fromEvent(reader, 'loadend').pipe(
map((read: any) => {
this.logger.debug('blobToJson:loadend *', JSON.parse(read.target.result));
return JSON.parse(read.target.result);
})
);
@merlosy
merlosy / nullable-radio-button.directive.ts
Last active January 23, 2018 09:13
Angular Material NullabelRadioButton
import { Directive, HostListener, OnInit, OnDestroy } from '@angular/core';
import { NgControl } from '@angular/forms';
import { combineLatest, tap, filter, take, startWith, debounceTime } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Directive({
selector: '[appNullableRadioButton]'
})
export class NullableRadioButtonDirective implements OnInit, OnDestroy {
@merlosy
merlosy / google-maps.spec.ts
Created February 19, 2018 10:24
Google Maps *Stubs* for Javascript API
/**
* This module aims to provides stubs Google Maps (Javascript API).
* It is not comprehensive, but a working solution that allows unit testing, without the async pain.
* @example
* import * as google from './google-maps.spec';
* (<any>window).google = google;
*/
export const google = {
maps: {
ControlPosition: {
@merlosy
merlosy / merge-cobertura.js
Last active February 21, 2023 12:22
Merge multiple cobertura report in XML (node script)
/**
* This is a node script to merge cobertura reports in XML format.
* It requires `xml2js` : `npm i -D xml2js`
*
* Execute with:
* ```
* node merge-cobertura.js coverage/file1.xml coverage/file2.xml to=coverage-final.xml
* ```
* @see https://github.com/Leonidas-from-XIV/node-xml2js
* @author Jérémy Legros
@merlosy
merlosy / lcov-model.js
Last active July 13, 2018 15:40
Merge multiple lcov.info report (node script)
/**
* @see http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php
*/
class Test {
constructor() {
this.FN = [];
this.FNDA = [];
this.DA = [];
this.BRDA = [];
}
@merlosy
merlosy / location.ts
Created March 30, 2021 14:20
Jest testing utils
/** Mocked location object. Includes the original to rollback to the native one */
export interface MockedLocation {
/** The original location definition */
original: Location;
/** Restore the original location object */
clear(): void;
}
@merlosy
merlosy / action.ts
Created March 23, 2022 21:25
Vuex class types
/**
Usage:
@Action(nameOf<ActionFn>('myAction'), { namespace: 'my-module'})
loadGraph!: ActionType<'myAction'>;
*/
function _createActions() {
return {