Skip to content

Instantly share code, notes, and snippets.

View erikvullings's full-sized avatar

Erik Vullings erikvullings

View GitHub Profile
@erikvullings
erikvullings / extract_domain_name.ts
Created October 23, 2022 08:25
Extract main domain name from a URL
const url = "https://www.lancasterguardian.co.uk/read-this/womens-world-cup-2023-groups-england-to-face-denmark-and-china-see-the-full-draw-3890113";
const url1 = "https://www.tudelft.nl";
const url2 = "https://whatsnew2day.com/world-cup-2023-draw-uswnt-face-vietnam-netherlands-playoff-winner-new-zealand-htmlns_mchannelrssns_campaign1490ito1490/";
const url3 = "https://www.news4jax.com/news/politics/2022/10/22/weapons-shortages-could-mean-hard-calls-for-ukraines-allies/";
const extractAgency = (url = '') => {
if (url.startsWith('https://t.me/')) return 'TELEGRAM';
let hostname: string;
try {
const u = new URL(url);
@erikvullings
erikvullings / utils.ts
Last active September 22, 2022 08:51
Utility functions
/* A list of useful functions snippets */
/**
* Create a GUID
* @see https://stackoverflow.com/a/2117523/319711
*
* @returns RFC4122 version 4 compliant GUID
*/
export const uuid4 = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@erikvullings
erikvullings / local-ldb.ts
Created June 11, 2022 10:43
IndexedDB wrapper in TypeScript
/**
* Use IndexedDB for local storage.
* Based on https://github.com/DVLP/localStorageDB, but converted to TypeScript and using async instead of callbacks.
* @source: https://github.com/DVLP/localStorageDB/blob/master/localdata.js
*
* Usage example
*
*
```ts
const test = async () => {
@erikvullings
erikvullings / walk.ts
Last active April 14, 2022 14:23
Recursively walk a directory in TypeScript
import fs from 'fs';
import path from 'path';
/**
* Recursively walk a directory asynchronously and obtain all file names (with full path).
*
* @param dir Folder name you want to recursively process
* @param done Callback function, returns all files with full path.
* @param filter Optional filter to specify which files to include,
* e.g. for json files: (f: string) => /.json$/.test(f)
@erikvullings
erikvullings / readme.md
Created January 30, 2022 10:50
Add SSH site to Windows Terminal

Source.

You can use a commandline field in your profile configuration to initiate an SSH connection on tab creation.

Step-by-step guide:

  1. Open Settings (Ctrl+,)
  2. Find the "list" array in the "profiles" object
  3. Duplicate a Command Prompt profile ("commandline": "cmd.exe")
  4. Change the commandline value to: ssh me@my-server -p 22 -i ~/.ssh/id_rsa (use your own connection command).
@erikvullings
erikvullings / README.md
Last active December 2, 2021 10:36
Asynchronous retry pattern with delay in Typescript
@erikvullings
erikvullings / Excel_time_to_UTC_time.md
Last active November 29, 2021 21:03
Convert Excel time to UTC time

The problem

I had a CSV file that needed to be processed on a web page. So I had to convert the Excel time to UTC time.

The solution

According to this answer on StackOverflow:

The Excel number for a modern date is most easily calculated as the number of days since 12/30/1899 on the Gregorian calendar.

Excel treats the mythical date 01/00/1900 (i.e., 12/31/1899) as corresponding to 0, and incorrectly treats year 1900 as a leap year. So for dates before 03/01/1900, the Excel number is effectively the number of days after 12/31/1899.

@erikvullings
erikvullings / README.md
Created November 10, 2021 13:08
EADDRINUSE error

EADDRINUSE error (Endpoint Address In Use)

Error: listen EADDRINUSE: address already in use :::1234

When node.js complains that an address is in use, this may happen because the service did not end properly. In order to kill the hanging process, do the following in an Admin PowerShell window:

netstat -ano|findstr "PID :1234"
@erikvullings
erikvullings / capitalize.ts
Created December 13, 2020 11:33
Capitalize first names derived from an email address (Dutch)
export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
/** Capitalize first names that are derived from an email, like anne_merel or jan_willem. */
export const capitalizeName = (s: string) =>
/^ij/i.test(s) // names like IJsbrand
? s.replace(/^ij/i, 'IJ')
: /_/.test(s) // names like jan_willem
? s.split('_').map(capitalize).join('-')
: capitalize(s); // other names