Skip to content

Instantly share code, notes, and snippets.

View mholtzhausen's full-sized avatar

Mark Holtzhausen mholtzhausen

View GitHub Profile
@mholtzhausen
mholtzhausen / ts2ms.js
Last active February 23, 2021 13:48
Timespan to Milliseconds
const timespanToMs=(timeSpan)=>{
let lut={'':1, ms:1, s:1000, m:60000, h:3600000, d: 86400000, w:604800000, y:31556952000 }
let ms=(`${timeSpan}`.match(/\d+[ywdhms]{0,2}\s?/g)).reduce((a,s)=>{
let [,v,m] =s.match(/(\d+)([ywdhms]{0,2})/)
return a + (v * lut[m])
},0)
return ms
}
let passTests=[
@mholtzhausen
mholtzhausen / Catch.js
Last active September 11, 2020 09:24
Catch -- try-catch wrapper for promises (with a bonus)
const Catch = async (promise, errorHandler) => {
if (promise instanceof Promise) {
try {
let returnValue = await promise
return returnValue
} catch (e) {
if (typeof errorHandler === 'function') return errorHandler(e)
return errorHandler
}
}
@mholtzhausen
mholtzhausen / HOME_FOLDER_.bashrc
Last active August 11, 2020 07:15
A script to make a function <name> that will autocomplete when finding a /<script> folder upstream
# ...
source ~/scriptfinder ss scripts
@mholtzhausen
mholtzhausen / ReadMe.md
Created July 28, 2020 12:11
Simple Js validation - schemas for objects - extensible

Simple Js validation - schemas for objects - extensible

validation types work like this: [+]typename[(arg,[arg,...])]

The + in the beginning makes it required any arguments applied will be passed to that type's validation function

@mholtzhausen
mholtzhausen / regexMatch.js
Created July 28, 2020 08:43
Regex Named Matches Wrapper
function regexMatch(regex, str){
let m;
let g=[]
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
g.push({...m.groups})
}
@mholtzhausen
mholtzhausen / ReadMe.md
Last active May 12, 2020 15:53
Find the first occurance of a string in the git-history of a file

git.find.first

I needed a tool to tell me the first time a certain string appeared in the git-history of a file. It was a node package, and I wanted to find out why it first got included.

Usage

node git.find.first.js <path-to-file> <search-term> [--open]

--open will open a github-url for that commit if you have a github repository

Vue.component("async", {
props: {
url: { type: String, default: "", required: true },
params: { type: Object, default: () => ({}) }
},
data() {
return {
pending: true,
error: false,
data: null
@mholtzhausen
mholtzhausen / pre-commit.sh
Last active February 12, 2022 16:54 — forked from dahjelle/pre-commit.sh
Pre-commit hook for eslint, linting *only* staged changes.
#!/bin/bash
clear
fileList=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(js|vue)$')
if [ ${#fileList} -lt 1 ]; then
echo -e "You have no staged .js or .vue files to test\n"
exit
fi
npx eslint ${fileList[*]} "$@"
if [ $? -ne 0 ]; then
echo -e "\nPlease fix the above linting issues before committing.\n"
@mholtzhausen
mholtzhausen / DForm.js
Last active February 14, 2020 08:45
Date Formatting
const SECONDS = {
MINUTE: 60,
HOUR: 3600,
DAY: 86400,
WEEK: 604800,
MONTH: 2630016, //30.44 day month average
YEAR: 31557600, //based on 365.25 days per year
}
class DForm {
import { serve } from "https://deno.land/[email protected]/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
let c=0
let st=null
for await (const req of s) {
if(st===null)st=Date.now()
let sd=(Date.now()-st)/1000
let rps=(c+1)/sd
req.respond({ body: `Hello World\nRequests Handled:${c++}\nSeconds:${sd}\nRPS: ${rps}` });