Skip to content

Instantly share code, notes, and snippets.

View mholtzhausen's full-sized avatar

Mark Holtzhausen mholtzhausen

View GitHub Profile
@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/std@v0.30.0/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}` });
@mholtzhausen
mholtzhausen / humanBytes
Created November 26, 2019 12:41
Bytes represented in human readable form
module.exports = (bytes, siUnits = false, m, l, d, e) => (m = Math, l = m.log, d = siUnits ? 1024 : 1e3, e = l(bytes) / l(d) | 0, `${bytes / m.pow(d, e).toFixed(2)} ${e ? `${'KMGTPEZY'[--e]}${siUnits ? 'iB' : 'B'}` : 'Bytes'}`)
@mholtzhausen
mholtzhausen / humanMs.js
Created November 26, 2019 12:21
Milliseconds in Human friendly output
const humanMs = function(ms) {
let s = Math.floor(ms / 1000)
let m = Math.floor(s / 60)
let h = Math.floor(m / 60)
let d = Math.floor(h / 24)
ms = ms - s * 1000
s = s - m * 60
m = m - h * 60
h = h - d * 24
@mholtzhausen
mholtzhausen / xPath.js
Last active November 19, 2019 12:09
XPATH in the browser
function Elem2Xpath(elt) {
function getElementIdx(elt) {
let count = 1;
for (let sib = elt.previousSibling; sib; sib = sib.previousSibling) {
if (sib.nodeType == 1 && sib.tagName == elt.tagName)
count++
}
return count;
}