Skip to content

Instantly share code, notes, and snippets.

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 / 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
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)
const type = (data) => {
return Object.prototype.toString.call(data).replace(/^\[object (.+)\]$/, "$1").toLowerCase()
}
@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))
}
@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 / 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',