Skip to content

Instantly share code, notes, and snippets.

View erikvullings's full-sized avatar

Erik Vullings erikvullings

View GitHub Profile
@erikvullings
erikvullings / cachify.ts
Created May 31, 2018 08:00
Created a cached version of any function with a single argument.
/**
* Create a cached version of a function.
*
* @example cachedCalcArea = cachify(calcArea);
* @param fn Function whose results you want to cache
* @see TypeScript version of https://dev.to/kepta/javascript-underdogs-part-1---the-weakmap-4jih
*/
export const cachify = <T extends object, R>(fn: (arg: T) => R) => {
const cache = new WeakMap(); // Use Map() when dealing with non-object arguments
return (arg: T) => {
@erikvullings
erikvullings / utils.ts
Last active January 19, 2019 11:46
TypeScript utilities: GUID and deepCopy (clone)
/**
* 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) => {
// tslint:disable-next-line:no-bitwise
const r = (Math.random() * 16) | 0;
@erikvullings
erikvullings / deep-copy.ts
Created July 30, 2018 10:40
Deep copy or clone in TypeScript
/**
* Deep copy function for TypeScript.
* @param T Generic type of target/copied value.
* @param target Target value to be copied.
* @see Source project, ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
*/
export const deepCopy = <T>(target: T): T => {
if (target === null) {
return target;
}
@erikvullings
erikvullings / deep-copy.ts
Last active October 5, 2023 13:27
Deep copy or clone in TypeScript
/**
* Deep copy function for TypeScript.
* @param T Generic type of target/copied value.
* @param target Target value to be copied.
* @see Source project, ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
* @see Code pen https://codepen.io/erikvullings/pen/ejyBYg
*/
export const deepCopy = <T>(target: T): T => {
if (target === null) {
return target;
@erikvullings
erikvullings / guid.ts
Created July 30, 2018 10:45
Create a new GUID or UUID in TypeScript
/**
* 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) => {
// tslint:disable-next-line:no-bitwise
const r = (Math.random() * 16) | 0;
@erikvullings
erikvullings / message-bus-service.ts
Created August 15, 2018 07:31
Message bus service, or pub/sub service, loosely based on postal.js, but created in TypeScript.
export type ICallback<T> = (data: T, envelope: IEnvelope<T>) => void;
/** Message enveloppe */
export interface IEnvelope<T> {
/* Uses DEFAULT_CHANNEL if no channel is provided */
channel?: string;
/** Topic name */
topic: string;
/** Payload */
data?: T;
@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 / toEnglish.vba
Last active February 15, 2024 10:17
Change the language of your PowerPoint presentation
'Change the language of your PowerPoint
'In newer PowerPoint version, you need to first save your PowerPoint with macros enabled.
'Go to the VIEW tab, select MACROS (at the right), enter a name, e.g. toEnglish, and press create to enter below text.
'Now you can run the macro from the same menu.
'Alternatively, but less complete, go to the VIEW tab, select OUTLINE, select all slides (using CTRL-A).
'Now go to the REVIEW tab, select Language, and change the proofing language.
'Also make sure that you set the WINDOWS Language (taskbar, bottom right) to the preferred language, otherwise all new text
'will have the same problem (Press CTRL + WINDOWS + SPACE to switch the Keyboard input language).
Option Explicit
Sub toEnglish()
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
@erikvullings
erikvullings / pdok.html
Created July 30, 2020 10:50
Add a Dutch background map to your leaflet application.
<!DOCTYPE html>
<html>
<head>
<title>PDOK achtergrondkaarten</title>
<!-- https://www.pdok.nl/introductie/-/article/basisregistratie-topografie-achtergrondkaarten-brt-a-
Zie ook de wizard hier:
https://nlmaps.nl/
PDOK biedt 4 base maps aan van NL: standaard, grijs, water, en pastel. Deze kun je gratis gebruiken