Skip to content

Instantly share code, notes, and snippets.

Avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / get-local-iso-string.js
Last active Nov 19, 2020
ISO 8601 date string – like Date.prototype.toISOString(), but with local timezone offset
View get-local-iso-string.js
function getLocalISOString(date) {
const offset = date.getTimezoneOffset()
const offsetAbs = Math.abs(offset)
const isoString = new Date(date.getTime() - offset * 60 * 1000).toISOString()
return `${isoString.slice(0, -1)}${offset > 0 ? '-' : '+'}${String(Math.floor(offsetAbs / 60)).padStart(2, '0')}:${String(offsetAbs % 60).padStart(2, '0')}`
}
@loilo
loilo / fixate-date.js
Created Nov 8, 2019
Jest: Mock datetime
View fixate-date.js
/**
* Fixates JavaScript's Date
* This is useful for snapshot testing data which refers to the current time
*
* Losely based on https://github.com/facebook/jest/issues/2234#issuecomment-324868057
*/
/**
* Keep a reference to the original date
*/
@loilo
loilo / regex-tag.md
Last active Oct 23, 2019
Regular Expression Template Tag in TypeScript
View regex-tag.md

Regular Expression Template Tag in TypeScript

I liked this gist enough that I made it a full-blown npm package. Check it out here.

After having used regular expression escape functions for years, it just crossed my mind that this might be way easier to achieve with a tagged template literal.

This is how to use this gist:

import rx from 'https://unpkg.com/@loilo/rx/dist/rx.mjs'
@loilo
loilo / shortcut.md
Last active May 13, 2020
Vue Global Keyboard Shortcut Mixin
View shortcut.md

Vue Global Keyboard Shortcut Mixin

This is a Vue.js mixin (or rather, a mixin factory) for simple keyboard shortcuts.

Features:

  • Tiny (<0.5 KB minified & gzipped)
  • Super easy to use
  • Proper restrictions (does not trigger when modifier keys don't match exactly or when an element has focus)

Note that this mixin targets modern browsers. If you need legacy browser support, you need to use a transpiler like Babel with the according polyfills.

@loilo
loilo / base64.mjs
Last active Jan 21, 2020
URL-Safe LZMA Compression
View base64.mjs
/**
* Convert between Uint8Array and Base64 strings
* Allows for any encoded JS string to be converted (as opposed to atob()/btoa() which only supports latin1)
*
* Original implementation by madmurphy on MDN
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_1_–_JavaScript%27s_UTF-16_%3E_base64
*/
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91
@loilo
loilo / read-blob.md
Last active Jan 4, 2020
Promise Wrapper for FileReader
View read-blob.md

Promise Wrapper for FileReader

This is a simple wrapper for the FileReader API. It allows converting a Blob (and therefore a File as well) to either a plain text string, a data URL or an ArrayBuffer with a nice and clean Promise API.

Signature

This is the TypeScript signature of the readBlob function:

/**
 * Read a blob or file and convert it to another data type
@loilo
loilo / idb-backup-and-restore.md
Last active Nov 4, 2020
Back up and restore an IndexedDB database
View idb-backup-and-restore.md

Back up and restore an IndexedDB database

This gist is an ES module which provides functions to import and export data from an IndexedDB database as JSON. It's based on Justin Emery's indexeddb-export-import package, but applies some adjustments that reflect better on the current browser landscape (i.e. better developer ergonomics but no support for Internet Explorer).

Usage

For each of the provided functionalities, you need a connected IDBDatabase instance.

Export Data

import { idb } from 'some-database'
@loilo
loilo / typescript-assets.ts
Created Aug 22, 2019
Some types I found useful during some tricky TypeScript programming
View typescript-assets.ts
/*
* These are some types I found useful during some tricky TypeScript programming.
* Many of them are not too common or intuitive to come up with, this is why I'm writing them down here for future lookup.
*
* Note: Be careful when copy-pasting, some of these types depend on others.
*/
/**
* Various utilities for working with classes
@loilo
loilo / readme.md
Created Apr 1, 2019
Sort by Samples
View readme.md

Sort by Samples

A sorting function that takes an array of samples. All sortable items which occur in the samples will be arranged in the order they occur there, all other items will be appended to the end in their original order or sorted with an optional provided comparision algorithm.

This can be useful when the items to sort are not completely known, but there are some well-known ones that should come first:

const pages = [ 'About', 'Products', 'Home', 'Contact', 'Carreer' ]

// No idea what pages exist, but if "Home" and "About" exist, they should come first
@loilo
loilo / moxy.js
Last active Jan 21, 2020
Moxy – a function that mocks an object's properties and methods with a proxy
View moxy.js
/**
* Mock an object with a proxy, overriding methods and properties
*
* @param {object} object An object to mock
* @param {object} implementations The properties/methods to virtually "merge" into the mocked object
*/
function moxy(object, implementations) {
return new Proxy(object, {
get(target, key, receiver) {
if (key in implementations) {
You can’t perform that action at this time.