Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@armanozak
armanozak / child-module-factory.ts
Created January 28, 2021 10:53
[How to Configure Angular Modules Loaded by the Router] A custom child module factory #blog #angular
import {
Compiler,
Injector,
ModuleWithProviders,
NgModuleFactory,
NgModuleRef,
StaticProvider,
Type
} from "@angular/core";
@armanozak
armanozak / foo.module.ts
Created January 28, 2021 10:48
[How to Configure Angular Modules Loaded by the Router] Making a module configurable #blog #angular
// other imports are removed for brevity
import { ModuleWithProviders } from "@angular/core";
@NgModule({
imports: [
RouterModule.forChild([
{
path: "",
component: FooComponent
}
@armanozak
armanozak / app.module.ts
Created January 28, 2021 10:45
[How to Configure Angular Modules Loaded by the Router] Lazy-loading a module via router #blog #angular
// remove FooModule from imports
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([
{
path: "",
loadChildren: () => import('./foo.module').then(m => m.FooModule),
}
@armanozak
armanozak / app.module.ts
Created January 28, 2021 10:43
[How to Configure Angular Modules Loaded by the Router] Loading a module via router #blog #angular
import { Component, NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { RouterModule } from "@angular/router";
import { FooModule } from "./foo.module";
@Component({
selector: "app-root",
template: "<router-outlet></router-outlet>"
})
export class AppComponent {}
@armanozak
armanozak / foo.module.ts
Created January 28, 2021 10:40
[How to Configure Angular Modules Loaded by the Router] A simple module with routing #blog #angular
import { Component, Inject, InjectionToken, NgModule } from "@angular/core";
import { RouterModule } from "@angular/router";
export const FOO = new InjectionToken<string>("FOO");
@Component({
selector: "app-foo",
template: "{{ foo }} works!"
})
export class FooComponent {
@armanozak
armanozak / .zshrc
Created December 20, 2020 11:23
[How to Initiate a Demo App Quickly in Your Terminal] Add a Quick Demo Function to Zsh #zsh #tips
# rest of your Zsh configuration here
demo () {
if [ ! "$#" -gt 0 ]; then echo "Missing application name!"; return 1; fi
case "$2" in
"")
mkdir $1;
cd $1;
npm init -y;
;;
@armanozak
armanozak / http-errors.js
Created December 16, 2020 11:17
[Custom Error Constructors] How to use inheritance for better error handling #javascript #tip
// http-errors.js
export class HttpError extends Error {
code;
url;
constructor(code, url, message) {
super(message);
this.code = code;
this.url = url;
}
@armanozak
armanozak / diacritics.js
Created October 7, 2020 08:50
[Normalize Diacritics] Convert diacritics to Latin-1 (ISO/IEC 8859-1) #tip #javascript
const diacriticsMap = {
a: 'a',
A: 'A',
ₐ: 'a',
á: 'a',
Á: 'A',
à: 'a',
À: 'A',
ă: 'a',
Ă: 'A',
@armanozak
armanozak / index.ts
Created October 1, 2020 16:21
[Strict Contract] How to create a binding interface #typescript #tip
type Strict<Contract, Class> = Class extends Contract
? { [K in keyof Class]: K extends keyof Contract ? Contract[K] : never }
: Contract;
interface MyContract {
foo: number;
bar: boolean;
}
type MyStrictContract = Strict<MyContract, MyClass>;
@armanozak
armanozak / index.ts
Last active October 1, 2020 10:22
[Variadic Tuples & Recursive Conditional Types] How tagged template literals can return type-safe functions #typescript #tip
type Repeat<T, Count, Acc extends any[] = []> = Acc['length'] extends Count ? Acc : Repeat<T, Count, [...Acc, T]>;
function i18n<Keys extends number[]>([result, ...parts]: TemplateStringsArray, ...keys: Keys) {
return (...param: Repeat<string, Keys['length']>) =>
keys.reduce((acc, key, i) => acc + (param as number[])[key] + parts[i], result);
}
const introduceEn = i18n`Hi. My name is ${0}. I work as a ${1} at ${2}.`;
const introduceTr = i18n`Merhaba. Benim adım ${0}. ${2} şirketinde ${1} olarak çalışıyorum.`;
// (param_0: string, param_1: string, param_2: string) => string