Skip to content

Instantly share code, notes, and snippets.

View trevorhreed's full-sized avatar

Trevor trevorhreed

  • Utah
  • 04:17 (UTC -06:00)
View GitHub Profile
@trevorhreed
trevorhreed / get-fn-params.js
Last active January 14, 2022 21:15
getFnParams: takes a function and returns the names of that function's parameters as an array
const fnParamsRe = /^(?:async)?\s*(?:(?:function\s+[a-zA-Z_][a-zA-Z0-9_]*|function|[a-zA-Z_][a-zA-Z0-9_]*)\s*(?:\(([^)]*)\))|(?:\(([^)]*)\)|([a-zA-Z_][a-zA-Z0-9_]*))\s*=>\s*{)/
const sepRe = /\s*,\s*/g
const getFnParams = fn => {
const source = fn.toString()
const match = fnParamsRe.exec(source) || []
const paramStr = match[1] || match[2] || match[3] || ''
return paramStr.split(sepRe).filter(Boolean)
}
@trevorhreed
trevorhreed / throttlePromiseFn.js
Last active December 14, 2017 21:27
Throttles the number of concurrent calls to a promise function
const throttlePromiseFn = module.exports = (fn, { max = 8, PromiseConstructor = Promise } = {}) => {
const queue = [];
let count = 0;
const call = (params, resolve, reject) => {
const promise = fn(...params);
promise.then((value)=>{
checkQueue();
typeof resolve === 'function' && resolve(value);
}).catch((reason)=>{
checkQueue();
@trevorhreed
trevorhreed / short-uuid.js
Created October 25, 2017 20:13
Shorter UUIDs
const bigInt = require("big-integer")
const symbols = `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`;
const v2r = (n, b) => {
if(!bigInt.isInstance(n)) n = bigInt(n);
let digits = '';
while(n > 0){
let r = n.divmod(b);
digits = symbols[r.remainder] + digits;
n = r.quotient;
@trevorhreed
trevorhreed / event-bus.js
Created September 15, 2017 21:48
Simple event bus
const EventBusFactory = () => {
let repo = {};
return {
add(key, fn){
if(typeof fn !== 'function') throw `Invalid parameter. Second parameter must be a function.`;
if(!repo[key]) repo[key] = [];
if(!repo[key].includes(fn)) repo[key].push(fn);
},
remove(key, fn){
if(!repo[key]) return;
var showCopy = (text) => {
let div = document.createElement('DIV');
let btn = document.createElement('BUTTON');
btn.textContent = 'Copy';
btn.style.border = 'solid 1px #aaa';
btn.style.background = '#fefefe';
btn.style.padding = '1em 3em';
btn.style.fontSize = '16px';
btn.addEventListener('click', (e)=>{
let textarea = document.createElement('TEXTAREA');
{
console.clear();
let app = (()=>{
function Invocable(name, fn){
this.name = name;
this.fn = fn;
}
let bagOfThings = {};
let getFnParams = (fn) => {
@trevorhreed
trevorhreed / email
Last active July 21, 2017 17:17
Medium - How A Printer From The 80’s Can Improve Your Programming - 1
// COMMON CODE
const DEFAULT_SENDER = '[email protected]';
const Email = require('some-email-service');
// EXAMPLE 1
let emailOne = (person, subject, body) => {
let email = new Email();
@trevorhreed
trevorhreed / full-text-search-algorithm.js
Last active May 31, 2025 00:56
A simple full-text-search algorithm
// Draft 2
ngm.service('searchArray', function(){
let THRESHHOLD = 0;
let FULL_EXACTMATCH_MODIFIER = 1.0;
let FULL_STARTSWITH_MODIFIER = 0.9;
let FULL_CONTAINS_MODIFIER = 0.3;
let FULL_PARTIAL_MODIFIER = 0.3;
let PRECISION = 2;
let re = /[\s\b]+/g;
let search = (term, arr, key) => {
// #region MISC
/*
Usage:
<image-input content-id="{{show.id}}" image-type="Show" ng-model="show.images"></image-input>
<image-input content-id="{{show.id}}" image-type="ListImage" ng-model="show.listImage"></image-input>
*/
@trevorhreed
trevorhreed / scroll-detection.js
Created February 22, 2017 17:25
Scroll percentage, infinite scroll
// This code is for infinite scrolling, which we may or may not implement
var wrapper = $('.test-scroll-wrapper');
var content = $('.test-scroll-content');
window.wrapper = wrapper;
window.content = content;
wrapper.on('scroll', function(){
var marginTop = Math.round(parseFloat(content.css('marginTop')));