Skip to content

Instantly share code, notes, and snippets.

View brookjordan's full-sized avatar

Brook Jordan brookjordan

View GitHub Profile
@brookjordan
brookjordan / contain-within-window.scss
Created January 9, 2019 05:30
Contains an element within a window, maintaining aspect ratio. Includes helper mixins to deal with min and max dimensions.
@mixin contain($aspect-y, $aspect-x) {
$aspect: $aspect-y / $aspect-x;
width: 100vw;
height: 100vw / $aspect;
@media (min-aspect-ratio: #{$aspect-y}/#{$aspect-x}) {
width: 100vh * $aspect;
height: 100vh;
}
@brookjordan
brookjordan / overloaded-method.js
Last active January 2, 2019 10:42
Allow an object property to be accessed as a method or… non method?
// Build the asserter
let assert = {
get modal() {
let modal = selector => ({
get exists() {
return assert.modalExists(selector);
},
});
Object.assign(modal, {
get exists() {
@brookjordan
brookjordan / fix-current-date.js
Created January 2, 2019 05:55
Set the Date object to assume the current date to be a fixed date. Useful for testing.
function fixCurrentDate(dateString = 'Tue Jan 01 2019 00:00:00 GMT+0800') {
let RealDate;
// Make sure we keep a reference to the orginal `Date`
if (window.Date.isFake && window.Date.RealDate) {
RealDate = window.Date.RealDate;
} else {
RealDate = window.Date;
}
// If we're fixing the date we'll no doubt be using it,
@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;
}
@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 //=> [ [ [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 / 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 / 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 / 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 / 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 / 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();