Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View electerious's full-sized avatar

Tobias Reich electerious

View GitHub Profile
@electerious
electerious / countries.js
Last active April 1, 2024 21:09 — forked from vxnick/gist:380904
All country codes in one object.
var countries = {
AF: 'Afghanistan',
AX: 'Aland Islands',
AL: 'Albania',
DZ: 'Algeria',
AS: 'American Samoa',
AD: 'Andorra',
AO: 'Angola',
AI: 'Anguilla',
AQ: 'Antarctica',
@electerious
electerious / closest.js
Last active November 6, 2015 22:14
Select the closest matching parent of an element
const closest = (elem, className) => {
if (elem==null || elem.classList==null) return null
return (elem.classList.contains(className) ? elem : closest(elem.parentNode, className))
}
@electerious
electerious / each.js
Last active January 24, 2017 09:14
forEach for Objects, Arrays and NodeLists
const each = (data, fn) => {
if (data==null) return false
if ((data).constructor===Object) return Array.prototype.forEach.call(Object.keys(data), (key) => fn(data[key], key, data))
else return Array.prototype.forEach.call(data, (item, i) => fn(item, i, data))
}
const type = (data) => {
return Object.prototype.toString.call(data).replace(/^\[object (.+)\]$/, "$1").toLowerCase()
}
const ajaxform = (form, next) => {
let url = form.action
let xhr = new XMLHttpRequest()
let data = new FormData(form)
xhr.open('POST', url)
xhr.onload = () => next(form, xhr)
xhr.send(data)
@electerious
electerious / html.js
Last active January 24, 2017 09:13
html = (literalSections, ...substs) => {
// Use raw literal sections: We don’t want
// backslashes (\n etc.) to be interpreted
let raw = literalSections.raw,
let result = ''
substs.forEach((subst, i) => {
// Retrieve the literal section preceding
escapeHTML = (html = '') => {
// Ensure that html is a string
html += ''
// Escape all critical characters
html = html.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
@electerious
electerious / scrollroot.js
Last active September 17, 2019 13:45
Cross-browser scroll element fetching
const scrollroot = (() => {
if ('scrollingElement' in document) return document.scrollingElement
const initial = document.documentElement.scrollTop
document.documentElement.scrollTop = initial + 1
const updated = document.documentElement.scrollTop
document.documentElement.scrollTop = initial
@electerious
electerious / stopEvent.js
Last active January 24, 2017 09:04
Stop event propagation and prevent the default
const stopEvent = (e = {}) => {
if (typeof e.stopPropagation==='function') e.stopPropagation()
if (typeof e.preventDefault==='function') e.preventDefault()
}
@electerious
electerious / limit.js
Created November 13, 2015 10:45
Limit a number between a min and max value
const limit = (min, max, num) => Math.min(Math.max(num, min), max)