Skip to content

Instantly share code, notes, and snippets.

@tomstuart
tomstuart / levenshtein.rb
Last active May 26, 2023 23:12
Approximate substring matching with Levenshtein distance
module Levenshtein
def self.substring_distance(needle, haystack)
distances = Array.new(haystack.length.succ, 0)
needle.each_char.with_index do |needle_char, needle_index|
next_distances = [needle_index.succ]
haystack.each_char.with_index do |haystack_char, haystack_index|
deletion, insertion, substitution =
distances[haystack_index.succ].succ,
@rvighne
rvighne / multiKey.js
Last active July 6, 2021 21:53
Create an object that has multiple keys pointing to the same value. It uses getters and setters to work its magic, but acts perfectly like a regular object, including enumeration, membership testing, and deleting properties.
function multiKey(keyGroups) {
let obj = {};
let props = {};
for (let keyGroup of keyGroups) {
let masterKey = keyGroup[0];
let prop = {
configurable: true,
enumerable: false,
@kosamari
kosamari / _ServiceWorker_for_github_pages.md
Last active April 1, 2024 05:44
ServiceWorker for github pages.

ServiceWorker for github pages

This is a ServiceWorker template to turn small github pages into offline ready app.

Why ?

Whenever I make small tools & toys, I create github repo and make a demo page using github pages (like this one).
Often these "apps" are just an index.html file with all the nessesary CSS and JavaScript in it (or maybe 2-3 html/css/js files). I wanted to cache these files so that I can access my tools offline as well.

Notes

Make sure your github pages have HTTPS enforced, you can check Settings > GitHub Pages > Enforce HTTPS of your repository.