Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@armanozak
armanozak / promise.js
Created August 4, 2020 15:54
[Promise.all vs Promise.any vs Promise.race] How to Combine Promises #tip #javascript
const apiUrl = 'https://jsonplaceholder.typicode.com';
const createUserEndpoint = userId => apiUrl + '/users/' + userId;
const createTodosEndpoint = userId => createUserEndpoint(userId) + '/todos';
const createAltTodosEndpoint = userId => apiUrl + '/todos?userId=' + userId;
const createTimeout = seconds => new Promise(
(_, reject) => setTimeout(() => reject('TIMEOUT'), seconds * 1000)
);
const fetchJSON = endpoint => fetch(endpoint).then(res => res.json());
@armanozak
armanozak / bigint.js
Last active August 6, 2020 05:43
[BigInt] How BigInt Works #tip #javascript
let maxSafeInteger = 2 ** 53 - 1;
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(maxSafeInteger); // 9007199254740991
console.log(maxSafeInteger + 1); // 9007199254740992
console.log(maxSafeInteger + 2); // 9007199254740992 😯
let bigInteger = 2n ** 53n - 1n;
maxSafeInteger = BigInt(Number.MAX_SAFE_INTEGER);
@armanozak
armanozak / sequence.js
Created August 8, 2020 16:17
[Creating an Array of Sequential Numbers] A Performance Comparison of Different Methods #javascript #tip
const log = console.log;
/* #1 Array.prototype.fill [0.7s/10M ops] */
log(Array(10).fill(0).map((_, i) => i));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
/* #2 Array.apply & Array.prototype.map [1.9s/10M ops] */
log(Array.apply(null, Array(10)).map((_, i) => i));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@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
@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 / 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 / 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 / .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 / 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 / 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 {}