Skip to content

Instantly share code, notes, and snippets.

@gleuch
gleuch / gist:2475825
Created April 24, 2012 02:58
Javascript documentfragment to string (w/ text selection)
// selection range
var range = window.getSelection().getRangeAt(0);
// plain text of selected range (if you want it w/o html)
var text = window.getSelection();
// document fragment with html for selection
var fragment = range.cloneContents();
// make new element, insert document fragment, then get innerHTML!
@ibrechin
ibrechin / jsspectrum.js
Created April 25, 2012 11:13
Generate evenly distributed spectrum in JavaScript
hslToRgb = function(_h, s, l) {
var h = Math.min(_h, 359)/60;
var c = (1-Math.abs((2*l)-1))*s;
var x = c*(1-Math.abs((h % 2)-1));
var m = l - (0.5*c);
var r = m, g = m, b = m;
if (h < 1) {
@rmehner
rmehner / delete-databases.js
Last active July 9, 2024 10:35
Delete all indexedDB databases
// Credit to @steobrien from https://gist.github.com/rmehner/b9a41d9f659c9b1c3340#gistcomment-2940034
// for modern browsers, this works:
const dbs = await window.indexedDB.databases()
dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) })
// for older browsers, have a look at previous revisions of this gist.
@malthe
malthe / dedent.js
Created December 16, 2014 11:25
This is similar to Python's "textwrap.dedent" function
function dedent(text) {
var re_whitespace = /^([ \t]*)(.*)\n/gm;
var l, m, i;
while ((m = re_whitespace.exec(text)) !== null) {
if (!m[2]) continue;
if (l = m[1].length) {
i = (i !== undefined) ? Math.min(i, l) : l;
} else break;
@ijprest
ijprest / convert-to-amd.js
Created July 20, 2015 23:49
Very simple script to convert a normal JS module to a RequireJS-compatible AMD module; intended for command-line usage, and integration into build systems.
#!/usr/bin/env node
///
/// Very simple script to convert a normal JS module to a RequireJS-compatible
/// AMD module; intended for command-line usage, and integration into build
/// systems.
///
/// Reads from stdin & writes to stdout; output can be piped to uglifyjs to
/// minify/compress the code.
///
/// Examples:
@cauerego
cauerego / IndexedDB101.js
Last active January 13, 2023 22:00 — forked from JamesMessinger/IndexedDB101.js
Very Simple IndexedDB Example
// quite untested, adapted from BigstickCarpet's gist, attempt to make it simpler to use
function openIndexedDB (fileindex) {
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
var openDB = indexedDB.open("MyDatabase", 1);
openDB.onupgradeneeded = function() {
var db = {}
@jamesthomson
jamesthomson / word2vec example.py
Created July 12, 2016 09:44
word2vec model example using simple text sample
import nltk
import gensim
sample="""Renewed fighting has broken out in South Sudan between forces loyal to the president and vice-president. A reporter in the capital, Juba, told the BBC gunfire and large explosions could be heard all over the city; he said heavy artillery was being used. More than 200 people are reported to have died in clashes since Friday. The latest violence came hours after the UN Security Council called on the warring factions to immediately stop the fighting. In a unanimous statement, the council condemned the violence "in the strongest terms" and expressed "particular shock and outrage" at attacks on UN sites. It also called for additional peacekeepers to be sent to South Sudan.
Chinese media say two Chinese UN peacekeepers have now died in Juba. Several other peacekeepers have been injured, as well as a number of civilians who have been caught in crossfire. The latest round of violence erupted when troops loyal to President Salva Kiir and first Vice-President Riek Machar began sho
@awjuliani
awjuliani / Q-Table Learning-Clean.ipynb
Last active October 25, 2022 07:57
Q-Table learning in OpenAI grid world.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@potpath
potpath / dijkstra.py
Last active March 1, 2023 23:40 — forked from econchick/gist:4666413
Python implementation of Dijkstra's Algorithm using heapq
import heapq
from collections import defaultdict
class Graph:
def __init__(self, n):
self.nodes = set(range(n))
self.edges = defaultdict(list)
self.distances = {}
@jeffhandley
jeffhandley / PropDefinitions.jsx
Created November 6, 2017 17:14
Defining PropTypes for nested components
/* NameDisplay.js */
import ReactPropTypes from 'prop-types';
const NameDisplay = ({name}) => (
<div>
<div>
First Name: {name.first}
</div>
<div>
Last Name: {name.last}