Forked from nilsandrey/name-shortening functions.js
Created
February 18, 2023 12:40
-
-
Save dinh/366d054d95b2af4530e680c215daba75 to your computer and use it in GitHub Desktop.
Name-shortening functions from Hacker News own JS for web site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From yesenadam at https://news.ycombinator.com/item?id=23590848 | |
function $(id) { return document.getElementById(id); } | |
function byClass (el, cl) { return el ? el.getElementsByClassName(cl) : [] } | |
function byTag (el, tg) { return el ? el.getElementsByTagName(tg) : [] } | |
function allof (cl) { return byClass(document, cl) } | |
function hasClass (el, cl) { var a = el.className.split(' '); return afind(cl, a) } | |
function addClass (el, cl) { if (el) { var a = el.className.split(' '); if (!afind(cl, a)) { a.unshift(cl); el.className = a.join(' ')}} } | |
function remClass (el, cl) { if (el) { var a = el.className.split(' '); arem(a, cl); el.className = a.join(' ') } } | |
function html (el) { return el ? el.innerHTML : null; } | |
function attr (el, name) { return el.getAttribute(name) } | |
function tonum (x) { var n = parseFloat(x); return isNaN(n) ? null : n } | |
function remEl (el) { el.parentNode.removeChild(el) } | |
function posf (f, a) { for (var i=0; i < a.length; i++) { if (f(a[i])) return i; } return -1; } | |
function apos (x, a) { return (typeof x == 'function') ? posf(x,a) : Array.prototype.indexOf.call(a,x) } | |
function afind (x, a) { var i = apos(x, a); return (i >= 0) ? a[i] : null; } | |
function acut (a, m, n) { return Array.prototype.slice.call(a, m, n) } | |
function aeach (fn, a) { return Array.prototype.forEach.call(a, fn) } | |
function arem (a, x) { var i = apos(x, a); if (i >= 0) { a.splice(i, 1); } return a; } | |
function alast (a) { return a[a.length - 1] } | |
function vis(el, on) { if (el) { (on ? remClass : addClass)(el, 'nosee') } } | |
function noshow (el) { addClass(el, 'noshow') } | |
function elShow (el) { remClass(el, 'noshow') } | |
function ind (el) { return (byTag(el, 'img')[0] || {}).width } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment