Skip to content

Instantly share code, notes, and snippets.

View mholtzhausen's full-sized avatar

Mark Holtzhausen mholtzhausen

View GitHub Profile
@mholtzhausen
mholtzhausen / loadYamlWithRefs.mjs
Last active July 10, 2024 14:12
Load a Yaml file, and resolve $ref references
import fs from 'fs'
import path from 'path'
import YAML from 'js-yaml'
/**
* Process an object with references
* @param {object} obj - the object to process
* @param {string} rootFolder - the folder where the yaml file is located
* @returns {object}
*/
@mholtzhausen
mholtzhausen / readLinesFromFile.js
Last active July 7, 2022 13:25
Read a number of lines from a file with Nodejs
const fs = require('fs')
const events = require('events')
const readline = require('readline')
const readLinesFromFile = async function(file, start, count) {
let end = start + count
let lines = []
try{
let rl = readline.createInterface({
input: fs.createReadStream(file),
@mholtzhausen
mholtzhausen / Readme.md
Last active October 5, 2021 11:48
DateTime Formatting made very very simple

DateUtil

A very simple local-time only date formatting tool that lives in one file and does the minimum for mvp.

How to use

const {format} = require('./dateUtil.js')

let date=Date.now()
let formatStr='www, DD mmmm, YYYY H:I:s.SS'
@mholtzhausen
mholtzhausen / Readme.md
Created June 22, 2021 14:00
Clear all Cookies on Primary domain and Subdomains

This function will remove all cookies that are:

  1. on the primary domain
  2. on the wildcard primary domain
  3. with the matching path (defaults to no-path or /)
  4. that is not an http cookie
@mholtzhausen
mholtzhausen / demo.js
Created April 21, 2021 08:37
Simple timer function
const {start, measure, create} = require('./timer.js')
const MAX=1000
var seed = Math.random()*1000;
function random() {
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
@mholtzhausen
mholtzhausen / demo.js
Last active April 15, 2021 14:35
Compact pared down version of npm debug module
const debug=require('./qdebug.js')
const log1 = debug('log1')
const log2 = debug('log2')
const sublog = log1.ext('sublog')
log1.info('hello world', {test:Math.random()})
log2.info('hello world', {test:Math.random()})
sublog.info('hello world', {test:Math.random()})
@mholtzhausen
mholtzhausen / .git_info_exclude
Last active February 23, 2021 13:32
Default Git Excludes
# VsCode Local Settings :: The team is too cool to standardise IDE
.vscode/
# Temp-Folder ::I am so dumb I need a persistent scratch-pad
tmp/
# Dev Scripts :: The team is too advanced for this shit
dev/
# Unofficial Docs :: The team is too clever for this shit
@mholtzhausen
mholtzhausen / script.dir.sh
Created January 4, 2021 08:01
Get bash script dir
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
@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
}
}