Skip to content

Instantly share code, notes, and snippets.

View johnloy's full-sized avatar

John Loy johnloy

View GitHub Profile
export declare type Constructor<T> = new (...args: any[]) => T
@johnloy
johnloy / bytesToSize.js
Created March 1, 2021 13:34 — forked from lanqy/bytesToSize.js
JavaScript To Convert Bytes To MB, KB, Etc
// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
@johnloy
johnloy / convertFromBaseToBase.js
Last active February 7, 2021 18:20
Convert the string representation of a number to another base
function convertFromBaseToBase(str, fromBase, toBase) {
var num = parseInt(str, fromBase);
return num.toString(toBase);
}
man unix_command | col -bx > man_page_file.txt
@johnloy
johnloy / log-page-web-components.js
Last active August 27, 2021 18:33
Bookmarklet to log web components used on this page
javascript:void((function(w, d)%7B/*begin*/import(%27https://unpkg.com/query-selector-shadow-dom%400.8.0/src/querySelectorDeep.js%27).then((%7BquerySelectorAllDeep: %24%24%7D) %3D> console.log(%5B...%24%24(%27*:defined%27)%5D.filter(e %3D> e.tagName.indexOf(%27-%27) > -1)))/*end*/%7D)( window, document ))%3B
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
@johnloy
johnloy / isError.js
Created January 23, 2020 13:55
Determine if a value is an error, in a browser context
function isError(value) {
switch (Object.prototype.toString.call(value)) {
case '[object Error]':
return true;
case '[object Exception]':
return true;
case '[object DOMException]':
return true;
defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool false # For VS Code
defaults write com.microsoft.VSCodeInsiders ApplePressAndHoldEnabled -bool false # For VS Code Insider
defaults write md.obsidian ApplePressAndHoldEnabled -bool false # For Obsidian
defaults delete -g ApplePressAndHoldEnabled
@johnloy
johnloy / unbounded-filter.js
Last active January 20, 2020 21:59
Unbound array function in JS using Function.prototype.bind
const filter = Function.prototype.call.bind(Array.prototype.filter)
filter([1,2,3], (x) => x > 1 )
@johnloy
johnloy / index.html
Last active July 30, 2019 13:38
State management with custom DOM events
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="module">
const createAction = (el, initialState, name, action) => {
let currentState = initialState;
document.body.addEventListener(name, (e) => {
el.render(e.detail.state);
});