Skip to content

Instantly share code, notes, and snippets.

@john-doherty
john-doherty / javascript-promise-timeout.js
Last active December 17, 2021 02:06
Adds a timeout to a JavaScript promise, rejects if not resolved within timeout period (uses requestAnimationFrame for better accuracy)
(function () {
'use strict';
/**
* wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time
* @param {integer} ms - milliseconds to wait before rejecting promise if not resolved
* @param {Promise} promise to monitor
* @example
* promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json'))
@mudrd8mz
mudrd8mz / gist:2828474
Created May 29, 2012 13:49
Git pre-commit hook that does not allow committing a change containing DONOTCOMMIT text
$ cat .git/hooks/pre-commit
#!/bin/bash
FOUND=$(git diff-index --cached -U0 HEAD -- | grep DONOTCOMMIT | wc -l)
if [[ $FOUND -gt 0 ]]; then
echo "pre-commit hook: DONOTCOMMIT detected, commit not allowed"
exit 1
fi
@jeroenvandijk
jeroenvandijk / datomic.schema_dump.clj
Last active January 23, 2020 10:58
(A) method to dump a datomic database schema
(ns datomic.schema-dump
(:require
[datomic.api :as d]
[clojure.pprint]))
(defmethod clojure.pprint/simple-dispatch datomic.db.DbId [v] (pr v))
(defmethod clojure.pprint/simple-dispatch datomic.function.Function [v] (pr v))
(defn database-url [name]
(str "datomic:mem://" name))
@hellerve
hellerve / capitalize-string.carp
Last active January 19, 2020 15:13
Our solutions to the puzzles from the Clojure Berlin Meetup in January, in Carp
; as an introduction, we tried to do string capitalization by hand,
; using as many different interesting idioms as possible
(defn capitalize-char [c]
(if (Char.lower-case? c)
(=> c
(to-int)
(- 32)
(from-int))
c))
@thheller
thheller / es6-class.cljs
Created August 24, 2017 13:51
class extends React.Component in CLJS
(defn my-component [props context updater]
(cljs.core/this-as this
(js/React.Component.call this props context updater)
;; anything else you want to set-up. use goog.object/set on this
this))
(gobj/extend
(.. my-component -prototype)
js/React.Component.prototype)
@bhb
bhb / gist:c121191c61453bec850a61cc428a109e
Created July 6, 2018 21:19
User-friendly REPL example
clj -Sdeps '{:deps {friendly {:git/url "https://gist.github.com/bhb/2686b023d074ac052dbc21f12f324f18" :sha "c6b0b7cb0a30e2edbf7050c0119ef038cf0f0ac2"}}}' -m friendly
user=> (let [:x 5] x)
Call to clojure.core/let did not conform to spec:
-- Spec failed --------------------
([:x 5] x)
^^
should satisfy
@mk
mk / dependency.clj
Created July 5, 2018 18:39
Figwheel selective build
(ns com.nextjournal.build-tools.figwheel.dependency
(:require [clojure.tools.namespace.track :as ctn.track]
[clojure.tools.namespace.dir :as ctn.dir]
[clojure.tools.namespace.file :as ctn.file]
[clojure.tools.namespace.dependency :as ctn.dep]
[clojure.tools.namespace.parse :as ctn.parse]
[clojure.tools.namespace.find :as ctn.find]
[clojure.java.io :as io]
[clojure.tools.reader :as reader])
(:import java.io.PushbackReader))
@YurySolovyov
YurySolovyov / class-names.cljs
Created July 2, 2017 12:17
class-names for ClojureScript
(defn class-names [& args]
(clojure.string/join " "
(mapv name
(reduce (fn [arr arg]
(cond
(or (string? arg)
(symbol? arg)
(keyword? arg)) (conj arr arg)
(vector? arg) (vec (concat arr arg))
(map? arg) (vec (concat arr
(def kw-cache #js{})
(defn cached-kw [fqn]
(if-some [kw (unchecked-get kw-cache fqn)]
kw
(let [kw (keyword fqn)]
(unchecked-set kw-cache fqn kw)
kw)))
(defn js->clj-fast
@metametadata
metametadata / react-bootstrap.cljs
Last active June 6, 2017 07:56
Fixing of caret/cursor jumps in text inputs of cljsjs.react-bootstrap
(ns app.components.react-bootstrap
(:require [cljsjs.react-bootstrap]
[reagent.core :as r]))
(def Alert (r/adapt-react-class (.-Alert js/ReactBootstrap)))
(def Button (r/adapt-react-class (.-Button js/ReactBootstrap)))
; ...
(def FormControl (r/adapt-react-class (.-FormControl js/ReactBootstrap))) ; WARNING: use FormControlFixed for text input controls instead
(defn FormControlFixed