Skip to content

Instantly share code, notes, and snippets.

View frankiehayward's full-sized avatar

Frankie Hayward frankiehayward

View GitHub Profile
@frankiehayward
frankiehayward / 0_reuse_code.js
Created September 18, 2017 17:00
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@frankiehayward
frankiehayward / mm_add_superscript
Last active January 23, 2018 14:42
Scan page tags for registration, trademark and copyright symbols then wrap with sup tag
<!-- Superscript Replace -->
<script>
(function($){
$('p,h1,h2,h3,h4,h5,h6').each(function(i){
var $this = $(this);
if (($this.text().indexOf("®") !== -1) || ($this.text().indexOf("&reg;") !== -1)) {
$this.html($this.html().replace(/(<sup>)?(&reg;|®)(<\/sup>)?/gi, '<sup>&reg;</sup>'));
}
if (($this.text().indexOf("™") !== -1) || ($this.text().indexOf("&trade;") !== -1)) {
$this.html($this.html().replace(/(<sup>)?(&trade;|™)(<\/sup>)?/gi, '<sup>&trade;</sup>'));

Keybase proof

I hereby claim:

  • I am frankiehayward on github.
  • I am frankiehayward (https://keybase.io/frankiehayward) on keybase.
  • I have a public key ASB2vkbDQp7lKwlSOgD4qh3ABtg2RH0aooVlCu2kG03YvQo

To claim this, I am signing this object:

@frankiehayward
frankiehayward / approximately_equal.js
Created October 9, 2019 14:36
This snippet checks whether two numbers are approximately equal to each other, with a small difference.
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
approximatelyEqual(Math.PI / 2.0, 1.5708); // true
@frankiehayward
frankiehayward / array_to_CSV.js
Created October 9, 2019 14:37
This snippet converts the elements to strings with comma-separated values.
const arrayToCSV = (arr, delimiter = ',') =>
arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
arrayToCSV([['a', 'b'], ['c', 'd']]); // '"a","b"\n"c","d"'
arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"'
@frankiehayward
frankiehayward / array_to_html.js
Created October 9, 2019 14:39
This snippet converts the elements of an array into `<li>` tags and appends them to the list of the given ID.
const arrayToHtmlList = (arr, listID) =>
(el => (
(el = document.querySelector('#' + listID)),
(el.innerHTML += arr.map(item => `<li>${item}</li>`).join(''))
))();
arrayToHtmlList(['item 1', 'item 2'], 'myListID');
@frankiehayward
frankiehayward / attempt.js
Created October 9, 2019 14:40
This snippet executes a function, returning either the result or the caught error object.
const attempt = (fn, ...args) => {
try {
return fn(...args);
} catch (e) {
return e instanceof Error ? e : new Error(e);
}
};
var elements = attempt(function(selector) {
return document.querySelectorAll(selector);
}, '>_>');
@frankiehayward
frankiehayward / average_by.js
Created October 9, 2019 14:42
This snippet returns the average of an array after initially doing the mapping of each element to a value using a given function.
const averageBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
arr.length;
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5
@frankiehayward
frankiehayward / bifurcate_by.js
Created October 9, 2019 14:46
This snippet splits values into two groups, based on a predicate function. If the predicate function returns a truthy value, the element will be placed in the first group. Otherwise, it will be placed in the second group. You can use `Array.prototype
const bifurcateBy = (arr, fn) =>
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b');
// [ ['beep', 'boop', 'bar'], ['foo'] ]
@frankiehayward
frankiehayward / bottom_visible.js
Created October 9, 2019 14:55
This snippet checks whether the bottom of a page is visible.
const bottomVisible = () =>
document.documentElement.clientHeight + window.scrollY >=
(document.documentElement.scrollHeight || document.documentElement.clientHeight);
bottomVisible(); // true