Skip to content

Instantly share code, notes, and snippets.

View FreekMencke's full-sized avatar

Freek Mencke FreekMencke

View GitHub Profile
@FreekMencke
FreekMencke / isDefined.ts
Created August 2, 2023 06:55
isDefined type guard helper
export const isDefined = <T>(value: T): value is NonNullable<T> => value !== undefined && value !== null;
@FreekMencke
FreekMencke / cache-image.directive.ts
Created September 17, 2022 13:22
Angular directive to cache images and share
import { HttpClient } from '@angular/common/http';
import { Directive, ElementRef, Injectable, Input, OnInit } from '@angular/core';
import { map, Observable, shareReplay } from 'rxjs';
@Injectable({
providedIn: 'root',
})
class CacheImageService {
imageRequests$: Map<string, Observable<string>> = new Map();
}
@FreekMencke
FreekMencke / ArrayUtils.ts
Created May 18, 2019 12:37
Writing Readable and Maintainable Code in TypeScript - ArrayUtils
export class ArrayUtils {
static chunk<T>(array: T[], chunkSize: number): T[][] {
const chunks: T[][] = [];
let index = 0;
while (index < array.length) {
chunks.push(array.slice(index, index + chunkSize));
index += chunkSize;
}
return chunks;
@FreekMencke
FreekMencke / information-logger.ts
Created May 18, 2019 12:12
How to Write Node.js Applications in TypeScript - TypeScript application
import os from 'os';
import { name, version } from '../package.json';
export class InformationLogger {
static logApplicationInformation(): void {
console.log({
application: {
name,
version,
@FreekMencke
FreekMencke / tsconfig.json
Created May 18, 2019 12:09
How to Write Node.js Applications in TypeScript - tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["dom", "es2018"],
"allowSyntheticDefaultImports": true,
"noUnusedLocals":true,
"removeComments": true,
"resolveJsonModule": true,
@FreekMencke
FreekMencke / webpack.config.js
Last active May 18, 2019 12:07
How to Write Node.js Applications in TypeScript - webpack.config.js - step 3
'use strict';
const NodemonPlugin = require('nodemon-webpack-plugin');
module.exports = (env = {}) => {
const config = {
entry: ['./src/main.ts'], // Let's change our entry file's extension to '.ts'
mode: env.development ? 'development' : 'production',
target: 'node',
devtool: env.development ? 'cheap-eval-source-map' : false,
@FreekMencke
FreekMencke / package.json
Created May 18, 2019 11:59
How to Write Node.js Applications in TypeScript - package.json - step 3
{
"name": "node-typescript",
"version": "0.0.1",
"author": "Freek Mencke",
"homepage": "https://medium.com/@freek_mencke",
"license": "MIT",
"scripts": {
"start": "webpack --progress --env.development --env.nodemon",
"start:prod": "webpack --progress --env.nodemon",
"build": "webpack --progress --env.development",
@FreekMencke
FreekMencke / webpack.config.js
Last active May 18, 2019 12:03
How to Write Node.js Applications in TypeScript - webpack.config.js - step 2
'use strict';
const NodemonPlugin = require('nodemon-webpack-plugin');
module.exports = (env = {}) => {
const config = {
entry: ['./src/main.js'],
mode: env.development ? 'development' : 'production',
target: 'node',
devtool: env.development ? 'inline-source-map' : false,
@FreekMencke
FreekMencke / package.json
Created May 18, 2019 11:52
How to Write Node.js Applications in TypeScript - package.json - step 2
{
"name": "node-typescript",
"version": "0.0.1",
"author": "Freek Mencke",
"homepage": "https://medium.com/@freek_mencke",
"license": "MIT",
"scripts": {
"start": "webpack --progress --env.development",
"start:prod": "webpack --progress"
},
@FreekMencke
FreekMencke / webpack.config.js
Last active May 18, 2019 11:50
How to Write Node.js Applications in TypeScript - webpack.config.js - step 1
'use strict';
module.exports = (env = {}) => {
const config = {
entry: ['./src/main.js'],
mode: env.development ? 'development' : 'production',
target: 'node',
// devtool alternatives: 'cheap-module-eval-source-map' (faster, less details) or 'cheap-eval-source-map' (fastest, even less details)
devtool: env.development ? 'inline-source-map' : false,
};