Skip to content

Instantly share code, notes, and snippets.

View brookjordan's full-sized avatar

Brook Jordan brookjordan

View GitHub Profile
@brookjordan
brookjordan / noteFrequency.js
Last active October 27, 2022 03:05
Get the wavelength of a note in the Chromatic scale
function noteFrequency(offset) {
// A4 has a defined, integer frequency. Let's use this as the base for calculations
const A4Frequency = 440;
// Allows:
// - multiple flats and sharps in any order
// - usage of '♭' or lowercase 'B' as flat
const noteRegExp = /^([a-gA-G])([♭b♯#]*)(-?\d+)?$/;
const semitoneMultiplier = 2 ** (1/12);
const scaleNoteCount = 12;
// Pre calculate offsets of each note from A
@brookjordan
brookjordan / _add-element.scss
Created December 5, 2017 09:27
In order to just add an element to a selector in SCSS is infuriatingly difficult. This attempts to ease the pain.
//
// Usage:
//
// .my-class {
// // other styles…
//
// @include add-element(a) {
// color: blue;
// }
//
@brookjordan
brookjordan / pentagon.js
Created December 10, 2017 17:22
Calculations for pentagon dimensions
class Pentagon {
static get SIDE_COUNT() { return 5; };
static get TOTAL_ANGLE() { return 180 * (Pentagon.SIDE_COUNT - 2); };
static get CORNER_ANGLE() { return Pentagon.TOTAL_ANGLE / Pentagon.SIDE_COUNT; };
static get SQRT_SIDES() { return Math.sqrt(Pentagon.SIDE_COUNT); };
constructor (from, value) {
if (isNaN(+from)) {
this[from] = value;
@brookjordan
brookjordan / build-sitemap.js
Created February 6, 2018 02:48
Crawls your site and generates a list of internal pages accessible by crawling from the home page.
var baseURL = 'https://www.tradegecko.com'
var pages = {};
var pageURLs = ['/'];
var uncrawledPageURLs = pageURLs.slice(0);
var crawledPageURLs = [];
var openCrawls = 0;
var maxCrawls = 3;
findNewPages();
@brookjordan
brookjordan / find-text-styles.js
Created April 16, 2018 00:55
Find unique text styles around a site
/*
* Use like:
* var a = new StyleCollector({ filterFonts : ['Open Sans', 'Raleway'] });
* a.search(); // => no of new fonts found
* console.log(a.textStyles); // => Array of unique found text styles
*/
class StyleCollector {
constructor({ filterFonts } = {}) {
this._foundStyles = [];
@brookjordan
brookjordan / await-control.js
Last active June 5, 2018 11:07
Pause an async environment until released
let okgo = new Promise(res=>{ window.cont = () => { res(); } });
await okgo;
@brookjordan
brookjordan / tab-map.js
Last active July 10, 2018 03:54
Create a list of tab-able elements sorted into tabbing order
function tabMap(within = document.documentElement) {
let TABBABLE_SELECTOR = [
'area',
'button',
'select',
'textarea',
'summary',
'details',
'[tabindex]',
'a[href]',
@brookjordan
brookjordan / arrange-pagination.js
Last active September 11, 2018 03:06
Pagination buttons
function calculateArrangement({ pageCount = 9, currentPageNumber = 1, buttonCount = 7 } = {}) {
buttonCount = +buttonCount;
if (!buttonCount || buttonCount < 7) { throw Error('Must have 7 or more') }
if (currentPageNumber > pageCount || currentPageNumber < 1) { throw Error('current page must be between 1 and page count') }
let requiredEndsButtonCount = 1;
let requiredCenterButtonCount = 3;
let requiredSpareCountForGoto = 1;
let requiredButtonCount =
@brookjordan
brookjordan / README.md
Last active October 30, 2018 05:15
Create multi-dimensional array with a useful getter and setter

Usage

let arr = new dimArray([3, 3, 3], Array(3 * 3 * 3).fill(0).map((_, i) => i));

Properties

arr.dimensionCount //=> 3
arr.size           //=> 27
arr.values //=&gt; [ [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ], [ [9, 10, 11], [12, 13, 14], [15, 16, 17] ], [ [18, 19, 20], [21, 22, 23], [24, 25, 26] ] ]
@brookjordan
brookjordan / random-string.js
Last active December 10, 2018 03:11
Create a random string using characters from the base 64 allowed chars
function randomString(length = 16) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/';
const charCount = chars.length;
let str = '';
while (length-- > 0) {
str += chars[Math.floor(Math.random() * charCount)];
}
return str;
}